1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.Types
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
12-- Data types for types
14-----------------------------------------------------------------------------
16{-# LANGUAGE DeriveDataTypeable #-}
17{-# LANGUAGE GeneralizedNewtypeDeriving #-}
18{-# LANGUAGE TemplateHaskell #-}
21module Language.PureScript.Types where
25import qualified Data.Aeson as A
26import qualified Data.Aeson.TH as A
28import Control.Monad.Unify
29import Control.Arrow (second)
30#if __GLASGOW_HASKELL__ < 710
31import Control.Applicative
33import Control.Monad ((<=<))
35import Language.PureScript.Names
36import Language.PureScript.Kinds
37import Language.PureScript.Traversals
40-- An identifier for the scope of a skolem variable
42newtype SkolemScope = SkolemScope { runSkolemScope :: Int } deriving (Show, Eq, Ord, Data, Typeable, A.ToJSON, A.FromJSON)
49 -- A unification variable of type Type
53 -- A named type variable
57 -- A type wildcard, as would appear in a partial type synonym
63 | TypeConstructor (Qualified ProperName)
69 -- A type synonym which is \"saturated\", i.e. fully applied
71 | SaturatedTypeSynonym (Qualified ProperName) [Type]
75 | ForAll String Type (Maybe SkolemScope)
77 -- A type with a set of type class constraints
79 | ConstrainedType [Constraint] Type
83 | Skolem String Int SkolemScope
91 | RCons String Type Type
93 -- A type with a kind annotation
95 | KindedType Type Kind
98 -- A placeholder used in pretty printing
100 | PrettyPrintFunction Type Type
102 -- A placeholder used in pretty printing
104 | PrettyPrintObject Type
106 -- A placeholder used in pretty printing
108 | PrettyPrintForAll [String] Type deriving (Show, Eq, Ord, Data, Typeable)
111-- A typeclass constraint
113type Constraint = (Qualified ProperName, [Type])
115$(A.deriveJSON A.defaultOptions ''Type)
118-- Convert a row to a list of pairs of labels and types
120rowToList :: Type -> ([(String, Type)], Type)
121rowToList (RCons name ty row) = let (tys, rest) = rowToList row
122 in ((name, ty):tys, rest)
126-- Convert a list of labels and types to a row
128rowFromList :: ([(String, Type)], Type) -> Type
129rowFromList ([], r) = r
130rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
133-- Check whether a type is a monotype
135isMonoType :: Type -> Bool
136isMonoType ForAll{} = False
140-- Universally quantify a type
142mkForAll :: [String] -> Type -> Type
143mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
146-- Replace a type variable, taking into account variable shadowing
148replaceTypeVars :: String -> Type -> Type -> Type
149replaceTypeVars v r = replaceAllTypeVars [(v, r)]
152-- Replace named type variables with types
154replaceAllTypeVars :: [(String, Type)] -> Type -> Type
155replaceAllTypeVars = go []
158 go :: [String] -> [(String, Type)] -> Type -> Type
163 go bs m (TypeApp t1 t2) = TypeApp (go bs m t1) (go bs m t2)
164 go bs m (SaturatedTypeSynonym name' ts) = SaturatedTypeSynonym name' $ map (go bs m) ts
165 go bs m f@(ForAll v t sco) | v `elem` keys = go bs (filter ((/= v) . fst) m) f
166 | v `elem` usedVars =
167 let v' = genName v (keys ++ bs ++ usedVars)
168 t' = go bs [(v, TypeVar v')] t
169 in ForAll v' (go (v' : bs) m t') sco
170 | otherwise = ForAll v (go (v : bs) m t) sco
173 usedVars = concatMap (usedTypeVariables . snd) m
174 go bs m (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs m)) cs) (go bs m t)
175 go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
176 go bs m (KindedType t k) = KindedType (go bs m t) k
179 genName orig inUse = try 0
181 try :: Integer -> String
182 try n | (orig ++ show n) `elem` inUse = try (n + 1)
183 | otherwise = orig ++ show n
186-- Collect all type variables appearing in a type
188usedTypeVariables :: Type -> [String]
189usedTypeVariables = nub . everythingOnTypes (++) go
195-- Collect all free type variables appearing in a type
197freeTypeVariables :: Type -> [String]
198freeTypeVariables = nub . go []
200 go :: [String] -> Type -> [String]
201 go bound (TypeVar v) | v `notElem` bound = [v]
202 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
203 go bound (SaturatedTypeSynonym _ ts) = concatMap (go bound) ts
204 go bound (ForAll v t _) = go (v : bound) t
205 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
206 go bound (RCons _ t r) = go bound t ++ go bound r
207 go bound (KindedType t _) = go bound t
211-- Universally quantify over all type variables appearing free in a type
213quantify :: Type -> Type
214quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
217-- Move all universal quantifiers to the front of a type
219moveQuantifiersToFront :: Type -> Type
220moveQuantifiersToFront = go [] []
222 go qs cs (ForAll q ty sco) = go ((q, sco) : qs) cs ty
223 go qs cs (ConstrainedType cs' ty) = go qs (cs ++ cs') ty
225 let constrained = case cs of
227 cs' -> ConstrainedType cs' ty
230 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
233-- Check if a type contains wildcards
235containsWildcards :: Type -> Bool
236containsWildcards = everythingOnTypes (||) go
239 go TypeWildcard = True
246everywhereOnTypes :: (Type -> Type) -> Type -> Type
247everywhereOnTypes f = go
249 go (TypeApp t1 t2) = f (TypeApp (go t1) (go t2))
250 go (SaturatedTypeSynonym name tys) = f (SaturatedTypeSynonym name (map go tys))
251 go (ForAll arg ty sco) = f (ForAll arg (go ty) sco)
252 go (ConstrainedType cs ty) = f (ConstrainedType (map (fmap (map go)) cs) (go ty))
253 go (RCons name ty rest) = f (RCons name (go ty) (go rest))
254 go (KindedType ty k) = f (KindedType (go ty) k)
255 go (PrettyPrintFunction t1 t2) = f (PrettyPrintFunction (go t1) (go t2))
256 go (PrettyPrintObject t) = f (PrettyPrintObject (go t))
257 go (PrettyPrintForAll args t) = f (PrettyPrintForAll args (go t))
260everywhereOnTypesTopDown :: (Type -> Type) -> Type -> Type
261everywhereOnTypesTopDown f = go . f
263 go (TypeApp t1 t2) = TypeApp (go (f t1)) (go (f t2))
264 go (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name (map (go . f) tys)
265 go (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
266 go (ConstrainedType cs ty) = ConstrainedType (map (fmap (map (go . f))) cs) (go (f ty))
267 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
268 go (KindedType ty k) = KindedType (go (f ty)) k
269 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
270 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
271 go (PrettyPrintForAll args t) = PrettyPrintForAll args (go (f t))
274everywhereOnTypesM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
275everywhereOnTypesM f = go
277 go (TypeApp t1 t2) = (TypeApp <$> go t1 <*> go t2) >>= f
278 go (SaturatedTypeSynonym name tys) = (SaturatedTypeSynonym name <$> mapM go tys) >>= f
279 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
280 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (sndM (mapM go)) cs <*> go ty) >>= f
281 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
282 go (KindedType ty k) = (KindedType <$> go ty <*> pure k) >>= f
283 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
284 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
285 go (PrettyPrintForAll args t) = (PrettyPrintForAll args <$> go t) >>= f
288everywhereOnTypesTopDownM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
289everywhereOnTypesTopDownM f = go <=< f
291 go (TypeApp t1 t2) = TypeApp <$> (f t1 >>= go) <*> (f t2 >>= go)
292 go (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym name <$> mapM (go <=< f) tys
293 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
294 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (sndM (mapM (go <=< f))) cs <*> (f ty >>= go)
295 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
296 go (KindedType ty k) = KindedType <$> (f ty >>= go) <*> pure k
297 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
298 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
299 go (PrettyPrintForAll args t) = PrettyPrintForAll args <$> (f t >>= go)
302everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
303everythingOnTypes (<>) f = go
305 go t@(TypeApp t1 t2) = f t <> go t1 <> go t2
306 go t@(SaturatedTypeSynonym _ tys) = foldl (<>) (f t) (map go tys)
307 go t@(ForAll _ ty _) = f t <> go ty
308 go t@(ConstrainedType cs ty) = foldl (<>) (f t) (map go $ concatMap snd cs) <> go ty
309 go t@(RCons _ ty rest) = f t <> go ty <> go rest
310 go t@(KindedType ty _) = f t <> go ty
311 go t@(PrettyPrintFunction t1 t2) = f t <> go t1 <> go t2
312 go t@(PrettyPrintObject t1) = f t <> go t1
313 go t@(PrettyPrintForAll _ t1) = f t <> go 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