1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE TemplateHaskell #-}
7module Language.PureScript.Types where
13import Data.Maybe (fromMaybe)
14import qualified Data.Aeson as A
15import qualified Data.Aeson.TH as A
17import Control.Arrow (second)
18import Control.Monad ((<=<))
20import Language.PureScript.Names
21import Language.PureScript.Kinds
22import Language.PureScript.Traversals
23import Language.PureScript.AST.SourcePos
26-- An identifier for the scope of a skolem variable
28newtype SkolemScope = SkolemScope { runSkolemScope :: Int }
29 deriving (Show, Read, Eq, Ord, A.ToJSON, A.FromJSON)
36 -- A unification variable of type Type
40 -- A named type variable
44 -- A type wildcard, as would appear in a partial type synonym
50 | TypeConstructor (Qualified (ProperName 'TypeName))
58 | ForAll String Type (Maybe SkolemScope)
60 -- A type with a set of type class constraints
62 | ConstrainedType [Constraint] Type
66 | Skolem String Int SkolemScope (Maybe SourceSpan)
74 | RCons String Type Type
76 -- A type with a kind annotation
78 | KindedType Type Kind
81 -- A placeholder used in pretty printing
83 | PrettyPrintFunction Type Type
85 -- A placeholder used in pretty printing
87 | PrettyPrintObject Type
89 -- A placeholder used in pretty printing
91 | PrettyPrintForAll [String] Type
92 deriving (Show, Read, Eq, Ord)
95-- A typeclass constraint
97type Constraint = (Qualified (ProperName 'ClassName), [Type])
99$(A.deriveJSON A.defaultOptions ''Type)
102-- Convert a row to a list of pairs of labels and types
104rowToList :: Type -> ([(String, Type)], Type)
105rowToList (RCons name ty row) = let (tys, rest) = rowToList row
106 in ((name, ty):tys, rest)
110-- Convert a list of labels and types to a row
112rowFromList :: ([(String, Type)], Type) -> Type
113rowFromList ([], r) = r
114rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
117-- Check whether a type is a monotype
119isMonoType :: Type -> Bool
120isMonoType ForAll{} = False
124-- Universally quantify a type
126mkForAll :: [String] -> Type -> Type
127mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
130-- Replace a type variable, taking into account variable shadowing
132replaceTypeVars :: String -> Type -> Type -> Type
133replaceTypeVars v r = replaceAllTypeVars [(v, r)]
136-- Replace named type variables with types
138replaceAllTypeVars :: [(String, Type)] -> Type -> Type
139replaceAllTypeVars = go []
142 go :: [String] -> [(String, Type)] -> Type -> Type
143 go _ m (TypeVar v) = fromMaybe (TypeVar v) (v `lookup` m)
144 go bs m (TypeApp t1 t2) = TypeApp (go bs m t1) (go bs m t2)
145 go bs m f@(ForAll v t sco) | v `elem` keys = go bs (filter ((/= v) . fst) m) f
146 | v `elem` usedVars =
147 let v' = genName v (keys ++ bs ++ usedVars)
148 t' = go bs [(v, TypeVar v')] t
149 in ForAll v' (go (v' : bs) m t') sco
150 | otherwise = ForAll v (go (v : bs) m t) sco
153 usedVars = concatMap (usedTypeVariables . snd) m
154 go bs m (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs m)) cs) (go bs m t)
155 go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
156 go bs m (KindedType t k) = KindedType (go bs m t) k
159 genName orig inUse = try 0
161 try :: Integer -> String
162 try n | (orig ++ show n) `elem` inUse = try (n + 1)
163 | otherwise = orig ++ show n
166-- Collect all type variables appearing in a type
168usedTypeVariables :: Type -> [String]
169usedTypeVariables = nub . everythingOnTypes (++) go
175-- Collect all free type variables appearing in a type
177freeTypeVariables :: Type -> [String]
178freeTypeVariables = nub . go []
180 go :: [String] -> Type -> [String]
181 go bound (TypeVar v) | v `notElem` bound = [v]
182 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
183 go bound (ForAll v t _) = go (v : bound) t
184 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
185 go bound (RCons _ t r) = go bound t ++ go bound r
186 go bound (KindedType t _) = go bound t
190-- Universally quantify over all type variables appearing free in a type
192quantify :: Type -> Type
193quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
196-- Move all universal quantifiers to the front of a type
198moveQuantifiersToFront :: Type -> Type
199moveQuantifiersToFront = go [] []
201 go qs cs (ForAll q ty sco) = go ((q, sco) : qs) cs ty
202 go qs cs (ConstrainedType cs' ty) = go qs (cs ++ cs') ty
204 let constrained = case cs of
206 cs' -> ConstrainedType cs' ty
209 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
212-- Check if a type contains wildcards
214containsWildcards :: Type -> Bool
215containsWildcards = everythingOnTypes (||) go
218 go TypeWildcard = True
225everywhereOnTypes :: (Type -> Type) -> Type -> Type
226everywhereOnTypes f = go
228 go (TypeApp t1 t2) = f (TypeApp (go t1) (go t2))
229 go (ForAll arg ty sco) = f (ForAll arg (go ty) sco)
230 go (ConstrainedType cs ty) = f (ConstrainedType (map (fmap (map go)) cs) (go ty))
231 go (RCons name ty rest) = f (RCons name (go ty) (go rest))
232 go (KindedType ty k) = f (KindedType (go ty) k)
233 go (PrettyPrintFunction t1 t2) = f (PrettyPrintFunction (go t1) (go t2))
234 go (PrettyPrintObject t) = f (PrettyPrintObject (go t))
235 go (PrettyPrintForAll args t) = f (PrettyPrintForAll args (go t))
238everywhereOnTypesTopDown :: (Type -> Type) -> Type -> Type
239everywhereOnTypesTopDown f = go . f
241 go (TypeApp t1 t2) = TypeApp (go (f t1)) (go (f t2))
242 go (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
243 go (ConstrainedType cs ty) = ConstrainedType (map (fmap (map (go . f))) cs) (go (f ty))
244 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
245 go (KindedType ty k) = KindedType (go (f ty)) k
246 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
247 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
248 go (PrettyPrintForAll args t) = PrettyPrintForAll args (go (f t))
251everywhereOnTypesM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
252everywhereOnTypesM f = go
254 go (TypeApp t1 t2) = (TypeApp <$> go t1 <*> go t2) >>= f
255 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
256 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (sndM (mapM go)) cs <*> go ty) >>= f
257 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
258 go (KindedType ty k) = (KindedType <$> go ty <*> pure k) >>= f
259 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
260 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
261 go (PrettyPrintForAll args t) = (PrettyPrintForAll args <$> go t) >>= f
264everywhereOnTypesTopDownM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
265everywhereOnTypesTopDownM f = go <=< f
267 go (TypeApp t1 t2) = TypeApp <$> (f t1 >>= go) <*> (f t2 >>= go)
268 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
269 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (sndM (mapM (go <=< f))) cs <*> (f ty >>= go)
270 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
271 go (KindedType ty k) = KindedType <$> (f ty >>= go) <*> pure k
272 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
273 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
274 go (PrettyPrintForAll args t) = PrettyPrintForAll args <$> (f t >>= go)
277everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
278everythingOnTypes (<>) f = go
280 go t@(TypeApp t1 t2) = f t <> go t1 <> go t2
281 go t@(ForAll _ ty _) = f t <> go ty
282 go t@(ConstrainedType cs ty) = foldl (<>) (f t) (map go $ concatMap snd cs) <> go ty
283 go t@(RCons _ ty rest) = f t <> go ty <> go rest
284 go t@(KindedType ty _) = f t <> go ty
285 go t@(PrettyPrintFunction t1 t2) = f t <> go t1 <> go t2
286 go t@(PrettyPrintObject t1) = f t <> go t1
287 go t@(PrettyPrintForAll _ t1) = f t <> go t1
290everythingWithContextOnTypes :: s -> r -> (r -> r -> r) -> (s -> Type -> (s, r)) -> Type -> r
291everythingWithContextOnTypes s0 r0 (<>) f = go' s0
293 go' s t = let (s', r) = f s t in r <> go s' t
294 go s (TypeApp t1 t2) = go' s t1 <> go' s t2
295 go s (ForAll _ ty _) = go' s ty
296 go s (ConstrainedType cs ty) = foldl (<>) r0 (map (go' s) $ concatMap snd cs) <> go' s ty
297 go s (RCons _ ty rest) = go' s ty <> go' s rest
298 go s (KindedType ty _) = go' s ty
299 go s (PrettyPrintFunction t1 t2) = go' s t1 <> go' s t2
300 go s (PrettyPrintObject t1) = go' s t1
301 go s (PrettyPrintForAll _ 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