-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathpb_parseScript.sml
More file actions
2090 lines (1889 loc) · 55.3 KB
/
pb_parseScript.sml
File metadata and controls
2090 lines (1889 loc) · 55.3 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
Parse and print for pbc, npbc_check
*)
Theory pb_parse
Ancestors
pbc pbc_normalise npbc_check
Libs
preamble
val _ = numLib.temp_prefer_num();
(*
Printing for pseudo-Boolean problems in (extended) OPB format
*)
(* Printing mlstring pbc *)
Definition lit_string_def:
(lit_string (Pos v) = v) ∧
(lit_string (Neg v) = strlit "~" ^ v)
End
Definition lhs_string_def:
lhs_string xs =
concatWith (strlit" ") (MAP(λ(c,l). concat [int_to_string #"-" c; strlit " " ; lit_string l]) xs)
End
Definition op_string_def:
(op_string Equal = strlit" = ") ∧
(op_string GreaterEqual = strlit" >= ") ∧
(op_string Greater = strlit" > ") ∧
(op_string LessEqual = strlit" <= ") ∧
(op_string Less = strlit" < ")
End
Definition pbc_string_def:
(pbc_string (pbop,xs,i) =
concat [
lhs_string xs;
op_string pbop;
int_to_string #"-" i; strlit " ;\n"])
End
Definition annot_pbc_string_def:
annot_pbc_string (annot,str) =
let s = pbc_string str in
case annot of NONE => s
| SOME l =>
concat [ strlit"@";l; strlit" ";s]
End
(* printing a string pbf, possibly with string annotation *)
Definition obj_string_def:
obj_string (f,c:int) =
let c_string =
if c = 0 then strlit"" else
strlit " " ^
int_to_string #"-" c in
strlit"min: " ^ lhs_string f ^ c_string ^ strlit" ;\n"
End
Definition pres_string_def:
pres_string pres =
let c_string =
concatWith (strlit" ") pres in
strlit"preserved: " ^ c_string ^ strlit" ;\n"
End
(* Problem without annotation *)
Definition print_prob_def:
print_prob (pres,obj,fml) =
let obstr = case obj of NONE => [] | SOME fc => [obj_string fc] in
let presstr = case pres of NONE => [] | SOME p => [pres_string p] in
obstr ++ presstr ++ MAP pbc_string fml
End
(* Problem with annotation *)
Definition print_annot_prob_def:
print_annot_prob (pres,obj,fml) =
let obstr = case obj of NONE => [] | SOME fc => [obj_string fc] in
let presstr = case pres of NONE => [] | SOME p => [pres_string p] in
obstr ++ presstr ++ MAP annot_pbc_string fml
End
(* Strips annotations *)
Definition strip_annot_prob_def:
strip_annot_prob (pres,obj,afml) =
(pres,obj,MAP SND afml)
End
(*
Parse an OPB file as a string-variabled PB problem
*)
(* TODO: copied from lpr_parsing *)
(* Everything recognized as a "blank" *)
Definition blanks_def:
blanks (c:char) ⇔ c = #" " ∨ c = #"\n" ∨ c = #"\t" ∨ c = #"\r"
End
Definition tokenize_def:
tokenize (s:mlstring) =
case mlint$fromString s of
NONE => INL s
| SOME i => INR i
End
Definition toks_def:
toks s = MAP tokenize (tokens blanks s)
End
(* OPB parser
Parses a literal s, ~s
Valid names may contain a-z,A-Z,0-9,[]{}_^- characters
*)
Definition parse_lit_def:
parse_lit s =
if strlen s ≥ 2 ∧ strsub s 0 = #"~" then
let ss = substring s 1 (strlen s - 1) in
if goodString ss then
SOME (Neg ss)
else NONE
else if strlen s ≥ 1 then
if goodString s then SOME (Pos s)
else NONE
else NONE
End
(*
EVAL ``parse_lit (strlit "xaAa_-1")``
EVAL ``parse_lit (strlit "~x1234")``
EVAL ``parse_lit (strlit "fancy_{1}^[-42]")``
*)
(* Parse the LHS of a constraint and returns the remainder of the line *)
Definition parse_constraint_LHS_aux_def:
(parse_constraint_LHS_aux (INR n::INL v::rest) acc =
case parse_lit v of NONE => (INR n::INL v::rest,REVERSE acc)
| SOME lit => parse_constraint_LHS_aux rest ((n,lit)::acc)) ∧
(parse_constraint_LHS_aux ls acc = (ls,REVERSE acc))
End
Definition parse_constraint_LHS_def:
parse_constraint_LHS ss = parse_constraint_LHS_aux ss []
End
(* Strip ; terminator from the end of a string *)
Definition strip_term_def:
strip_term s =
if strlen s ≥ 1 ∧ strsub s (strlen s - 1) = #";"
then SOME (substring s 0 (strlen s - 1))
else NONE
End
(* Strip ; from the end of a tokenized line and retokenize *)
Definition strip_term_line_aux_def:
(strip_term_line_aux [] acc = NONE) ∧
(strip_term_line_aux [x] acc =
case x of INR n => NONE
| INL s =>
if s = strlit ";"
then SOME (REVERSE acc)
else
case strip_term s of
SOME s =>
SOME (REVERSE (tokenize s::acc))
| NONE => NONE) ∧
(strip_term_line_aux (x::xs) acc =
strip_term_line_aux xs (x::acc))
End
Definition strip_term_line_def:
strip_term_line ss = strip_term_line_aux ss []
End
(*
EVAL ``strip_term_line (toks (strlit "foo bar aaa;"))``
EVAL ``strip_term_line (toks (strlit "foo bar bbb ;"))``
EVAL ``strip_term_line (toks (strlit "foo bar ~1;"))``
EVAL ``strip_term_line (toks (strlit "foo bar 1 ;"))``
*)
Definition parse_op_def:
parse_op cmp =
if cmp = strlit ">=" then SOME GreaterEqual
else if cmp = strlit "=" then SOME Equal
else if cmp = strlit ">" then SOME Greater
else if cmp = strlit "<=" then SOME LessEqual
else if cmp = strlit "<" then SOME Less
else NONE
End
Definition parse_constraint_def:
parse_constraint line =
case OPTION_MAP parse_constraint_LHS (strip_term_line line) of
NONE => NONE
| SOME(rest,lhs) =>
(case rest of
[INL cmp; INR deg] =>
(case parse_op cmp of
NONE => NONE
| SOME op =>
SOME ((op,lhs,deg):mlstring pbc))
| _ => NONE)
End
(* strips off the head of a line as an annotation *)
Definition parse_annot_def:
parse_annot line =
case line of
(INL s)::ls =>
if strlen s ≥ 1 ∧ strsub s 0 = #"@" then
(SOME (substring s 1 (strlen s - 1)), ls)
else (NONE, line)
| _ => (NONE, line)
End
Definition parse_annot_constraint_def:
parse_annot_constraint line =
case parse_annot line of (annot,line) =>
OPTION_MAP (λres. (annot,res)) (parse_constraint line)
End
(*
EVAL ``parse_annot (toks (strlit "2 ~x1 1 ~x3 >= 1;"))``;
EVAL ``parse_constraint (toks (strlit "2 ~x1 1 ~x3 >= 1 ;"))``;
EVAL ``parse_annot_constraint (toks (strlit "@aaa 2 ~x1 1 ~x3 >= 1;"))``;
*)
Definition parse_constraints_def:
(parse_constraints [] acc = SOME (REVERSE acc)) ∧
(parse_constraints (s::ss) acc =
case parse_annot_constraint s of
NONE => NONE
| SOME pbc => parse_constraints ss (pbc::acc))
End
Definition nocomment_line_def:
(nocomment_line (INL c::cs) =
(strlen c < 1 ∨ strsub c 0 ≠ #"*")) ∧
(nocomment_line _ = T)
End
(* Allow the min objective to end with either a constant or not:
lin_term constant
lin_term
*)
(* parse objective term in pbc ... (; stripped) *)
Definition parse_obj_term_def:
parse_obj_term xs =
case parse_constraint_LHS xs of (rest,obj) =>
case rest of
[] => SOME (obj,0:int)
| [INR c] => SOME (obj,c:int)
| _ => NONE
End
(* parse objective line in PBF *)
Definition parse_obj_def:
(parse_obj [] = NONE) ∧
(parse_obj (x::xs) =
if x = INL (strlit "min:") then
OPTION_BIND (strip_term_line xs) parse_obj_term
else NONE
)
End
(*
EVAL``parse_obj (toks (strlit"min: 1 ~x199 1 x2 ~5 ;"))``
EVAL``parse_obj (toks (strlit"min: 1 ~x199 1 x2 ~5;"))``
EVAL``parse_obj (toks (strlit"min: 1 ~x199 1 x2 ;"))``
EVAL``parse_obj (toks (strlit"min: 1 ~x199 1 x2;"))``
EVAL``parse_obj (toks (strlit"min: 1 ~x199 1 x2 5;"))``
*)
(* raw string but must be a valid var name *)
Definition parse_var_raw_def:
(parse_var_raw (INL s) =
if goodString s then SOME s else NONE) ∧
(parse_var_raw _ = NONE)
End
Definition parse_vars_raw_def:
(parse_vars_raw [] = SOME []) ∧
(parse_vars_raw (s::ss) =
case parse_var_raw s of NONE => NONE | SOME v =>
case parse_vars_raw ss of NONE => NONE | SOME vs => SOME (v::vs))
End
(* parse projection or preserved line in PBF *)
Definition parse_pres_def:
(parse_pres [] = NONE) ∧
(parse_pres (x::xs) =
if x = INL (strlit "preserved:") then
OPTION_BIND (strip_term_line xs) parse_vars_raw
else NONE)
End
(*
EVAL``parse_pres (toks (strlit"preserved: x1 x2 x2 a b c ;"))``
EVAL``parse_pres (toks (strlit"preserved: x1 x2 x2 a b c;"))``
*)
(* parse optional objective line *)
Definition parse_obj_maybe_def:
(parse_obj_maybe [] = (NONE , [])) ∧
(parse_obj_maybe (l::ls) =
case parse_obj l of
NONE => (NONE, l::ls)
| SOME obj => (SOME obj, ls))
End
(* parse optional preserved line *)
Definition parse_pres_maybe_def:
(parse_pres_maybe [] = (NONE , [])) ∧
(parse_pres_maybe (l::ls) =
case parse_pres l of
NONE => (NONE, l::ls)
| SOME pres => (SOME pres, ls))
End
Definition parse_obj_pres_maybe_def:
parse_obj_pres_maybe ls =
case parse_obj_maybe ls of
| (SOME obj,ls) =>
(SOME obj, parse_pres_maybe ls)
| (NONE, ls) =>
(case parse_pres_maybe ls of
| (SOME pres,ls) =>
(case parse_obj_maybe ls of (ob,ls) => (ob, SOME pres, ls))
| (NONE,ls) => (NONE,NONE,ls))
End
(* Parse the tokenized pbf file *)
Definition parse_pbf_toks_def:
parse_pbf_toks tokss =
let nocomments = FILTER nocomment_line tokss in
let (obj,pres,rest) = parse_obj_pres_maybe nocomments in
case parse_constraints rest [] of
NONE => NONE
| SOME pbf => SOME (pres,obj,pbf)
End
(* Parse a list of strings in annotated pbf format *)
Definition parse_pbf_def:
parse_pbf strs = parse_pbf_toks (MAP toks strs)
End
(*
Parsing a proof file
*)
(* Fast tokenization *)
Definition fromString_unsafe_def:
fromString_unsafe str =
if strlen str = 0
then 0i
else if strsub str 0 = #"~" ∨
strsub str 0 = #"-"
then ~&fromChars_unsafe (strlen str - 1)
(substring str 1 (strlen str - 1))
else &fromChars_unsafe (strlen str) str
End
Definition is_numeric_def:
is_numeric c =
let n = ORD c in
48 ≤ n ∧ n ≤ 57
End
Definition is_num_prefix_def:
is_num_prefix c =
(c = #"-" ∨ c = #"+" ∨ c = #"~")
End
(* parse as integers, except if it ends with ; *)
Definition int_start_def:
int_start s =
if strlen s > 0
then
if strsub s (strlen s - 1) = #";" then F
else
is_numeric (strsub s 0) ∨
(strlen s > 1 ∧
is_num_prefix (strsub s 0) ∧
is_numeric (strsub s 1))
else
F
End
Definition tokenize_fast_def:
tokenize_fast (s:mlstring) =
if int_start s then
INR (fromString_unsafe s)
else INL s
End
Definition toks_fast_def:
toks_fast s = MAP tokenize_fast (tokens blanks s)
End
Type name_fun[pp] = “:(mlstring -> α -> (num # α) option) # α”
(* String literals are automatically mapped to internal names *)
Definition apply_lit_def:
apply_lit ((f,ns):'a name_fun) (Pos x) =
(case f x ns of
| NONE => NONE
| SOME (y,ns1) => SOME (Pos y,f,ns1)) ∧
apply_lit (f,ns) (Neg x) =
(case f x ns of
| NONE => NONE
| SOME (y,ns1) => SOME (Neg y,f,ns1))
End
(* Unused, but useful for testing *)
Definition plainVar_nf_def:
plainVar_nf s (u:unit) =
if strlen s ≥ 1 ∧ strsub s 0 = #"x" then
case mlint$fromNatString (substring s 1 (strlen s - 1)) of
NONE => NONE
| SOME n => SOME (n,())
else
NONE
End
(* The cutting planes polish notation stack is formed from constraints, where
factors and variables are also encoded using Id *)
Definition parse_lit_num_def:
parse_lit_num (f_ns:'a name_fun) s =
case parse_lit s of
| NONE => NONE
| SOME l => apply_lit f_ns l
End
Definition binopc_def:
binopc stack =
case stack of
Id a::b::rest => SOME (a,b,rest)
| _ => NONE
End
(* Parsing cutting planes in polish notation (header "pol", ";" stripped) *)
Definition parse_cutting_aux_def:
(parse_cutting_aux f_ns (x::xs) stack =
case x of INR n =>
if n ≥ 0 then
parse_cutting_aux f_ns xs (Id (Num n) :: stack)
else NONE
| INL s =>
if strlen s = 1
then
let c = strsub s 0 in
if c = #"+" then
(case stack of
a::b::rest => parse_cutting_aux f_ns xs (Add b a::rest)
| _ => NONE)
else if c = #"*" then
(case binopc stack of NONE => NONE
| SOME (a,b,rest) => parse_cutting_aux f_ns xs (Mul b a::rest))
else if c = #"w" then
(case stack of
Lit (Pos v)::a::rest => parse_cutting_aux f_ns xs (Weak a [v]::rest)
| _ => NONE)
else if c = #"s" then
(case stack of
a::rest => parse_cutting_aux f_ns xs (Sat a::rest)
| _ => NONE)
else if c = #"c" then
(case binopc stack of NONE => NONE
| SOME (a,b,rest) => parse_cutting_aux f_ns xs (Div Cd b a::rest))
else if c = #"d" then
(case binopc stack of NONE => NONE
| SOME (a,b,rest) => parse_cutting_aux f_ns xs (Div Dd b a::rest))
else if c = #"m" then
(case binopc stack of NONE => NONE
| SOME (a,b,rest) => parse_cutting_aux f_ns xs (Div Md b a::rest))
else if c = #"n" then
(case binopc stack of NONE => NONE
| SOME (a,b,rest) => parse_cutting_aux f_ns xs (Div Nd b a::rest))
else if c = #"-" then
(case binopc stack of NONE => NONE
| SOME (a,b,rest) => parse_cutting_aux f_ns xs (Minus b a::rest))
else
(case parse_lit_num f_ns s of
| SOME (l,f_ns1)=> parse_cutting_aux f_ns1 xs (Lit l::stack)
| NONE => NONE)
else
case parse_lit_num f_ns s of
| SOME (l,f_ns1)=> parse_cutting_aux f_ns1 xs (Lit l::stack)
| NONE => NONE) ∧
(parse_cutting_aux f_ns [] [c] = SOME (c,f_ns)) ∧
(parse_cutting_aux f_ns [] _ = NONE)
End
Definition parse_cutting_def:
parse_cutting f_ns ls = parse_cutting_aux f_ns ls []
End
(*
EVAL ``parse_cutting (plainVar_nf,()) (toks_fast (strlit "8 2 + 3 + 1 s + 9 2 n + x5 2 * + 5 -"))``;
*)
(* Parse a pseudo-Boolean constraint *)
Definition map_f_ns_def:
(map_f_ns f_ns [] = SOME ([],f_ns)) ∧
(map_f_ns f_ns ((c,l)::xs) =
case apply_lit f_ns l of
NONE => NONE
| SOME (l',f_ns') =>
case map_f_ns f_ns' xs of NONE => NONE
| SOME (ys,f_ns'') =>
SOME ((c,l')::ys,f_ns''))
End
Definition parse_constraint_npbc_def:
parse_constraint_npbc f_ns line =
case parse_constraint_LHS line of (rest,lhs) =>
(case rest of (INL cmp :: INR deg :: rest) =>
if cmp = strlit">=" then
case map_f_ns f_ns lhs of NONE => NONE
| SOME (lhs',f_ns') =>
SOME (
pbc_to_npbc (GreaterEqual,lhs',deg),
rest,
f_ns')
else
NONE
| _ => NONE)
End
(* A rup hint must be a sequence of numbers *)
Definition strip_rup_hint_aux_def:
(strip_rup_hint_aux [] acc = SOME (REVERSE acc)) ∧
(strip_rup_hint_aux (x::xs) acc =
case x of INR n =>
if n ≥ 0 then
strip_rup_hint_aux xs (Num n::acc)
else NONE
| INL s =>
if s = strlit"~" then
strip_rup_hint_aux xs (0::acc)
else NONE)
End
Definition strip_rup_hint_def:
strip_rup_hint xs = strip_rup_hint_aux xs []
End
Definition parse_rup_hint_def:
(parse_rup_hint [] = NONE) ∧
(parse_rup_hint (x::xs) =
case x of
INL s =>
if s = strlit ":" then strip_rup_hint xs else NONE
| _ => NONE)
End
(* Parsing RUP step (header "rup", ";" stripped) *)
Definition parse_rup_def:
parse_rup f_ns line =
case parse_constraint_npbc f_ns line of
SOME (constr,rest,f_ns') =>
(case parse_rup_hint rest of NONE => NONE
| SOME ns => SOME(constr,ns,f_ns'))
| _ => NONE
End
(*
EVAL ``parse_rup (plainVar_nf,()) (toks_fast (strlit "1 ~x5 1 x26 +1 x27 1 x28 1 x29 >= 1 : 6 11 ~"))``
*)
Definition start_subproof_def:
start_subproof rest ⇔
rest = [INL (strlit ":"); INL (strlit"subproof")]
End
Definition colon_id_opt_def:
colon_id_opt rest =
case rest of
[INL term; INR id] =>
if term = strlit":" ∧ id ≥ 0 then SOME (SOME (Num id))
else NONE
| [] => SOME NONE
| _ => NONE
End
Definition parse_pbc_header_def:
parse_pbc_header f_ns line =
case parse_constraint_npbc f_ns line of
SOME (constr,rest,f_ns') =>
if start_subproof rest
then SOME (constr,f_ns')
else NONE
| _ => NONE
End
(*
EVAL ``parse_pbc_header (plainVar_nf,()) (toks_fast (strlit "3 x1 -3 x2 2 x3 >= 5 : subproof"))``
*)
Definition strip_numbers_aux_def:
(strip_numbers_aux [] acc = SOME (REVERSE acc)) ∧
(strip_numbers_aux (x::xs) acc =
case x of INR n =>
if n ≥ 0 then
strip_numbers_aux xs (Num n::acc)
else NONE
| INL _ => NONE)
End
Definition strip_numbers_def:
strip_numbers xs = strip_numbers_aux xs []
End
(* Parse a single line of lstep including ; terminator
pbc does not end with terminator.
NONE -> failed to parse
INL c -> beginning of a proof-by-contradiction
INR s -> lstep *)
Definition parse_lstep_aux_def:
(parse_lstep_aux f_ns s =
case s of [] => NONE
| (r::rs) =>
if r = INL (strlit "pbc") then
case parse_pbc_header f_ns rs of NONE => NONE
| SOME (c,f_ns') =>
SOME (INR c,f_ns')
else
case strip_term_line rs of NONE => NONE
| SOME rs =>
if r = INL (strlit "deld") then
(case strip_numbers rs of NONE => NONE
| SOME n => SOME (INL (Delete n),f_ns))
else if r = INL (strlit "pol") then
(case parse_cutting f_ns rs of NONE => NONE
| SOME (p,f_ns') => SOME (INL (Cutting p),f_ns'))
else if r = INL (strlit "rup") then
(case parse_rup f_ns rs of NONE => NONE
| SOME (c,ns,f_ns') => SOME (INL (Rup c ns),f_ns'))
else if r = INL (strlit "e") then
(case parse_constraint_npbc f_ns rs of
NONE => NONE
| SOME (c,rest,f_ns') =>
case colon_id_opt rest of
| SOME (SOME res) =>
SOME (INL (Check res c), f_ns')
| _ => NONE)
else if r = INL (strlit "ia") then
(case parse_constraint_npbc f_ns rs of
NONE => NONE
| SOME (c,rest,f_ns') =>
case colon_id_opt rest of
| SOME (SOME res) =>
SOME (INL (ImplyAdd res c), f_ns')
| _ => NONE)
else if r = INL (strlit "e") then
(case parse_constraint_npbc f_ns rs of
NONE => NONE
| SOME (c,rest,f_ns') =>
case colon_id_opt rest of
| SOME (SOME res) =>
SOME (INL (Check res c), f_ns')
| _ => NONE)
else if r = INL (strlit "*") then SOME (INL NoOp,f_ns)
else NONE)
End
(* qed mark : ID ; --- ID is optional *)
Definition check_mark_qed_id_opt_def:
check_mark_qed_id_opt mark (h:(mlstring + int) list ) =
case strip_term_line h of NONE => NONE
| SOME rs =>
case rs of
INL q :: rest =>
if q = strlit"qed" then
case rest of
s :: rest' =>
if s = mark
then colon_id_opt rest'
else colon_id_opt rest
| [] => SOME NONE
else NONE
| _ => NONE
End
(* qed mark : ID ; *)
Definition check_mark_qed_id_def:
check_mark_qed_id mark (h:(mlstring + int) list ) =
case check_mark_qed_id_opt mark h of
SOME (SOME res) => SOME res
| _ => NONE
End
(*
EVAL `` check_mark_qed_id (INL (strlit"pbc")) (toks_fast (strlit"qed pbc : 4 ;"))``
EVAL `` check_mark_qed_id_opt (INL (strlit"pbc")) (toks_fast (strlit"qed pbc;"))``
EVAL `` check_mark_qed_id_opt (INL (strlit"pbc")) (toks_fast (strlit"qed;"))``
*)
(* Parsing lsteps in a subproof until parsing
fails and return the failing line
(note: this expects a failing line, i.e.,
in an lstep parse, it should match everything nested
pbc step except the outermost one ) *)
Definition parse_lsteps_aux_def:
(parse_lsteps_aux f_ns
([]:(mlstring + int) list list)
(acc:lstep list) = NONE) ∧
(parse_lsteps_aux f_ns (s::ss) acc =
case parse_lstep_aux f_ns s of
| NONE => SOME (REVERSE acc, f_ns, s, ss)
| SOME (INL step, f_ns') =>
parse_lsteps_aux f_ns' ss (step::acc)
| SOME (INR c, f_ns') =>
case parse_lsteps_aux f_ns' ss [] of
NONE => NONE
| SOME (pf,f_ns'',s,rest) =>
if LENGTH rest < LENGTH ss then
case check_mark_qed_id (INL (strlit"pbc")) s of
NONE => NONE
| SOME id =>
parse_lsteps_aux f_ns'' rest (Con c pf id::acc)
else NONE)
Termination
WF_REL_TAC ‘measure (LENGTH o FST o SND)’
\\ rw[] \\ fs[]
End
Theorem parse_lsteps_aux_LENGTH:
∀f_ns ss acc acc' f_ns' s ss'.
parse_lsteps_aux f_ns ss acc = SOME(acc',f_ns' ,s,ss') ⇒
LENGTH ss' < LENGTH ss
Proof
ho_match_mp_tac parse_lsteps_aux_ind>>
rw[parse_lsteps_aux_def]>>
every_case_tac>>
gs[]
QED
Theorem parse_lsteps_aux_thm:
(∀f_ns acc.
parse_lsteps_aux f_ns
([]:(mlstring + int) list list)
(acc:lstep list) = NONE) ∧
(∀f_ns s ss acc.
parse_lsteps_aux f_ns (s::ss) acc =
case parse_lstep_aux f_ns s of
| NONE => SOME (REVERSE acc, f_ns, s, ss)
| SOME (INL step, f_ns') =>
parse_lsteps_aux f_ns' ss (step::acc)
| SOME (INR c, f_ns') =>
case parse_lsteps_aux f_ns' ss [] of
NONE => NONE
| SOME (pf,f_ns'',s,rest) =>
case check_mark_qed_id (INL (strlit"pbc")) s of
NONE => NONE
| SOME id =>
parse_lsteps_aux f_ns'' rest (Con c pf id::acc))
Proof
rw[]
>-
simp[Once parse_lsteps_aux_def]
>- (
simp[Once parse_lsteps_aux_def]>>
every_case_tac>>fs[]>>
imp_res_tac parse_lsteps_aux_LENGTH)
QED
Definition parse_lsteps_def:
parse_lsteps f_ns ss = parse_lsteps_aux f_ns ss []
End
(*
EVAL`` parse_lsteps (plainVar_nf,())
(MAP toks_fast [
strlit "pbc +3 x1 +3 x2 +2 x3 >= 5 : subproof";
strlit "pbc +3 x1 +3 x2 +2 x3 >= 5 : subproof";
strlit"pol 1 2 +;";
strlit"rup >= 1 : 1234;";
strlit"qed pbc : 4;";
strlit"qed pbc : 4;";
strlit"qed pbc : 4;";
])``
*)
Definition parse_var_def:
parse_var f_ns v =
case parse_lit_num f_ns v of
SOME (Pos n,f_ns) => SOME (n,f_ns)
| _ => NONE
End
(* Parse a substitution {var -> lit} *)
Definition parse_subst_aux_def:
(parse_subst_aux f_ns (INL v :: INL arr :: r ::rest) acc =
if arr = strlit "->" then
case parse_var f_ns v of
NONE => ([],[],f_ns)
| SOME (v,f_ns) =>
(case r of
INR r =>
if r = 0:int then parse_subst_aux f_ns rest ((v,INL F)::acc)
else if r = 1 then parse_subst_aux f_ns rest ((v,INL T)::acc)
else ([],[],f_ns)
| INL r =>
(case parse_lit_num f_ns r of NONE => ([],[],f_ns)
| SOME (l, f_ns) => parse_subst_aux f_ns rest ((v,INR l)::acc)))
else ([],[],f_ns)) ∧
(parse_subst_aux f_ns ls acc = (ls, acc,f_ns))
End
Definition parse_subst_def:
(parse_subst f_ns [] = NONE) ∧
(parse_subst f_ns (x::xs) =
case x of
INL s =>
if s = strlit ":" then
SOME (parse_subst_aux f_ns xs [])
else NONE
| _ => NONE)
End
(* A strengthening rule header *)
Definition parse_red_header_def:
parse_red_header f_ns line =
case parse_constraint_npbc f_ns line of
SOME (constr,rest,f_ns') =>
(case parse_subst f_ns' rest of
NONE => NONE
| SOME (rest,ss,f_ns'') =>
if start_subproof rest
then SOME (constr,ss,f_ns'')
else NONE)
| _ => NONE
End
(* parse a subproof and returns the next failing line
Type subproof = ``:((((num + num) # num) option, (lstep list)) alist)``;
*)
Definition mk_acc_def:
mk_acc pf acc = if pf = [] then acc else (NONE,pf)::acc
End
(* We map #n to n-1 internally *)
Definition parse_hash_num_def:
parse_hash_num s =
if strlen s ≥ 1 ∧ strsub s 0 = #"#" then
OPTION_MAP (λn. n-1)
(mlint$fromNatString (substring s 1 (strlen s - 1)))
else NONE
End
Definition parse_subgoal_num_def:
parse_subgoal_num line =
case line of
[INR n] => if n>=0 then SOME (INL (Num n)) else NONE
| [INL s] =>
(case parse_hash_num s of
SOME n => SOME (INR n)
| NONE => NONE)
| _ => NONE
End
Definition parse_proofgoal_def:
parse_proofgoal (h:(mlstring + int) list ) =
case h of
INL e::rs =>
(if e = (strlit"proofgoal") then
parse_subgoal_num rs
else NONE)
| _ => NONE
End
Definition mk_proofgoal_mark_def:
mk_proofgoal_mark (ind : num + num) =
case ind of
INL n => INR (&n:int)
| INR n => INL (strlit"#" ^ toString (n+1))
End
Definition parse_subproof_aux_def:
(parse_subproof_aux f_ns ss (acc:subproof) =
case parse_lsteps f_ns ss of
NONE => NONE
| SOME (pf,f_ns',s,rest) =>
let acc = mk_acc pf acc in
case parse_proofgoal s of
NONE => SOME (REVERSE acc, f_ns', s, rest)
| SOME ind =>
(case parse_lsteps f_ns' rest of
NONE => NONE
| SOME (pf,f_ns'',s,rest) =>
case check_mark_qed_id (mk_proofgoal_mark ind) s of
NONE => NONE
| SOME id =>
parse_subproof_aux f_ns'' rest ((SOME (ind,id),pf)::acc)
)
)
Termination
WF_REL_TAC ‘measure (LENGTH o FST o SND)’
\\ rw[] \\ fs[parse_lsteps_def]>>
imp_res_tac parse_lsteps_aux_LENGTH>>
fs[]
End
Definition parse_subproof_def:
parse_subproof f_ns ss =
parse_subproof_aux f_ns ss []
End
(*
EVAL`` parse_subproof (plainVar_nf,())
(MAP toks_fast [
strlit"pol 12 11 + 5 + 6 +;";
strlit"proofgoal #1";
strlit"pol 12 11 + 5 + 6 +;";
strlit "pbc +3 x1 +3 x2 +2 x3 >= 5 : subproof";
strlit"pol 1 2 +;";
strlit "pbc +3 x1 +3 x2 +2 x3 >= 5 : subproof";
strlit"pol 1 2 +;";
strlit"rup >= 1 : 1234;";
strlit"qed pbc : 4;";
strlit"rup >= 1 : 1234;";
strlit"qed pbc : 4;";
strlit"pol 14 x6 +;";
strlit"qed #1 : 15;";
strlit"pol 12 11 + 5 + 6 +;";
strlit "pbc +3 x1 +3 x2 +2 x3 >= 5 : subproof";
strlit"pol 1 2 +;";
strlit"rup >= 1 : 1234;";
strlit"qed pbc : 4;";
strlit"proofgoal 1";
strlit"pol 16 2 +;";
strlit"qed 1 : 17;";
strlit"qed;";
])``
*)
(* leq -> 0, geq -> 1 *)
Definition parse_scopetext_def:
parse_scopetext line =
case line of
[INL s] =>
if s = strlit"leq" then SOME 0
else if s = strlit"geq" then SOME 1
else NONE
| _ => NONE
End
Definition parse_scopehead_def:
parse_scopehead (h:(mlstring + int) list ) =
case h of
INL e::rs =>
(if e = (strlit"scope") then
parse_scopetext rs
else NONE)
| _ => NONE
End
Definition mk_scope_mark_def:
mk_scope_mark sc =
if sc = 0n then [INL (strlit"scope"); INL (strlit "leq")]
else [INL (strlit"scope"); INL (strlit "geq")]
End
Definition check_end_def:
check_end marks (h:(mlstring + int) list ) =
case strip_term_line h of NONE => F
| SOME rs =>
case rs of
INL q :: rest =>
q = strlit"end" ∧
(rest = [] ∨
rest = marks)
| _ => F
End
Theorem parse_subproof_aux_LENGTH:
∀f_ns ss acc res acc' f_ns' ss'.
parse_subproof_aux f_ns ss acc = SOME(res,acc',f_ns',ss') ⇒
LENGTH ss' < LENGTH ss
Proof
ho_match_mp_tac (fetch "-" "parse_subproof_aux_ind")>>
rw[]>>
pop_assum mp_tac>>
simp[Once parse_subproof_aux_def]>>
every_case_tac>>fs[parse_lsteps_def]>>
rw[]>>imp_res_tac parse_lsteps_aux_LENGTH>>
fs[]
QED
(* parse a scope and returns the next failing line
Type scope = ``:(num option, subproof) alist``;
*)
Definition parse_scope_aux_def:
(parse_scope_aux f_ns ss (acc:(num option, subproof) alist) =
case parse_subproof f_ns ss of