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 #-}
18module Language.PureScript.Types where
22import Data.Generics (everything, mkT, mkQ, everywhereBut)
24import Control.Monad.Unify
25import Control.Arrow ((***))
27import Language.PureScript.Names
30-- An identifier for the scope of a skolem variable
32newtype SkolemScope = SkolemScope { runSkolemScope :: Int } deriving (Show, Eq, Ord, Data, Typeable)
39 -- A unification variable of type Type
47 -- A named type variable
53 | TypeConstructor (Qualified ProperName)
59 -- A type synonym which is \"saturated\", i.e. fully applied
61 | SaturatedTypeSynonym (Qualified ProperName) [Type]
65 | ForAll String Type (Maybe SkolemScope)
67 -- A type with a set of type class constraints
69 | ConstrainedType [(Qualified ProperName, [Type])] Type
73 | Skolem Int SkolemScope
81 | RCons String Type Type
83 -- A placeholder used in pretty printing
85 | PrettyPrintFunction Type Type
87 -- A placeholder used in pretty printing
89 | PrettyPrintArray Type deriving (Show, Eq, Data, Typeable)
92-- Type constructor for functions
95tyFunction = TypeConstructor $ (Qualified $ Just $ ModuleName [ProperName "Prim"]) (ProperName "Function")
98-- Type constructor for strings
101tyString = TypeConstructor $ (Qualified $ Just $ ModuleName [ProperName "Prim"]) (ProperName "String")
104-- Type constructor for numbers
107tyNumber = TypeConstructor $ (Qualified $ Just $ ModuleName [ProperName "Prim"]) (ProperName "Number")
110-- Type constructor for booleans
113tyBoolean = TypeConstructor $ (Qualified $ Just $ ModuleName [ProperName "Prim"]) (ProperName "Boolean")
116-- Type constructor for arrays
119tyArray = TypeConstructor $ (Qualified $ Just $ ModuleName [ProperName "Prim"]) (ProperName "Array")
122-- Smart constructor for function types
124function :: Type -> Type -> Type
125function t1 t2 = TypeApp (TypeApp tyFunction t1) t2
128-- Convert a row to a list of pairs of labels and types
130rowToList :: Type -> ([(String, Type)], Type)
131rowToList (RCons name ty row) = let (tys, rest) = rowToList row
132 in ((name, ty):tys, rest)
136-- Convert a list of labels and types to a row
138rowFromList :: ([(String, Type)], Type) -> Type
139rowFromList ([], r) = r
140rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
143-- Check whether a type is a monotype
145isMonoType :: Type -> Bool
146isMonoType (ForAll _ _ _) = False
150-- Universally quantify a type
152mkForAll :: [String] -> Type -> Type
153mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
156-- The empty record type
162-- Replace a type variable, taking into account variable shadowing
164replaceTypeVars :: String -> Type -> Type -> Type
165replaceTypeVars = replaceTypeVars' []
167 replaceTypeVars' bound name replacement = go bound
169 go :: [String] -> Type -> Type
170 go bs (Object r) = Object $ go bs r
171 go bs (TypeVar v) | v == name = replacement
172 go bs (TypeApp t1 t2) = TypeApp (go bs t1) (go bs t2)
173 go bs (SaturatedTypeSynonym name ts) = SaturatedTypeSynonym name $ map (go bs) ts
174 go bs f@(ForAll v t sco) | v == name = f
175 | v `elem` usedTypeVariables replacement =
176 let v' = genName v (name : bs ++ usedTypeVariables replacement)
177 t' = replaceTypeVars' bs v (TypeVar v') t
178 in ForAll v' (go (v' : bs) t') sco
179 | otherwise = ForAll v (go (v : bs) t) sco
180 go bs (ConstrainedType cs t) = ConstrainedType (map (id *** map (go bs)) cs) (go bs t)
181 go bs (RCons name t r) = RCons name (go bs t) (go bs r)
183 genName orig inUse = try 0
185 try n | (orig ++ show n) `elem` inUse = try (n + 1)
186 | otherwise = orig ++ show n
189-- Replace named type variables with types
191replaceAllTypeVars :: [(String, Type)] -> Type -> Type
192replaceAllTypeVars = foldl (\f (name, ty) -> replaceTypeVars name ty . f) id
195-- Collect all type variables appearing in a type
197usedTypeVariables :: Type -> [String]
198usedTypeVariables = nub . everything (++) (mkQ [] go)
204-- Collect all free type variables appearing in a type
206freeTypeVariables :: Type -> [String]
207freeTypeVariables = nub . go []
209 go :: [String] -> Type -> [String]
210 go bound (Object r) = go bound r
211 go bound (TypeVar v) | v `notElem` bound = [v]
212 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
213 go bound (SaturatedTypeSynonym _ ts) = concatMap (go bound) ts
214 go bound (ForAll v t _) = go (v : bound) t
215 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
216 go bound (RCons _ t r) = go bound t ++ go bound r
220-- Universally quantify over all type variables appearing free in a type
222quantify :: Type -> Type
223quantify ty = foldr (\arg t -> ForAll arg t Nothing) ty $ freeTypeVariables ty
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