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 deriving (Show, Eq, Data, Typeable)
92-- Convert a row to a list of pairs of labels and types
94rowToList :: Type -> ([(String, Type)], Type)
95rowToList (RCons name ty row) = let (tys, rest) = rowToList row
96 in ((name, ty):tys, rest)
100-- Convert a list of labels and types to a row
102rowFromList :: ([(String, Type)], Type) -> Type
103rowFromList ([], r) = r
104rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
107-- Check whether a type is a monotype
109isMonoType :: Type -> Bool
110isMonoType ForAll{} = False
114-- Universally quantify a type
116mkForAll :: [String] -> Type -> Type
117mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
120-- The empty record type
126-- Replace a type variable, taking into account variable shadowing
128replaceTypeVars :: String -> Type -> Type -> Type
129replaceTypeVars = replaceTypeVars' []
131 replaceTypeVars' bound name replacement = go bound
133 go :: [String] -> Type -> Type
134 go bs (Object r) = Object $ go bs r
135 go _ (TypeVar v) | v == name = replacement
136 go bs (TypeApp t1 t2) = TypeApp (go bs t1) (go bs t2)
137 go bs (SaturatedTypeSynonym name' ts) = SaturatedTypeSynonym name' $ map (go bs) ts
138 go bs f@(ForAll v t sco) | v == name = f
139 | v `elem` usedTypeVariables replacement =
140 let v' = genName v (name : bs ++ usedTypeVariables replacement)
141 t' = replaceTypeVars' bs v (TypeVar v') t
142 in ForAll v' (go (v' : bs) t') sco
143 | otherwise = ForAll v (go (v : bs) t) sco
144 go bs (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs)) cs) (go bs t)
145 go bs (RCons name' t r) = RCons name' (go bs t) (go bs r)
147 genName orig inUse = try 0
149 try n | (orig ++ show n) `elem` inUse = try (n + 1)
150 | otherwise = orig ++ show n
153-- Replace named type variables with types
155replaceAllTypeVars :: [(String, Type)] -> Type -> Type
156replaceAllTypeVars = foldl (\f (name, ty) -> replaceTypeVars name ty . f) id
159-- Collect all type variables appearing in a type
161usedTypeVariables :: Type -> [String]
162usedTypeVariables = nub . everything (++) (mkQ [] go)
168-- Collect all free type variables appearing in a type
170freeTypeVariables :: Type -> [String]
171freeTypeVariables = nub . go []
173 go :: [String] -> Type -> [String]
174 go bound (Object r) = go bound r
175 go bound (TypeVar v) | v `notElem` bound = [v]
176 go bound (TypeApp t1 t2) = go bound t1 ++ go bound t2
177 go bound (SaturatedTypeSynonym _ ts) = concatMap (go bound) ts
178 go bound (ForAll v t _) = go (v : bound) t
179 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
180 go bound (RCons _ t r) = go bound t ++ go bound r
184-- Universally quantify over all type variables appearing free in a type
186quantify :: Type -> Type
187quantify 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