git.iko.soy/rowtype-yoga/purescript-native
on e5ffbf9 help sign in
168 lines 3.9 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Types
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- Data types for types
13--
14-----------------------------------------------------------------------------
16{-# LANGUAGE DeriveDataTypeable #-}
18module Language.PureScript.Types where
20import Data.Data
21import Data.Generics (mkT, mkQ, everywhereBut)
23import Control.Monad.Unify
25import Language.PureScript.Names
27-- |
28-- An identifier for the scope of a skolem variable
29--
30newtype SkolemScope = SkolemScope { runSkolemScope :: Int } deriving (Show, Eq, Ord, Data, Typeable)
32-- |
33-- The type of types
34--
35data Type
36 -- |
37 -- A unification variable of type Type
38 --
39 = TUnknown Unknown
40 -- |
41 -- Javascript numbers
42 --
43 | Object Type
44 -- |
45 -- A named type variable
46 --
47 | TypeVar String
48 -- |
49 -- A type constructor
50 --
51 | TypeConstructor (Qualified ProperName)
52 -- |
53 -- A type application
54 --
55 | TypeApp Type Type
56 -- |
57 -- A type synonym which is \"saturated\", i.e. fully applied
58 --
59 | SaturatedTypeSynonym (Qualified ProperName) [Type]
60 -- |
61 -- Forall quantifier
62 --
63 | ForAll String Type (Maybe SkolemScope)
64 -- |
65 -- A type with a set of type class constraints
66 --
67 | ConstrainedType [(Qualified ProperName, Type)] Type
68 -- |
69 -- A skolem constant
70 --
71 | Skolem Int SkolemScope
72 -- |
73 -- An empty row
74 --
75 | REmpty
76 -- |
77 -- A non-empty row
78 --
79 | RCons String Type Type
80 -- |
81 -- A placeholder used in pretty printing
82 --
83 | PrettyPrintFunction Type Type
84 -- |
85 -- A placeholder used in pretty printing
86 --
87 | PrettyPrintArray Type deriving (Show, Eq, Data, Typeable)
89-- |
90-- Type constructor for functions
91--
92tyFunction :: Type
93tyFunction = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prim") (ProperName "Function")
95-- |
96-- Type constructor for strings
97--
98tyString :: Type
99tyString = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prim") (ProperName "String")
101-- |
102-- Type constructor for numbers
103--
104tyNumber :: Type
105tyNumber = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prim") (ProperName "Number")
107-- |
108-- Type constructor for booleans
109--
110tyBoolean :: Type
111tyBoolean = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prim") (ProperName "Boolean")
113-- |
114-- Type constructor for arrays
115--
116tyArray :: Type
117tyArray = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prim") (ProperName "Array")
119-- |
120-- Smart constructor for function types
121--
122function :: Type -> Type -> Type
123function t1 t2 = TypeApp (TypeApp tyFunction t1) t2
125-- |
126-- Convert a row to a list of pairs of labels and types
127--
128rowToList :: Type -> ([(String, Type)], Type)
129rowToList (RCons name ty row) = let (tys, rest) = rowToList row
130 in ((name, ty):tys, rest)
131rowToList r = ([], r)
133-- |
134-- Convert a list of labels and types to a row
135--
136rowFromList :: ([(String, Type)], Type) -> Type
137rowFromList ([], r) = r
138rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
140-- |
141-- Check whether a type is a monotype
142--
143isMonoType :: Type -> Bool
144isMonoType (ForAll _ _ _) = False
145isMonoType ty = True
147-- |
148-- Universally quantify a type
149--
150mkForAll :: [String] -> Type -> Type
151mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
153-- |
154-- The empty record type
155--
156unit :: Type
157unit = Object REmpty
159-- |
160-- Replace a type variable, taking into account variable shadowing
161--
162replaceTypeVars :: (Data d) => String -> Type -> d -> d
163replaceTypeVars name t = everywhereBut (mkQ False isShadowed) (mkT replaceTypeVar)
164 where
165 replaceTypeVar (TypeVar v) | v == name = t
166 replaceTypeVar other = other
167 isShadowed (ForAll v _ _) | v == name = True
168 isShadowed _ = False