git.iko.soy/rowtype-yoga/purescript-native
on c4186db help sign in
276 lines 9.4 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.Traversals
31-- |
32-- An identifier for the scope of a skolem variable
33--
34newtype SkolemScope = SkolemScope { runSkolemScope :: Int } deriving (Show, Eq, Ord, Data, Typeable)
36-- |
37-- The type of types
38--
39data Type
40 -- |
41 -- A unification variable of type Type
42 --
43 = TUnknown Unknown
44 -- |
45 -- A named type variable
46 --
47 | TypeVar String
48 -- |
49 -- A type constructor
50 --
51 | TypeConstructor (Qualified ProperName)
52 -- |
53 -- A type application
54 --
55 | TypeApp Type Type
56 -- |
57 -- A type synonym which is \"saturated\", i.e. fully applied
58 --
59 | SaturatedTypeSynonym (Qualified ProperName) [Type]
60 -- |
61 -- Forall quantifier
62 --
63 | ForAll String Type (Maybe SkolemScope)
64 -- |
65 -- A type with a set of type class constraints
66 --
67 | ConstrainedType [(Qualified ProperName, [Type])] Type
68 -- |
69 -- A skolem constant
70 --
71 | Skolem String Int SkolemScope
72 -- |
73 -- An empty row
74 --
75 | REmpty
76 -- |
77 -- A non-empty row
78 --
79 | RCons String Type Type
80 -- |
81 -- A placeholder used in pretty printing
82 --
83 | PrettyPrintFunction Type Type
84 -- |
85 -- A placeholder used in pretty printing
86 --
87 | PrettyPrintArray Type
88 -- |
89 -- A placeholder used in pretty printing
90 --
91 | PrettyPrintObject Type
92 -- |
93 -- A placeholder used in pretty printing
94 --
95 | PrettyPrintForAll [String] Type deriving (Show, Eq, Data, Typeable)
97-- |
98-- Convert a row to a list of pairs of labels and types
99--
100rowToList :: Type -> ([(String, Type)], Type)
101rowToList (RCons name ty row) = let (tys, rest) = rowToList row
102 in ((name, ty):tys, rest)
103rowToList r = ([], r)
105-- |
106-- Convert a list of labels and types to a row
107--
108rowFromList :: ([(String, Type)], Type) -> Type
109rowFromList ([], r) = r
110rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
112-- |
113-- Check whether a type is a monotype
114--
115isMonoType :: Type -> Bool
116isMonoType ForAll{} = False
117isMonoType _ = True
119-- |
120-- Universally quantify a type
121--
122mkForAll :: [String] -> Type -> Type
123mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
125-- |
126-- Replace a type variable, taking into account variable shadowing
127--
128replaceTypeVars :: String -> Type -> Type -> Type
129replaceTypeVars = replaceTypeVars' []
130 where
131 replaceTypeVars' bound name replacement = go bound
132 where
133 go :: [String] -> Type -> Type
134 go _ (TypeVar v) | v == name = replacement
135 go bs (TypeApp t1 t2) = TypeApp (go bs t1) (go bs t2)
136 go bs (SaturatedTypeSynonym name' ts) = SaturatedTypeSynonym name' $ map (go bs) ts
137 go bs f@(ForAll v t sco) | v == name = f
138 | v `elem` usedTypeVariables replacement =
139 let v' = genName v (name : bs ++ usedTypeVariables replacement)
140 t' = replaceTypeVars' bs v (TypeVar v') t
141 in ForAll v' (go (v' : bs) t') sco
142 | otherwise = ForAll v (go (v : bs) t) sco
143 go bs (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs)) cs) (go bs t)
144 go bs (RCons name' t r) = RCons name' (go bs t) (go bs r)
145 go _ ty = ty
146 genName orig inUse = try 0
147 where
148 try :: Integer -> String
149 try n | (orig ++ show n) `elem` inUse = try (n + 1)
150 | otherwise = orig ++ show n
152-- |
153-- Replace named type variables with types
154--
155replaceAllTypeVars :: [(String, Type)] -> Type -> Type
156replaceAllTypeVars = foldl (\f (name, ty) -> replaceTypeVars name ty . f) id
158-- |
159-- Collect all type variables appearing in a type
160--
161usedTypeVariables :: Type -> [String]
162usedTypeVariables = nub . everythingOnTypes (++) go
163 where
164 go (TypeVar v) = [v]
165 go _ = []
167-- |
168-- Collect all free type variables appearing in a type
169--
170freeTypeVariables :: Type -> [String]
171freeTypeVariables = nub . go []
172 where
173 go :: [String] -> Type -> [String]
174 go bound (TypeVar v) | v `notElem` bound = [v]
175 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
176 go bound (SaturatedTypeSynonym _ ts) = concatMap (go bound) ts
177 go bound (ForAll v t _) = go (v : bound) t
178 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
179 go bound (RCons _ t r) = go bound t ++ go bound r
180 go _ _ = []
182-- |
183-- Universally quantify over all type variables appearing free in a type
184--
185quantify :: Type -> Type
186quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
188-- |
189-- Move all universal quantifiers to the front of a type
190--
191moveQuantifiersToFront :: Type -> Type
192moveQuantifiersToFront = go [] []
193 where
194 go qs cs (ForAll q ty sco) = go ((q, sco) : qs) cs ty
195 go qs cs (ConstrainedType cs' ty) = go qs (cs ++ cs') ty
196 go qs cs ty =
197 let constrained = case cs of
198 [] -> ty
199 cs' -> ConstrainedType cs' ty
200 in case qs of
201 [] -> constrained
202 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
204--
205-- Traversals
206--
208everywhereOnTypes :: (Type -> Type) -> Type -> Type
209everywhereOnTypes f = go
210 where
211 go (TypeApp t1 t2) = f (TypeApp (go t1) (go t2))
212 go (SaturatedTypeSynonym name tys) = f (SaturatedTypeSynonym name (map go tys))
213 go (ForAll arg ty sco) = f (ForAll arg (go ty) sco)
214 go (ConstrainedType cs ty) = f (ConstrainedType (map (fmap (map go)) cs) (go ty))
215 go (RCons name ty rest) = f (RCons name (go ty) (go rest))
216 go (PrettyPrintFunction t1 t2) = f (PrettyPrintFunction (go t1) (go t2))
217 go (PrettyPrintArray t) = f (PrettyPrintArray (go t))
218 go (PrettyPrintObject t) = f (PrettyPrintObject (go t))
219 go (PrettyPrintForAll args t) = f (PrettyPrintForAll args (go t))
220 go other = f other
222everywhereOnTypesTopDown :: (Type -> Type) -> Type -> Type
223everywhereOnTypesTopDown f = go . f
224 where
225 go (TypeApp t1 t2) = TypeApp (go (f t1)) (go (f t2))
226 go (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name (map (go . f) tys)
227 go (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
228 go (ConstrainedType cs ty) = ConstrainedType (map (fmap (map (go . f))) cs) (go (f ty))
229 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
230 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
231 go (PrettyPrintArray t) = PrettyPrintArray (go (f t))
232 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
233 go (PrettyPrintForAll args t) = PrettyPrintForAll args (go (f t))
234 go other = f other
236everywhereOnTypesM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
237everywhereOnTypesM f = go
238 where
239 go (TypeApp t1 t2) = (TypeApp <$> go t1 <*> go t2) >>= f
240 go (SaturatedTypeSynonym name tys) = (SaturatedTypeSynonym name <$> mapM go tys) >>= f
241 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
242 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (sndM (mapM go)) cs <*> go ty) >>= f
243 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
244 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
245 go (PrettyPrintArray t) = (PrettyPrintArray <$> go t) >>= f
246 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
247 go (PrettyPrintForAll args t) = (PrettyPrintForAll args <$> go t) >>= f
248 go other = f other
250everywhereOnTypesTopDownM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
251everywhereOnTypesTopDownM f = go <=< f
252 where
253 go (TypeApp t1 t2) = TypeApp <$> (f t1 >>= go) <*> (f t2 >>= go)
254 go (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name <$> mapM (go <=< f) tys
255 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
256 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (sndM (mapM (go <=< f))) cs <*> (f ty >>= go)
257 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
258 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
259 go (PrettyPrintArray t) = PrettyPrintArray <$> (f t >>= go)
260 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
261 go (PrettyPrintForAll args t) = PrettyPrintForAll args <$> (f t >>= go)
262 go other = f other
264everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
265everythingOnTypes (<>) f = go
266 where
267 go t@(TypeApp t1 t2) = f t <> go t1 <> go t2
268 go t@(SaturatedTypeSynonym _ tys) = foldl (<>) (f t) (map go tys)
269 go t@(ForAll _ ty _) = f t <> go ty
270 go t@(ConstrainedType cs ty) = foldl (<>) (f t) (map go $ concatMap snd cs) <> go ty
271 go t@(RCons _ ty rest) = f t <> go ty <> go rest
272 go t@(PrettyPrintFunction t1 t2) = f t <> go t1 <> go t2
273 go t@(PrettyPrintArray t1) = f t <> go t1
274 go t@(PrettyPrintObject t1) = f t <> go t1
275 go t@(PrettyPrintForAll _ t1) = f t <> go t1
276 go other = f other