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
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
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-- The empty record type
130-- Replace a type variable, taking into account variable shadowing
132replaceTypeVars :: String -> Type -> Type -> Type
133replaceTypeVars = replaceTypeVars' []
135 replaceTypeVars' bound name replacement = go bound
137 go :: [String] -> Type -> Type
138 go bs (Object r) = Object $ go bs r
139 go _ (TypeVar v) | v == name = replacement
140 go bs (TypeApp t1 t2) = TypeApp (go bs t1) (go bs t2)
141 go bs (SaturatedTypeSynonym name' ts) = SaturatedTypeSynonym name' $ map (go bs) ts
142 go bs f@(ForAll v t sco) | v == name = f
143 | v `elem` usedTypeVariables replacement =
144 let v' = genName v (name : bs ++ usedTypeVariables replacement)
145 t' = replaceTypeVars' bs v (TypeVar v') t
146 in ForAll v' (go (v' : bs) t') sco
147 | otherwise = ForAll v (go (v : bs) t) sco
148 go bs (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs)) cs) (go bs t)
149 go bs (RCons name' t r) = RCons name' (go bs t) (go bs r)
151 genName orig inUse = try 0
153 try :: Integer -> String
154 try n | (orig ++ show n) `elem` inUse = try (n + 1)
155 | otherwise = orig ++ show n
158-- Replace named type variables with types
160replaceAllTypeVars :: [(String, Type)] -> Type -> Type
161replaceAllTypeVars = foldl (\f (name, ty) -> replaceTypeVars name ty . f) id
164-- Collect all type variables appearing in a type
166usedTypeVariables :: Type -> [String]
167usedTypeVariables = nub . everything (++) (mkQ [] go)
173-- Collect all free type variables appearing in a type
175freeTypeVariables :: Type -> [String]
176freeTypeVariables = nub . go []
178 go :: [String] -> Type -> [String]
179 go bound (Object r) = go bound r
180 go bound (TypeVar v) | v `notElem` bound = [v]
181 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
182 go bound (SaturatedTypeSynonym _ ts) = concatMap (go bound) ts
183 go bound (ForAll v t _) = go (v : bound) t
184 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
185 go bound (RCons _ t r) = go bound t ++ go bound r
189-- Universally quantify over all type variables appearing free in a type
191quantify :: Type -> Type
192quantify 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