/
MetaDebug.lean
457 lines (389 loc) · 9.16 KB
/
MetaDebug.lean
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
/-
Copyright (c) 2022 Arthur Paulino. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Arthur Paulino
-/
import Lean
import FxyLang.Implementation.Execution
import FxyLang.Implementation.Syntax
/- This file parses Fxy code from a `.lean` file. It uses metaprogramming and is
meant for testing/debugging purposes, only -/
def Program.getBindersNames? : Program → Option (NEList String)
| eval (.lam $ .mk ns ..) => some ns
| seq (decl _ (eval (.lam $ .mk ns ..) )) _ => some ns
| _ => none
def Program.isSequence : Program → Bool
| seq .. => Bool.true
| _ => Bool.false
def blank (n : Nat) : String :=
let rec blankAux (cs : List Char) : Nat → List Char
| 0 => cs
| n + 1 => ' ' :: ' ' :: (blankAux cs n)
⟨blankAux [] n⟩
def progToString (p : Program) : String :=
let rec aux (l : Nat) : Program → String
| Program.skip => s!"{blank l}skip"
| .seq p q => s!"{blank (l-2)}{aux l p}\n{aux l q}"
| .eval e => s!"{blank l}{exprToString e}"
| .loop e p => s!"{blank l}while {exprToString e} do\n{aux (l+2) p}"
| .decl n p =>
let pString := if p.isSequence
then s!"\n{aux (l+2) p}"
else s!" {aux (l-2) p}"
match p.getBindersNames? with
| none => s!"{blank l}{n} :=" ++ pString
| some ns => s!"{blank l}{n} {ns.unfoldStrings} :=" ++ pString
| .fork e p q =>
s!"{blank l}if {exprToString e} then\n{aux (l+2) p}\n" ++
s!"else\n{aux (l+2) q}"
| .print e => s!"{blank l}!print {exprToString e}"
aux 0 p
def Program.toString (p : Program) := progToString p
instance : ToString Program := ⟨Program.toString⟩
open Lean in
def mkApp' (name : Name) (e : Expr) : Expr :=
mkApp (mkConst name) e
def negFloat (f : Float) : Float :=
-1.0 * f
open Lean Elab Meta in
def elabLiteral : Syntax → TermElabM Expr
| `(literal| $n:num) =>
mkAppM ``Literal.int #[mkApp' ``Int.ofNat (mkNatLit n.toNat)]
| `(literal| true) => mkAppM ``Literal.bool #[mkConst ``Bool.true]
| `(literal| false) => mkAppM ``Literal.bool #[mkConst ``Bool.false]
| `(literal| $s:str) => mkAppM ``Literal.str #[mkStrLit $ s.isStrLit?.getD ""]
| `(literal| $[-%$neg]?$f:scientific) => do
if neg.isNone then
mkAppM ``Literal.float #[← Term.elabScientificLit f (mkConst ``Float)]
else
let f ← Term.elabScientificLit f (mkConst ``Float)
mkAppM ``Literal.float #[mkApp' ``negFloat f]
| _ => throwUnsupportedSyntax
open Lean in
def elabStringOfIdent (id : Syntax) : Expr :=
mkStrLit id.getId.toString
open Lean Elab in
def elabBinOp : Syntax → TermElabM Expr
| `(binop| +) => return mkConst ``BinOp.add
| `(binop| *) => return mkConst ``BinOp.mul
| `(binop| <) => return mkConst ``BinOp.lt
| `(binop| <=) => return mkConst ``BinOp.le
| `(binop| >) => return mkConst ``BinOp.gt
| `(binop| >=) => return mkConst ``BinOp.ge
| `(binop| =) => return mkConst ``BinOp.eq
| `(binop| !=) => return mkConst ``BinOp.ne
| _ => throwUnsupportedSyntax
open Lean Elab Meta in
partial def elabExpression : Syntax → TermElabM Expr
| `(expression| $v:literal) => do mkAppM ``Expression.lit #[← elabLiteral v]
| `(expression| $n:ident) => mkAppM ``Expression.var #[elabStringOfIdent n]
| `(expression| $e:expression $[$es:expression]*) => do
match ← es.data.mapM elabExpression with
| [] => unreachable!
| e' :: es =>
let l ← mkListLit (Lean.mkConst ``Expression) es
let nl ← mkAppM ``List.toNEList #[e', l]
mkAppM ``Expression.app #[← elabExpression e, nl]
| `(expression| ! $p:expression) => do
mkAppM ``Expression.unOp #[mkConst ``UnOp.not, ← elabExpression p]
| `(expression| $eₗ:expression $o:binop $eᵣ:expression) => do
mkAppM ``Expression.binOp
#[← elabBinOp o, ← elabExpression eₗ, ← elabExpression eᵣ]
| `(expression| [$ls:literal,*]) => do
let l ← ls.getElems.data.mapM elabLiteral
mkAppM ``Expression.list #[← mkListLit (Lean.mkConst ``_root_.Literal) l]
| `(expression| ($e:expression)) => elabExpression e
| _ => throwUnsupportedSyntax
open Lean Elab Meta in
partial def elabProgram : Syntax → TermElabM Expr
| `(program| skip) => return mkConst ``Program.skip
| `(program| $e:expression) => do
mkAppM ``Program.eval #[← elabExpression e]
| `(programSeq| $p:program $[$ps:program]*) => do
ps.foldlM (init := ← elabProgram p) fun a b => do
mkAppM ``Program.seq #[a, ← elabProgram b]
| `(program| $n:ident $ns:ident* := $p:programSeq) => do
let ns := ns.data
if ¬ ((ns.map fun n => n.getId.toString).noDup) then
throwError s!"definition of curried function {n.getId.toString} has " ++
"duplicated variables"
let ns := ns.map elabStringOfIdent -- each element represents a String
match ns with
| [] => mkAppM ``Program.decl #[elabStringOfIdent n, ← elabProgram p]
| n' :: ns =>
let l ← mkListLit (Lean.mkConst ``String) ns -- List String
let nl ← mkAppM ``List.toNEList #[n', l] -- NEList String
let h ← mkEqRefl (← mkAppM ``NEList.noDup #[nl]) -- proof of noDup
mkAppM ``Program.decl #[
elabStringOfIdent n,
mkApp' ``Program.eval $
mkApp' ``Expression.lam $
← mkAppM ``Lambda.mk #[nl, h, ← elabProgram p]
]
| `(program| if $e:expression then $p:programSeq $[else $q:programSeq]?) => do
let q ← match q with
| none => pure $ mkConst ``Program.skip
| some q => elabProgram q
mkAppM ``Program.fork
#[← elabExpression e, ← elabProgram p, q]
| `(program| while $e:expression do $p:programSeq) => do
mkAppM ``Program.loop #[← elabExpression e, ← elabProgram p]
| `(program| !print $e:expression) => do
return mkApp' ``Program.print (← elabExpression e)
| _ => throwUnsupportedSyntax
elab ">>" ppLine p:programSeq ppLine "<<" : term => elabProgram p
def Context.toString (c : Context) : String :=
c.toList.foldl (init := "")
fun acc (n, val) => acc ++ s!"{n}:\t{val}\n"
instance : ToString Context := ⟨Context.toString⟩
def Result.toString : Result → String
| val v => v.toString
| err t m => m
instance : ToString Result := ⟨Result.toString⟩
open Lean.Meta Lean.Elab.Command Lean.Elab.Term in
elab "#assert " x:term:60 " = " y:term:60 : command =>
liftTermElabM `assert do
let x ← elabTerm x none
let y ← elabTerm y none
synthesizeSyntheticMVarsNoPostponing
unless (← isDefEq x y) do
throwError "{← reduce x}\n------------------------\n{← reduce y}"
------- ↓↓ testing area ↓↓
#eval >>
countTo n :=
i := 0
while i < n do
i := i + 1
i
if countTo 42 = 42 then
true
else
false
<<.run
#eval >>
a := 1 + 1
[1, 2, 3, 1.3, "oi"] + 3
<<.run
#eval >>
f x y z := x + y + z
q := f 3 4
q 5
x := 1
<<.run
#eval >>
f x y z := x + y + z
f 3 4 5
<<.run
#eval >>
f x y z := x + y + z
(f 3 4) 5
<<.run
#eval >>
a x := x
b x := 2 * x
(b 1) > (a 1)
<<.run
#eval >>
a := 0
while a < 5 do
a := a + 1
<<.run
#eval >>
if 1 < 0 then
a := 1
else
a := 4
<<.run
#eval >>
if true * false then
a := 1
else
a := 4
<<.run
#eval >>
if true * (false) then
a := 1
else
a := 4
<<
#eval >>
if (true * (false)) then
a := 1
else
a := 4
<<
#eval >>
x x := x
x 4
<<.run
#eval >>
g x y := x + y
f x :=
a := 1
b := 2
c := g a b
x
q := f 3
q
<<.run
#eval >>
f n :=
s := 0
i := 0
while i < n do
i := i + 1
s := s + i
s
x := f 5
x
<<.run
def px := >>
x := 1
a := 2
<<.toString
def px' := >>
x := 1
a := 2
<<.toString
def px'' := >>
x := 1
a := 2
<<.toString
#assert px = px'
#assert px' = px''
def pw := >>
x := 1
s := 0
a := 0
while a < 5 do
a := a + 1
s := s + a
x := 1
s
<<
def pw' := >>
x := 1
s := 0
a := 0
while a < 5 do
a := a + 1
s := s + a
x := 1
s
<<
#assert pw = pw'
def p1 := >>
min x y :=
if x < y
then x
else y
min 5 3
<<.toString
def p1' := >>
min x y :=
if x < y then x
else y
min 5 3
<<.toString
def p1'' := >>
min x y :=
if x < y
then x
else y
min 5 3
<<.toString
def p1''' := >>
min x y :=
if x < y then
x
else
y
min 5 3
<<.toString
def p2 := >>
min x y :=
if x < y
then x
else y
min 5 3
<<.toString
#assert p1 = p1'
#assert p1 = p1''
#assert p1 = p1'''
#assert p1 = p2
def pIf := >>
if a = 5 then
b := 3
<<
def pIf' := >>
if a = 5 then
b := 3
else skip
<<
#assert pIf = pIf'
def p3 := >>
while 1 < a do
x := 2
a < 3
<<.toString
def p4 := >>
while 1 < a do x := 2
a < 3
<<.toString
def p5 := >>
while 1 < a do x := 2
a < 3
<<.toString
#assert p3 = p4
#assert p4 = p5
def p6 := >>
f x y :=
x + y
f3 := f 3
f32 := f3 2
<<.toString
def p6' := >>
f x y := x + y
f3 := f 3
f32 := f3 2
<<.toString
def p7 := >>
f x y := x + y
f3 := f 3
f32 := f3 2
<<.toString
def p8 := >>
f x y := x + y
f3 := f 3
f32 := f3 2
<<.toString
#assert p6 = p6'
#assert p6 = p7
#assert p7 = p8
def p9 := >>
a :=
x := 5
y = 5
2 + 5
<<.toString
def p9' := >>
a :=
x := 5
y = 5
2 + 5
<<.toString
def p10 := >>
a :=
x := 5
y = 5
2 + 5
<<.toString
def p11 := >>
a :=
x := 5
y = 5
2 + 5
<<.toString
#assert p9 = p9'
#assert p9 = p10
#assert p10 = p11