-
Notifications
You must be signed in to change notification settings - Fork 3
/
Gamma.hs
113 lines (88 loc) · 3.28 KB
/
Gamma.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
--------------------------------------------------------------------------
-- The type environment Gamma
--------------------------------------------------------------------------
module Gamma( Gamma
, gamma0 -- the initial gamma
, gammaFind
, gammaExtend, gammaExtendLam
, gammaCoDomain
, gammaDepth
) where
import qualified Data.Map as Map
import Types
import Parser( readType )
--------------------------------------------------------------------------
-- initial gamma
--------------------------------------------------------------------------
gamma0 :: Gamma
gamma0
= gammaCreate
-- builtin functions
[("(,)", "forall a b. a -> b -> (a,b)")
,("[]", "forall a. [a]")
,(":", "forall a. a -> [a] -> [a]")
,("$", "forall a b. (a -> b) -> a -> b")
,("if", "forall a. Bool -> a -> a -> a")
-- standard functions
,("id", "forall a. a -> a")
,("apply", "forall a b. (a -> b) -> a -> b")
,("const", "forall a b. a -> b -> a")
,("choose", "forall a. a -> a -> a")
,("revapp", "forall a b. a -> (a -> b) -> b")
,("undefined", "forall a. a")
-- booleans
,("True", "Bool")
,("False", "Bool")
-- integers
,("plus", "Int -> Int -> Int")
,("lt", "Int -> Int -> Bool")
,("gt", "Int -> Int -> Bool")
,("inc", "Int -> Int")
-- polymorphic functions
,("ids", "[forall a. a -> a]")
,("auto", "(forall a. a -> a) -> (forall a. a -> a)")
,("xauto", "forall a. (forall b. b -> b) -> a -> a")
,("takeAuto", "((forall a. a -> a) -> (forall a. a -> a)) -> (forall a. a -> a)")
-- lists
,("single", "forall a. a -> [a]")
,("head", "forall a. [a] -> a")
,("tail", "forall a. [a] -> [a]")
,("map", "forall a b. (a -> b) -> [a] -> [b]")
,("length", "forall a. [a] -> Int")
,("null", "forall a. [a]-> Bool")
-- tuples
,("fst", "forall a b. (a,b) -> a")
,("snd", "forall a b. (a,b) -> b")
-- ST monad
,("runST", "forall a. (forall s. ST s a) -> a")
,("newRef", "forall a s. a -> ST s (Ref s a)")
,("returnST", "forall a s. a -> ST s a")
]
--------------------------------------------------------------------------
-- Gamma is a mapping from (term) variables to types
--------------------------------------------------------------------------
-- | Gamma maps term variables to types
data Gamma = Gamma Rank (Map.Map Name Type)
gammaCreate :: [(String,String)] -> Gamma
gammaCreate bindings
= gammaFromList $ [(name,readType tp) | (name,tp) <- bindings]
gammaEmpty
= Gamma 0 Map.empty
gammaFromList xs
= Gamma 0 (Map.fromList xs)
gammaFind (Gamma d g) name
= case Map.lookup name g of
Nothing -> error ("unbound variable: " ++ name)
Just tp -> tp
gammaExtend (Gamma d g) name tp
= Gamma d (Map.insert name tp g)
gammaExtendLam (Gamma d g) name tp
= Gamma (d+1) (Map.insert name tp g)
gammaDepth :: Gamma -> Rank
gammaDepth (Gamma d m)
= d
instance Show Gamma where
show (Gamma d g) = show g
gammaCoDomain :: Gamma -> [Type]
gammaCoDomain (Gamma d g)
= Map.elems g