1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE TemplateHaskell #-}
7module Language.PureScript.Types where
11import Control.Monad ((<=<))
12import qualified Data.Aeson as A
13import qualified Data.Aeson.TH as A
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
23import Language.PureScript.Label (Label)
24import Language.PureScript.PSString (PSString)
27-- An identifier for the scope of a skolem variable
29newtype SkolemScope = SkolemScope { runSkolemScope :: Int }
30 deriving (Show, Eq, Ord, A.ToJSON, A.FromJSON)
36 -- | A unification variable of type Type
38 -- | A named type variable
40 -- | A type-level string
41 | TypeLevelString PSString
42 -- | A type wildcard, as would appear in a partial type synonym
43 | TypeWildcard SourceSpan
44 -- | A type constructor
45 | TypeConstructor (Qualified (ProperName 'TypeName))
46 -- | A type operator. This will be desugared into a type constructor during the
47 -- "operators" phase of desugaring.
48 | TypeOp (Qualified (OpName 'TypeOpName))
49 -- | A type application
51 -- | Forall quantifier
52 | ForAll Text Type (Maybe SkolemScope)
53 -- | A type with a set of type class constraints
54 | ConstrainedType [Constraint] Type
55 -- | A skolem constant
56 | Skolem Text Int SkolemScope (Maybe SourceSpan)
60 | RCons Label Type Type
61 -- | A type with a kind annotation
62 | KindedType Type Kind
63 -- | A placeholder used in pretty printing
64 | PrettyPrintFunction Type Type
65 -- | A placeholder used in pretty printing
66 | PrettyPrintObject Type
67 -- | A placeholder used in pretty printing
68 | PrettyPrintForAll [Text] Type
69 -- | Binary operator application. During the rebracketing phase of desugaring,
70 -- this data constructor will be removed.
71 | BinaryNoParensType Type Type Type
72 -- | Explicit parentheses. During the rebracketing phase of desugaring, this
73 -- data constructor will be removed.
75 -- Note: although it seems this constructor is not used, it _is_ useful,
76 -- since it prevents certain traversals from matching.
78 deriving (Show, Eq, Ord)
80-- | Additional data relevant to type class constraints
82 = PartialConstraintData [[Text]] Bool
83 -- ^ Data to accompany a Partial constraint generated by the exhaustivity checker.
84 -- It contains (rendered) binder information for those binders which were
85 -- not matched, and a flag indicating whether the list was truncated or not.
86 -- Note: we use 'String' here because using 'Binder' would introduce a cyclic
87 -- dependency in the module graph.
88 deriving (Show, Eq, Ord)
90-- | A typeclass constraint
91data Constraint = Constraint
92 { constraintClass :: Qualified (ProperName 'ClassName)
93 -- ^ constraint class name
94 , constraintArgs :: [Type]
96 , constraintData :: Maybe ConstraintData
97 -- ^ additional data relevant to this constraint
98 } deriving (Show, Eq, Ord)
100mapConstraintArgs :: ([Type] -> [Type]) -> Constraint -> Constraint
101mapConstraintArgs f c = c { constraintArgs = f (constraintArgs c) }
103overConstraintArgs :: Functor f => ([Type] -> f [Type]) -> Constraint -> f Constraint
104overConstraintArgs f c = (\args -> c { constraintArgs = args }) <$> f (constraintArgs c)
106$(A.deriveJSON A.defaultOptions ''Type)
107$(A.deriveJSON A.defaultOptions ''Constraint)
108$(A.deriveJSON A.defaultOptions ''ConstraintData)
111-- Convert a row to a list of pairs of labels and types
113rowToList :: Type -> ([(Label, Type)], Type)
114rowToList (RCons name ty row) = let (tys, rest) = rowToList row
115 in ((name, ty):tys, rest)
119-- Convert a list of labels and types to a row
121rowFromList :: ([(Label, Type)], Type) -> Type
122rowFromList ([], r) = r
123rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
126-- Check whether a type is a monotype
128isMonoType :: Type -> Bool
129isMonoType ForAll{} = False
133-- Universally quantify a type
135mkForAll :: [Text] -> Type -> Type
136mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
139-- Replace a type variable, taking into account variable shadowing
141replaceTypeVars :: Text -> Type -> Type -> Type
142replaceTypeVars v r = replaceAllTypeVars [(v, r)]
145-- Replace named type variables with types
147replaceAllTypeVars :: [(Text, Type)] -> Type -> Type
148replaceAllTypeVars = go []
151 go :: [Text] -> [(Text, Type)] -> Type -> Type
152 go _ m (TypeVar v) = fromMaybe (TypeVar v) (v `lookup` m)
153 go bs m (TypeApp t1 t2) = TypeApp (go bs m t1) (go bs m t2)
154 go bs m f@(ForAll v t sco) | v `elem` keys = go bs (filter ((/= v) . fst) m) f
155 | v `elem` usedVars =
156 let v' = genName v (keys ++ bs ++ usedVars)
157 t' = go bs [(v, TypeVar v')] t
158 in ForAll v' (go (v' : bs) m t') sco
159 | otherwise = ForAll v (go (v : bs) m t) sco
162 usedVars = concatMap (usedTypeVariables . snd) m
163 go bs m (ConstrainedType cs t) = ConstrainedType (map (mapConstraintArgs (map (go bs m))) cs) (go bs m t)
164 go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
165 go bs m (KindedType t k) = KindedType (go bs m t) k
166 go bs m (BinaryNoParensType t1 t2 t3) = BinaryNoParensType (go bs m t1) (go bs m t2) (go bs m t3)
167 go bs m (ParensInType t) = ParensInType (go bs m t)
170 genName orig inUse = try' 0
172 try' :: Integer -> Text
173 try' n | (orig <> T.pack (show n)) `elem` inUse = try' (n + 1)
174 | otherwise = orig <> T.pack (show n)
177-- Collect all type variables appearing in a type
179usedTypeVariables :: Type -> [Text]
180usedTypeVariables = nub . everythingOnTypes (++) go
186-- Collect all free type variables appearing in a type
188freeTypeVariables :: Type -> [Text]
189freeTypeVariables = nub . go []
191 go :: [Text] -> Type -> [Text]
192 go bound (TypeVar v) | v `notElem` bound = [v]
193 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
194 go bound (ForAll v t _) = go (v : bound) t
195 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . constraintArgs) cs ++ go bound t
196 go bound (RCons _ t r) = go bound t ++ go bound r
197 go bound (KindedType t _) = go bound t
198 go bound (BinaryNoParensType t1 t2 t3) = go bound t1 ++ go bound t2 ++ go bound t3
199 go bound (ParensInType t) = go bound t
203-- Universally quantify over all type variables appearing free in a type
205quantify :: Type -> Type
206quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
209-- Move all universal quantifiers to the front of a type
211moveQuantifiersToFront :: Type -> Type
212moveQuantifiersToFront = go [] []
214 go qs cs (ForAll q ty sco) = go ((q, sco) : qs) cs ty
215 go qs cs (ConstrainedType cs' ty) = go qs (cs ++ cs') ty
217 let constrained = case cs of
219 cs' -> ConstrainedType cs' ty
222 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
225-- Check if a type contains wildcards
227containsWildcards :: Type -> Bool
228containsWildcards = everythingOnTypes (||) go
231 go TypeWildcard{} = True
238everywhereOnTypes :: (Type -> Type) -> Type -> Type
239everywhereOnTypes f = go
241 go (TypeApp t1 t2) = f (TypeApp (go t1) (go t2))
242 go (ForAll arg ty sco) = f (ForAll arg (go ty) sco)
243 go (ConstrainedType cs ty) = f (ConstrainedType (map (mapConstraintArgs (map go)) cs) (go ty))
244 go (RCons name ty rest) = f (RCons name (go ty) (go rest))
245 go (KindedType ty k) = f (KindedType (go ty) k)
246 go (PrettyPrintFunction t1 t2) = f (PrettyPrintFunction (go t1) (go t2))
247 go (PrettyPrintObject t) = f (PrettyPrintObject (go t))
248 go (PrettyPrintForAll args t) = f (PrettyPrintForAll args (go t))
249 go (BinaryNoParensType t1 t2 t3) = f (BinaryNoParensType (go t1) (go t2) (go t3))
250 go (ParensInType t) = f (ParensInType (go t))
253everywhereOnTypesTopDown :: (Type -> Type) -> Type -> Type
254everywhereOnTypesTopDown f = go . f
256 go (TypeApp t1 t2) = TypeApp (go (f t1)) (go (f t2))
257 go (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
258 go (ConstrainedType cs ty) = ConstrainedType (map (mapConstraintArgs (map (go . f))) cs) (go (f ty))
259 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
260 go (KindedType ty k) = KindedType (go (f ty)) k
261 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
262 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
263 go (PrettyPrintForAll args t) = PrettyPrintForAll args (go (f t))
264 go (BinaryNoParensType t1 t2 t3) = BinaryNoParensType (go (f t1)) (go (f t2)) (go (f t3))
265 go (ParensInType t) = ParensInType (go (f t))
268everywhereOnTypesM :: Monad m => (Type -> m Type) -> Type -> m Type
269everywhereOnTypesM f = go
271 go (TypeApp t1 t2) = (TypeApp <$> go t1 <*> go t2) >>= f
272 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
273 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (overConstraintArgs (mapM go)) cs <*> go ty) >>= f
274 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
275 go (KindedType ty k) = (KindedType <$> go ty <*> pure k) >>= f
276 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
277 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
278 go (PrettyPrintForAll args t) = (PrettyPrintForAll args <$> go t) >>= f
279 go (BinaryNoParensType t1 t2 t3) = (BinaryNoParensType <$> go t1 <*> go t2 <*> go t3) >>= f
280 go (ParensInType t) = (ParensInType <$> go t) >>= f
283everywhereOnTypesTopDownM :: Monad m => (Type -> m Type) -> Type -> m Type
284everywhereOnTypesTopDownM f = go <=< f
286 go (TypeApp t1 t2) = TypeApp <$> (f t1 >>= go) <*> (f t2 >>= go)
287 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
288 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (overConstraintArgs (mapM (go <=< f))) cs <*> (f ty >>= go)
289 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
290 go (KindedType ty k) = KindedType <$> (f ty >>= go) <*> pure k
291 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
292 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
293 go (PrettyPrintForAll args t) = PrettyPrintForAll args <$> (f t >>= go)
294 go (BinaryNoParensType t1 t2 t3) = BinaryNoParensType <$> (f t1 >>= go) <*> (f t2 >>= go) <*> (f t3 >>= go)
295 go (ParensInType t) = ParensInType <$> (f t >>= go)
298everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
299everythingOnTypes (<+>) f = go
301 go t@(TypeApp t1 t2) = f t <+> go t1 <+> go t2
302 go t@(ForAll _ ty _) = f t <+> go ty
303 go t@(ConstrainedType cs ty) = foldl (<+>) (f t) (map go $ concatMap constraintArgs cs) <+> go ty
304 go t@(RCons _ ty rest) = f t <+> go ty <+> go rest
305 go t@(KindedType ty _) = f t <+> go ty
306 go t@(PrettyPrintFunction t1 t2) = f t <+> go t1 <+> go t2
307 go t@(PrettyPrintObject t1) = f t <+> go t1
308 go t@(PrettyPrintForAll _ t1) = f t <+> go t1
309 go t@(BinaryNoParensType t1 t2 t3) = f t <+> go t1 <+> go t2 <+> go t3
310 go t@(ParensInType t1) = f t <+> go t1
313everythingWithContextOnTypes :: s -> r -> (r -> r -> r) -> (s -> Type -> (s, r)) -> Type -> r
314everythingWithContextOnTypes s0 r0 (<+>) f = go' s0
316 go' s t = let (s', r) = f s t in r <+> go s' t
317 go s (TypeApp t1 t2) = go' s t1 <+> go' s t2
318 go s (ForAll _ ty _) = go' s ty
319 go s (ConstrainedType cs ty) = foldl (<+>) r0 (map (go' s) $ concatMap constraintArgs cs) <+> go' s ty
320 go s (RCons _ ty rest) = go' s ty <+> go' s rest
321 go s (KindedType ty _) = go' s ty
322 go s (PrettyPrintFunction t1 t2) = go' s t1 <+> go' s t2
323 go s (PrettyPrintObject t1) = go' s t1
324 go s (PrettyPrintForAll _ t1) = go' s t1
325 go s (BinaryNoParensType t1 t2 t3) = go' s t1 <+> go' s t2 <+> go' s t3
326 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