1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE TemplateHaskell #-}
7module Language.PureScript.Types where
11import Control.Monad ((<=<))
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
23-- An identifier for the scope of a skolem variable
25newtype SkolemScope = SkolemScope { runSkolemScope :: Int }
26 deriving (Show, Read, Eq, Ord, A.ToJSON, A.FromJSON)
33 -- A unification variable of type Type
37 -- A named type variable
41 -- A type wildcard, as would appear in a partial type synonym
43 | TypeWildcard SourceSpan
47 | TypeConstructor (Qualified (ProperName 'TypeName))
49 -- A type operator. This will be desugared into a type constructor during the
50 -- "operators" phase of desugaring.
52 | TypeOp (Qualified (OpName 'TypeOpName))
60 | ForAll String Type (Maybe SkolemScope)
62 -- A type with a set of type class constraints
64 | ConstrainedType [Constraint] Type
68 | Skolem String Int SkolemScope (Maybe SourceSpan)
76 | RCons String Type Type
78 -- A type with a kind annotation
80 | KindedType Type Kind
82 -- A placeholder used in pretty printing
84 | PrettyPrintFunction Type Type
86 -- A placeholder used in pretty printing
88 | PrettyPrintObject Type
90 -- A placeholder used in pretty printing
92 | PrettyPrintForAll [String] Type
94 -- Binary operator application. During the rebracketing phase of desugaring,
95 -- this data constructor will be removed.
97 | BinaryNoParensType Type Type Type
99 -- Explicit parentheses. During the rebracketing phase of desugaring, this
100 -- data constructor will be removed.
102 -- Note: although it seems this constructor is not used, it _is_ useful,
103 -- since it prevents certain traversals from matching.
106 deriving (Show, Read, Eq, Ord)
108-- | Additional data relevant to type class constraints
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]
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)
139-- Convert a row to a list of pairs of labels and types
141rowToList :: Type -> ([(String, Type)], Type)
142rowToList (RCons name ty row) = let (tys, rest) = rowToList row
143 in ((name, ty):tys, rest)
147-- Convert a list of labels and types to a row
149rowFromList :: ([(String, Type)], Type) -> Type
150rowFromList ([], r) = r
151rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
154-- Check whether a type is a monotype
156isMonoType :: Type -> Bool
157isMonoType ForAll{} = False
161-- Universally quantify a type
163mkForAll :: [String] -> Type -> Type
164mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
167-- Replace a type variable, taking into account variable shadowing
169replaceTypeVars :: String -> Type -> Type -> Type
170replaceTypeVars v r = replaceAllTypeVars [(v, r)]
173-- Replace named type variables with types
175replaceAllTypeVars :: [(String, Type)] -> Type -> Type
176replaceAllTypeVars = go []
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
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)
198 genName orig inUse = try 0
200 try :: Integer -> String
201 try n | (orig ++ show n) `elem` inUse = try (n + 1)
202 | otherwise = orig ++ show n
205-- Collect all type variables appearing in a type
207usedTypeVariables :: Type -> [String]
208usedTypeVariables = nub . everythingOnTypes (++) go
214-- Collect all free type variables appearing in a type
216freeTypeVariables :: Type -> [String]
217freeTypeVariables = nub . go []
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
231-- Universally quantify over all type variables appearing free in a type
233quantify :: Type -> Type
234quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
237-- Move all universal quantifiers to the front of a type
239moveQuantifiersToFront :: Type -> Type
240moveQuantifiersToFront = go [] []
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
245 let constrained = case cs of
247 cs' -> ConstrainedType cs' ty
250 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
253-- Check if a type contains wildcards
255containsWildcards :: Type -> Bool
256containsWildcards = everythingOnTypes (||) go
259 go TypeWildcard{} = True
266everywhereOnTypes :: (Type -> Type) -> Type -> Type
267everywhereOnTypes f = go
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))
281everywhereOnTypesTopDown :: (Type -> Type) -> Type -> Type
282everywhereOnTypesTopDown f = go . f
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))
296everywhereOnTypesM :: Monad m => (Type -> m Type) -> Type -> m Type
297everywhereOnTypesM f = go
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
311everywhereOnTypesTopDownM :: Monad m => (Type -> m Type) -> Type -> m Type
312everywhereOnTypesTopDownM f = go <=< f
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)
326everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
327everythingOnTypes (<>) f = go
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
341everythingWithContextOnTypes :: s -> r -> (r -> r -> r) -> (s -> Type -> (s, r)) -> Type -> r
342everythingWithContextOnTypes s0 r0 (<>) f = go' s0
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
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list