git.iko.soy/rowtype-yoga/purescript-native
on f56ed2b help sign in
305 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 wildcard, as would appear in a partial type synonym
51 --
52 | TypeWildcard
53 -- |
54 -- A type constructor
55 --
56 | TypeConstructor (Qualified ProperName)
57 -- |
58 -- A type application
59 --
60 | TypeApp Type Type
61 -- |
62 -- A type synonym which is \"saturated\", i.e. fully applied
63 --
64 | SaturatedTypeSynonym (Qualified ProperName) [Type]
65 -- |
66 -- Forall quantifier
67 --
68 | ForAll String Type (Maybe SkolemScope)
69 -- |
70 -- A type with a set of type class constraints
71 --
72 | ConstrainedType [Constraint] Type
73 -- |
74 -- A skolem constant
75 --
76 | Skolem String Int SkolemScope
77 -- |
78 -- An empty row
79 --
80 | REmpty
81 -- |
82 -- A non-empty row
83 --
84 | RCons String Type Type
85 -- |
86 -- A type with a kind annotation
87 --
88 | KindedType Type Kind
89 --
90 -- |
91 -- A placeholder used in pretty printing
92 --
93 | PrettyPrintFunction Type 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-- A typeclass constraint
105--
106type Constraint = (Qualified ProperName, [Type])
108-- |
109-- Convert a row to a list of pairs of labels and types
110--
111rowToList :: Type -> ([(String, Type)], Type)
112rowToList (RCons name ty row) = let (tys, rest) = rowToList row
113 in ((name, ty):tys, rest)
114rowToList r = ([], r)
116-- |
117-- Convert a list of labels and types to a row
118--
119rowFromList :: ([(String, Type)], Type) -> Type
120rowFromList ([], r) = r
121rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
123-- |
124-- Check whether a type is a monotype
125--
126isMonoType :: Type -> Bool
127isMonoType ForAll{} = False
128isMonoType _ = True
130-- |
131-- Universally quantify a type
132--
133mkForAll :: [String] -> Type -> Type
134mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
136-- |
137-- Replace a type variable, taking into account variable shadowing
138--
139replaceTypeVars :: String -> Type -> Type -> Type
140replaceTypeVars v r = replaceAllTypeVars [(v, r)]
142-- |
143-- Replace named type variables with types
144--
145replaceAllTypeVars :: [(String, Type)] -> Type -> Type
146replaceAllTypeVars = go []
147 where
149 go :: [String] -> [(String, Type)] -> Type -> Type
150 go _ m (TypeVar v) =
151 case v `lookup` m of
152 Just r -> r
153 Nothing -> TypeVar v
154 go bs m (TypeApp t1 t2) = TypeApp (go bs m t1) (go bs m t2)
155 go bs m (SaturatedTypeSynonym name' ts) = SaturatedTypeSynonym name' $ map (go bs m) ts
156 go bs m f@(ForAll v t sco) | v `elem` keys = go bs (filter ((/= v) . fst) m) f
157 | v `elem` usedVars =
158 let v' = genName v (keys ++ bs ++ usedVars)
159 t' = go bs [(v, TypeVar v')] t
160 in ForAll v' (go (v' : bs) m t') sco
161 | otherwise = ForAll v (go (v : bs) m t) sco
162 where
163 keys = map fst m
164 usedVars = concatMap (usedTypeVariables . snd) m
165 go bs m (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs m)) cs) (go bs m t)
166 go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
167 go bs m (KindedType t k) = KindedType (go bs m t) k
168 go _ _ ty = ty
170 genName orig inUse = try 0
171 where
172 try :: Integer -> String
173 try n | (orig ++ show n) `elem` inUse = try (n + 1)
174 | otherwise = orig ++ show n
176-- |
177-- Collect all type variables appearing in a type
178--
179usedTypeVariables :: Type -> [String]
180usedTypeVariables = nub . everythingOnTypes (++) go
181 where
182 go (TypeVar v) = [v]
183 go _ = []
185-- |
186-- Collect all free type variables appearing in a type
187--
188freeTypeVariables :: Type -> [String]
189freeTypeVariables = nub . go []
190 where
191 go :: [String] -> Type -> [String]
192 go bound (TypeVar v) | v `notElem` bound = [v]
193 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
194 go bound (SaturatedTypeSynonym _ ts) = concatMap (go bound) ts
195 go bound (ForAll v t _) = go (v : bound) t
196 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
197 go bound (RCons _ t r) = go bound t ++ go bound r
198 go bound (KindedType t _) = go bound t
199 go _ _ = []
201-- |
202-- Universally quantify over all type variables appearing free in a type
203--
204quantify :: Type -> Type
205quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
207-- |
208-- Move all universal quantifiers to the front of a type
209--
210moveQuantifiersToFront :: Type -> Type
211moveQuantifiersToFront = go [] []
212 where
213 go qs cs (ForAll q ty sco) = go ((q, sco) : qs) cs ty
214 go qs cs (ConstrainedType cs' ty) = go qs (cs ++ cs') ty
215 go qs cs ty =
216 let constrained = case cs of
217 [] -> ty
218 cs' -> ConstrainedType cs' ty
219 in case qs of
220 [] -> constrained
221 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
223-- |
224-- Check if a type contains wildcards
225--
226containsWildcards :: Type -> Bool
227containsWildcards = everythingOnTypes (||) go
228 where
229 go :: Type -> Bool
230 go TypeWildcard = True
231 go _ = False
233--
234-- Traversals
235--
237everywhereOnTypes :: (Type -> Type) -> Type -> Type
238everywhereOnTypes f = go
239 where
240 go (TypeApp t1 t2) = f (TypeApp (go t1) (go t2))
241 go (SaturatedTypeSynonym name tys) = f (SaturatedTypeSynonym name (map go tys))
242 go (ForAll arg ty sco) = f (ForAll arg (go ty) sco)
243 go (ConstrainedType cs ty) = f (ConstrainedType (map (fmap (map go)) cs) (go ty))
244 go (RCons name ty rest) = f (RCons name (go ty) (go rest))
245 go (KindedType ty k) = f (KindedType (go ty) k)
246 go (PrettyPrintFunction t1 t2) = f (PrettyPrintFunction (go t1) (go t2))
247 go (PrettyPrintObject t) = f (PrettyPrintObject (go t))
248 go (PrettyPrintForAll args t) = f (PrettyPrintForAll args (go t))
249 go other = f other
251everywhereOnTypesTopDown :: (Type -> Type) -> Type -> Type
252everywhereOnTypesTopDown f = go . f
253 where
254 go (TypeApp t1 t2) = TypeApp (go (f t1)) (go (f t2))
255 go (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name (map (go . f) tys)
256 go (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
257 go (ConstrainedType cs ty) = ConstrainedType (map (fmap (map (go . f))) cs) (go (f ty))
258 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
259 go (KindedType ty k) = KindedType (go (f ty)) k
260 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
261 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
262 go (PrettyPrintForAll args t) = PrettyPrintForAll args (go (f t))
263 go other = f other
265everywhereOnTypesM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
266everywhereOnTypesM f = go
267 where
268 go (TypeApp t1 t2) = (TypeApp <$> go t1 <*> go t2) >>= f
269 go (SaturatedTypeSynonym name tys) = (SaturatedTypeSynonym name <$> mapM go tys) >>= f
270 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
271 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (sndM (mapM go)) cs <*> go ty) >>= f
272 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
273 go (KindedType ty k) = (KindedType <$> go ty <*> pure k) >>= f
274 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
275 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
276 go (PrettyPrintForAll args t) = (PrettyPrintForAll args <$> go t) >>= f
277 go other = f other
279everywhereOnTypesTopDownM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
280everywhereOnTypesTopDownM f = go <=< f
281 where
282 go (TypeApp t1 t2) = TypeApp <$> (f t1 >>= go) <*> (f t2 >>= go)
283 go (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name <$> mapM (go <=< f) tys
284 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
285 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (sndM (mapM (go <=< f))) cs <*> (f ty >>= go)
286 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
287 go (KindedType ty k) = KindedType <$> (f ty >>= go) <*> pure k
288 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
289 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
290 go (PrettyPrintForAll args t) = PrettyPrintForAll args <$> (f t >>= go)
291 go other = f other
293everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
294everythingOnTypes (<>) f = go
295 where
296 go t@(TypeApp t1 t2) = f t <> go t1 <> go t2
297 go t@(SaturatedTypeSynonym _ tys) = foldl (<>) (f t) (map go tys)
298 go t@(ForAll _ ty _) = f t <> go ty
299 go t@(ConstrainedType cs ty) = foldl (<>) (f t) (map go $ concatMap snd cs) <> go ty
300 go t@(RCons _ ty rest) = f t <> go ty <> go rest
301 go t@(KindedType ty _) = f t <> go ty
302 go t@(PrettyPrintFunction t1 t2) = f t <> go t1 <> go t2
303 go t@(PrettyPrintObject t1) = f t <> go t1
304 go t@(PrettyPrintForAll _ t1) = f t <> go t1
305 go other = f other