git.iko.soy/rowtype-yoga/purescript-native
on d2dfad0 help sign in
355 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 (OpName 'TypeOpName))
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 -- A placeholder used in pretty printing
83 --
84 | PrettyPrintFunction Type Type
85 -- |
86 -- A placeholder used in pretty printing
87 --
88 | PrettyPrintObject Type
89 -- |
90 -- A placeholder used in pretty printing
91 --
92 | PrettyPrintForAll [String] Type
93 -- |
94 -- Binary operator application. During the rebracketing phase of desugaring,
95 -- this data constructor will be removed.
96 --
97 | BinaryNoParensType Type Type Type
98 -- |
99 -- Explicit parentheses. During the rebracketing phase of desugaring, this
100 -- data constructor will be removed.
101 --
102 -- Note: although it seems this constructor is not used, it _is_ useful,
103 -- since it prevents certain traversals from matching.
104 --
105 | ParensInType Type
106 deriving (Show, Read, Eq, Ord)
108-- | Additional data relevant to type class constraints
109data ConstraintData
110 = PartialConstraintData [[String]] Bool
111 -- ^ Data to accompany a Partial constraint generated by the exhaustivity checker.
112 -- It contains (rendered) binder information for those binders which were
113 -- not matched, and a flag indicating whether the list was truncated or not.
114 -- Note: we use 'String' here because using 'Binder' would introduce a cyclic
115 -- dependency in the module graph.
116 deriving (Show, Read, Eq, Ord)
118-- | A typeclass constraint
119data Constraint = Constraint
120 { constraintClass :: Qualified (ProperName 'ClassName)
121 -- ^ constraint class name
122 , constraintArgs :: [Type]
123 -- ^ type arguments
124 , constraintData :: Maybe ConstraintData
125 -- ^ additional data relevant to this constraint
126 } deriving (Show, Read, Eq, Ord)
128mapConstraintArgs :: ([Type] -> [Type]) -> Constraint -> Constraint
129mapConstraintArgs f c = c { constraintArgs = f (constraintArgs c) }
131overConstraintArgs :: Functor f => ([Type] -> f [Type]) -> Constraint -> f Constraint
132overConstraintArgs f c = (\args -> c { constraintArgs = args }) <$> f (constraintArgs c)
134$(A.deriveJSON A.defaultOptions ''Type)
135$(A.deriveJSON A.defaultOptions ''Constraint)
136$(A.deriveJSON A.defaultOptions ''ConstraintData)
138-- |
139-- Convert a row to a list of pairs of labels and types
140--
141rowToList :: Type -> ([(String, Type)], Type)
142rowToList (RCons name ty row) = let (tys, rest) = rowToList row
143 in ((name, ty):tys, rest)
144rowToList r = ([], r)
146-- |
147-- Convert a list of labels and types to a row
148--
149rowFromList :: ([(String, Type)], Type) -> Type
150rowFromList ([], r) = r
151rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
153-- |
154-- Check whether a type is a monotype
155--
156isMonoType :: Type -> Bool
157isMonoType ForAll{} = False
158isMonoType _ = True
160-- |
161-- Universally quantify a type
162--
163mkForAll :: [String] -> Type -> Type
164mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
166-- |
167-- Replace a type variable, taking into account variable shadowing
168--
169replaceTypeVars :: String -> Type -> Type -> Type
170replaceTypeVars v r = replaceAllTypeVars [(v, r)]
172-- |
173-- Replace named type variables with types
174--
175replaceAllTypeVars :: [(String, Type)] -> Type -> Type
176replaceAllTypeVars = go []
177 where
179 go :: [String] -> [(String, Type)] -> Type -> Type
180 go _ m (TypeVar v) = fromMaybe (TypeVar v) (v `lookup` m)
181 go bs m (TypeApp t1 t2) = TypeApp (go bs m t1) (go bs m t2)
182 go bs m f@(ForAll v t sco) | v `elem` keys = go bs (filter ((/= v) . fst) m) f
183 | v `elem` usedVars =
184 let v' = genName v (keys ++ bs ++ usedVars)
185 t' = go bs [(v, TypeVar v')] t
186 in ForAll v' (go (v' : bs) m t') sco
187 | otherwise = ForAll v (go (v : bs) m t) sco
188 where
189 keys = map fst m
190 usedVars = concatMap (usedTypeVariables . snd) m
191 go bs m (ConstrainedType cs t) = ConstrainedType (map (mapConstraintArgs (map (go bs m))) cs) (go bs m t)
192 go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
193 go bs m (KindedType t k) = KindedType (go bs m t) k
194 go bs m (BinaryNoParensType t1 t2 t3) = BinaryNoParensType (go bs m t1) (go bs m t2) (go bs m t3)
195 go bs m (ParensInType t) = ParensInType (go bs m t)
196 go _ _ ty = ty
198 genName orig inUse = try 0
199 where
200 try :: Integer -> String
201 try n | (orig ++ show n) `elem` inUse = try (n + 1)
202 | otherwise = orig ++ show n
204-- |
205-- Collect all type variables appearing in a type
206--
207usedTypeVariables :: Type -> [String]
208usedTypeVariables = nub . everythingOnTypes (++) go
209 where
210 go (TypeVar v) = [v]
211 go _ = []
213-- |
214-- Collect all free type variables appearing in a type
215--
216freeTypeVariables :: Type -> [String]
217freeTypeVariables = nub . go []
218 where
219 go :: [String] -> Type -> [String]
220 go bound (TypeVar v) | v `notElem` bound = [v]
221 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
222 go bound (ForAll v t _) = go (v : bound) t
223 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . constraintArgs) cs ++ go bound t
224 go bound (RCons _ t r) = go bound t ++ go bound r
225 go bound (KindedType t _) = go bound t
226 go bound (BinaryNoParensType t1 t2 t3) = go bound t1 ++ go bound t2 ++ go bound t3
227 go bound (ParensInType t) = go bound t
228 go _ _ = []
230-- |
231-- Universally quantify over all type variables appearing free in a type
232--
233quantify :: Type -> Type
234quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
236-- |
237-- Move all universal quantifiers to the front of a type
238--
239moveQuantifiersToFront :: Type -> Type
240moveQuantifiersToFront = go [] []
241 where
242 go qs cs (ForAll q ty sco) = go ((q, sco) : qs) cs ty
243 go qs cs (ConstrainedType cs' ty) = go qs (cs ++ cs') ty
244 go qs cs ty =
245 let constrained = case cs of
246 [] -> ty
247 cs' -> ConstrainedType cs' ty
248 in case qs of
249 [] -> constrained
250 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
252-- |
253-- Check if a type contains wildcards
254--
255containsWildcards :: Type -> Bool
256containsWildcards = everythingOnTypes (||) go
257 where
258 go :: Type -> Bool
259 go TypeWildcard{} = True
260 go _ = False
262--
263-- Traversals
264--
266everywhereOnTypes :: (Type -> Type) -> Type -> Type
267everywhereOnTypes f = go
268 where
269 go (TypeApp t1 t2) = f (TypeApp (go t1) (go t2))
270 go (ForAll arg ty sco) = f (ForAll arg (go ty) sco)
271 go (ConstrainedType cs ty) = f (ConstrainedType (map (mapConstraintArgs (map go)) cs) (go ty))
272 go (RCons name ty rest) = f (RCons name (go ty) (go rest))
273 go (KindedType ty k) = f (KindedType (go ty) k)
274 go (PrettyPrintFunction t1 t2) = f (PrettyPrintFunction (go t1) (go t2))
275 go (PrettyPrintObject t) = f (PrettyPrintObject (go t))
276 go (PrettyPrintForAll args t) = f (PrettyPrintForAll args (go t))
277 go (BinaryNoParensType t1 t2 t3) = f (BinaryNoParensType (go t1) (go t2) (go t3))
278 go (ParensInType t) = f (ParensInType (go t))
279 go other = f other
281everywhereOnTypesTopDown :: (Type -> Type) -> Type -> Type
282everywhereOnTypesTopDown f = go . f
283 where
284 go (TypeApp t1 t2) = TypeApp (go (f t1)) (go (f t2))
285 go (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
286 go (ConstrainedType cs ty) = ConstrainedType (map (mapConstraintArgs (map (go . f))) cs) (go (f ty))
287 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
288 go (KindedType ty k) = KindedType (go (f ty)) k
289 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
290 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
291 go (PrettyPrintForAll args t) = PrettyPrintForAll args (go (f t))
292 go (BinaryNoParensType t1 t2 t3) = BinaryNoParensType (f (go t1)) (f (go t2)) (f (go t3))
293 go (ParensInType t) = ParensInType (f (go t))
294 go other = f other
296everywhereOnTypesM :: Monad m => (Type -> m Type) -> Type -> m Type
297everywhereOnTypesM f = go
298 where
299 go (TypeApp t1 t2) = (TypeApp <$> go t1 <*> go t2) >>= f
300 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
301 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (overConstraintArgs (mapM go)) cs <*> go ty) >>= f
302 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
303 go (KindedType ty k) = (KindedType <$> go ty <*> pure k) >>= f
304 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
305 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
306 go (PrettyPrintForAll args t) = (PrettyPrintForAll args <$> go t) >>= f
307 go (BinaryNoParensType t1 t2 t3) = (BinaryNoParensType <$> go t1 <*> go t2 <*> go t3) >>= f
308 go (ParensInType t) = (ParensInType <$> go t) >>= f
309 go other = f other
311everywhereOnTypesTopDownM :: Monad m => (Type -> m Type) -> Type -> m Type
312everywhereOnTypesTopDownM f = go <=< f
313 where
314 go (TypeApp t1 t2) = TypeApp <$> (f t1 >>= go) <*> (f t2 >>= go)
315 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
316 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (overConstraintArgs (mapM (go <=< f))) cs <*> (f ty >>= go)
317 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
318 go (KindedType ty k) = KindedType <$> (f ty >>= go) <*> pure k
319 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
320 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
321 go (PrettyPrintForAll args t) = PrettyPrintForAll args <$> (f t >>= go)
322 go (BinaryNoParensType t1 t2 t3) = BinaryNoParensType <$> (f t1 >>= go) <*> (f t2 >>= go) <*> (f t3 >>= go)
323 go (ParensInType t) = ParensInType <$> (f t >>= go)
324 go other = f other
326everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
327everythingOnTypes (<>) f = go
328 where
329 go t@(TypeApp t1 t2) = f t <> go t1 <> go t2
330 go t@(ForAll _ ty _) = f t <> go ty
331 go t@(ConstrainedType cs ty) = foldl (<>) (f t) (map go $ concatMap constraintArgs cs) <> go ty
332 go t@(RCons _ ty rest) = f t <> go ty <> go rest
333 go t@(KindedType ty _) = f t <> go ty
334 go t@(PrettyPrintFunction t1 t2) = f t <> go t1 <> go t2
335 go t@(PrettyPrintObject t1) = f t <> go t1
336 go t@(PrettyPrintForAll _ t1) = f t <> go t1
337 go t@(BinaryNoParensType t1 t2 t3) = f t <> go t1 <> go t2 <> go t3
338 go t@(ParensInType t1) = f t <> go t1
339 go other = f other
341everythingWithContextOnTypes :: s -> r -> (r -> r -> r) -> (s -> Type -> (s, r)) -> Type -> r
342everythingWithContextOnTypes s0 r0 (<>) f = go' s0
343 where
344 go' s t = let (s', r) = f s t in r <> go s' t
345 go s (TypeApp t1 t2) = go' s t1 <> go' s t2
346 go s (ForAll _ ty _) = go' s ty
347 go s (ConstrainedType cs ty) = foldl (<>) r0 (map (go' s) $ concatMap constraintArgs cs) <> go' s ty
348 go s (RCons _ ty rest) = go' s ty <> go' s rest
349 go s (KindedType ty _) = go' s ty
350 go s (PrettyPrintFunction t1 t2) = go' s t1 <> go' s t2
351 go s (PrettyPrintObject t1) = go' s t1
352 go s (PrettyPrintForAll _ t1) = go' s t1
353 go s (BinaryNoParensType t1 t2 t3) = go' s t1 <> go' s t2 <> go' s t3
354 go s (ParensInType t1) = go' s t1
355 go _ _ = r0