git.iko.soy/rowtype-yoga/purescript-native
on 447c7a0 help sign in
356 lines 12 kB history raw
1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE TemplateHaskell #-}
4-- |
5-- Data types for types
6--
7module Language.PureScript.Types where
9import Prelude.Compat
11import Control.Monad ((<=<))
13import Data.List (nub)
14import Data.Maybe (fromMaybe)
15import qualified Data.Aeson as A
16import qualified Data.Aeson.TH as A
18import Language.PureScript.AST.SourcePos
19import Language.PureScript.Kinds
20import Language.PureScript.Names
22-- |
23-- An identifier for the scope of a skolem variable
24--
25newtype SkolemScope = SkolemScope { runSkolemScope :: Int }
26 deriving (Show, Read, Eq, Ord, A.ToJSON, A.FromJSON)
28-- |
29-- The type of types
30--
31data Type
32 -- |
33 -- A unification variable of type Type
34 --
35 = TUnknown Int
36 -- |
37 -- A named type variable
38 --
39 | TypeVar String
40 -- |
41 -- A type wildcard, as would appear in a partial type synonym
42 --
43 | TypeWildcard SourceSpan
44 -- |
45 -- A type constructor
46 --
47 | TypeConstructor (Qualified (ProperName 'TypeName))
48 -- |
49 -- A type operator. This will be desugared into a type constructor during the
50 -- "operators" phase of desugaring.
51 --
52 | TypeOp (Qualified Ident)
53 -- |
54 -- A type application
55 --
56 | TypeApp Type Type
57 -- |
58 -- Forall quantifier
59 --
60 | ForAll String Type (Maybe SkolemScope)
61 -- |
62 -- A type with a set of type class constraints
63 --
64 | ConstrainedType [Constraint] Type
65 -- |
66 -- A skolem constant
67 --
68 | Skolem String Int SkolemScope (Maybe SourceSpan)
69 -- |
70 -- An empty row
71 --
72 | REmpty
73 -- |
74 -- A non-empty row
75 --
76 | RCons String Type Type
77 -- |
78 -- A type with a kind annotation
79 --
80 | KindedType Type Kind
81 --
82 -- |
83 -- A placeholder used in pretty printing
84 --
85 | PrettyPrintFunction Type Type
86 -- |
87 -- A placeholder used in pretty printing
88 --
89 | PrettyPrintObject Type
90 -- |
91 -- A placeholder used in pretty printing
92 --
93 | PrettyPrintForAll [String] Type
94 -- |
95 -- Binary operator application. During the rebracketing phase of desugaring,
96 -- this data constructor will be removed.
97 --
98 | BinaryNoParensType Type Type Type
99 -- |
100 -- Explicit parentheses. During the rebracketing phase of desugaring, this
101 -- data constructor will be removed.
102 --
103 -- Note: although it seems this constructor is not used, it _is_ useful,
104 -- since it prevents certain traversals from matching.
105 --
106 | ParensInType Type
107 deriving (Show, Read, Eq, Ord)
109-- | Additional data relevant to type class constraints
110data ConstraintData
111 = PartialConstraintData [[String]] Bool
112 -- ^ Data to accompany a Partial constraint generated by the exhaustivity checker.
113 -- It contains (rendered) binder information for those binders which were
114 -- not matched, and a flag indicating whether the list was truncated or not.
115 -- Note: we use 'String' here because using 'Binder' would introduce a cyclic
116 -- dependency in the module graph.
117 deriving (Show, Read, Eq, Ord)
119-- | A typeclass constraint
120data Constraint = Constraint
121 { constraintClass :: Qualified (ProperName 'ClassName)
122 -- ^ constraint class name
123 , constraintArgs :: [Type]
124 -- ^ type arguments
125 , constraintData :: Maybe ConstraintData
126 -- ^ additional data relevant to this constraint
127 } deriving (Show, Read, Eq, Ord)
129mapConstraintArgs :: ([Type] -> [Type]) -> Constraint -> Constraint
130mapConstraintArgs f c = c { constraintArgs = f (constraintArgs c) }
132overConstraintArgs :: Functor f => ([Type] -> f [Type]) -> Constraint -> f Constraint
133overConstraintArgs f c = (\args -> c { constraintArgs = args }) <$> f (constraintArgs c)
135$(A.deriveJSON A.defaultOptions ''Type)
136$(A.deriveJSON A.defaultOptions ''Constraint)
137$(A.deriveJSON A.defaultOptions ''ConstraintData)
139-- |
140-- Convert a row to a list of pairs of labels and types
141--
142rowToList :: Type -> ([(String, Type)], Type)
143rowToList (RCons name ty row) = let (tys, rest) = rowToList row
144 in ((name, ty):tys, rest)
145rowToList r = ([], r)
147-- |
148-- Convert a list of labels and types to a row
149--
150rowFromList :: ([(String, Type)], Type) -> Type
151rowFromList ([], r) = r
152rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
154-- |
155-- Check whether a type is a monotype
156--
157isMonoType :: Type -> Bool
158isMonoType ForAll{} = False
159isMonoType _ = True
161-- |
162-- Universally quantify a type
163--
164mkForAll :: [String] -> Type -> Type
165mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
167-- |
168-- Replace a type variable, taking into account variable shadowing
169--
170replaceTypeVars :: String -> Type -> Type -> Type
171replaceTypeVars v r = replaceAllTypeVars [(v, r)]
173-- |
174-- Replace named type variables with types
175--
176replaceAllTypeVars :: [(String, Type)] -> Type -> Type
177replaceAllTypeVars = go []
178 where
180 go :: [String] -> [(String, Type)] -> Type -> Type
181 go _ m (TypeVar v) = fromMaybe (TypeVar v) (v `lookup` m)
182 go bs m (TypeApp t1 t2) = TypeApp (go bs m t1) (go bs m t2)
183 go bs m f@(ForAll v t sco) | v `elem` keys = go bs (filter ((/= v) . fst) m) f
184 | v `elem` usedVars =
185 let v' = genName v (keys ++ bs ++ usedVars)
186 t' = go bs [(v, TypeVar v')] t
187 in ForAll v' (go (v' : bs) m t') sco
188 | otherwise = ForAll v (go (v : bs) m t) sco
189 where
190 keys = map fst m
191 usedVars = concatMap (usedTypeVariables . snd) m
192 go bs m (ConstrainedType cs t) = ConstrainedType (map (mapConstraintArgs (map (go bs m))) cs) (go bs m t)
193 go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
194 go bs m (KindedType t k) = KindedType (go bs m t) k
195 go bs m (BinaryNoParensType t1 t2 t3) = BinaryNoParensType (go bs m t1) (go bs m t2) (go bs m t3)
196 go bs m (ParensInType t) = ParensInType (go bs m t)
197 go _ _ ty = ty
199 genName orig inUse = try 0
200 where
201 try :: Integer -> String
202 try n | (orig ++ show n) `elem` inUse = try (n + 1)
203 | otherwise = orig ++ show n
205-- |
206-- Collect all type variables appearing in a type
207--
208usedTypeVariables :: Type -> [String]
209usedTypeVariables = nub . everythingOnTypes (++) go
210 where
211 go (TypeVar v) = [v]
212 go _ = []
214-- |
215-- Collect all free type variables appearing in a type
216--
217freeTypeVariables :: Type -> [String]
218freeTypeVariables = nub . go []
219 where
220 go :: [String] -> Type -> [String]
221 go bound (TypeVar v) | v `notElem` bound = [v]
222 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
223 go bound (ForAll v t _) = go (v : bound) t
224 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . constraintArgs) cs ++ go bound t
225 go bound (RCons _ t r) = go bound t ++ go bound r
226 go bound (KindedType t _) = go bound t
227 go bound (BinaryNoParensType t1 t2 t3) = go bound t1 ++ go bound t2 ++ go bound t3
228 go bound (ParensInType t) = go bound t
229 go _ _ = []
231-- |
232-- Universally quantify over all type variables appearing free in a type
233--
234quantify :: Type -> Type
235quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
237-- |
238-- Move all universal quantifiers to the front of a type
239--
240moveQuantifiersToFront :: Type -> Type
241moveQuantifiersToFront = go [] []
242 where
243 go qs cs (ForAll q ty sco) = go ((q, sco) : qs) cs ty
244 go qs cs (ConstrainedType cs' ty) = go qs (cs ++ cs') ty
245 go qs cs ty =
246 let constrained = case cs of
247 [] -> ty
248 cs' -> ConstrainedType cs' ty
249 in case qs of
250 [] -> constrained
251 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
253-- |
254-- Check if a type contains wildcards
255--
256containsWildcards :: Type -> Bool
257containsWildcards = everythingOnTypes (||) go
258 where
259 go :: Type -> Bool
260 go TypeWildcard{} = True
261 go _ = False
263--
264-- Traversals
265--
267everywhereOnTypes :: (Type -> Type) -> Type -> Type
268everywhereOnTypes f = go
269 where
270 go (TypeApp t1 t2) = f (TypeApp (go t1) (go t2))
271 go (ForAll arg ty sco) = f (ForAll arg (go ty) sco)
272 go (ConstrainedType cs ty) = f (ConstrainedType (map (mapConstraintArgs (map go)) cs) (go ty))
273 go (RCons name ty rest) = f (RCons name (go ty) (go rest))
274 go (KindedType ty k) = f (KindedType (go ty) k)
275 go (PrettyPrintFunction t1 t2) = f (PrettyPrintFunction (go t1) (go t2))
276 go (PrettyPrintObject t) = f (PrettyPrintObject (go t))
277 go (PrettyPrintForAll args t) = f (PrettyPrintForAll args (go t))
278 go (BinaryNoParensType t1 t2 t3) = f (BinaryNoParensType (go t1) (go t2) (go t3))
279 go (ParensInType t) = f (ParensInType (go t))
280 go other = f other
282everywhereOnTypesTopDown :: (Type -> Type) -> Type -> Type
283everywhereOnTypesTopDown f = go . f
284 where
285 go (TypeApp t1 t2) = TypeApp (go (f t1)) (go (f t2))
286 go (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
287 go (ConstrainedType cs ty) = ConstrainedType (map (mapConstraintArgs (map (go . f))) cs) (go (f ty))
288 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
289 go (KindedType ty k) = KindedType (go (f ty)) k
290 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
291 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
292 go (PrettyPrintForAll args t) = PrettyPrintForAll args (go (f t))
293 go (BinaryNoParensType t1 t2 t3) = BinaryNoParensType (f (go t1)) (f (go t2)) (f (go t3))
294 go (ParensInType t) = ParensInType (f (go t))
295 go other = f other
297everywhereOnTypesM :: Monad m => (Type -> m Type) -> Type -> m Type
298everywhereOnTypesM f = go
299 where
300 go (TypeApp t1 t2) = (TypeApp <$> go t1 <*> go t2) >>= f
301 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
302 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (overConstraintArgs (mapM go)) cs <*> go ty) >>= f
303 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
304 go (KindedType ty k) = (KindedType <$> go ty <*> pure k) >>= f
305 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
306 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
307 go (PrettyPrintForAll args t) = (PrettyPrintForAll args <$> go t) >>= f
308 go (BinaryNoParensType t1 t2 t3) = (BinaryNoParensType <$> go t1 <*> go t2 <*> go t3) >>= f
309 go (ParensInType t) = (ParensInType <$> go t) >>= f
310 go other = f other
312everywhereOnTypesTopDownM :: Monad m => (Type -> m Type) -> Type -> m Type
313everywhereOnTypesTopDownM f = go <=< f
314 where
315 go (TypeApp t1 t2) = TypeApp <$> (f t1 >>= go) <*> (f t2 >>= go)
316 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
317 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (overConstraintArgs (mapM (go <=< f))) cs <*> (f ty >>= go)
318 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
319 go (KindedType ty k) = KindedType <$> (f ty >>= go) <*> pure k
320 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
321 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
322 go (PrettyPrintForAll args t) = PrettyPrintForAll args <$> (f t >>= go)
323 go (BinaryNoParensType t1 t2 t3) = BinaryNoParensType <$> (f t1 >>= go) <*> (f t2 >>= go) <*> (f t3 >>= go)
324 go (ParensInType t) = ParensInType <$> (f t >>= go)
325 go other = f other
327everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
328everythingOnTypes (<>) f = go
329 where
330 go t@(TypeApp t1 t2) = f t <> go t1 <> go t2
331 go t@(ForAll _ ty _) = f t <> go ty
332 go t@(ConstrainedType cs ty) = foldl (<>) (f t) (map go $ concatMap constraintArgs cs) <> go ty
333 go t@(RCons _ ty rest) = f t <> go ty <> go rest
334 go t@(KindedType ty _) = f t <> go ty
335 go t@(PrettyPrintFunction t1 t2) = f t <> go t1 <> go t2
336 go t@(PrettyPrintObject t1) = f t <> go t1
337 go t@(PrettyPrintForAll _ t1) = f t <> go t1
338 go t@(BinaryNoParensType t1 t2 t3) = f t <> go t1 <> go t2 <> go t3
339 go t@(ParensInType t1) = f t <> go t1
340 go other = f other
342everythingWithContextOnTypes :: s -> r -> (r -> r -> r) -> (s -> Type -> (s, r)) -> Type -> r
343everythingWithContextOnTypes s0 r0 (<>) f = go' s0
344 where
345 go' s t = let (s', r) = f s t in r <> go s' t
346 go s (TypeApp t1 t2) = go' s t1 <> go' s t2
347 go s (ForAll _ ty _) = go' s ty
348 go s (ConstrainedType cs ty) = foldl (<>) r0 (map (go' s) $ concatMap constraintArgs cs) <> go' s ty
349 go s (RCons _ ty rest) = go' s ty <> go' s rest
350 go s (KindedType ty _) = go' s ty
351 go s (PrettyPrintFunction t1 t2) = go' s t1 <> go' s t2
352 go s (PrettyPrintObject t1) = go' s t1
353 go s (PrettyPrintForAll _ t1) = go' s t1
354 go s (BinaryNoParensType t1 t2 t3) = go' s t1 <> go' s t2 <> go' s t3
355 go s (ParensInType t1) = go' s t1
356 go _ _ = r0