-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.sml
166 lines (133 loc) · 4.21 KB
/
env.sml
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
local
open Internal
in
structure Env :> sig
type t
val initial : t
val insert : struct_ -> t -> t
val uvs : t -> U.Set.t
structure Module : sig
exception Unbound of module_ident
val lookup : t -> module_ident -> semsig
val insert : module_ident -> semsig -> t -> t
end
structure Signature : sig
exception Unbound of signature_ident
val lookup : t -> signature_ident -> asig
end
structure Value : sig
exception Unbound of value_ident
datatype entry
= ModL of path * scheme
| CoreL of tycon
val lookup : t -> value_ident -> entry
val insert : value_ident -> entry -> t -> t
end
structure Type : sig
exception Unbound of type_ident
val lookup : t -> type_ident -> tycon * kind
end
structure TVar : sig
exception Unbound of tvar
val lookup : t -> tvar -> fvar
val insert : tvar -> fvar -> t -> t
end
end = struct
datatype value_entry
= ModL of path * scheme
| CoreL of tycon
structure T = Map (TVar)
type t =
{ m : semsig ModuleIdent.Map.t
, s : asig SignatureIdent.Map.t
, v : value_entry ValueIdent.Map.t
, t : (tycon * kind) TypeIdent.Map.t
, tv : fvar T.t
}
val initial : t =
{ m = ModuleIdent.Map.empty
, s = SignatureIdent.Map.empty
, v = ValueIdent.Map.empty
, t = TypeIdent.Map.from_list [(TypeIdent.from_string "unit", (Unit, KBase))]
, tv = T.empty
}
fun insert (s : struct_) (e : t) =
( { m = ModuleIdent.Map.fold (fn (k, v, acc) => ModuleIdent.Map.insert k v acc) (#m e) (#m s)
, s = SignatureIdent.Map.fold (fn (k, v, acc) => SignatureIdent.Map.insert k v acc) (#s e) (#s s)
, v = ValueIdent.Map.fold (fn (k, v, acc) => ValueIdent.Map.insert k (ModL v) acc) (#v e) (#v s)
, t = TypeIdent.Map.fold (fn (k, v, acc) => TypeIdent.Map.insert k v acc) (#t e) (#t s)
, tv = #tv e
}
)
local
structure S = U.Set
val op@ = U.Lwd.append
fun union l s = foldl (fn (v, acc) => S.insert v acc) s (U.Lwd.to_list l)
in
fun uvs ({m, s, v, t, ...} : t) =
let
fun fm k = ModuleIdent.Map.fold (fn (_, s, acc) => union (uvs_semsig s) acc) k m
fun fs k = SignatureIdent.Map.fold (fn (_, asig, acc) => union (uvs_asig asig) acc) k s
fun fv k =
ValueIdent.Map.fold (fn (_, ModL(p, s), acc) => union (uvs_path p @ uvs_scheme s) acc
| (_, CoreL _, acc) => acc) k v
fun ft k = TypeIdent.Map.fold (fn (_, (ty, _), acc) => union (uvs_tycon ty) acc) k t
in
(ft o fv o fs o fm) S.empty
end
end
structure Module = struct
exception Unbound of module_ident
fun lookup (e : t) id =
valOf (ModuleIdent.Map.lookup id (#m e))
handle Option => raise Unbound id
fun insert id x (e : t) =
{ m = ModuleIdent.Map.insert id x (#m e)
, s = #s e
, v = #v e
, t = #t e
, tv = #tv e
}
end
structure Signature = struct
exception Unbound of signature_ident
fun lookup (e : t) id =
valOf (SignatureIdent.Map.lookup id (#s e))
handle Option => raise Unbound id
end
structure Value = struct
exception Unbound of value_ident
datatype entry = datatype value_entry
fun lookup (e : t) id =
valOf (ValueIdent.Map.lookup id (#v e))
handle Option => raise Unbound id
fun insert id x (e : t) =
{ m = #m e
, s = #s e
, v = ValueIdent.Map.insert id x (#v e)
, t = #t e
, tv = #tv e
}
end
structure Type = struct
exception Unbound of type_ident
fun lookup (e : t) id =
valOf (TypeIdent.Map.lookup id (#t e))
handle Option => raise Unbound id
end
structure TVar = struct
exception Unbound of tvar
fun lookup ({tv, ...} : t) id =
valOf (T.lookup id tv)
handle Option => raise Unbound id
fun insert id x (e : t) =
{ m = #m e
, s = #s e
, v = #v e
, t = #t e
, tv = T.insert id x (#tv e)
}
end
end
end
type env = Env.t