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 Control.Monad.Unify
25import Language.PureScript.Names
28-- An identifier for the scope of a skolem variable
30newtype SkolemScope = SkolemScope { runSkolemScope :: Int } deriving (Show, Eq, Ord, Data, Typeable)
37 -- A unification variable of type Type
45 -- A named type variable
51 | TypeConstructor (Qualified ProperName)
57 -- A type synonym which is \"saturated\", i.e. fully applied
59 | SaturatedTypeSynonym (Qualified ProperName) [Type]
63 | ForAll String Type (Maybe SkolemScope)
65 -- A type with a set of type class constraints
67 | ConstrainedType [(Qualified ProperName, Type)] Type
71 | Skolem Int SkolemScope
79 | RCons String Type Type
81 -- A placeholder used in pretty printing
83 | PrettyPrintFunction Type Type
85 -- A placeholder used in pretty printing
87 | PrettyPrintArray Type deriving (Show, Eq, Data, Typeable)
90-- Type constructor for functions
93tyFunction = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Function")
96-- Type constructor for strings
99tyString = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "String")
102-- Type constructor for numbers
105tyNumber = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Number")
108-- Type constructor for booleans
111tyBoolean = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Boolean")
114-- Type constructor for arrays
117tyArray = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Array")
120-- Smart constructor for function types
122function :: Type -> Type -> Type
123function t1 t2 = TypeApp (TypeApp tyFunction t1) t2
126-- Convert a row to a list of pairs of labels and types
128rowToList :: Type -> ([(String, Type)], Type)
129rowToList (RCons name ty row) = let (tys, rest) = rowToList row
130 in ((name, ty):tys, rest)
134-- Convert a list of labels and types to a row
136rowFromList :: ([(String, Type)], Type) -> Type
137rowFromList ([], r) = r
138rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
141-- Check whether a type is a monotype
143isMonoType :: Type -> Bool
144isMonoType (ForAll _ _ _) = False
148-- Universally quantify a type
150mkForAll :: [String] -> Type -> Type
151mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
154-- The empty record type
160-- Replace a type variable, taking into account variable shadowing
162replaceTypeVars :: (Data d) => String -> Type -> d -> d
163replaceTypeVars name t = everywhereBut (mkQ False isShadowed) (mkT replaceTypeVar)
165 replaceTypeVar (TypeVar v) | v == name = t
166 replaceTypeVar other = other
167 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