-
Notifications
You must be signed in to change notification settings - Fork 4
/
dist.jl
430 lines (393 loc) · 11 KB
/
dist.jl
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# Define DistSTLC
module Typ
using Dice
@inductive T TBool() TFun(T, T)
end
module Expr
using Dice
using Main: DistNat, Typ
@inductive T Var(DistNat) Bool(AnyBool) Abs(Typ.T, T) App(T, T)
end
to_coq(::Type{Expr.T}) = "Expr"
to_coq(::Type{Typ.T}) = "Typ"
function term_size(e::Expr.T)
match(e, [
:Var => (i) -> DistUInt32(1),
:Bool => (b) -> DistUInt32(1),
:App => (f, x) -> DistUInt32(1) + term_size(f) + term_size(x),
:Abs => (ty, e′) -> DistUInt32(1) + term_size(e′),
])
end
function term_size(e::Opt.T{Expr.T})
match(e, [
:Some => e -> term_size(e),
:None => () -> DistUInt32(1024),
])
end
function num_apps(e::Opt.T{Expr.T})
match(e, [
:Some => x -> num_apps(x),
:None => () -> DistUInt32(1024),
])
end
function num_apps(e::Expr.T)
match(e, [
:Var => (i) -> DistUInt32(0),
:Bool => (b) -> DistUInt32(0),
:App => (f, x) -> DistUInt32(1) + num_apps(f) + num_apps(x),
:Abs => (ty, e′) -> num_apps(e′),
])
end
stlc_ctor_to_id = Dict(
:Var => DistInt32(0),
:Bool => DistInt32(1),
:App => DistInt32(2),
:Abs => DistInt32(3),
)
function ctor_to_id(ctor::Expr.T)
match(ctor, [
:Var => _ -> stlc_ctor_to_id[:Var]
:Bool => _ -> stlc_ctor_to_id[:Bool]
:App => (_, _) -> stlc_ctor_to_id[:App]
:Abs => (_, _) -> stlc_ctor_to_id[:Abs]
])
end
function opt_ctor_to_id(opt_ctor::Opt.T{Expr.T})
match(opt_ctor, [
:Some => ctor_to_id,
:None => () -> DistInt32(-1),
])
end
function collect_constructors(e)
match(e, [
:Var => (i) -> DistVector([stlc_ctor_to_id[:Var]]),
:Bool => (b) -> DistVector([stlc_ctor_to_id[:Bool]]),
:App => (f, x) -> prob_append(prob_extend(collect_constructors(f), collect_constructors(x)), stlc_ctor_to_id[:App]),
:Abs => (ty, e′) -> prob_append(collect_constructors(e′), stlc_ctor_to_id[:Abs]),
])
end
# https://stackoverflow.com/questions/59338968/printing-lambda-expressions-in-haskell
parens(b, s) = if b "($(s))" else s end
@enum StrfyCtx free=0 fun=1 arg=2
function ty_str(ty, free=true)
name, children = ty
if name == :TBool
"Bool"
else
t1, t2 = children
parens(
!free,
"$(ty_str(t1, false)) -> $(ty_str(t2, true))"
)
end
end
function var_str(i)
i += 1 # 1-idx
vars = ["x", "y", "z", "w"]
if i <= 0
"badvar_$(i)"
elseif i <= length(vars)
vars[i]
else
string('a' + i - length(vars) - 1)
end
end
function stlc_str(ast, depth=0, p=free)
name, children = ast
if name == :Var
i, = children
i isa Integer || (i = nat_ast_to_int(i))
# i is the number of steps from the *top* of the env, see gen_var
var_depth = depth - i - 1
var_str(var_depth)
elseif name == :Bool
v, = children
string(v)
elseif name == :Abs
ty, e = children
parens(p > free, "λ$(var_str(depth)):$(ty_str(ty)). $(stlc_str(e, depth + 1, free))")
elseif name == :App
e1, e2 = children
parens(
p > fun,
"$(stlc_str(e1, depth, fun)) $(stlc_str(e2, depth, arg))"
)
else
error("Bad node $(name)")
end
end
# ironic abuse of types
function error_ty(ty)
ty isa AbstractString
end
function get_error(ty)
ty
end
function opt_map(f, x::Tuple)
name, children = x
if name == :Some
e, = children
f(e)
elseif name == :None
nothing
else
error()
end
end
function opt_map(f, x::Opt.T)
@match x [
None() -> nothing,
Some(x) -> f(x),
]
end
function diff_test_typecheck(expr_dist, expr)
@assert isdeterministic(expr_dist)
opt_map(expr_dist) do expr_dist
opt_map(expr) do expr
ty1 = typecheck(expr)
ty2_dist = pr(typecheck(expr_dist))
@assert length(ty2_dist) == 1
ty2 = first(keys(ty2_dist))
if error_ty(ty1)
@assert ty2 == (:None, [])
else
@assert ty2 == (:Some, [ty1]) "$ty1 $ty2"
end
end
end
end
function to_int(x::DistUInt32)
dist = pr(x)
@assert length(dist) == 1
first(keys(dist))
end
function typecheck(ast::Expr.T, gamma, depth=0)::Opt.T{Typ.T}
@match ast [
Var(i) -> begin
var_depth = depth - to_int(i) - 1
haskey(gamma, var_depth) || return Opt.None(Typ.T)
Opt.Some(gamma[var_depth])
end,
Bool(_) -> Opt.Some(Typ.TBool()),
Abs(t_in, e) -> begin
gamma′ = copy(gamma)
gamma′[depth] = t_in
Opt.map(Typ.T, typecheck(e, gamma′, depth + 1)) do t_out
Typ.TFun(t_in, t_out)
end
end,
App(e1, e2) -> begin
Opt.bind(Typ.T, typecheck(e1, gamma, depth)) do t1
@match t1 [
TBool() -> Opt.None(Typ.T),
TFun(t1_in, t1_out) -> Opt.bind(Typ.T, typecheck(e2, gamma, depth)) do t2
if prob_equals(t1_in, t2)
Opt.Some(t1_out)
else
Opt.None(Typ.T)
end
end,
]
end
end,
]
end
function typecheck_opt(ast)
name, children = ast
if name == :Some
e, = children
ty = typecheck(e)
if error_ty(ty)
println("Failed to typecheck $(stlc_str(e))")
println(get_error(ty))
println()
end
elseif name == :None
# do nothing
else
error("Bad node $(name)")
end
end
typecheck(ast) = typecheck(ast, Dict())
function typecheck(ast::Tuple, gamma, depth=0)
name, children = ast
if name == :Var
i, = children
i isa Integer || (i = nat_ast_to_int(i))
var_depth = depth - i - 1
if !haskey(gamma, var_depth)
return "Unknown var $(var_str(var_depth))"
end
gamma[var_depth]
elseif name == :Bool
(:TBool, [])
elseif name == :Abs
t_in, e = children
gamma′ = copy(gamma)
gamma′[depth] = t_in
t_out = typecheck(e, gamma′, depth + 1)
error_ty(t_out) && return t_out
(:TFun, [t_in, t_out])
elseif name == :App
e1, e2 = children
t1 = typecheck(e1, gamma, depth)
error_ty(t1) && return t1
if t1[1] != :TFun
return "\"$(stlc_str(e1, depth))\" typechecked to $(ty_str(t1)), expected function"
end
t2 = typecheck(e2, gamma, depth)
error_ty(t2) && return t2
t1_in, t1_out = t1[2]
if t1_in != t2
return "Expected \"$(stlc_str(e2, depth))\" to be $(ty_str(t1_in)), got $(ty_str(t2))"
end
t1_out
else
error("Bad node $(name)")
end
end
function eq_except_numbers(x::Typ.T, y::Typ.T)
@match x [
TBool() -> (@match y [
TBool() -> true,
TFun(_, _) -> false,
]),
TFun(a1, b1) -> (@match y [
TBool() -> false,
TFun(a2, b2) -> eq_except_numbers(a1, a2) & eq_except_numbers(b1, b2),
]),
]
end
function eq_except_numbers(x::Expr.T, y::Expr.T)
@match x [
Var(_) -> (@match y [
Var(_) -> true,
Bool(_) -> false,
App(_, _) -> false,
Abs(_, _) -> false,
]),
Bool(_) -> (@match y [
Var(_) -> false,
Bool(_) -> true,
App(_, _) -> false,
Abs(_, _) -> false,
]),
App(f1, x1) -> (@match y [
Var(_) -> false,
Bool(_) -> false,
App(f2, x2) -> eq_except_numbers(f1, f2) & eq_except_numbers(x1, x2),
Abs(_, _) -> false,
]),
Abs(ty1, e1) -> (@match y [
Var(_) -> false,
Bool(_) -> false,
App(_, _) -> false,
Abs(ty2, e2) -> eq_except_numbers(ty1, ty2) & eq_except_numbers(e1, e2),
]),
]
end
function has_app(x::Expr.T)
@match x [
Var(_) -> false,
Bool(_) -> false,
App(_, _) -> true,
Abs(_, e) -> has_app(e),
]
end
function eq_structure(x::Expr.T, y::Expr.T)
@match x [
Var(_) -> (@match y [
Var(_) -> true,
Bool(_) -> false,
App(_, _) -> false,
Abs(_, _) -> false,
]),
Bool(_) -> (@match y [
Var(_) -> false,
Bool(_) -> true,
App(_, _) -> false,
Abs(_, _) -> false,
]),
App(f1, x1) -> (@match y [
Var(_) -> false,
Bool(_) -> false,
App(f2, x2) -> eq_structure(f1, f2) & eq_structure(x1, x2),
Abs(_, _) -> false,
]),
Abs(_, e1) -> (@match y [
Var(_) -> false,
Bool(_) -> false,
App(_, _) -> false,
Abs(_, e2) -> eq_structure(e1, e2),
]),
]
end
function eq_except_numbers(x::Opt.T{T}, y::Opt.T{T}) where T
@match x [
Some(xv) -> (@match y [
Some(yv) -> eq_except_numbers(xv, yv),
None() -> false,
]),
None() -> (@match y [
Some(_) -> false,
None() -> true,
])
]
end
function eq_structure(x::Opt.T{T}, y::Opt.T{T}) where T
@match x [
Some(xv) -> (@match y [
Some(yv) -> eq_structure(xv, yv),
None() -> false,
]),
None() -> (@match y [
Some(_) -> false,
None() -> true,
])
]
end
function eq_num_apps(x::Opt.T{T}, y::Opt.T{T}) where T
@match x [
Some(xv) -> (@match y [
Some(yv) -> prob_equals(num_apps(xv), num_apps(yv)),
None() -> false,
]),
None() -> (@match y [
Some(_) -> false,
None() -> true,
])
]
end
function sat_num_apps(e::Expr.T, k::DistUInt32)
@match e [
Var(_) -> DistUInt32(0),
Bool(_) -> DistUInt32(0),
App(f, x) -> min(min(DistUInt32(1), k) + sat_num_apps(f, k) + sat_num_apps(x, k), k),
Abs(_, e′) -> sat_num_apps(e′, k),
]
end
# TODO: why is saturating at 1 different than eq_has_app?
function sat_eq_num_apps(x::Opt.T{T}, y::Opt.T{T}, k::Integer) where T
@match x [
Some(xv) -> (@match y [
Some(yv) -> prob_equals(sat_num_apps(xv, DistUInt32(k)), sat_num_apps(yv, DistUInt32(k))),
None() -> false,
]),
None() -> (@match y [
Some(_) -> false,
None() -> true,
])
]
end
function eq_has_app(x::Opt.T{T}, y::Opt.T{T}) where T
@match x [
Some(xv) -> (@match y [
Some(yv) -> prob_equals(has_app(xv), has_app(yv)),
None() -> false,
]),
None() -> (@match y [
Some(_) -> false,
None() -> true,
])
]
end