git.iko.soy/rowtype-yoga/purescript-native
on fd82591 help sign in
311 lines 10 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 #-}
17{-# LANGUAGE GeneralizedNewtypeDeriving #-}
18{-# LANGUAGE TemplateHaskell #-}
20module Language.PureScript.Types where
22import Data.Data
23import Data.List (nub)
24import qualified Data.Aeson as A
25import qualified Data.Aeson.TH as A
27import Control.Monad.Unify
28import Control.Arrow (second)
29import Control.Applicative
30import Control.Monad ((<=<))
32import Language.PureScript.Names
33import Language.PureScript.Kinds
34import Language.PureScript.Traversals
36-- |
37-- An identifier for the scope of a skolem variable
38--
39newtype SkolemScope = SkolemScope { runSkolemScope :: Int } deriving (Show, Eq, Ord, Data, Typeable, A.ToJSON, A.FromJSON)
41-- |
42-- The type of types
43--
44data Type
45 -- |
46 -- A unification variable of type Type
47 --
48 = TUnknown Unknown
49 -- |
50 -- A named type variable
51 --
52 | TypeVar String
53 -- |
54 -- A type wildcard, as would appear in a partial type synonym
55 --
56 | TypeWildcard
57 -- |
58 -- A type constructor
59 --
60 | TypeConstructor (Qualified ProperName)
61 -- |
62 -- A type application
63 --
64 | TypeApp Type Type
65 -- |
66 -- A type synonym which is \"saturated\", i.e. fully applied
67 --
68 | SaturatedTypeSynonym (Qualified ProperName) [Type]
69 -- |
70 -- Forall quantifier
71 --
72 | ForAll String Type (Maybe SkolemScope)
73 -- |
74 -- A type with a set of type class constraints
75 --
76 | ConstrainedType [Constraint] Type
77 -- |
78 -- A skolem constant
79 --
80 | Skolem String Int SkolemScope
81 -- |
82 -- An empty row
83 --
84 | REmpty
85 -- |
86 -- A non-empty row
87 --
88 | RCons String Type Type
89 -- |
90 -- A type with a kind annotation
91 --
92 | KindedType Type Kind
93 --
94 -- |
95 -- A placeholder used in pretty printing
96 --
97 | PrettyPrintFunction Type Type
98 -- |
99 -- A placeholder used in pretty printing
100 --
101 | PrettyPrintObject Type
102 -- |
103 -- A placeholder used in pretty printing
104 --
105 | PrettyPrintForAll [String] Type deriving (Show, Eq, Ord, Data, Typeable)
107-- |
108-- A typeclass constraint
109--
110type Constraint = (Qualified ProperName, [Type])
112$(A.deriveJSON A.defaultOptions ''Type)
114-- |
115-- Convert a row to a list of pairs of labels and types
116--
117rowToList :: Type -> ([(String, Type)], Type)
118rowToList (RCons name ty row) = let (tys, rest) = rowToList row
119 in ((name, ty):tys, rest)
120rowToList r = ([], r)
122-- |
123-- Convert a list of labels and types to a row
124--
125rowFromList :: ([(String, Type)], Type) -> Type
126rowFromList ([], r) = r
127rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
129-- |
130-- Check whether a type is a monotype
131--
132isMonoType :: Type -> Bool
133isMonoType ForAll{} = False
134isMonoType _ = True
136-- |
137-- Universally quantify a type
138--
139mkForAll :: [String] -> Type -> Type
140mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
142-- |
143-- Replace a type variable, taking into account variable shadowing
144--
145replaceTypeVars :: String -> Type -> Type -> Type
146replaceTypeVars v r = replaceAllTypeVars [(v, r)]
148-- |
149-- Replace named type variables with types
150--
151replaceAllTypeVars :: [(String, Type)] -> Type -> Type
152replaceAllTypeVars = go []
153 where
155 go :: [String] -> [(String, Type)] -> Type -> Type
156 go _ m (TypeVar v) =
157 case v `lookup` m of
158 Just r -> r
159 Nothing -> TypeVar v
160 go bs m (TypeApp t1 t2) = TypeApp (go bs m t1) (go bs m t2)
161 go bs m (SaturatedTypeSynonym name' ts) = SaturatedTypeSynonym name' $ map (go bs m) ts
162 go bs m f@(ForAll v t sco) | v `elem` keys = go bs (filter ((/= v) . fst) m) f
163 | v `elem` usedVars =
164 let v' = genName v (keys ++ bs ++ usedVars)
165 t' = go bs [(v, TypeVar v')] t
166 in ForAll v' (go (v' : bs) m t') sco
167 | otherwise = ForAll v (go (v : bs) m t) sco
168 where
169 keys = map fst m
170 usedVars = concatMap (usedTypeVariables . snd) m
171 go bs m (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs m)) cs) (go bs m t)
172 go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
173 go bs m (KindedType t k) = KindedType (go bs m t) k
174 go _ _ ty = ty
176 genName orig inUse = try 0
177 where
178 try :: Integer -> String
179 try n | (orig ++ show n) `elem` inUse = try (n + 1)
180 | otherwise = orig ++ show n
182-- |
183-- Collect all type variables appearing in a type
184--
185usedTypeVariables :: Type -> [String]
186usedTypeVariables = nub . everythingOnTypes (++) go
187 where
188 go (TypeVar v) = [v]
189 go _ = []
191-- |
192-- Collect all free type variables appearing in a type
193--
194freeTypeVariables :: Type -> [String]
195freeTypeVariables = nub . go []
196 where
197 go :: [String] -> Type -> [String]
198 go bound (TypeVar v) | v `notElem` bound = [v]
199 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
200 go bound (SaturatedTypeSynonym _ ts) = concatMap (go bound) ts
201 go bound (ForAll v t _) = go (v : bound) t
202 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
203 go bound (RCons _ t r) = go bound t ++ go bound r
204 go bound (KindedType t _) = go bound t
205 go _ _ = []
207-- |
208-- Universally quantify over all type variables appearing free in a type
209--
210quantify :: Type -> Type
211quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
213-- |
214-- Move all universal quantifiers to the front of a type
215--
216moveQuantifiersToFront :: Type -> Type
217moveQuantifiersToFront = go [] []
218 where
219 go qs cs (ForAll q ty sco) = go ((q, sco) : qs) cs ty
220 go qs cs (ConstrainedType cs' ty) = go qs (cs ++ cs') ty
221 go qs cs ty =
222 let constrained = case cs of
223 [] -> ty
224 cs' -> ConstrainedType cs' ty
225 in case qs of
226 [] -> constrained
227 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
229-- |
230-- Check if a type contains wildcards
231--
232containsWildcards :: Type -> Bool
233containsWildcards = everythingOnTypes (||) go
234 where
235 go :: Type -> Bool
236 go TypeWildcard = True
237 go _ = False
239--
240-- Traversals
241--
243everywhereOnTypes :: (Type -> Type) -> Type -> Type
244everywhereOnTypes f = go
245 where
246 go (TypeApp t1 t2) = f (TypeApp (go t1) (go t2))
247 go (SaturatedTypeSynonym name tys) = f (SaturatedTypeSynonym name (map go tys))
248 go (ForAll arg ty sco) = f (ForAll arg (go ty) sco)
249 go (ConstrainedType cs ty) = f (ConstrainedType (map (fmap (map go)) cs) (go ty))
250 go (RCons name ty rest) = f (RCons name (go ty) (go rest))
251 go (KindedType ty k) = f (KindedType (go ty) k)
252 go (PrettyPrintFunction t1 t2) = f (PrettyPrintFunction (go t1) (go t2))
253 go (PrettyPrintObject t) = f (PrettyPrintObject (go t))
254 go (PrettyPrintForAll args t) = f (PrettyPrintForAll args (go t))
255 go other = f other
257everywhereOnTypesTopDown :: (Type -> Type) -> Type -> Type
258everywhereOnTypesTopDown f = go . f
259 where
260 go (TypeApp t1 t2) = TypeApp (go (f t1)) (go (f t2))
261 go (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name (map (go . f) tys)
262 go (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
263 go (ConstrainedType cs ty) = ConstrainedType (map (fmap (map (go . f))) cs) (go (f ty))
264 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
265 go (KindedType ty k) = KindedType (go (f ty)) k
266 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
267 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
268 go (PrettyPrintForAll args t) = PrettyPrintForAll args (go (f t))
269 go other = f other
271everywhereOnTypesM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
272everywhereOnTypesM f = go
273 where
274 go (TypeApp t1 t2) = (TypeApp <$> go t1 <*> go t2) >>= f
275 go (SaturatedTypeSynonym name tys) = (SaturatedTypeSynonym name <$> mapM go tys) >>= f
276 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
277 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (sndM (mapM go)) cs <*> go ty) >>= f
278 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
279 go (KindedType ty k) = (KindedType <$> go ty <*> pure k) >>= f
280 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
281 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
282 go (PrettyPrintForAll args t) = (PrettyPrintForAll args <$> go t) >>= f
283 go other = f other
285everywhereOnTypesTopDownM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
286everywhereOnTypesTopDownM f = go <=< f
287 where
288 go (TypeApp t1 t2) = TypeApp <$> (f t1 >>= go) <*> (f t2 >>= go)
289 go (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name <$> mapM (go <=< f) tys
290 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
291 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (sndM (mapM (go <=< f))) cs <*> (f ty >>= go)
292 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
293 go (KindedType ty k) = KindedType <$> (f ty >>= go) <*> pure k
294 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
295 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
296 go (PrettyPrintForAll args t) = PrettyPrintForAll args <$> (f t >>= go)
297 go other = f other
299everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
300everythingOnTypes (<>) f = go
301 where
302 go t@(TypeApp t1 t2) = f t <> go t1 <> go t2
303 go t@(SaturatedTypeSynonym _ tys) = foldl (<>) (f t) (map go tys)
304 go t@(ForAll _ ty _) = f t <> go ty
305 go t@(ConstrainedType cs ty) = foldl (<>) (f t) (map go $ concatMap snd cs) <> go ty
306 go t@(RCons _ ty rest) = f t <> go ty <> go rest
307 go t@(KindedType ty _) = f t <> go ty
308 go t@(PrettyPrintFunction t1 t2) = f t <> go t1 <> go t2
309 go t@(PrettyPrintObject t1) = f t <> go t1
310 go t@(PrettyPrintForAll _ t1) = f t <> go t1
311 go other = f other