-
Notifications
You must be signed in to change notification settings - Fork 0
/
type-inference-code.arr
302 lines (255 loc) · 9.6 KB
/
type-inference-code.arr
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
provide *
provide-types *
import shared-gdrive("type-inference-definitions.arr", "0BwTSVb9gEh9-MHlNNkJOZUVOTHc") as C
t-num = C.t-num
t-bool = C.t-bool
t-fun = C.t-fun
t-list = C.t-list
t-var = C.t-var
# Feel free to modify any of the following types and function signatures. They
# are provided for your convenience.
data Constraint:
| constraint(ty-var :: C.TypeVar, ty :: C.Type)
end
type ConstraintSet = List<Constraint>
fun gen-constraints(expr :: C.Expr, ret :: C.TypeVar, env :: C.TEnv) -> ConstraintSet:
doc: ```Generates a type variable for each part of an expression,
and constructs constraints among them.```
cases (C.Expr) expr:
| e-op(op, left, right) => op-help(op, left, right, ret, env)
| e-un-op(op, ex) => un-help(op, ex, ret, env)
| e-if(cond, consq, altern) => if-help(cond, consq, altern, ret, env)
| e-let(name, ex, body) => let-help(name, ex, body, ret, env)
| e-lam(param, body) => lam-help(param, body, ret, env)
| e-app(func, arg) => app-help(func, arg, ret, env)
| e-id(name) => link(constraint(ret, t-var(lookup(name, env))), empty)
| e-num(value) => link(constraint(ret, t-num), empty)
| e-bool(value) => link(constraint(ret, t-bool), empty)
| e-empty() => link(constraint(ret, t-list(t-var(C.fresh-var()))), empty)
end
end
fun op-help(op :: C.Operator, left :: C.Expr, right :: C.Expr, ret :: C.TypeVar, env :: C.TEnv) -> ConstraintSet:
doc: "generate constraints for binary operator expressions"
l-var = C.fresh-var()
r-var = C.fresh-var()
l-cons = gen-constraints(left, l-var, env)
r-cons = gen-constraints(right, r-var, env)
cases (C.Operator) op:
| op-plus =>
ret-con = constraint(ret, t-num)
l-con = constraint(l-var, t-num)
r-con = constraint(r-var, t-num)
link(ret-con, link(l-con, link(r-con, l-cons.append(r-cons))))
| op-num-eq =>
ret-con = constraint(ret, t-bool)
l-con = constraint(l-var, t-num)
r-con = constraint(r-var, t-num)
link(ret-con, link(l-con, link(r-con, l-cons.append(r-cons))))
| op-link =>
list-type = C.fresh-var()
ret-con = constraint(ret, t-list(t-var(list-type)))
l-con = constraint(l-var, t-var(list-type))
r-con = constraint(r-var, t-list(t-var(list-type)))
link(ret-con, link(l-con, link(r-con, l-cons.append(r-cons))))
end
end
fun un-help(op :: C.UnaryOperator, expr :: C.Expr, ret :: C.TypeVar, env :: C.TEnv) -> ConstraintSet:
doc: "generate constraints for unary operator expressions"
expr-var = C.fresh-var()
expr-cons = gen-constraints(expr, expr-var, env)
list-type = C.fresh-var()
expr-con = constraint(expr-var, t-list(t-var(list-type)))
cases (C.UnaryOperator) op:
| op-first =>
ret-con = constraint(ret, t-var(list-type))
link(ret-con, link(expr-con, expr-cons))
| op-rest =>
ret-con = constraint(ret, t-list(t-var(list-type)))
link(ret-con, link(expr-con, expr-cons))
| op-is-empty =>
ret-con = constraint(ret, t-bool)
link(ret-con, link(expr-con, expr-cons))
end
end
fun if-help(cond :: C.Expr, consq :: C.Expr, altern :: C.Expr, ret :: C.TypeVar, env :: C.TEnv) -> ConstraintSet:
doc: "generate constraints for if expressions"
cond-var = C.fresh-var()
consq-var = C.fresh-var()
altern-var = C.fresh-var()
cond-cons = gen-constraints(cond, cond-var, env)
consq-cons = gen-constraints(consq, consq-var, env)
altern-cons = gen-constraints(altern, altern-var, env)
cond-con = constraint(cond-var, t-bool)
branch-con = constraint(consq-var, t-var(altern-var))
ret-con = constraint(ret, t-var(consq-var))
link(ret-con, link(branch-con, link(cond-con, cond-cons.append(consq-cons.append(altern-cons)))))
end
fun let-help(name :: String, expr :: C.Expr, body :: C.Expr, ret :: C.TypeVar, env :: C.TEnv) -> ConstraintSet:
doc: "generate constraints for let expressions"
expr-var = C.fresh-var()
body-var = C.fresh-var()
expr-cons = gen-constraints(expr, expr-var, env)
new-env = link(C.type-cell(name, expr-var), env)
body-cons = gen-constraints(body, body-var, new-env)
ret-con = constraint(ret, t-var(body-var))
link(ret-con, expr-cons.append(body-cons))
end
fun lam-help(param :: String, body :: C.Expr, ret :: C.TypeVar, env :: C.TEnv) -> ConstraintSet:
doc: "generate constraints for lambda expressions"
body-var = C.fresh-var()
param-var = C.fresh-var()
new-env = link(C.type-cell(param, param-var), env)
body-cons = gen-constraints(body, body-var, new-env)
ret-con = constraint(ret, t-fun(t-var(param-var), t-var(body-var)))
link(ret-con, body-cons)
end
fun app-help(func :: C.Expr, arg :: C.Expr, ret :: C.TypeVar, env :: C.TEnv) -> ConstraintSet:
doc: "generate constraints for function application expressions"
func-var = C.fresh-var()
arg-var = C.fresh-var()
func-cons = gen-constraints(func, func-var, env)
arg-cons = gen-constraints(arg, arg-var, env)
func-con = constraint(func-var, t-fun(t-var(arg-var), t-var(ret)))
link(func-con, arg-cons.append(func-cons))
end
fun lookup(name :: String, env :: C.TEnv) -> C.TypeVar:
doc: "find the binding with the given identifier name in the type environment and return the corresponding type-var"
cases (List) env:
| empty => raise(C.unbound-id-err(name))
| link(hd, tl) =>
if name == hd.id-name:
hd.var-type
else:
lookup(name, tl)
end
end
end
fun flatten(l :: List<List>) -> List:
doc: "turn a list of lists into a list"
cases (List) l:
| empty => empty
| link(hd, tl) => hd.append(flatten(tl))
end
end
fun compare-tys(ty1 :: C.Type, ty2 :: C.Type) -> ConstraintSet:
doc: "check that two types are consistent, and add constraints where they are not identical but still consistent; only called when two constraints have the same ty-var"
if C.is-t-var(ty1):
[list: constraint(ty1.name, ty2)]
else:
if C.is-t-var(ty2):
[list: constraint(ty2.name, ty1)]
else:
cases (C.Constructor) ty1.con:
| c-num =>
if C.is-c-num(ty2.con):
empty
else:
raise(C.unification-err(ty1, ty2))
end
| c-bool =>
if C.is-c-bool(ty2.con):
empty
else:
raise(C.unification-err(ty1, ty2))
end
| c-fun =>
if C.is-c-fun(ty2.con):
arg-cons = compare-tys(ty1.fields.get(0), ty2.fields.get(0))
ret-cons = compare-tys(ty1.fields.get(1), ty2.fields.get(1))
arg-cons.append(ret-cons)
else:
raise(C.unification-err(ty1, ty2))
end
| c-list =>
if C.is-c-list(ty2.con):
compare-tys(ty1.fields.get(0), ty2.fields.get(0))
else:
raise(C.unification-err(ty1, ty2))
end
end
end
end
end
fun sub-or-grow(sub :: Constraint, in :: Constraint) -> ConstraintSet:
doc: "compare types if two constraints have same ty-var, otherwise substitute"
if sub.ty-var == in.ty-var:
compare-tys(sub.ty, in.ty)
else:
[list: constraint(in.ty-var, substitute(sub, in.ty))]
end
end
fun substitute(sub :: Constraint, in-ty :: C.Type) -> C.Type:
doc: "substitute all instances of the constraint in the given type"
if C.is-t-var(in-ty):
if sub.ty-var == in-ty.name:
sub.ty
else:
in-ty
end
else:
cases (C.Constructor) in-ty.con:
| c-fun => C.t-con(C.c-fun, [list: substitute(sub, in-ty.fields.get(0)), substitute(sub, in-ty.fields.get(1))])
| c-list => C.t-con(C.c-list, [list: substitute(sub, in-ty.fields.get(0))])
| else => in-ty
end
end
end
fun contains(v :: C.TypeVar, ty :: C.Type) -> Boolean:
doc: "perform the contains check, i.e. ty-var of a constraint is not contained within its corresponding type"
if C.is-t-var(ty):
v == ty.name
else:
cases (C.Constructor) ty.con:
| c-num => false
| c-bool => false
| c-fun => contains(v, ty.fields.get(0)) or contains(v, ty.fields.get(1))
| c-list => contains(v, ty.fields.get(0))
end
end
end
fun unify(constraints :: ConstraintSet, sub :: ConstraintSet) -> ConstraintSet:
doc: ```Perform type unification to solve a set of constraints.```
cases (ConstraintSet) constraints:
| empty => sub
| link(hd, tl) =>
if contains(hd.ty-var, hd.ty):
raise(C.occurs-check-err(hd.ty-var, hd.ty))
else:
new-cons = flatten(tl.map(sub-or-grow(hd, _)))
new-sub = link(hd, sub.map(lam(x): constraint(x.ty-var, substitute(hd, x.ty)) end))
unify(new-cons, new-sub)
end
end
end
fun type-infer(e :: String) -> C.Type:
doc: "parses and infers the type of the expression"
expr = C.parse(e)
ret = C.fresh-var()
constraints = gen-constraints(expr, ret, empty)
sub = unify(constraints, empty)
C.normalize-type(pretty-print(ret, sub))
end
fun pretty-print(ret :: C.TypeVar, sub :: ConstraintSet) -> C.Type:
doc: "replace all instances of a ty-var in a type with the type contained in the substitution"
fun helper(ty :: C.Type, shadow sub :: ConstraintSet) -> C.Type:
if C.is-t-var(ty):
find-in-sub(ty.name, sub)
else:
cases (C.Constructor) ty.con:
| c-num => ty
| c-bool => ty
| c-fun => C.t-con(C.c-fun, [list: helper(ty.fields.get(0), sub), helper(ty.fields.get(1), sub)])
| c-list => C.t-con(C.c-list, [list: helper(ty.fields.get(0), sub)])
end
end
end
helper(find-in-sub(ret, sub), sub)
end
fun find-in-sub(name :: String, sub :: ConstraintSet) -> C.Type:
doc: "find the constraint corresponding to the given type-var in the substitution"
cases (List) sub:
| empty => t-var(name)
| link(hd, tl) =>
if hd.ty-var == name: hd.ty else: find-in-sub(name, tl) end
end
end