git.iko.soy/rowtype-yoga/purescript-native
on 38034d2 help sign in
315 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 #-}
19{-# LANGUAGE CPP #-}
21module Language.PureScript.Types where
23import Data.Data
24import Data.List (nub)
25import Data.Maybe (fromMaybe)
26import qualified Data.Aeson as A
27import qualified Data.Aeson.TH as A
29import Control.Monad.Unify
30import Control.Arrow (second)
31#if __GLASGOW_HASKELL__ < 710
32import Control.Applicative
33#endif
34import Control.Monad ((<=<))
36import Language.PureScript.Names
37import Language.PureScript.Kinds
38import Language.PureScript.Traversals
40-- |
41-- An identifier for the scope of a skolem variable
42--
43newtype SkolemScope = SkolemScope { runSkolemScope :: Int } deriving (Show, Read, Eq, Ord, Data, Typeable, A.ToJSON, A.FromJSON)
45-- |
46-- The type of types
47--
48data Type
49 -- |
50 -- A unification variable of type Type
51 --
52 = TUnknown Unknown
53 -- |
54 -- A named type variable
55 --
56 | TypeVar String
57 -- |
58 -- A type wildcard, as would appear in a partial type synonym
59 --
60 | TypeWildcard
61 -- |
62 -- A type constructor
63 --
64 | TypeConstructor (Qualified ProperName)
65 -- |
66 -- A type application
67 --
68 | TypeApp Type 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, Read,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) = fromMaybe (TypeVar v) (v `lookup` m)
157 go bs m (TypeApp t1 t2) = TypeApp (go bs m t1) (go bs m t2)
158 go bs m f@(ForAll v t sco) | v `elem` keys = go bs (filter ((/= v) . fst) m) f
159 | v `elem` usedVars =
160 let v' = genName v (keys ++ bs ++ usedVars)
161 t' = go bs [(v, TypeVar v')] t
162 in ForAll v' (go (v' : bs) m t') sco
163 | otherwise = ForAll v (go (v : bs) m t) sco
164 where
165 keys = map fst m
166 usedVars = concatMap (usedTypeVariables . snd) m
167 go bs m (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs m)) cs) (go bs m t)
168 go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
169 go bs m (KindedType t k) = KindedType (go bs m t) k
170 go _ _ ty = ty
172 genName orig inUse = try 0
173 where
174 try :: Integer -> String
175 try n | (orig ++ show n) `elem` inUse = try (n + 1)
176 | otherwise = orig ++ show n
178-- |
179-- Collect all type variables appearing in a type
180--
181usedTypeVariables :: Type -> [String]
182usedTypeVariables = nub . everythingOnTypes (++) go
183 where
184 go (TypeVar v) = [v]
185 go _ = []
187-- |
188-- Collect all free type variables appearing in a type
189--
190freeTypeVariables :: Type -> [String]
191freeTypeVariables = nub . go []
192 where
193 go :: [String] -> Type -> [String]
194 go bound (TypeVar v) | v `notElem` bound = [v]
195 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
196 go bound (ForAll v t _) = go (v : bound) t
197 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
198 go bound (RCons _ t r) = go bound t ++ go bound r
199 go bound (KindedType t _) = go bound t
200 go _ _ = []
202-- |
203-- Universally quantify over all type variables appearing free in a type
204--
205quantify :: Type -> Type
206quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
208-- |
209-- Move all universal quantifiers to the front of a type
210--
211moveQuantifiersToFront :: Type -> Type
212moveQuantifiersToFront = go [] []
213 where
214 go qs cs (ForAll q ty sco) = go ((q, sco) : qs) cs ty
215 go qs cs (ConstrainedType cs' ty) = go qs (cs ++ cs') ty
216 go qs cs ty =
217 let constrained = case cs of
218 [] -> ty
219 cs' -> ConstrainedType cs' ty
220 in case qs of
221 [] -> constrained
222 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
224-- |
225-- Check if a type contains wildcards
226--
227containsWildcards :: Type -> Bool
228containsWildcards = everythingOnTypes (||) go
229 where
230 go :: Type -> Bool
231 go TypeWildcard = True
232 go _ = False
234--
235-- Traversals
236--
238everywhereOnTypes :: (Type -> Type) -> Type -> Type
239everywhereOnTypes f = go
240 where
241 go (TypeApp t1 t2) = f (TypeApp (go t1) (go t2))
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 (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
256 go (ConstrainedType cs ty) = ConstrainedType (map (fmap (map (go . f))) cs) (go (f ty))
257 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
258 go (KindedType ty k) = KindedType (go (f ty)) k
259 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
260 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
261 go (PrettyPrintForAll args t) = PrettyPrintForAll args (go (f t))
262 go other = f other
264everywhereOnTypesM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
265everywhereOnTypesM f = go
266 where
267 go (TypeApp t1 t2) = (TypeApp <$> go t1 <*> go t2) >>= f
268 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
269 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (sndM (mapM go)) cs <*> go ty) >>= f
270 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
271 go (KindedType ty k) = (KindedType <$> go ty <*> pure k) >>= f
272 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
273 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
274 go (PrettyPrintForAll args t) = (PrettyPrintForAll args <$> go t) >>= f
275 go other = f other
277everywhereOnTypesTopDownM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
278everywhereOnTypesTopDownM f = go <=< f
279 where
280 go (TypeApp t1 t2) = TypeApp <$> (f t1 >>= go) <*> (f t2 >>= go)
281 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
282 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (sndM (mapM (go <=< f))) cs <*> (f ty >>= go)
283 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
284 go (KindedType ty k) = KindedType <$> (f ty >>= go) <*> pure k
285 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
286 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
287 go (PrettyPrintForAll args t) = PrettyPrintForAll args <$> (f t >>= go)
288 go other = f other
290everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
291everythingOnTypes (<>) f = go
292 where
293 go t@(TypeApp t1 t2) = f t <> go t1 <> go t2
294 go t@(ForAll _ ty _) = f t <> go ty
295 go t@(ConstrainedType cs ty) = foldl (<>) (f t) (map go $ concatMap snd cs) <> go ty
296 go t@(RCons _ ty rest) = f t <> go ty <> go rest
297 go t@(KindedType ty _) = f t <> go ty
298 go t@(PrettyPrintFunction t1 t2) = f t <> go t1 <> go t2
299 go t@(PrettyPrintObject t1) = f t <> go t1
300 go t@(PrettyPrintForAll _ t1) = f t <> go t1
301 go other = f other
303everythingWithContextOnTypes :: s -> r -> (r -> r -> r) -> (s -> Type -> (s, r)) -> Type -> r
304everythingWithContextOnTypes s0 r0 (<>) f = go' s0
305 where
306 go' s t = let (s', r) = f s t in r <> go s' t
307 go s (TypeApp t1 t2) = go' s t1 <> go' s t2
308 go s (ForAll _ ty _) = go' s ty
309 go s (ConstrainedType cs ty) = foldl (<>) r0 (map (go' s) $ concatMap snd cs) <> go' s ty
310 go s (RCons _ ty rest) = go' s ty <> go' s rest
311 go s (KindedType ty _) = go' s ty
312 go s (PrettyPrintFunction t1 t2) = go' s t1 <> go' s t2
313 go s (PrettyPrintObject t1) = go' s t1
314 go s (PrettyPrintForAll _ t1) = go' s t1
315 go _ _ = r0