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