-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathigor.ey
executable file
·484 lines (422 loc) · 17.9 KB
/
igor.ey
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/home/drahflow/elymas/elymas/loaded
{ { _ sys .asm .rawAddress txt .produce .u defv }' scope }
<
/generate deffd
0 ==m # placeholder, zero value is never read
{ =m [ m .v keys { m .v -01 . } each ] }' "#dom" defmd
{ =m m .v ==v =*f v keys { v -01 . f }' each } "#each" defmd
{ 0 } "#istart" deffd
{ =m m .v keys len eq }' "#iend" defmd
{ 1 add }" "#istep" deffd
{ =m m .v keys * }' "#itrans" defmd
{ =m m .v -01 sys .asm .rawAddress txt .produce .u .?' }' /has defmd
{ =m m .v ==v "#iclone" | * ==n v keys { _ v -01 . -01 n =[] } each n }' /clone defmd
{ < generate ==v "put" =* > }' _ "#iclone" deffd
> -- /objset deffd
{ ==y ==x 1 y { x mul } rep } /exp deffst
{ 0 }" ==:ANDIF_ZERO
<
sys .|executeIdentifier "~" deffd
{ quoted { ~ANDIF_ZERO "?" ~ "*" ~ ~not ~not } { { 0 } ? * not not } ? * }
> -- /andif defq
<
[ ] ==a [ ] ==b
{ =a =b
a len b len eq {
1 a dom { _ a * -01 b * eq and }" each
}" { 0 }" ? *
}'
> -- /arrEq deffd
{ "}" | *
{ ==f 0 ==running
{ f } { * }" ; { =*f { running not { 1 =running f 0 =running } rep } } ;
} *
}" "}!" defq
map ==constants
map ==assertions
objset ==blocks
{ # ==parentBlock
< map ==f map ==e list ==d map ==v _ ==parent >' _ blocks .put
} /childBlock deffd
< > childBlock _ ==emptyBlock ==currentBlock
< map ==:cache { _ cache .has not { _ _ cache =[] }" rep cache * }' > -- /makeUnique deffd
<
{ sys .linux .gettimeofday -- 1000000 mul add } /t deffd
< { == }' > ==time =*setTime
< { == }' > ==count =*setCount
{ ==l 0 l setTime 0 l setCount
{ =*f { t ==start f t start sub time l . add l setTime count l . 1 add l setCount } }
quoted not { * } rep
}
{
time keys _ { ==b ==a time a . time b . lt } order -01* {
[ -01 ==k
k time -01 . txt .produce .u " μs "
k count -01 . txt .produce .u " calls "
k
] |cat fold dump
} each
}
> -- /perfstats deffd -- { -- } "τ" defq
{ ==n =*f { n dump f "/" n cat dump } } "δ" deffst
<
{ -- } "$$" defq # { ==name _ "$" name cat " " cat -01 cat dump } "$$" deffd
txt .consume .|hu "%" defq
{ "2120" "-" | |le "021" "-" | |ge |and } /in defq
{ "Unconfigured peek/take/get/set/noErr/snip" die } -000000 =*peek =*take =*get =*set =*noErr =*snip { } =*setupInput
{ | { ==:f /f sys .executeIdentifier _ * -- } * }" "@" deffd
str .|infix =*:infix
# parser generator
{ _ sys .typed .type 1 eq { ==str { 1 ==r str { peek eq r and =r take }' each r _ |noErr rep }' } { }" ? * } /lit deffd
{ lit =*p { get , p { --, ,-- }" { ,--- set }" ? * 1 }' } ",?" deffd
{ lit =*p { { get , p }' { --, ,-- }' loop ,--- set 1 }' } ",*" deffd
{ _ ,* ,; } ",+" deffd
{ lit =*q lit =*p { get , p { --, ,-- 1 }" { ,--- set q }" ? * }' } ",|" deffd
{ lit ==q lit =*p { p q { 0 }" ? * }' } ",;" deffd
{ [ [ }" { ] |lit each ] ",;" | fold }" -01 ",[" deffd ",]" deffd
{ defvst }' =*:defp
{
==name "}'" | * { ,[ }' -01 ; { ,] }' ; { * _ name "_make" cat defp * }_ name "_make" cat defp
"{" | * name "_make" cat "|" | "*" | "}'" | * name defp
} "}==" defq
"{" | "(" defq { 1 "}'" | * }' ")" defq
{ lit =*p lit =*q {
{ get , p { ,--- _ set , 0 }" { ,--- _ set , q }" ? * }' { --, ,-- }' loop ,--- set 1
}' } /upto deffd
# compare Specification of the Metamath Language
{ { peek take %24 neq }' { }' loop 1 }" ==sequenceEndingInDollar
{ peek take ==c c %09 eq c %0A eq or c %0C eq or c %0D eq or c %20 eq or } ==whitechar
{ peek take ==c c %41 %5A in c %61 %7A in or c %30 %39 in or c %2D eq or c %5F eq or c %2E eq or } ==labelchar
{ peek take ==c c %41 %5A in c %3F eq or } ==compressedchar
{ peek take ==c c %24 neq c %20 gt and c %7F lt and } ==mathchar
{ "$(" sequenceEndingInDollar ")" upto ")" white }==comment
{ whitechar comment ,| include ,| ,+ }==white
{ "$[" white ( |setupInput get ) mathchar ,* (
get snip "including: " -1101 cat dump parseFile "finished " -01 cat dump _ =setupInput *
) white "$]" white }==include
{ ( get ) mathchar ,+ ( get snip makeUnique ) white }==mathsymbol
{ ( get ) labelchar ,+ ( get snip makeUnique ) white }==label
{ "$c" white ,[ mathsymbol ( $$c 1 -01 constants =[] ) ,] ,+ "$." white }==cstatement
{ "$v" white ,[ mathsymbol ( $$v 1 -01 currentBlock .v =[] ) ,] ,+ "$." white }==vstatement
{ label "$f" white ( $$f [ ) mathsymbol mathsymbol ( ] -01 currentBlock .f =[] ) "$." white }==fstatement
{ label "$e" white ( $$e [ ) mathsymbol mathsymbol ,* ( ] -01 currentBlock .e =[] ) "$." white }==estatement
{ "$d" white ( [ ) mathsymbol mathsymbol ,+ ( ] currentBlock .d .append1 ) "$." white }==dstatement
{ label "$a" white ( $$a [ ) mathsymbol mathsymbol ,* ( ]
< "a" ==type currentBlock ==ctx ==thm { "Assumption has no proof" die }" =*proof _ ==name
{ mandatoryHypotheses ==hyps }' { mandatoryDisjunct ==disj }' { allDisjunct ==allDisj }' > -01*02*03*0
-01 assertions =[] ) "$." white
}==astatement
{ "?" ( "?" ) white }==questionmark
{ ( [ ) label questionmark ,| ,* ( ] { -- }_ ) }==proof
{ "(" white ( [ ) label ,* ( ] ) ")" white ( [ ) ,[ ( get ) ,[ compressedchar ,] ,+ ( get snip ) white ,] ,* ( ] |cat fold
< ==data _ ==labels len ==labelCount { _ ==hypotheses len ==hypCount
[ 0 data { ==c
[
{ c %41 ge c %54 le and } { 20 mul c %40 sub add _ dump ==n
[
{ n 1 sub hypCount lt } { n 1 sub hypotheses * }
{ n 1 sub hypCount sub labelCount lt } { n 1 sub hypCount sub labels * }
{ 1 } { n 1 sub hypCount sub labelCount sub txt .produce .u "@ " -01 cat }
] conds
0 }
{ c %55 ge c %59 le and } { 5 mul c %54 sub add _ dump }
{ c %5A eq } { -- "!" 0 }
{ c %3F eq } { -- "?" 0 }
{ 1 } { "Invalid compressed proof" die }
] conds
} each -- ]
} > -- ) }==compressedproof
{ label "$p" white ( $$p [ ) mathsymbol mathsymbol ,* ( ] ) "$="
white compressedproof proof ,| (
< "p" ==type currentBlock ==ctx -01 ==thm =*proof _ ==name
{ mandatoryHypotheses ==hyps }' { mandatoryDisjunct ==disj }' { allDisjunct ==allDisj }' > -01*02*03*0
# -101 "simpl31" eq { perfstats "done" die } rep
# -101 "vtoclga" eq { perfstats "done" die } rep
# _ "a17d" eq { -101 verifyProof "done" die } rep # TODO: this line is just debugging
# -100 verifyProof dump
-01 assertions =[] ) "$." white
}==pstatement
{ "${" ( currentBlock childBlock =currentBlock ) white statements ( currentBlock .parent =currentBlock ) "$}" white }==block
{ [ estatement pstatement dstatement block vstatement fstatement astatement cstatement white ] ",|" | fold ,* }==statements
{ ":" via
"" ==s 0 ==i 0 ==slen 0 ==last 0 ==eof
{
{
i slen lt not {
eof not {
s 1024 1024 mul :read cat =s
s len _ =slen
txt .produce .u "Just read to: " -01 cat dump }" rep
}" rep
i slen lt { i s * }" { 1 neg 1 =eof }" ? *
}' =peek
{ i 1 add =i }' =take { i }' =get { =i }' =set { i =last }' =noErr { s infix }' =snip
} _ =setupInput *
{ statements * { i s len neq { ??metamath.trailing-garbage } rep } { ??metamath } ? * }
{ -- < last s str .postfix ==remaining > ??!' } ?!metamath
}
> -- /parse deffd
{ sys .file _ ":" via -01 :open parse :close } /parseFile deffd
{ ==ctx ==sym 0 ==found { ctx .?'v found not and }' { sym ctx .v ==v v .has found or =found ctx .parent =ctx }' loop found } /isVar deffd
{ ==ctx ==lbl 0 ==found { ctx .?'e found not and }' { lbl ctx .e ==e e .has found or =found ctx .parent =ctx }' loop found } /isEHyp deffd
{ ==ctx ==lbl 0 ==found { ctx .?'f found not and }' { lbl ctx .f ==f f .has found or =found ctx .parent =ctx }' loop found } /isFHyp deffd
{ -01 ==lbl { _ .e lbl -01 .has not }' { .parent }' loop .e lbl -01 * } /getEHyp deffd
{ -01 ==lbl { _ .f lbl -01 .has not }' { .parent }' loop .f lbl -01 * } /getFHyp deffd
{ constants .has }' /isConst deffd
{ _ ==pstm .ctx ==ctx map _ ==vars # will be returned
{ ==sym [
{ sym ctx isVar }' { 1 sym vars =[] }'
{ sym isConst }' { }'
{ 1 }' { "Undeclared symbol: " sym cat die }'
] conds } ==collect
pstm .thm collect each
ctx { _ .?'e }' { _ .e { collect each }' each .parent }' loop --
} /mandatoryVariables deffd
{ _ ==pstm mandatoryVariables ==vars list _ ==hyps # will be returned
[ pstm .ctx { _ .?'e }' { _ .parent }' loop -- ] reverse ==ctxs
ctxs { _ ==ctx .f dom { _ ==k ctx .f * 1 -01 * vars .has { k hyps .append1 }' rep } each } each
ctxs { .e dom { hyps .append1 }' each }' each
} /mandatoryHypotheses deffd
{ ==pstm list _ ==disj # will be returned
[ pstm .ctx { _ .?'d }' { _ .parent }' loop -- ] reverse ==ctxs
ctxs { .d { _ len ==l ==dstm
0 l range { ==i
i 1 add l range { ==j
[ i dstm * j dstm * ] disj .append1
} each
} each
} each }' each
} /allDisjunct deffd
{ _ ==pstm mandatoryVariables ==vars list _ ==disj # will be returned
# "Vars: " dump
# vars dom dump
[ pstm .ctx { _ .?'d }' { _ .parent }' loop -- ] reverse ==ctxs
ctxs { .d {
==dstm
0 dstm len range { _ ==i dstm * vars .has {
i 1 add dstm len range { _ ==j dstm * vars .has {
[ i dstm * j dstm * ] disj .append1
}' rep } each
}' rep } each
} each }' each
} /mandatoryDisjunct deffd
{ _ ==pstm _ .hyps ==hyps .disj ==disj
"Mandatory hyps: " dump
hyps |dump each
"Mandatory disjunct: " dump
disj |dump each
"^^^^^^^^^^^^^^^^^^^" dump
hyps pstm .proof
} /computeProof deffd
{ _ ==pstm _ .ctx ==ctx computeProof
list ==stack # the verification stack
list ==savedTheorems # Z-saved theorems
{ ==lbl
[
{ lbl ctx isEHyp } { lbl ctx getEHyp stack .append1 }
{ lbl ctx isFHyp } { lbl ctx getFHyp stack .append1 }
{ lbl "!" eq } { stack len 1 sub stack * savedTheorems .append1 }
{ lbl len _ { -- 0 lbl * 0 "@" * eq } rep }
{ 2 lbl str .postfix txt .consume .u savedTheorems * stack .append1 }
{ lbl assertions .has } {
"Applying: " lbl cat dump
lbl assertions * _ ==ass .hyps ==hyps
map ==substitution
"Mandatory hypotheses:" dump
hyps dump
0 hyps len range
_ { hyps * dump } each
_ { _ ==i hyps * _ ==h ass .ctx isFHyp {
h ass .ctx getFHyp ==mask
stack len hyps len sub i add stack * ==target
0 mask * 0 target * neq { mask dump target dump "Constant symbol mismatch" die } rep
[ 1 target len range { target * } each ] 1 mask * substitution =[]
} rep } each
substitution dom { ==k k " => " cat k substitution * { " " -01 cat cat } fold cat dump } each
{ _ ==i hyps * _ ==h ass .ctx isEHyp {
stack len hyps len sub i add stack * ==target
[ h ass .ctx getEHyp { # ==sym
_ ass .ctx isVar { substitution * _ len dearray } rep
} each ] ==mask
mask target arrEq not { mask dump target dump "Hypthesis does not match stack" die } rep
} rep } each
ass .disj { =*d
"Checking disjunct " 0 d cat " <=> " cat 1 d cat dump
0 d substitution * { ass .ctx isVar } grep { ==v
1 d substitution * { ass .ctx isVar } grep { ==w
v " :=: " w cat cat dump
v w eq { "Disjunct variable requirement broken" die } rep
[ pstm .allDisj { =*d 0 d v eq 1 d w eq and 0 d w eq 1 d v eq and or } each ] any not
{ "Disjunct variable requirement not carried" die } rep
} each
} each
} each
"All checks ok. Inserting new statement." dump
hyps len { stack .pop } rep
[ ass .thm { _ ass .ctx isVar { substitution * _ len dearray } rep } each ] stack .append1
}
{ 1 } { "Invalid proof step: " lbl cat die }
] conds
"Proof stack" dump
stack { { " " -01 cat cat } fold dump } each
"^^^^^^^^^^^" dump
} each
[
{ stack len 1 neq } { "Stack not empty after proof." dump 0 }
{ 0 stack * pstm .thm arrEq not } { "Final stack contents: " dump 0 stack * dump "and theorem: " pstm .thm dump "do not match" dump 0 }
{ 1 } { 1 }
] conds
} /verifyProof deffd
{
sys .argv len [ 2 3 ] eq any not { "usage: ./igor input.mm [contination freeze]" die } rep
1 sys .argv * parseFile
} { _ dump _ keys dump .remaining die } ?!metamath
{ =*f 0 ==computed "" ==?result {
computed not { 1 =computed f =result }" rep
result
}' } /memoized deffst
{ map _ ==variables # returned
blocks { .f _ =*d dom
{ _ ==n d _ 0 -01 * ==nonterminal 1 -01 * ==var
nonterminal var variables =[]
} each
} each
} memoized /variables deffd
{ ==ctx map _ ==result # returned
[ ctx { _ .?'e }' { _ .parent }' loop -- ] reverse ==ctxs
ctxs { _ ==c .f _ =*fs dom { _ fs -01 result =[] } each } each
} /collectFHyps deffst
{ ==ctx map _ ==result # returned
[ ctx { _ .?'e }' { _ .parent }' loop -- ] reverse ==ctxs
ctxs { _ ==c .e _ =*es dom { _ es -01 result =[] } each } each
} /collectEHyps deffst
{
map ==nonterminals
map ==variableNonTerminals # nonterminal -> variable -> theorem name
blocks { .f _ =*d dom
{ _ ==thm d _ 0 -01 * ==nonterminal 1 -01 * ==var
nonterminal variableNonTerminals .has not { map nonterminal variableNonTerminals =[] } rep
nonterminal variableNonTerminals * ==variableTypedefs # variable -> theorem name
var variableTypedefs .has { var variableTypedefs * len thm len gt } { 1 } ? *
{ thm var variableTypedefs =[] } rep
} each
} each
map ==relevantRules # nonterminal -> [ assertion ... ]
map ==singularRules # token -> nonterminal (for when only a single token is expanded, this is only an optimization)
[ "/" ] ==:NONTRIVIALSINGULARTOKENS # / as in [ A / x ] ph is _not_ in fact a class, but / as in 6 / 3 is.
assertions dom { assertions * ==a
a .type "a" eq {
0 a .thm * _ ==nt variableNonTerminals .has {
a .ctx collectEHyps dom len 0 eq {
a .thm len 2 eq { 1 a .thm * constants .has } andif
{ 1 a .thm * NONTRIVIALSINGULARTOKENS eq any not } andif {
a 1 a .thm * singularRules =[]
} {
1 a .thm len range a .thm * ==expectedTokens
nt relevantRules .has not { [ ] nt relevantRules =[] } rep
nt relevantRules * [ a ] cat nt relevantRules =[]
} ? *
} rep
} rep
} rep
} each
parser .glr ":" via
map ==parserNonterminals
variableNonTerminals dom { ==nt nt :nonterminal nt parserNonterminals =[] } each
variableNonTerminals dom { ==nt
nt parserNonterminals *
[ " " nt cat ] # terminal for variable and singularRules tokens
{ :0 }' # keep value
:rule
nt relevantRules .has {
nt relevantRules * { _ ==assertion .thm ==thm
[ ] ==childParsesToKeep
nt parserNonterminals *
[
1 thm len range { _ ==i thm * ==tok
tok constants .has { tok }
{
tok variables * parserNonterminals *
childParsesToKeep [ thm len i sub 1 sub ] cat =childParsesToKeep
} ? *
} each
] _ len ==childCount
{ ==s [
childParsesToKeep { s -01 :child }' each
s childCount 1 sub :child logicStart
s :0 logicEnd
assertion .name
] }
:rule
} each
} rep
} each
map ==automata # nonterminal -> automaton
variableNonTerminals dom { _ ==nt parserNonterminals * ==NT
NT :automaton nt automata =[]
} each
[ automata singularRules ]
} memoized /prepareLogicParsers deffd
# generate parser functions
# { ==toks ==start [...] <parse> end success[0/1] }
{ =*variableTranslation
prepareLogicParsers 2 dearray ==singularRules ==automata
< "" ==tok [ ] ==value
{ =tok =value
tok singularRules .has {
[ 0 value * 1 value * tok singularRules * .name ] " " tok singularRules * .thm 0 -01 * cat
}' {
value tok # nothing to do
}' ? *
}'
> -- /singularRulesTranslation deffst
map ==globalCache # for repeated invocations
0 ==globalCacheResetCounter # kill global cache once in a while to allow GC
map _ ==parsers # returned
automata dom { ==nt {
==toks ==start
[ nt toks { " " } each start txt .produce .hu sys .asmroutines .assemblestr ==cacheKey
cacheKey globalCache .has not {
globalCacheResetCounter 1 add _ =globalCacheResetCounter 500 gt {
0 =globalCacheResetCounter map =globalCache
} rep
[
nt automata * ==automaton
automaton .run =*consume
start toks len range { ==i [ i _ 1 add "" ] i toks * singularRulesTranslation variableTranslation consume -- } each
[ toks len toks len "" ] "" consume automaton .result ==result
result len 1 gt { result dump "ambiguous grammar" die } rep
result len 1 eq ==success
success { 0 result * } rep success
] cacheKey globalCache =[]
} τuncachedParser rep
cacheKey globalCache * _ len dearray
} τparsers nt parsers =[] } each
} /generateLogicParsers deffd
< "" ==tok
{ =tok
tok variables .has {
" " tok variables * cat
}' { tok }' ? *
}'
> -- /noMetaVariables deffst
{ ==tok
tok len { 0 tok * 0 "$" * eq } andif { tok ".(.*)\\.[0-9]+" regex } andif { =tok } rep
tok variables .has {
" " tok variables * cat
} { tok } ? *
} /alsoMetaVariables deffst
{ "Generating logic parser...\n" sys .out .writeall |noMetaVariables generateLogicParsers } memoized /logicParsers deffst
{ "Generating meta-logic parser...\n" sys .out .writeall |alsoMetaVariables generateLogicParsers } memoized /metaLogicParsers deffst
{ ==node node len 3 sub node * } /logicStart deffd
{ ==node node len 2 sub node * } /logicEnd deffd
{ ==node node len _ 3 sub node * -01 2 sub node * range } /logicRange deffd
{ ==node node len 1 sub node * } /logicNonTerminal deffd
{ ==node node len 3 sub } /logicChildCount deffd
sys .argv len 3 eq {
logicParsers --
metaLogicParsers --
{ _ { * }_ 2 sys .argv * sys .freeze * } !! *
}" rep
"igor-interactive.ey" include
# vim: syn=elymas