git.iko.soy/rowtype-yoga/purescript-native
on a979da1 help sign in
295 lines 9.9 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Types
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- Data types for types
13--
14-----------------------------------------------------------------------------
16{-# LANGUAGE DeriveDataTypeable #-}
18module Language.PureScript.Types where
20import Data.Data
21import Data.List (nub)
23import Control.Monad.Unify
24import Control.Arrow (second)
25import Control.Applicative
26import Control.Monad ((<=<))
28import Language.PureScript.Names
29import Language.PureScript.Kinds
30import Language.PureScript.Traversals
32-- |
33-- An identifier for the scope of a skolem variable
34--
35newtype SkolemScope = SkolemScope { runSkolemScope :: Int } deriving (Show, Eq, Ord, Data, Typeable)
37-- |
38-- The type of types
39--
40data Type
41 -- |
42 -- A unification variable of type Type
43 --
44 = TUnknown Unknown
45 -- |
46 -- A named type variable
47 --
48 | TypeVar String
49 -- |
50 -- A type constructor
51 --
52 | TypeConstructor (Qualified ProperName)
53 -- |
54 -- A type application
55 --
56 | TypeApp Type Type
57 -- |
58 -- A type synonym which is \"saturated\", i.e. fully applied
59 --
60 | SaturatedTypeSynonym (Qualified ProperName) [Type]
61 -- |
62 -- Forall quantifier
63 --
64 | ForAll String Type (Maybe SkolemScope)
65 -- |
66 -- A type with a set of type class constraints
67 --
68 | ConstrainedType [(Qualified ProperName, [Type])] Type
69 -- |
70 -- A skolem constant
71 --
72 | Skolem String Int SkolemScope
73 -- |
74 -- An empty row
75 --
76 | REmpty
77 -- |
78 -- A non-empty row
79 --
80 | RCons String Type Type
81 -- |
82 -- A type with a kind annotation
83 --
84 | KindedType Type Kind
85 --
86 -- |
87 -- A placeholder used in pretty printing
88 --
89 | PrettyPrintFunction Type Type
90 -- |
91 -- A placeholder used in pretty printing
92 --
93 | PrettyPrintArray Type
94 -- |
95 -- A placeholder used in pretty printing
96 --
97 | PrettyPrintObject Type
98 -- |
99 -- A placeholder used in pretty printing
100 --
101 | PrettyPrintForAll [String] Type deriving (Show, Eq, Data, Typeable)
103-- |
104-- Convert a row to a list of pairs of labels and types
105--
106rowToList :: Type -> ([(String, Type)], Type)
107rowToList (RCons name ty row) = let (tys, rest) = rowToList row
108 in ((name, ty):tys, rest)
109rowToList r = ([], r)
111-- |
112-- Convert a list of labels and types to a row
113--
114rowFromList :: ([(String, Type)], Type) -> Type
115rowFromList ([], r) = r
116rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
118-- |
119-- Check whether a type is a monotype
120--
121isMonoType :: Type -> Bool
122isMonoType ForAll{} = False
123isMonoType _ = True
125-- |
126-- Universally quantify a type
127--
128mkForAll :: [String] -> Type -> Type
129mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
131-- |
132-- Replace a type variable, taking into account variable shadowing
133--
134replaceTypeVars :: String -> Type -> Type -> Type
135replaceTypeVars v r = replaceAllTypeVars [(v, r)]
137-- |
138-- Replace named type variables with types
139--
140replaceAllTypeVars :: [(String, Type)] -> Type -> Type
141replaceAllTypeVars = go []
142 where
144 go :: [String] -> [(String, Type)] -> Type -> Type
145 go _ m (TypeVar v) =
146 case v `lookup` m of
147 Just r -> r
148 Nothing -> TypeVar v
149 go bs m (TypeApp t1 t2) = TypeApp (go bs m t1) (go bs m t2)
150 go bs m (SaturatedTypeSynonym name' ts) = SaturatedTypeSynonym name' $ map (go bs m) ts
151 go bs m f@(ForAll v t sco) | v `elem` keys = go bs (filter ((/= v) . fst) m) f
152 | v `elem` usedVars =
153 let v' = genName v (keys ++ bs ++ usedVars)
154 t' = go bs [(v, TypeVar v')] t
155 in ForAll v' (go (v' : bs) m t') sco
156 | otherwise = ForAll v (go (v : bs) m t) sco
157 where
158 keys = map fst m
159 usedVars = concatMap (usedTypeVariables . snd) m
160 go bs m (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs m)) cs) (go bs m t)
161 go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
162 go bs m (KindedType t k) = KindedType (go bs m t) k
163 go _ _ ty = ty
165 genName orig inUse = try 0
166 where
167 try :: Integer -> String
168 try n | (orig ++ show n) `elem` inUse = try (n + 1)
169 | otherwise = orig ++ show n
171-- |
172-- Collect all type variables appearing in a type
173--
174usedTypeVariables :: Type -> [String]
175usedTypeVariables = nub . everythingOnTypes (++) go
176 where
177 go (TypeVar v) = [v]
178 go _ = []
180-- |
181-- Collect all free type variables appearing in a type
182--
183freeTypeVariables :: Type -> [String]
184freeTypeVariables = nub . go []
185 where
186 go :: [String] -> Type -> [String]
187 go bound (TypeVar v) | v `notElem` bound = [v]
188 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
189 go bound (SaturatedTypeSynonym _ ts) = concatMap (go bound) ts
190 go bound (ForAll v t _) = go (v : bound) t
191 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
192 go bound (RCons _ t r) = go bound t ++ go bound r
193 go bound (KindedType t _) = go bound t
194 go _ _ = []
196-- |
197-- Universally quantify over all type variables appearing free in a type
198--
199quantify :: Type -> Type
200quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
202-- |
203-- Move all universal quantifiers to the front of a type
204--
205moveQuantifiersToFront :: Type -> Type
206moveQuantifiersToFront = go [] []
207 where
208 go qs cs (ForAll q ty sco) = go ((q, sco) : qs) cs ty
209 go qs cs (ConstrainedType cs' ty) = go qs (cs ++ cs') ty
210 go qs cs ty =
211 let constrained = case cs of
212 [] -> ty
213 cs' -> ConstrainedType cs' ty
214 in case qs of
215 [] -> constrained
216 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
218--
219-- Traversals
220--
222everywhereOnTypes :: (Type -> Type) -> Type -> Type
223everywhereOnTypes f = go
224 where
225 go (TypeApp t1 t2) = f (TypeApp (go t1) (go t2))
226 go (SaturatedTypeSynonym name tys) = f (SaturatedTypeSynonym name (map go tys))
227 go (ForAll arg ty sco) = f (ForAll arg (go ty) sco)
228 go (ConstrainedType cs ty) = f (ConstrainedType (map (fmap (map go)) cs) (go ty))
229 go (RCons name ty rest) = f (RCons name (go ty) (go rest))
230 go (KindedType ty k) = f (KindedType (go ty) k)
231 go (PrettyPrintFunction t1 t2) = f (PrettyPrintFunction (go t1) (go t2))
232 go (PrettyPrintArray t) = f (PrettyPrintArray (go t))
233 go (PrettyPrintObject t) = f (PrettyPrintObject (go t))
234 go (PrettyPrintForAll args t) = f (PrettyPrintForAll args (go t))
235 go other = f other
237everywhereOnTypesTopDown :: (Type -> Type) -> Type -> Type
238everywhereOnTypesTopDown f = go . f
239 where
240 go (TypeApp t1 t2) = TypeApp (go (f t1)) (go (f t2))
241 go (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name (map (go . f) tys)
242 go (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
243 go (ConstrainedType cs ty) = ConstrainedType (map (fmap (map (go . f))) cs) (go (f ty))
244 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
245 go (KindedType ty k) = KindedType (go (f ty)) k
246 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
247 go (PrettyPrintArray t) = PrettyPrintArray (go (f t))
248 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
249 go (PrettyPrintForAll args t) = PrettyPrintForAll args (go (f t))
250 go other = f other
252everywhereOnTypesM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
253everywhereOnTypesM f = go
254 where
255 go (TypeApp t1 t2) = (TypeApp <$> go t1 <*> go t2) >>= f
256 go (SaturatedTypeSynonym name tys) = (SaturatedTypeSynonym name <$> mapM go tys) >>= f
257 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
258 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (sndM (mapM go)) cs <*> go ty) >>= f
259 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
260 go (KindedType ty k) = (KindedType <$> go ty <*> pure k) >>= f
261 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
262 go (PrettyPrintArray t) = (PrettyPrintArray <$> go t) >>= f
263 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
264 go (PrettyPrintForAll args t) = (PrettyPrintForAll args <$> go t) >>= f
265 go other = f other
267everywhereOnTypesTopDownM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
268everywhereOnTypesTopDownM f = go <=< f
269 where
270 go (TypeApp t1 t2) = TypeApp <$> (f t1 >>= go) <*> (f t2 >>= go)
271 go (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name <$> mapM (go <=< f) tys
272 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
273 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (sndM (mapM (go <=< f))) cs <*> (f ty >>= go)
274 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
275 go (KindedType ty k) = KindedType <$> (f ty >>= go) <*> pure k
276 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
277 go (PrettyPrintArray t) = PrettyPrintArray <$> (f t >>= go)
278 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
279 go (PrettyPrintForAll args t) = PrettyPrintForAll args <$> (f t >>= go)
280 go other = f other
282everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
283everythingOnTypes (<>) f = go
284 where
285 go t@(TypeApp t1 t2) = f t <> go t1 <> go t2
286 go t@(SaturatedTypeSynonym _ tys) = foldl (<>) (f t) (map go tys)
287 go t@(ForAll _ ty _) = f t <> go ty
288 go t@(ConstrainedType cs ty) = foldl (<>) (f t) (map go $ concatMap snd cs) <> go ty
289 go t@(RCons _ ty rest) = f t <> go ty <> go rest
290 go t@(KindedType ty _) = f t <> go ty
291 go t@(PrettyPrintFunction t1 t2) = f t <> go t1 <> go t2
292 go t@(PrettyPrintArray t1) = f t <> go t1
293 go t@(PrettyPrintObject t1) = f t <> go t1
294 go t@(PrettyPrintForAll _ t1) = f t <> go t1
295 go other = f other