-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathpb_parseScript.sml
1351 lines (1233 loc) · 35 KB
/
pb_parseScript.sml
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, npb_check
*)
open preamble pbcTheory pbc_normaliseTheory npbc_checkTheory;
val _ = new_theory "pb_parse";
val _ = numLib.temp_prefer_num();
(* Print 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
(*
Parse an OPB file as string pbc
*)
(* 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_def:
(parse_constraint_LHS (INR n::INL v::rest) acc =
case parse_lit v of NONE => (INR n::INL v::rest,REVERSE acc)
| SOME lit => parse_constraint_LHS rest ((n,lit)::acc)) ∧
(parse_constraint_LHS ls acc = (ls,REVERSE acc))
End
(* strip ; from the end of a string/number *)
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
Definition strip_terminator_def:
strip_terminator s =
if strlen s ≥ 1 ∧ strsub s (strlen s - 1) = #";"
then mlint$fromString (substring s 0 (strlen s - 1))
else NONE
End
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 parse_constraint_LHS line [] of (rest,lhs) =>
let cmpdeg =
(case rest of
[INL cmp; INR deg; INL term] =>
if term ≠ str #";" then NONE else SOME(cmp,deg)
| [INL cmp; INL degterm] =>
(case strip_terminator degterm of NONE => NONE
| SOME deg => SOME(cmp,deg))
| _ => NONE) in
case cmpdeg of NONE => NONE
| SOME (cmp, deg) =>
case parse_op cmp of NONE => NONE
| SOME op =>
SOME ((op,lhs,deg):mlstring pbc)
End
(* EVAL ``parse_constraint (toks (strlit "2 ~x1 1 ~x3 >= 1;"))``; *)
Definition parse_constraints_def:
(parse_constraints [] acc = SOME (REVERSE acc)) ∧
(parse_constraints (s::ss) acc =
case parse_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:
lin_term constant ;
lin_term constant;
lin_term ;
lin_term;
*)
(* parse objective term in pbc ... ; *)
Definition parse_obj_term_def:
parse_obj_term xs =
case parse_constraint_LHS xs [] of
(rest,obj) =>
case rest of
[INL s] =>
if s = strlit";" then SOME (obj,0:int)
else
(case strip_terminator s of NONE => NONE
| SOME i => SOME (obj,i))
| [INR c;INL s] =>
if s = strlit";" then SOME (obj,c:int) else
(case strip_term s of NONE => NONE
| SOME v =>
case parse_lit v of NONE => NONE
| SOME lit =>
SOME(SNOC (c,lit) obj, 0))
| _ => NONE
End
(*
EVAL``parse_obj_term (toks (strlit"1 ~x199 1 x2 ~5 ;"))``
EVAL``parse_obj_term (toks (strlit"1 ~x199 1 x2 ;"))``
EVAL``parse_obj_term (toks (strlit"1 ~x199 1 x2;"))``
EVAL``parse_obj_term (toks (strlit"1 ~x199 1 x2 5;"))``
*)
(* parse objective line in PBF *)
Definition parse_obj_def:
(parse_obj [] = NONE) ∧
(parse_obj (x::xs) =
if x = INL (strlit "min:") then
parse_obj_term xs
else NONE
)
End
(* 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 "preserve_init") then
parse_vars_raw xs
else NONE)
End
(* 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 pbf format *)
Definition parse_pbf_def:
parse_pbf strs = parse_pbf_toks (MAP toks strs)
End
(*
Parsing a proof file
*)
Type name_fun[pp] = “:(mlstring -> α -> (num # α) option) # α”
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
(* The 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 parse_cutting_def:
(parse_cutting f_ns (x::xs) stack =
case x of INR n =>
if n ≥ 0 then
parse_cutting f_ns xs (Id (Num n) :: stack)
else NONE
| INL s =>
if s = str #"+" then
(case stack of
a::b::rest => parse_cutting f_ns xs (Add b a::rest)
| _ => NONE)
else if s = str #"*" then
(case stack of
Id a::b::rest => parse_cutting f_ns xs (Mul b a::rest)
| _ => NONE)
else if s = str #"d" then
(case stack of
Id a::b::rest => parse_cutting f_ns xs (Div b a::rest)
| _ => NONE)
else if s = str #"s" then
(case stack of
a::rest => parse_cutting f_ns xs (Sat a::rest)
| _ => NONE)
else if s = str #"w" then
(case stack of
Lit (Pos v)::a::rest => parse_cutting f_ns xs (Weak a v::rest)
| _ => NONE)
else
case parse_lit_num f_ns s of
| SOME (l,f_ns1)=> parse_cutting f_ns1 xs (Lit l::stack)
| NONE => NONE) ∧
(parse_cutting f_ns [] [c] = SOME (c,f_ns)) ∧
(parse_cutting f_ns [] _ = NONE)
End
Definition strip_numbers_def:
(strip_numbers [] acc = SOME (REVERSE acc)) ∧
(strip_numbers (x::xs) acc =
case x of INR n =>
if n ≥ 0 then
strip_numbers xs (Num n::acc)
else NONE
| INL _ => NONE)
End
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 ls =
let (ls, acc,f_ns) = parse_subst_aux f_ns ls [] in
(ls, acc, f_ns)
End
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 :: INL term :: rest) =>
if term = str #";" ∧ 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
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 (rest,ss,f_ns'') =>
(case rest of
[INL term; INL beg] =>
if term = strlit";" ∧ beg = strlit"begin" then
SOME (constr,ss,f_ns'')
else NONE
| _ => NONE))
| _ => NONE
End
(* formatting
rup constraint ; ID1 ID2 ... *)
Definition parse_constraint_npbc_2_def:
parse_constraint_npbc_2 f_ns line =
case parse_constraint_LHS line [] of (rest,lhs) =>
(case rest of (INL cmp :: INR deg :: INL term :: rest) =>
if term = str #";" ∧ 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
Definition strip_numbers_end_def:
(strip_numbers_end [] acc = SOME (REVERSE acc)) ∧
(strip_numbers_end (x::xs) acc =
case x of INR n =>
if n ≥ 0 then
strip_numbers_end xs (Num n::acc)
else NONE
| INL s =>
if s = strlit"~" then
strip_numbers_end xs (0::acc)
else NONE)
End
Definition parse_rup_def:
parse_rup f_ns line =
case parse_constraint_npbc_2 f_ns line of
SOME (constr,rest,f_ns') =>
(case strip_numbers_end rest [] of NONE => NONE
| SOME ns => SOME(constr,ns,f_ns'))
| _ => NONE
End
(*
EVAL``parse_obj_term (toks (strlit"1 ~x199 1 x2 ;"))``
EVAL``parse_obj_term (toks (strlit"1 ~x199 1 x2;"))``
EVAL``parse_obj_term (toks (strlit"1 ~x199 1 x2 5;"))``
*)
(* Parse a single line of an lstep,
Except "Red", which will be handled later *)
Definition parse_lstep_aux_def:
(parse_lstep_aux f_ns s =
case s of [] => NONE
| (r::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 "red") then
case parse_red_header f_ns rs of NONE => NONE
| SOME (c,s,f_ns') =>
SOME (INR (c,s),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 rest of [INR id] =>
if id ≥ 0 then
SOME (INL (Check (Num id) c), f_ns')
else NONE
| _ => NONE)
else if r = INL (strlit "*") then SOME (INL NoOp,f_ns)
else NONE)
End
Definition check_end_def:
check_end (h:(mlstring + int) list ) =
case h of
[INL e; INR n] =>
if e = (strlit"end") ∧ n ≥ 0 then SOME (Num n) else NONE
| _ => NONE
End
Definition is_empty_def:
is_empty v ⇔ v = []
End
(* Parsing lsteps in a subproof until parsing
fails and return the failing line
(note: this expects a failing line)
redundancy steps can not appear in subproofs
*)
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,s), f_ns') =>
if ¬(is_empty s) then NONE
else
case parse_lsteps_aux f_ns' ss [] of
NONE => NONE
| SOME (pf,f_ns'',s,rest) =>
if LENGTH rest < LENGTH ss then
case check_end 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,s), f_ns') =>
if ¬(is_empty s) then NONE
else
case parse_lsteps_aux f_ns' ss [] of
NONE => NONE
| SOME (pf,f_ns'',s,rest) =>
case check_end 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
(* parse the body of a redundancy proof, which is of the form:
lsteps
proofgoal ... begin
end (id)
lsteps
proofgoal ... begin
end (id)
end (NO ID HERE)
*)
(* #n -> n-1 *)
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 check_end_opt_def:
check_end_opt (h:(mlstring + int) list ) =
case h of
[INL e; INR n] =>
if e = (strlit"end") ∧ n ≥ 0 then SOME (SOME (Num n)) else NONE
| [INL e] =>
if e = (strlit"end") then SOME NONE else NONE
| _ => NONE
End
Definition parse_red_body_def:
(parse_red_body (h:(mlstring + int) list ) =
case check_end_opt h of
NONE =>
(case parse_proofgoal h of
SOME ind => SOME (INR ind)
| NONE => NONE)
| SOME res => SOME (INL res))
End
Definition mk_acc_def:
mk_acc pf (acc:(( ((num + num) # num) option,lstep list) alist)) = if pf = [] then acc else (NONE,pf)::acc
End
Definition parse_red_aux_def:
(parse_red_aux f_ns ss
(acc:(( ((num + num) # num) option,lstep list) alist)) =
case parse_lsteps_aux f_ns ss [] of
NONE => NONE
| SOME (pf,f_ns',s,rest) =>
let acc' = mk_acc pf acc in
(case parse_red_body s of
NONE => NONE
| SOME (INL res) => SOME (res,REVERSE acc', f_ns', rest)
| SOME (INR ind) =>
(case parse_lsteps_aux f_ns' rest [] of
NONE => NONE
| SOME (pf,f_ns'',s,rest2) =>
case check_end s of
NONE => NONE
| SOME id =>
parse_red_aux f_ns'' rest2 ((SOME (ind,id),pf)::acc')
))
)
Termination
WF_REL_TAC ‘measure (LENGTH o FST o SND)’
\\ rw[] \\ fs[]>>
imp_res_tac parse_lsteps_aux_LENGTH>>
fs[]
End
Theorem parse_red_aux_LENGTH:
∀f_ns ss acc res acc' f_ns' ss'.
parse_red_aux f_ns ss acc = SOME(res,acc',f_ns',ss') ⇒
LENGTH ss' < LENGTH ss
Proof
ho_match_mp_tac (fetch "-" "parse_red_aux_ind")>>
rw[]>>
pop_assum mp_tac>>
simp[Once parse_red_aux_def]>>
every_case_tac>>fs[]>>
rw[]>>imp_res_tac parse_lsteps_aux_LENGTH>>
fs[]
QED
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 = #"-")
End
Definition int_start_def:
int_start s =
((strlen s > 0 ∧ is_numeric (strsub s 0)) ∨
(strlen s > 1 ∧
is_num_prefix (strsub s 0) ∧
is_numeric (strsub s 1)))
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
val headertrm = rconc (EVAL``toks_fast (strlit"pseudo-Boolean proof version 2.0")``);
(* We could check that the number of constraints is correct here *)
Definition check_f_line_def:
check_f_line s =
case s of [] => F
| x::xs => x = INL(strlit "f")
End
Definition parse_header_def:
parse_header ss =
case ss of
x::y::rest =>
if x = ^headertrm ∧ check_f_line y
then SOME rest
else NONE
| _ => NONE
End
(* Check special cases whether a Red step can be converted to a Con step *)
Definition reduce_pf_def:
reduce_pf s pf res =
if is_empty s then
case res of NONE => NONE
| SOME id =>(
case pf of
| [(NONE,pf)] => SOME (pf,id)
| [] => SOME ([],id)
| _ => NONE)
else NONE
End
(* Parse 1 sstep,
until an unparseable line is reached *)
Definition parse_sstep_def:
(parse_sstep f_ns [] = NONE) ∧
(parse_sstep f_ns (s::ss) =
case parse_lstep_aux f_ns s of
NONE => SOME (INL s,f_ns,ss)
| SOME (INL step,f_ns') =>
SOME (INR (Lstep step),f_ns',ss)
| SOME (INR (c,s),f_ns') =>
(
case parse_red_aux f_ns' ss [] of
NONE => NONE
| SOME (res, pf,f_ns'',rest) =>
case reduce_pf s pf res of
NONE => SOME (INR (Red c s pf res),f_ns'',rest)
| SOME (pf,id) =>
SOME (INR (Lstep (Con c pf id)),f_ns'',rest)
))
End
Definition parse_pre_order_head_def:
parse_preorder_head s =
case s of
[x;INL name] =>
if x = INL (strlit"pre_order") then
SOME name
else NONE
| _ => NONE
End
Definition hashString_nf_def:
hashString_nf s t = SOME(hashString s,t)
End
Definition parse_vars_line_aux_def:
(parse_vars_line_aux f_ns [] = SOME ([],f_ns)) ∧
(parse_vars_line_aux f_ns (x::xs) =
case x of
INL v =>
(case parse_var f_ns v of
NONE => NONE
| SOME (vv,f_ns') =>
(case parse_vars_line_aux f_ns' xs of NONE => NONE
| SOME (vvs,f_ns'') => SOME (vv::vvs,f_ns'')))
| _ => NONE)
End
Definition parse_vars_line_def:
parse_vars_line n l =
case l of
h::rest =>
if h = INL n then
OPTION_MAP FST (parse_vars_line_aux (hashString_nf,()) rest)
else NONE
| _ => NONE
End
Definition parse_vars_aux_def:
parse_vars_aux v l r a e =
if v = [INL (strlit"vars")] ∧
a = [INL (strlit"aux")] ∧
e = [INL (strlit"end")] then
case parse_vars_line (strlit "left") l of NONE => NONE
| SOME us =>
case parse_vars_line (strlit "right") r of NONE => NONE
| SOME vs => SOME (us,vs)
else NONE
End
Definition parse_vars_block_def:
parse_vars_block ss =
case ss of
v::l::r::a::e::rest =>
(case parse_vars_aux v l r a e of NONE => NONE
| SOME res => SOME (res,rest))
| _ => NONE
End
(* parse constraints *)
Definition parse_def_aux_def:
(parse_def_aux [] acc = NONE) ∧
(parse_def_aux (s::ss) acc =
if s = [INL (strlit"end")] then
SOME(REVERSE acc,ss)
else
case parse_constraint_npbc (hashString_nf,()) s of
NONE => NONE
| SOME (c,_) => parse_def_aux ss (c::acc))
End
Definition parse_def_block_def:
parse_def_block ss =
case ss of
h::rest =>
if h = [INL (strlit"def")] then
parse_def_aux rest []
else NONE
| _ => NONE
End
(* parse a regular proof block with an extra "proof" and "end" *)
Definition parse_proof_block_def:
parse_proof_block ss =
case ss of [] => NONE
| h::ss =>
if h = [INL(strlit"proof")] then
case parse_red_aux (hashString_nf,()) ss [] of
| SOME (NONE,pf,_,rest) =>
(case rest of
x::rest =>
if x = [INL(strlit "end")]
then
SOME (pf,rest)
else NONE
| _ => NONE)
| _ => NONE
else NONE
End
Definition parse_trans_aux_def:
parse_trans_aux h v f e =
if
h = [INL (strlit"transitivity")] ∧
v = [INL (strlit"vars")] ∧
e = [INL (strlit"end")]
then
case parse_vars_line (strlit "fresh_right") f of NONE => NONE
| SOME ws => SOME ws
else NONE
End
Definition parse_trans_block_def:
parse_trans_block ss =
case ss of
h::v::f::e::ss =>
(case parse_trans_aux h v f e of NONE => NONE
| SOME ws =>
case parse_proof_block ss of NONE => NONE
| SOME (pf, ss) =>
SOME (ws,pf,ss))
| _ => NONE
End
Definition parse_refl_block_def:
parse_refl_block ss =
case ss of
h::ss =>
if h = [INL (strlit"reflexivity")] then
parse_proof_block ss
else NONE
| _ => NONE
End
Definition parse_trans_refl_block_def:
parse_trans_refl_block ss =
case parse_trans_block ss of NONE => NONE
| SOME (ws,pft,ss) =>
case parse_refl_block ss of NONE => NONE
| SOME(pfr,ss) => SOME (ws, pfr, pft, ss)
End
Definition parse_pre_order_def:
parse_pre_order ss =
case parse_vars_block ss of NONE => NONE
| SOME ((us,vs), ss) =>
case parse_def_block ss of NONE => NONE
| SOME (f, ss) =>
case parse_trans_refl_block ss of NONE => NONE
| SOME (ws, pfr, pft, ss) =>
case ss of [] => NONE
| h::ss =>
if h = [INL (strlit"end")] then
SOME ((f,us,vs),ws,pfr,pft,ss)
else NONE
End
(*
EVAL ``
parse_pre_order (MAP toks [
strlit" vars";
strlit" left u1";
strlit" right v1";
strlit" aux";
strlit"end";
strlit" def";
strlit" >= 1048575 ;";
strlit"end";
strlit" transitivity";
strlit" vars";
strlit" fresh_right w1";
strlit"end";
strlit" proof";
strlit"* prove provided by user";
strlit"proofgoal #1";
strlit"pol 1 2 + 3 +";
strlit"end 4";
strlit"end";
strlit"end";
strlit" reflexivity";
strlit" proof";
strlit" proofgoal #1";
strlit" end 5";
strlit" end";
strlit" end";
strlit"end";
])``
*)
(* Partially parsed information *)
Datatype:
par =
| Done cstep
| Dompar npbc subst_raw
| StoreOrderpar mlstring
| CheckedDeletepar num subst_raw
| ChangeObjpar bool ((int # num) list # int)
| ChangePrespar bool num npbc
End
Definition parse_load_order_def:
(parse_load_order f_ns [] = SOME (Done UnloadOrder,f_ns)) ∧
(parse_load_order f_ns (INL r::rs) =
case parse_vars_line_aux f_ns rs of
NONE => NONE
| SOME (ws,f_ns') => SOME (Done (LoadOrder r ws),f_ns')) ∧
(parse_load_order f_ns _ = NONE)
End
Definition parse_delc_header_def:
parse_delc_header f_ns line =
case line of
INR id::INL term::rest =>
if term = str #";" ∧ id ≥ 0 then
(case parse_subst f_ns rest of (rest,ss,f_ns') =>
(case rest of
[INL term; INL beg] =>
if term = strlit";" ∧ beg = strlit"begin" then
SOME (Num id,ss,f_ns')
else NONE
| _ => NONE))
else NONE
| _ => NONE
End
Definition insert_lit_def:
(insert_lit (Pos v) (f:bool spt) = sptree$insert v T f) ∧
(insert_lit (Neg v) f = insert v F f)
End
Definition parse_assg_def:
(parse_assg f_ns [] acc = SOME (acc,NONE,f_ns)) ∧
(parse_assg f_ns (INL s::ss) acc =
case parse_lit_num f_ns s of
NONE =>
if s = strlit":" then