git.iko.soy/rowtype-yoga/purescript-native
on ab7e5f6 help sign in
314 lines 10 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Types
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- Data types for types
13--
14-----------------------------------------------------------------------------
16{-# LANGUAGE DeriveDataTypeable #-}
17{-# LANGUAGE GeneralizedNewtypeDeriving #-}
18{-# LANGUAGE TemplateHaskell #-}
20module Language.PureScript.Types where
22import Prelude ()
23import Prelude.Compat
25import Data.Data
26import Data.List (nub)
27import Data.Maybe (fromMaybe)
28import qualified Data.Aeson as A
29import qualified Data.Aeson.TH as A
31import Control.Arrow (second)
32import Control.Monad ((<=<))
34import Language.PureScript.Names
35import Language.PureScript.Kinds
36import Language.PureScript.Traversals
37import Language.PureScript.AST.SourcePos
39-- |
40-- An identifier for the scope of a skolem variable
41--
42newtype SkolemScope = SkolemScope { runSkolemScope :: Int } deriving (Show, Read, Eq, Ord, Data, Typeable, A.ToJSON, A.FromJSON)
44-- |
45-- The type of types
46--
47data Type
48 -- |
49 -- A unification variable of type Type
50 --
51 = TUnknown Int
52 -- |
53 -- A named type variable
54 --
55 | TypeVar String
56 -- |
57 -- A type wildcard, as would appear in a partial type synonym
58 --
59 | TypeWildcard
60 -- |
61 -- A type constructor
62 --
63 | TypeConstructor (Qualified ProperName)
64 -- |
65 -- A type application
66 --
67 | TypeApp Type Type
68 -- |
69 -- Forall quantifier
70 --
71 | ForAll String Type (Maybe SkolemScope)
72 -- |
73 -- A type with a set of type class constraints
74 --
75 | ConstrainedType [Constraint] Type
76 -- |
77 -- A skolem constant
78 --
79 | Skolem String Int SkolemScope (Maybe SourceSpan)
80 -- |
81 -- An empty row
82 --
83 | REmpty
84 -- |
85 -- A non-empty row
86 --
87 | RCons String Type Type
88 -- |
89 -- A type with a kind annotation
90 --
91 | KindedType Type Kind
92 --
93 -- |
94 -- A placeholder used in pretty printing
95 --
96 | PrettyPrintFunction Type Type
97 -- |
98 -- A placeholder used in pretty printing
99 --
100 | PrettyPrintObject Type
101 -- |
102 -- A placeholder used in pretty printing
103 --
104 | PrettyPrintForAll [String] Type deriving (Show, Read,Eq, Ord, Data, Typeable)
106-- |
107-- A typeclass constraint
108--
109type Constraint = (Qualified ProperName, [Type])
111$(A.deriveJSON A.defaultOptions ''Type)
113-- |
114-- Convert a row to a list of pairs of labels and types
115--
116rowToList :: Type -> ([(String, Type)], Type)
117rowToList (RCons name ty row) = let (tys, rest) = rowToList row
118 in ((name, ty):tys, rest)
119rowToList r = ([], r)
121-- |
122-- Convert a list of labels and types to a row
123--
124rowFromList :: ([(String, Type)], Type) -> Type
125rowFromList ([], r) = r
126rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
128-- |
129-- Check whether a type is a monotype
130--
131isMonoType :: Type -> Bool
132isMonoType ForAll{} = False
133isMonoType _ = True
135-- |
136-- Universally quantify a type
137--
138mkForAll :: [String] -> Type -> Type
139mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
141-- |
142-- Replace a type variable, taking into account variable shadowing
143--
144replaceTypeVars :: String -> Type -> Type -> Type
145replaceTypeVars v r = replaceAllTypeVars [(v, r)]
147-- |
148-- Replace named type variables with types
149--
150replaceAllTypeVars :: [(String, Type)] -> Type -> Type
151replaceAllTypeVars = go []
152 where
154 go :: [String] -> [(String, Type)] -> Type -> Type
155 go _ m (TypeVar v) = fromMaybe (TypeVar v) (v `lookup` m)
156 go bs m (TypeApp t1 t2) = TypeApp (go bs m t1) (go bs m t2)
157 go bs m f@(ForAll v t sco) | v `elem` keys = go bs (filter ((/= v) . fst) m) f
158 | v `elem` usedVars =
159 let v' = genName v (keys ++ bs ++ usedVars)
160 t' = go bs [(v, TypeVar v')] t
161 in ForAll v' (go (v' : bs) m t') sco
162 | otherwise = ForAll v (go (v : bs) m t) sco
163 where
164 keys = map fst m
165 usedVars = concatMap (usedTypeVariables . snd) m
166 go bs m (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs m)) cs) (go bs m t)
167 go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
168 go bs m (KindedType t k) = KindedType (go bs m t) k
169 go _ _ ty = ty
171 genName orig inUse = try 0
172 where
173 try :: Integer -> String
174 try n | (orig ++ show n) `elem` inUse = try (n + 1)
175 | otherwise = orig ++ show n
177-- |
178-- Collect all type variables appearing in a type
179--
180usedTypeVariables :: Type -> [String]
181usedTypeVariables = nub . everythingOnTypes (++) go
182 where
183 go (TypeVar v) = [v]
184 go _ = []
186-- |
187-- Collect all free type variables appearing in a type
188--
189freeTypeVariables :: Type -> [String]
190freeTypeVariables = nub . go []
191 where
192 go :: [String] -> Type -> [String]
193 go bound (TypeVar v) | v `notElem` bound = [v]
194 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
195 go bound (ForAll v t _) = go (v : bound) t
196 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
197 go bound (RCons _ t r) = go bound t ++ go bound r
198 go bound (KindedType t _) = go bound t
199 go _ _ = []
201-- |
202-- Universally quantify over all type variables appearing free in a type
203--
204quantify :: Type -> Type
205quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
207-- |
208-- Move all universal quantifiers to the front of a type
209--
210moveQuantifiersToFront :: Type -> Type
211moveQuantifiersToFront = go [] []
212 where
213 go qs cs (ForAll q ty sco) = go ((q, sco) : qs) cs ty
214 go qs cs (ConstrainedType cs' ty) = go qs (cs ++ cs') ty
215 go qs cs ty =
216 let constrained = case cs of
217 [] -> ty
218 cs' -> ConstrainedType cs' ty
219 in case qs of
220 [] -> constrained
221 qs' -> foldl (\ty' (q, sco) -> ForAll q ty' sco) constrained qs'
223-- |
224-- Check if a type contains wildcards
225--
226containsWildcards :: Type -> Bool
227containsWildcards = everythingOnTypes (||) go
228 where
229 go :: Type -> Bool
230 go TypeWildcard = True
231 go _ = False
233--
234-- Traversals
235--
237everywhereOnTypes :: (Type -> Type) -> Type -> Type
238everywhereOnTypes f = go
239 where
240 go (TypeApp t1 t2) = f (TypeApp (go t1) (go t2))
241 go (ForAll arg ty sco) = f (ForAll arg (go ty) sco)
242 go (ConstrainedType cs ty) = f (ConstrainedType (map (fmap (map go)) cs) (go ty))
243 go (RCons name ty rest) = f (RCons name (go ty) (go rest))
244 go (KindedType ty k) = f (KindedType (go ty) k)
245 go (PrettyPrintFunction t1 t2) = f (PrettyPrintFunction (go t1) (go t2))
246 go (PrettyPrintObject t) = f (PrettyPrintObject (go t))
247 go (PrettyPrintForAll args t) = f (PrettyPrintForAll args (go t))
248 go other = f other
250everywhereOnTypesTopDown :: (Type -> Type) -> Type -> Type
251everywhereOnTypesTopDown f = go . f
252 where
253 go (TypeApp t1 t2) = TypeApp (go (f t1)) (go (f t2))
254 go (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
255 go (ConstrainedType cs ty) = ConstrainedType (map (fmap (map (go . f))) cs) (go (f ty))
256 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
257 go (KindedType ty k) = KindedType (go (f ty)) k
258 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
259 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
260 go (PrettyPrintForAll args t) = PrettyPrintForAll args (go (f t))
261 go other = f other
263everywhereOnTypesM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
264everywhereOnTypesM f = go
265 where
266 go (TypeApp t1 t2) = (TypeApp <$> go t1 <*> go t2) >>= f
267 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
268 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (sndM (mapM go)) cs <*> go ty) >>= f
269 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
270 go (KindedType ty k) = (KindedType <$> go ty <*> pure k) >>= f
271 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
272 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
273 go (PrettyPrintForAll args t) = (PrettyPrintForAll args <$> go t) >>= f
274 go other = f other
276everywhereOnTypesTopDownM :: (Functor m, Applicative m, Monad m) => (Type -> m Type) -> Type -> m Type
277everywhereOnTypesTopDownM f = go <=< f
278 where
279 go (TypeApp t1 t2) = TypeApp <$> (f t1 >>= go) <*> (f t2 >>= go)
280 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
281 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (sndM (mapM (go <=< f))) cs <*> (f ty >>= go)
282 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
283 go (KindedType ty k) = KindedType <$> (f ty >>= go) <*> pure k
284 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
285 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
286 go (PrettyPrintForAll args t) = PrettyPrintForAll args <$> (f t >>= go)
287 go other = f other
289everythingOnTypes :: (r -> r -> r) -> (Type -> r) -> Type -> r
290everythingOnTypes (<>) f = go
291 where
292 go t@(TypeApp t1 t2) = f t <> go t1 <> go t2
293 go t@(ForAll _ ty _) = f t <> go ty
294 go t@(ConstrainedType cs ty) = foldl (<>) (f t) (map go $ concatMap snd cs) <> go ty
295 go t@(RCons _ ty rest) = f t <> go ty <> go rest
296 go t@(KindedType ty _) = f t <> go ty
297 go t@(PrettyPrintFunction t1 t2) = f t <> go t1 <> go t2
298 go t@(PrettyPrintObject t1) = f t <> go t1
299 go t@(PrettyPrintForAll _ t1) = f t <> go t1
300 go other = f other
302everythingWithContextOnTypes :: s -> r -> (r -> r -> r) -> (s -> Type -> (s, r)) -> Type -> r
303everythingWithContextOnTypes s0 r0 (<>) f = go' s0
304 where
305 go' s t = let (s', r) = f s t in r <> go s' t
306 go s (TypeApp t1 t2) = go' s t1 <> go' s t2
307 go s (ForAll _ ty _) = go' s ty
308 go s (ConstrainedType cs ty) = foldl (<>) r0 (map (go' s) $ concatMap snd cs) <> go' s ty
309 go s (RCons _ ty rest) = go' s ty <> go' s rest
310 go s (KindedType ty _) = go' s ty
311 go s (PrettyPrintFunction t1 t2) = go' s t1 <> go' s t2
312 go s (PrettyPrintObject t1) = go' s t1
313 go s (PrettyPrintForAll _ t1) = go' s t1
314 go _ _ = r0