-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexprinterop.jl
More file actions
468 lines (420 loc) · 15 KB
/
Copy pathexprinterop.jl
File metadata and controls
468 lines (420 loc) · 15 KB
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# AST
#####
"""Coerce GATs to GAT contexts"""
fromexpr(g::GAT, e, t) = fromexpr(GATContext(g), e, t)
function parse_methodapp(c::GATContext, head::Symbol, argexprs)
args = Vector{AlgTerm}(fromexpr.(Ref(c), argexprs, Ref(AlgTerm)))
fun = fromexpr(c, head, Ident)
signature = AlgSort.(Ref(c), args)
method = try
methodlookup(c, fun, signature)
catch e
error("couldn't find method for $(Expr(:call, head, argexprs...))")
end
MethodApp{AlgTerm}(fun, method, args)
end
function toexpr(c::Context, m::MethodApp)
Expr(:call, toexpr(c, m.head), toexpr.(Ref(c), m.args)...)
end
function toexpr(c::Context, m::AlgDot)
Expr(:., toexpr(c, m.body), QuoteNode(m.head))
end
function fromexpr(c::GATContext, e, ::Type{AlgTerm})
@match e begin
s::Symbol => begin
x = fromexpr(c, s, Ident)
value = getvalue(c[x])
if value isa AlgType
AlgTerm(fromexpr(c, s, Ident))
else
error("nullary constructors must be explicitly called: $e")
end
end
Expr(:., body, QuoteNode(head)) => begin
t = fromexpr(c, body, AlgTerm)
algstruct = c[AlgSort(c, t).method] |> getvalue
AlgTerm(AlgDot(head, t))
end
Expr(:call, head::Symbol, argexprs...) => AlgTerm(parse_methodapp(c, head, argexprs))
Expr(:(::), val, type) => AlgTerm(Constant(val, fromexpr(c, type, AlgType)))
e::Expr => error("could not parse AlgTerm from $e")
constant::Constant => AlgTerm(constant)
end
end
function toexpr(c::Context, term::AlgTerm)
toexpr(c, term.body)
end
function fromexpr(p::GATContext, e, ::Type{AlgType})::AlgType
@match e begin
s::Symbol => AlgType(parse_methodapp(p, s, []))
Expr(:call, head, args...) && if head != :(==) end =>
AlgType(parse_methodapp(p, head, args))
Expr(:call, :(==), lhs, rhs) =>
AlgType(p, fromexpr(p, lhs, AlgTerm), fromexpr(p, rhs, AlgTerm))
_ => error("could not parse AlgType from $e")
end
end
function toexpr(c::Context, type::AlgType)
if isapp(type)
if length(type.body.args) == 0
toexpr(c, type.body.head)
else
Expr(:call, toexpr(c, type.body.head), toexpr.(Ref(c), type.body.args)...)
end
elseif iseq(type)
Expr(:call, :(==), toexpr.(Ref(c), type.body.equands)...)
end
end
function fromexpr(c::GATContext, e, ::Type{AlgSort})
e isa Symbol || error("expected a Symbol to parse a sort, got: $e")
decl = ident(c.theory; name=e)
method = only(allmethods(c.theory.resolvers[decl]))[2]
AlgSort(decl, method)
end
function toexpr(c::GATContext, s::AlgSort)
toexpr(c, getdecl(s))
end
toexpr(c::Context, constant::Constant; kw...) =
Expr(:(::), constant.value, toexpr(c, constant.type; kw...))
function fromexpr(c::GATContext, e, ::Type{InCtx{T}}; kw...) where T
(termexpr, localcontext) = @match e begin
Expr(:call, :(⊣), binding, tscope) => (binding, fromexpr(c, tscope, TypeScope))
e => (e, TypeScope())
end
term = fromexpr(AppendContext(c, localcontext), termexpr, T)
InCtx{T}(localcontext, term)
end
function toexpr(c::Context, tic::InCtx; kw...)
c′ = AppendContext(c, tic.ctx)
etrm = toexpr(c′, tic.val; kw...)
flat = TypeScope(tic.ctx)
ectx = toexpr(c, flat; kw...)
Expr(:call, :(⊣), etrm, ectx)
end
# Judgments
###########
function toexpr(p::Context, b::Binding{AlgType})
Expr(:(::), nameof(b), toexpr(p, getvalue(b)))
end
function bindingexprs(c::Context, s::HasScope)
c′ = AppendContext(c, s)
[Expr(:(::), nameof(b), toexpr(c′, getvalue(b))) for b in s]
end
function toexpr(c::Context, ts::TypeScope)
Expr(:vect, bindingexprs(c, ts)...)
end
function judgmenthead(_::GAT, name, _::Union{AlgDeclaration, AlgAccessor})
nothing
end
function judgmenthead(_::GAT, _, judgment::AlgTypeConstructor)
name = nameof(getdecl(judgment))
untyped = if isempty(judgment.args)
name
else
Expr(:call, name, nameof.(argsof(judgment))...)
end
Expr(:(::), untyped, :TYPE)
end
function judgmenthead(theory::GAT, _, judgment::AlgTermConstructor)
name = nameof(getdecl(judgment))
untyped = Expr(:call, name, nameof.(argsof(judgment))...)
c = GATContext(theory, judgment.localcontext)
Expr(:(::), untyped, toexpr(c, judgment.type))
end
function judgmenthead(theory::GAT, name, judgment::AlgAxiom)
c = GATContext(theory, judgment.localcontext)
untyped = Expr(:call, :(==), toexpr(c, judgment.equands[1]), toexpr(c, judgment.equands[2]))
if isnothing(name)
untyped
else
Expr(:(:=), name, untyped)
end
end
function judgmenthead(theory::GAT, name′, judgment::AlgFunction)
isnothing(name′) || error("AlgFunction received name argument $name′")
name = nameof(getdecl(judgment))
call = Expr(:call, name, nameof.(argsof(judgment))...)
val = toexpr(GATContext(theory, judgment.localcontext), judgment.value)
Expr(:(:=), call, val)
end
function toexpr(theory::GAT, judgment::AlgStruct)
name = nameof(getdecl(judgment))
ctx = GATContext(theory, judgment.localcontext)
callargs = [toexpr(ctx, a) for a in typeargsof(judgment)]
body = [toexpr(ctx, a) for a in argsof(judgment)]
call = Expr(:call, name, callargs...)
lc = [b for (i,b) in enumerate(getbindings(judgment.localcontext))
if LID(i) ∉ judgment.typeargs ∪ judgment.fields] |> TypeScope
stexpr = Expr(:struct, false, call, Expr(:block, body...))
if isempty(lc)
return stexpr
else
return Expr(:call, :⊣, stexpr, toexpr(theory, lc))
end
end
function toexpr(c::GAT, binding::Binding{Judgment})
judgment = getvalue(binding)
name = nameof(binding)
# FIXME
if judgment isa Alias
return nothing
elseif judgment isa AlgStruct
return toexpr(c, judgment)
end
head = judgmenthead(c, name, judgment)
if isnothing(head)
return nothing
end
Expr(:call, :(⊣), head, Expr(:vect, bindingexprs(c, judgment.localcontext)...))
end
# fromexpr
"""
`f(pushbinding!, expr)` should inspect `expr` and call `pushbinding!`
0 or more times with two arguments: the name and value of a new binding.
"""
function parse_scope!(f::Function, scope::Scope, lines::AbstractVector)
currentln = nothing
for e in lines
@match e begin
l::LineNumberNode => (currentln = l)
_ => f(e) do binding
Scopes.unsafe_pushbinding!(scope, setline(binding, currentln))
end
end
end
scope
end
function fromexpr(c::GATContext, e, ::Type{Binding{AlgType}}; line=nothing)
@match e begin
Expr(:(::), name::Symbol, type_expr) =>
Binding{AlgType}(name, fromexpr(c, type_expr, AlgType), line)
_ => error("could not parse binding of name to type from $e")
end
end
function parse_binding_expr!(c::GATContext, pushbinding!, e)
p!(name, type_expr) = pushbinding!(Binding{AlgType}(name, fromexpr(c, type_expr, AlgType)))
@match e begin
a::Symbol => p!(a, :default)
Expr(:tuple, names...) => p!.(names, Ref(:default))
Expr(:(::), Expr(:tuple, names...), T) => p!.(names, Ref(T))
Expr(:(::), name, T) => p!(name, T)
Expr(:call, :(==), lhs, rhs) =>
pushbinding!(Binding{AlgType}(
nothing, AlgType(c, fromexpr(c, lhs, AlgTerm), fromexpr(c, rhs, AlgTerm))
))
_ => error("invalid binding expression $e")
end
end
function fromexpr(p::GATContext, e, ::Type{TypeScope})
ts = TypeScope()
c = AppendContext(p, ts)
parse_scope!(ts.scope, e.args) do pushbinding!, arg
parse_binding_expr!(c, pushbinding!, arg)
end
ts
end
function parseargs!(theory::GAT, exprs::AbstractVector, scope::TypeScope)
c = GATContext(theory, scope)
linenumber = nothing
Vector{LID}(filter(x->x isa LID, map(exprs) do expr
binding_expr = @match expr begin
a::Symbol => getlid(ident(scope; name=a))
l::LineNumberNode => begin linenumber = l end
:($a :: $T) => begin
binding = fromexpr(c, expr, Binding{AlgType})
Scopes.unsafe_pushbinding!(scope.scope, setline(binding, linenumber))
LID(length(scope))
end
_ => error("invalid argument expression $expr")
end
end))
end
function parseaxiom!(theory::GAT, localcontext, sort_expr, e; name=nothing)
@match e begin
Expr(:call, :(==), lhs_expr, rhs_expr) => begin
c = GATContext(theory, localcontext)
equands = fromexpr.(Ref(c), [lhs_expr, rhs_expr], Ref(AlgTerm))
sorts = sortcheck.(Ref(c), equands)
@assert allequal(sorts)
sort = if isnothing(sort_expr)
first(sorts)
else
fromexpr(c, sort_expr, AlgSort)
end
axiom = AlgAxiom(localcontext, sort, equands)
Scopes.unsafe_pushbinding!(theory, Binding{Judgment}(name, axiom))
end
_ => error("failed to parse equation from $e")
end
end
function parseconstructor!(theory::GAT, localcontext, type_expr, e)
(name, arglist) = @match e begin
Expr(:call, name, args...) => (name, args)
name::Symbol => (name, [])
_ => error("failed to parse head of term constructor $e")
end
args = parseargs!(theory, arglist, localcontext)
@match type_expr begin
:TYPE => begin
decl = hasname!(theory, name)
typecon = AlgTypeConstructor(decl, localcontext, args)
X = Scopes.unsafe_pushbinding!(theory, Binding{Judgment}(nothing, typecon))
for (i, arg) in enumerate(argsof(typecon))
argname = nameof(arg)
argdecl = hasname!(theory, argname)
Scopes.unsafe_pushbinding!(theory, Binding{Judgment}(nothing, AlgAccessor(argdecl, decl, X, i)))
end
end
_ => begin
c = GATContext(theory, localcontext)
type = @match type_expr begin
Expr(:vect, _...) => fromexpr(c, type_expr, TypeScope)
_ => fromexpr(c, type_expr, AlgType)
end
decl = hasname!(theory, name)
termcon = AlgTermConstructor(decl, localcontext, args, type)
m = Scopes.unsafe_pushbinding!(theory, Binding{Judgment}(nothing, termcon))
end
end
end
"""
This is necessary because the intuitive precedence rules for the symbols
that we use do not match the Julia precedence rules. In theory, this could
be written with some algorithm that recalculates precedence, but I am too lazy
to write that, so instead I just special case everything.
"""
function normalize_judgment(e)
@match e begin
:($name := $lhs == $rhs :: $typ ⊣ $ctx) => :((($name := ($lhs == $rhs)) :: $typ) ⊣ $ctx)
:($lhs == $rhs :: $typ ⊣ $ctx) => :((($lhs == $rhs) :: $typ) ⊣ $ctx)
:(($lhs == $rhs :: $typ) ⊣ $ctx) => :((($lhs == $rhs) :: $typ) ⊣ $ctx)
:($trmcon :: $typ ⊣ $ctx) => :(($trmcon :: $typ) ⊣ $ctx)
:($name := $lhs == $rhs ⊣ $ctx) => :((($name := ($lhs == $rhs))) ⊣ $ctx)
:($name := $fun ⊣ $ctx) => :(($name := $fun) ⊣ $ctx)
:($lhs == $rhs ⊣ $ctx) => :(($lhs == $rhs) ⊣ $ctx)
:($(trmcon::Symbol) ⊣ $ctx) => :(($trmcon :: default) ⊣ $ctx)
:($f($(args...)) ⊣ $ctx) && if f ∉ [:(==), :(⊣)] end => :(($f($(args...)) :: default) ⊣ $ctx)
trmcon::Symbol => :($trmcon :: default)
:($f($(args...))) && if f ∉ [:(==), :(⊣)] end => :($e :: default)
_ => e
end
end
function parse_binding_line!(theory::GAT, e, linenumber)
e = normalize_judgment(e)
(binding, localcontext) = @match e begin
Expr(:call, :(⊣), binding, ctxexpr) && if ctxexpr.head == :vect end =>
(binding, fromexpr(theory, ctxexpr, TypeScope))
e => (e, TypeScope())
end
c = GATContext(theory, localcontext)
(head, type_expr) = @match binding begin
Expr(:(::), head, type_expr) => (head, type_expr)
_ => (binding, nothing)
end
@match head begin
Expr(:(:=), name, equation) => @match equation begin
Expr(:call, :(==), _, _) => parseaxiom!(theory, localcontext, type_expr, equation; name)
_ => parsefunction!(theory, localcontext, type_expr, name, equation)
end
Expr(:call, :(==), _, _) => parseaxiom!(theory, localcontext, type_expr, head)
_ => parseconstructor!(theory, localcontext, type_expr, head)
end
end
function parsefunction!(theory::GAT, localcontext, sort_expr, call, e)
isnothing(sort_expr) || error("No explicit sort for functions $call :: $sort_expr")
name, args′ = @match call begin
Expr(:call, name, args...) => (name, args)
end
args = parseargs!(theory, args′, localcontext)
c = GATContext(theory, localcontext)
decl = hasname!(theory, name)
trm = fromexpr(c, e, AlgTerm)
fun = AlgFunction(decl, localcontext, args, trm)
Scopes.unsafe_pushbinding!(theory, Binding{Judgment}(nothing, fun))
end
function parse_struct!(theory::GAT, e, linenumber, ctx=nothing)
localcontext = isnothing(ctx) ? TypeScope() : fromexpr(theory, ctx, TypeScope)
(name, args...) = @match e begin
Expr(:struct, false, Expr(:call, name, lc...), Expr(:block, body...)) => begin
typeargs = parseargs!(theory, lc, localcontext)
ts_args = fromexpr(GATContext(theory, localcontext),Expr(:vect,body...),TypeScope)
args = OrderedDict(nameof(x)=>getvalue(ts_args[x]) for x in getidents(ts_args))
(name, localcontext, typeargs, args)
end
end
decl = hasname!(theory, name)
str = AlgStruct(decl, args...)
X = Scopes.unsafe_pushbinding!(theory, Binding{Judgment}(nothing, str))
for arg in typeargsof(str)
argname = nameof(arg)
i = ident(localcontext; name=argname).lid.val
argdecl = hasname!(theory, argname)
b = Binding{Judgment}(nothing, AlgAccessor(argdecl, decl, X, i))
Scopes.unsafe_pushbinding!(theory, b)
end
end
# GATs
######
"""
This only works when `seg` is a segment of `theory`
"""
function toexpr(theory::GAT, seg::GATSegment)
e = Expr(:block)
for binding in seg
if !isnothing(getline(binding))
push!(e.args, getline(binding))
end
line = toexpr(theory, binding)
if !isnothing(line)
push!(e.args, line)
end
end
e
end
function parse_gat_line!(theory::GAT, e::Expr, linenumber; current_module)
try
@match e begin
Expr(:struct, _...) => parse_struct!(theory, e, linenumber)
Expr(:call, :⊣, Expr(:struct, _...), ctx) => parse_struct!(theory, e.args[2], linenumber, ctx)
Expr(:macrocall, var"@op", _, aliasexpr) => begin
lines = @match aliasexpr begin
Expr(:block, lines...) => lines
_ => [aliasexpr]
end
for line in lines
@switch line begin
@case (_::LineNumberNode)
nothing
@case :($alias := $name)
# check if there is already a declaration for name, if not, create declaration
decl = hasname!(theory, name)
binding = Binding{Judgment}(alias, Alias(decl), linenumber)
Scopes.unsafe_pushbinding!(theory, binding)
@case _
error("could not match @op expression $line")
end
end
end
_ => begin
parse_binding_line!(theory, e, linenumber)
end
end
catch _
error("error parsing expression $e at line $linenumber")
end
end
function fromexpr(parent::GAT, e, ::Type{GAT}; name=parent.name, current_module::Vector{Symbol}=Symbol[])
theory = copy(parent; name)
unsafe_newsegment!(theory)
e.head == :block || error("expected a block to parse into a GAT, got: $e")
linenumber = nothing
for line in e.args
@match line begin
l::LineNumberNode => begin
linenumber = l
end
_ => parse_gat_line!(theory, line, linenumber; current_module)
end
end
theory
end