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, mkQ)
24import Control.Monad.Unify
25import Control.Arrow (second)
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
43 -- A named type variable
49 | TypeConstructor (Qualified ProperName)
55 -- A type synonym which is \"saturated\", i.e. fully applied
57 | SaturatedTypeSynonym (Qualified ProperName) [Type]
61 | ForAll String Type (Maybe SkolemScope)
63 -- A type with a set of type class constraints
65 | ConstrainedType [(Qualified ProperName, [Type])] Type
69 | Skolem Int SkolemScope
77 | RCons String Type Type
79 -- A placeholder used in pretty printing
81 | PrettyPrintFunction Type Type
83 -- A placeholder used in pretty printing
85 | PrettyPrintArray Type
87 -- A placeholder used in pretty printing
89 | PrettyPrintObject Type
91 -- A placeholder used in pretty printing
93 | PrettyPrintForAll [String] Type deriving (Show, Eq, Data, Typeable)
96-- Convert a row to a list of pairs of labels and types
98rowToList :: Type -> ([(String, Type)], Type)
99rowToList (RCons name ty row) = let (tys, rest) = rowToList row
100 in ((name, ty):tys, rest)
104-- Convert a list of labels and types to a row
106rowFromList :: ([(String, Type)], Type) -> Type
107rowFromList ([], r) = r
108rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
111-- Check whether a type is a monotype
113isMonoType :: Type -> Bool
114isMonoType ForAll{} = False
118-- Universally quantify a type
120mkForAll :: [String] -> Type -> Type
121mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
124-- Replace a type variable, taking into account variable shadowing
126replaceTypeVars :: String -> Type -> Type -> Type
127replaceTypeVars = replaceTypeVars' []
129 replaceTypeVars' bound name replacement = go bound
131 go :: [String] -> Type -> Type
132 go _ (TypeVar v) | v == name = replacement
133 go bs (TypeApp t1 t2) = TypeApp (go bs t1) (go bs t2)
134 go bs (SaturatedTypeSynonym name' ts) = SaturatedTypeSynonym name' $ map (go bs) ts
135 go bs f@(ForAll v t sco) | v == name = f
136 | v `elem` usedTypeVariables replacement =
137 let v' = genName v (name : bs ++ usedTypeVariables replacement)
138 t' = replaceTypeVars' bs v (TypeVar v') t
139 in ForAll v' (go (v' : bs) t') sco
140 | otherwise = ForAll v (go (v : bs) t) sco
141 go bs (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs)) cs) (go bs t)
142 go bs (RCons name' t r) = RCons name' (go bs t) (go bs r)
144 genName orig inUse = try 0
146 try :: Integer -> String
147 try n | (orig ++ show n) `elem` inUse = try (n + 1)
148 | otherwise = orig ++ show n
151-- Replace named type variables with types
153replaceAllTypeVars :: [(String, Type)] -> Type -> Type
154replaceAllTypeVars = foldl (\f (name, ty) -> replaceTypeVars name ty . f) id
157-- Collect all type variables appearing in a type
159usedTypeVariables :: Type -> [String]
160usedTypeVariables = nub . everything (++) (mkQ [] go)
166-- Collect all free type variables appearing in a type
168freeTypeVariables :: Type -> [String]
169freeTypeVariables = nub . go []
171 go :: [String] -> Type -> [String]
172 go bound (TypeVar v) | v `notElem` bound = [v]
173 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
174 go bound (SaturatedTypeSynonym _ ts) = concatMap (go bound) ts
175 go bound (ForAll v t _) = go (v : bound) t
176 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
177 go bound (RCons _ t r) = go bound t ++ go bound r
181-- Universally quantify over all type variables appearing free in a type
183quantify :: Type -> Type
184quantify 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