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
21import Data.Generics (mkT, mkQ, everywhereBut)
23import Language.PureScript.Names
24import Language.PureScript.Unknown (Unknown(..))
31 -- A unification variable of type Type
33 = TUnknown (Unknown Type)
43 -- Javascript booleans
47 -- Javascript array type constructor
51 -- Records, parameterized by a row of types
55 -- A function, with zero or more arguments
57 | Function [Type] Type
59 -- A named type variable
65 | TypeConstructor (Qualified ProperName)
71 -- A type synonym which is \"saturated\", i.e. fully applied
73 | SaturatedTypeSynonym (Qualified ProperName) [Type]
79 -- A type with a set of type class constraints
81 | ConstrainedType [(Qualified ProperName, Type)] Type
93 | RCons String Type 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
115isMonoType ty = isPolyType ty
118-- Check whather a type is a valid polytype
120isPolyType :: Type -> Bool
121isPolyType (Object ps) = all isPolyType (map snd . fst $ rowToList ps)
122isPolyType (Function args ret) = all isPolyType args && isPolyType ret
123isPolyType (TypeApp t1 t2) = isMonoType t1 && isMonoType t2
124isPolyType (SaturatedTypeSynonym _ args) = all isPolyType args
125isPolyType (ForAll _ ty) = isPolyType ty
129-- Universally quantify a type
131mkForAll :: [String] -> Type -> Type
132mkForAll = flip . foldl . flip $ ForAll
135-- The empty record type
141-- Replace a type variable, taking into account variable shadowing
143replaceTypeVars :: (Data d) => String -> Type -> d -> d
144replaceTypeVars name t = everywhereBut (mkQ False isShadowed) (mkT replaceTypeVar)
146 replaceTypeVar (TypeVar v) | v == name = t
147 replaceTypeVar other = other
148 isShadowed (ForAll v _) | v == name = True
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