-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathnpbc_parseProgScript.sml
3867 lines (3623 loc) · 118 KB
/
npbc_parseProgScript.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
(*
Add shared pbp parsing, normalization and other common stuff to npbc_arrayProg
*)
open preamble basis npbc_checkTheory pb_parseTheory npbc_arrayProgTheory pbc_normaliseTheory;
val _ = new_theory "npbc_parseProg"
val _ = translation_extends"npbc_arrayProg";
val xlet_autop = xlet_auto >- (TRY( xcon) >> xsimpl)
val r = translate strip_numbers_def;
val strip_numbers_side_def = theorem "strip_numbers_side_def";
val strip_numbers_side = Q.prove(
`∀x y. strip_numbers_side x y <=> T`,
Induct>>rw[Once strip_numbers_side_def]>>
intLib.ARITH_TAC) |> update_precondition;
val r = translate pbcTheory.map_lit_def;
val r = translate (hashNon_def |> SIMP_RULE std_ss [non_list_def]);
val r = translate hashChar_def;
val r = translate hashChars_alt_def;
val r = translate hashString_def;
(* TODO: decouple parse_lit from goodChar *)
val r = translate goodChar_def;
val r = translate goodChars_def;
val r = translate goodString_def;
val goodchars_side_def = fetch "-" "goodchars_side_def";
Theorem goodchars_side:
∀n s. n ≤ LENGTH s ⇒ goodchars_side n (strlit s)
Proof
Induct>>rw[]>>
simp[Once goodchars_side_def]
QED
val goodstring_side = Q.prove(
`∀x. goodstring_side x = T`,
Cases>>EVAL_TAC>>
match_mp_tac goodchars_side>>
simp[]) |> update_precondition;
val r = translate parse_lit_def;
val r = translate apply_lit_def;
val r = translate parse_lit_num_def;
val r = translate parse_cutting_def;
val parse_cutting_side_def = theorem "parse_cutting_side_def";
val parse_cutting_side = Q.prove(
`∀x y z. parse_cutting_side x y z <=> T`,
Induct_on`y`>>rw[Once parse_cutting_side_def]>>
intLib.ARITH_TAC) |> update_precondition;
val r = translate parse_var_def;
val r = translate parse_subst_aux_def;
val r = translate parse_subst_def;
val r = translate pbcTheory.lit_var_def;
val r = translate compact_lhs_def;
val r = translate term_le_def;
val r = translate mk_coeff_def;
val r = translate normalise_lhs_def;
val r = translate mergesortTheory.sort2_def;
val r = translate mergesortTheory.sort3_def;
val r = translate mergesortTheory.merge_def;
val r = translate DROP_def;
val r = translate (mergesortTheory.mergesortN_def |> SIMP_RULE std_ss [DIV2_def]);
Triviality mergesortn_ind:
mergesortn_ind (:'a)
Proof
once_rewrite_tac [fetch "-" "mergesortn_ind_def"]
\\ rpt gen_tac
\\ rpt (disch_then strip_assume_tac)
\\ match_mp_tac (latest_ind ())
\\ rpt strip_tac
\\ last_x_assum match_mp_tac
\\ rpt strip_tac
\\ gvs [FORALL_PROD, DIV2_def]
QED
val _ = mergesortn_ind |> update_precondition;
Triviality mergesortn_side:
∀x y z.
mergesortn_side x y z
Proof
completeInduct_on`y`>>
rw[Once (fetch "-" "mergesortn_side_def")]>>
simp[arithmeticTheory.DIV2_def]
>- (
first_x_assum match_mp_tac>>
simp[]>>
match_mp_tac dividesTheory.DIV_POS>>
simp[])
>>
match_mp_tac DIV_LESS_EQ>>
simp[]
QED
val _ = mergesortn_side |> update_precondition;
val r = translate mergesortTheory.mergesort_def;
val r = translate mergesortTheory.sort2_tail_def;
val r = translate mergesortTheory.sort3_tail_def;
val r = translate mergesortTheory.merge_tail_def;
val r = translate (mergesortTheory.mergesortN_tail_def |> SIMP_RULE std_ss [DIV2_def]);
Triviality mergesortn_tail_ind:
mergesortn_tail_ind (:'a)
Proof
once_rewrite_tac [fetch "-" "mergesortn_tail_ind_def"]
\\ rpt gen_tac
\\ rpt (disch_then strip_assume_tac)
\\ match_mp_tac (latest_ind ())
\\ rpt strip_tac
\\ last_x_assum match_mp_tac
\\ rpt strip_tac
\\ gvs [FORALL_PROD, DIV2_def]
QED
val _ = mergesortn_tail_ind |> update_precondition;
Triviality mergesortn_tail_side:
∀w x y z.
mergesortn_tail_side w x y z
Proof
completeInduct_on`y`>>
rw[Once (fetch "-" "mergesortn_tail_side_def")]>>
simp[arithmeticTheory.DIV2_def]
>- (
first_x_assum match_mp_tac>>
simp[]>>
match_mp_tac dividesTheory.DIV_POS>>
simp[])
>>
match_mp_tac DIV_LESS_EQ>>
simp[]
QED
val _ = mergesortn_tail_side |> update_precondition;
val r = translate mergesortTheory.mergesort_tail_def;
val r = translate pbc_to_npbc_def;
val pbc_to_npbc_side = Q.prove(
`∀x. pbc_to_npbc_side x <=> T`,
EVAL_TAC>>rw[]>>
intLib.ARITH_TAC) |> update_precondition;
val r = translate parse_constraint_LHS_def;
val r = translate pbcTheory.map_pbc_def;
val r = translate map_f_ns_def;
val r = translate parse_constraint_npbc_def;
val r = translate parse_red_header_def;
val r = translate parse_constraint_npbc_2_def;
val r = translate strip_numbers_end_def;
val strip_numbers_end_side_def = fetch "-" "strip_numbers_end_side_def";
val strip_numbers_end_side = Q.prove(
`∀x y. strip_numbers_end_side x y <=> T`,
Induct>>rw[Once strip_numbers_end_side_def]>>
intLib.ARITH_TAC) |> update_precondition;
val r = translate parse_rup_def;
val r = translate parse_lstep_aux_def;
val parse_lstep_aux_side_def = fetch "-" "parse_lstep_aux_side_def";
val parse_lstep_aux_side = Q.prove(
`∀x y. parse_lstep_aux_side x y <=> T`,
rw[Once parse_lstep_aux_side_def]>>fs[]>>
intLib.ARITH_TAC) |> update_precondition;
val r = translate check_end_def;
val check_end_side = Q.prove(
`∀x. check_end_side x <=> T`,
EVAL_TAC>>rw[]>>
intLib.ARITH_TAC) |> update_precondition;
val r = translate blanks_def;
open mlintTheory;
(* TODO: Mostly copied from mlintTheory *)
val result = translate (fromChar_unsafe_def |> REWRITE_RULE [GSYM ml_translatorTheory.sub_check_def]);
Definition fromChars_range_unsafe_tail_def:
fromChars_range_unsafe_tail b n str mul acc =
if n ≤ b then acc
else
let m = n - 1 in
fromChars_range_unsafe_tail b m str (mul * 10)
(acc + fromChar_unsafe (strsub str m) * mul)
Termination
WF_REL_TAC`measure (λ(b,n,_). n)`>>
rw[]
End
Theorem fromChars_range_unsafe_tail_eq:
∀n l s mul acc.
fromChars_range_unsafe_tail l (n+l) s mul acc =
(fromChars_range_unsafe l n s) * mul + acc
Proof
Induct
>-
rw[Once fromChars_range_unsafe_tail_def,fromChars_range_unsafe_def]>>
rw[]>>
simp[Once fromChars_range_unsafe_tail_def,ADD1,fromChars_range_unsafe_def]>>
fs[ADD1]
QED
Theorem fromChars_range_unsafe_alt:
fromChars_range_unsafe l n s =
fromChars_range_unsafe_tail l (n+l) s 1 0
Proof
rw[fromChars_range_unsafe_tail_eq]
QED
val result = translate fromChars_range_unsafe_tail_def;
val fromchars_range_unsafe_tail_side_def = theorem"fromchars_range_unsafe_tail_side_def";
Theorem fromchars_range_unsafe_tail_side_def[allow_rebind]:
∀a1 a0 a2 a3 a4.
fromchars_range_unsafe_tail_side a0 a1 a2 a3 a4 ⇔
¬(a1 ≤ a0) ⇒
(T ∧ a1 < 1 + strlen a2 ∧ 0 < strlen a2) ∧
fromchars_range_unsafe_tail_side a0 (a1 − 1) a2 (a3 * 10)
(a4 + fromChar_unsafe (strsub a2 (a1 − 1)) * a3)
Proof
Induct>>
rw[Once fromchars_range_unsafe_tail_side_def]>>
simp[]>>eq_tac>>rw[ADD1]>>
gvs[]
QED
val result = translate fromChars_range_unsafe_alt;
val res = translate_no_ind (mlintTheory.fromChars_unsafe_def
|> REWRITE_RULE[maxSmall_DEC_def,padLen_DEC_eq]);
Triviality fromChars_unsafe_ind:
fromchars_unsafe_ind
Proof
rewrite_tac [fetch "-" "fromchars_unsafe_ind_def"]
\\ rpt gen_tac
\\ rpt (disch_then strip_assume_tac)
\\ match_mp_tac (latest_ind ())
\\ rpt strip_tac
\\ last_x_assum match_mp_tac
\\ rpt strip_tac
\\ fs [FORALL_PROD]
\\ fs [padLen_DEC_eq,ADD1]
QED
val _ = fromChars_unsafe_ind |> update_precondition;
val result = translate pb_parseTheory.fromString_unsafe_def;
val fromstring_unsafe_side_def = definition"fromstring_unsafe_side_def";
val fromchars_unsafe_side_def = theorem"fromchars_unsafe_side_def";
val fromchars_range_unsafe_side_def = fetch "-" "fromchars_range_unsafe_side_def";
Theorem fromchars_unsafe_side_thm:
∀n s. n ≤ LENGTH s ⇒ fromchars_unsafe_side n (strlit s)
Proof
completeInduct_on`n` \\ rw[]
\\ rw[Once fromchars_unsafe_side_def,fromchars_range_unsafe_side_def,fromchars_range_unsafe_tail_side_def]
QED
val fromString_unsafe_side = Q.prove(
`∀x. fromstring_unsafe_side x = T`,
Cases
\\ rw[fromstring_unsafe_side_def]
\\ Cases_on`s` \\ fs[mlstringTheory.substring_def]
\\ simp_tac bool_ss [ONE,SEG_SUC_CONS,SEG_LENGTH_ID]
\\ match_mp_tac fromchars_unsafe_side_thm
\\ rw[]) |> update_precondition;
val _ = translate is_numeric_def;
val _ = translate is_num_prefix_def;
val _ = translate int_start_def;
val _ = translate tokenize_fast_def;
Definition not_is_empty_def:
not_is_empty v ⇔ v ≠ []
End
val _ = translate not_is_empty_def;
val parse_lsteps_aux = process_topdecs`
fun parse_lsteps_aux f_ns fd lno acc =
case TextIO.b_inputLineTokens #"\n" fd blanks tokenize_fast of
None => raise Fail (format_failure lno "reached EOF while reading PBP steps")
| Some s =>
case parse_lstep_aux f_ns s of
None => (List.rev acc,(f_ns,(s,lno+1)))
| Some (Inl step,f_ns') =>
parse_lsteps_aux f_ns' fd (lno+1) (step::acc)
| Some (Inr (c,s),f_ns') =>
if not_is_empty s then
raise Fail (format_failure (lno+1) "only contradiction steps allowed in nested proof steps")
else
(case parse_lsteps_aux f_ns' fd (lno+1) [] of
(pf,(f_ns'',(s,lno'))) =>
case check_end s of
None => raise Fail (format_failure lno' "subproof not terminated with contradiction id")
| Some id =>
parse_lsteps_aux f_ns'' fd (lno'+1) (Con c pf id::acc))`
|> append_prog;
val blanks_v_thm = theorem "blanks_v_thm";
val tokenize_fast_v_thm = theorem "tokenize_fast_v_thm";
val b_inputLineTokens_specialize =
b_inputLineTokens_spec_lines
|> Q.GEN `f` |> Q.SPEC`blanks`
|> Q.GEN `fv` |> Q.SPEC`blanks_v`
|> Q.GEN `g` |> Q.ISPEC`tokenize_fast`
|> Q.GEN `gv` |> Q.ISPEC`tokenize_fast_v`
|> Q.GEN `a` |> Q.ISPEC`SUM_TYPE STRING_TYPE INT`
|> SIMP_RULE std_ss [blanks_v_thm,tokenize_fast_v_thm,blanks_def] ;
Theorem EqualityType_SUM_TYPE:
EqualityType t1 ∧ EqualityType t2 ⇒
EqualityType (SUM_TYPE t1 t2)
Proof
EVAL_TAC>>rw[]
>- (
Cases_on`x1`>>fs[SUM_TYPE_def]>>
simp[no_closures_def]>>
metis_tac[])
>- (
Cases_on`x1`>>Cases_on`x2`>>
fs[SUM_TYPE_def])>>
Cases_on`x1`>>Cases_on`x2`>>
fs[SUM_TYPE_def]>>
EVAL_TAC>>
metis_tac[]
QED
Theorem EqualityType_PBC_LIT_TYPE:
EqualityType a ⇒
EqualityType (PBC_LIT_TYPE a)
Proof
EVAL_TAC>>
rw[]>>
Cases_on`x1`>>fs[PBC_LIT_TYPE_def]>>
TRY(Cases_on`x2`>>fs[PBC_LIT_TYPE_def])>>
EVAL_TAC>>
metis_tac[]
QED
Theorem STDIO_INSTREAM_LINES_refl:
STDIO A *
INSTREAM_LINES c B C D E ==>>
STDIO A *
INSTREAM_LINES c B C D E
Proof
xsimpl
QED
Theorem STDIO_INSTREAM_LINES_refl_gc:
STDIO A *
INSTREAM_LINES c B C D E ==>>
STDIO A *
INSTREAM_LINES c B C D E * GC
Proof
xsimpl
QED
Theorem not_is_empty_eq:
not_is_empty v ⇔
¬is_empty v
Proof
EVAL_TAC>>
Cases_on`v`>>fs[]>>
EVAL_TAC>>
simp[]
QED
Overload "fns_TYPE" = ``λa. PAIR_TYPE (STRING_TYPE --> a --> OPTION_TYPE (PAIR_TYPE NUM a)) a``
Theorem parse_lsteps_aux_spec:
∀fns ss acc fd fdv lines lno lnov accv fs fnsv.
fns_TYPE a fns fnsv ∧
NUM lno lnov ∧
LIST_TYPE (NPBC_CHECK_LSTEP_TYPE) acc accv ∧
MAP toks_fast lines = ss
⇒
app (p : 'ffi ffi_proj)
^(fetch_v "parse_lsteps_aux" (get_ml_prog_state()))
[fnsv; fdv; lnov; accv]
(STDIO fs * INSTREAM_LINES #"\n" fd fdv lines fs)
(POSTve
(λv.
SEP_EXISTS k lines' acc' fns' s lno' rest.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k) *
&(
PAIR_TYPE (LIST_TYPE (NPBC_CHECK_LSTEP_TYPE))
(PAIR_TYPE (fns_TYPE a)
(PAIR_TYPE (LIST_TYPE (SUM_TYPE STRING_TYPE INT))
NUM)) (acc',fns',s,lno') v ∧
parse_lsteps_aux fns ss acc = SOME(acc',fns',s,rest) ∧
MAP toks_fast lines' = rest))
(λe.
SEP_EXISTS k lines'.
STDIO (forwardFD fs fd k) * INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k) *
&(Fail_exn e ∧ parse_lsteps_aux fns ss acc = NONE))
)
Proof
ho_match_mp_tac parse_lsteps_aux_ind>>
rw[]
>- (
xcf "parse_lsteps_aux" (get_ml_prog_state ())>>
xlet ‘(POSTv v.
SEP_EXISTS k.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv [] (forwardFD fs fd k) *
&OPTION_TYPE (LIST_TYPE (SUM_TYPE STRING_TYPE INT)) NONE v)’
THEN1 (
xapp_spec b_inputLineTokens_specialize
\\ qexists_tac ‘emp’
\\ qexists_tac ‘[]’
\\ xsimpl
\\ metis_tac[STDIO_INSTREAM_LINES_refl,STDIO_INSTREAM_LINES_refl_gc])>>
fs[OPTION_TYPE_def]>>
xmatch >>
rpt xlet_autop>>
xraise>>xsimpl>>
simp[Once parse_lsteps_aux_thm]>>
simp[Once parse_lsteps_aux_thm]>>
simp[Fail_exn_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])
>- (
xcfs ["parse_lsteps_aux"] (get_ml_prog_state ())>>
`?l ls. lines = l::ls` by
(Cases_on`lines`>>fs[])>>
rw[]>>
fs[]>>
xlet ‘(POSTv v.
SEP_EXISTS k.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv ls (forwardFD fs fd k) *
& OPTION_TYPE (LIST_TYPE (SUM_TYPE STRING_TYPE INT)) (SOME (toks_fast l)) v)’
THEN1 (
xapp_spec b_inputLineTokens_specialize
\\ qexists_tac ‘emp’
\\ qexists_tac ‘l::ls’
\\ qexists_tac ‘fs’
\\ qexists_tac ‘fd’ \\ xsimpl \\ fs []
\\ rw [] \\ qexists_tac ‘x’ \\ xsimpl
\\ simp[toks_fast_def]) >>
fs[OPTION_TYPE_def]>>
xmatch>>
xlet_autop>>
simp[Once parse_lsteps_aux_thm]>>
TOP_CASE_TAC>>fs[OPTION_TYPE_def]
>- ((* parse_lstep_aux gives None *)
xmatch>>
rpt xlet_autop>>
xcon>>
xsimpl>- (
simp[PAIR_TYPE_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
simp[Once parse_lsteps_aux_thm])>>
(* parse_lstep_aux gives Some *)
TOP_CASE_TAC>>fs[]>>
TOP_CASE_TAC>>fs[PAIR_TYPE_def,SUM_TYPE_def]
(* INL *)
>- (
xmatch>>
rpt xlet_autop>>
xapp>>
first_x_assum (irule_at Any)>>simp[]>>
first_x_assum (irule_at Any)>>simp[LIST_TYPE_def]>>
xsimpl>>
qexists_tac`forwardFD fs fd k`>>xsimpl>>
qexists_tac`fd`>>qexists_tac`emp`>>xsimpl>>
rw[]
>- (
fs[PAIR_TYPE_def]>>
simp[forwardFD_o]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
simp[forwardFD_o]>>
fs[Once parse_lsteps_aux_thm]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
(* INR *)
TOP_CASE_TAC>>fs[]>>
qpat_x_assum`PAIR_TYPE _ _ _ _` mp_tac>>
simp[Once PAIR_TYPE_def]>>
strip_tac>>
xmatch>>
xlet_auto >- (
xsimpl>>
simp (eq_lemmas()))>>
rename1`is_empty tt`>>
reverse (Cases_on`is_empty tt`>>fs[not_is_empty_eq])
>- (
xif>>asm_exists_tac>>xsimpl>>
rpt xlet_autop>>
xraise>>xsimpl>>
simp[Once parse_lsteps_aux_thm,Fail_exn_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
xif>>asm_exists_tac>>xsimpl>>
rpt xlet_autop>>
xlet`(POSTve
(λv.
SEP_EXISTS k' lines' acc' fns' s' lno' rest.
STDIO (forwardFD (forwardFD fs fd k) fd k') *
INSTREAM_LINES #"\n" fd fdv lines'
(forwardFD (forwardFD fs fd k) fd k') *
&(
PAIR_TYPE (LIST_TYPE (NPBC_CHECK_LSTEP_TYPE))
(PAIR_TYPE (fns_TYPE a)
(PAIR_TYPE (LIST_TYPE (SUM_TYPE STRING_TYPE INT))
NUM)) (acc',fns',s',lno') v ∧
parse_lsteps_aux r ss [] = SOME(acc',fns',s',rest) ∧
MAP toks_fast lines' = rest))
(λe.
SEP_EXISTS k' lines'.
STDIO (forwardFD (forwardFD fs fd k) fd k') *
INSTREAM_LINES #"\n" fd fdv lines'
(forwardFD (forwardFD fs fd k) fd k') *
&(Fail_exn e ∧ parse_lsteps_aux r ss [] = NONE)))`
>- (
first_x_assum xapp_spec>>
simp[LIST_TYPE_def]>>
asm_exists_tac>>simp[PULL_EXISTS]>>
asm_exists_tac>>simp[]>>
xsimpl>>
qexists_tac`emp`>>qexists_tac`(forwardFD fs fd k)`>>
qexists_tac`fd`>>
xsimpl>>
rw[]>>
simp[PAIR_TYPE_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])
>- (
xsimpl>>
rw[]>>simp[Once parse_lsteps_aux_thm,forwardFD_o]>>
metis_tac[STDIO_INSTREAM_LINES_refl])>>
qpat_x_assum`PAIR_TYPE _ _ _ _` mp_tac>>
simp[Once PAIR_TYPE_def]>>
simp[Once PAIR_TYPE_def]>>
simp[Once PAIR_TYPE_def]>>
strip_tac>>
xmatch>>
xlet_autop>>
Cases_on`check_end s'`>>fs[OPTION_TYPE_def]>>
xmatch
>- (
rpt xlet_autop>>
xraise>>
xsimpl >>
simp[Once parse_lsteps_aux_thm,forwardFD_o,Fail_exn_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
rpt xlet_autop>>
last_x_assum xapp_spec>>
xsimpl>>
first_x_assum (irule_at Any)>>simp[]>>
qexists_tac`lines'`>>
simp[LIST_TYPE_def,NPBC_CHECK_LSTEP_TYPE_def]>>
`LENGTH lines' < LENGTH ss` by (
imp_res_tac parse_lsteps_aux_LENGTH>>
metis_tac[LENGTH_MAP])>>
simp[forwardFD_o]>>
qexists_tac`forwardFD fs fd (k + k')`>>
qexists_tac`fd`>>qexists_tac`emp`>>
xsimpl>>
rw[]
>-
fs[]
>- (
fs[PAIR_TYPE_def]>>
asm_exists_tac>>xsimpl>>
simp[forwardFD_o]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
simp[Once parse_lsteps_aux_thm]>>
simp[forwardFD_o]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])
QED
val r = translate (parse_hash_num_def |> ONCE_REWRITE_RULE [GSYM sub_check_def]);
val parse_hash_num_side_def = fetch "-" "parse_hash_num_side_def";
val parse_hash_num_side = Q.prove(
`∀x . parse_hash_num_side x <=> T`,
Induct>>rw[Once parse_hash_num_side_def,sub_check_def]
) |> update_precondition;
val r = translate parse_subgoal_num_def;
val parse_subgoal_num_side_def = fetch "-" "parse_subgoal_num_side_def";
val parse_subgoal_num_side = Q.prove(
`∀x . parse_subgoal_num_side x <=> T`,
Induct>>rw[Once parse_subgoal_num_side_def]>>
intLib.ARITH_TAC) |> update_precondition;
val r = translate parse_proofgoal_def;
val r = translate check_end_opt_def;
val check_end_opt_side = Q.prove(
`∀x. check_end_opt_side x <=> T`,
EVAL_TAC>>rw[]>>
intLib.ARITH_TAC) |> update_precondition;
val r = translate parse_red_body_def;
val r = translate mk_acc_def;
val parse_red_aux = process_topdecs`
fun parse_red_aux f_ns fd lno acc =
case parse_lsteps_aux f_ns fd lno [] of
(pf,(f_ns',(s,lno'))) =>
let val acc' = mk_acc pf acc in
case parse_red_body s of
None => raise Fail (format_failure lno' "unable to parse subproof")
| Some (Inl res) => (res,(List.rev acc', (f_ns', lno')))
| Some (Inr ind) =>
(case parse_lsteps_aux f_ns' fd lno' [] of
(pf,(f_ns'',(s,lno''))) =>
case check_end s of
None => raise Fail (format_failure lno'' "subproof not terminated with contradiction id")
| Some id =>
parse_red_aux f_ns'' fd lno'' ((Some (ind,id),pf)::acc')
)
end` |> append_prog
Theorem parse_red_aux_spec:
∀fns ss acc fd fdv lines lno lnov accv fs fnsv.
fns_TYPE a fns fnsv ∧
NUM lno lnov ∧
LIST_TYPE (PAIR_TYPE (OPTION_TYPE (PAIR_TYPE (SUM_TYPE NUM NUM) NUM)) (LIST_TYPE (NPBC_CHECK_LSTEP_TYPE))) acc accv ∧
MAP toks_fast lines = ss
⇒
app (p : 'ffi ffi_proj)
^(fetch_v "parse_red_aux" (get_ml_prog_state()))
[fnsv; fdv; lnov; accv]
(STDIO fs * INSTREAM_LINES #"\n" fd fdv lines fs)
(POSTve
(λv.
SEP_EXISTS k lines' res acc' fns' s lno' rest.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k) *
&(
PAIR_TYPE (OPTION_TYPE NUM)
(PAIR_TYPE (LIST_TYPE (PAIR_TYPE (OPTION_TYPE (PAIR_TYPE (SUM_TYPE NUM NUM) NUM)) (LIST_TYPE (NPBC_CHECK_LSTEP_TYPE))))
(PAIR_TYPE (fns_TYPE a)
NUM)) (res,acc',fns',lno') v ∧
parse_red_aux fns ss acc = SOME(res,acc',fns',rest) ∧
MAP toks_fast lines' = rest))
(λe.
SEP_EXISTS k lines'.
STDIO (forwardFD fs fd k) * INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k) *
&(Fail_exn e ∧ parse_red_aux fns ss acc = NONE))
)
Proof
ho_match_mp_tac parse_red_aux_ind>>
rw[]>>
simp[Once parse_red_aux_def]>>
xcf "parse_red_aux" (get_ml_prog_state ())>>
xlet_autop>>
xlet`(POSTve
(λv.
SEP_EXISTS k lines' acc' fns' s lno' rest.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k) *
&(
PAIR_TYPE (LIST_TYPE (NPBC_CHECK_LSTEP_TYPE))
(PAIR_TYPE (fns_TYPE a)
(PAIR_TYPE (LIST_TYPE (SUM_TYPE STRING_TYPE INT))
NUM)) (acc',fns',s,lno') v ∧
parse_lsteps_aux fns (MAP toks_fast lines) [] = SOME(acc',fns',s,rest) ∧
MAP toks_fast lines' = rest))
(λe.
SEP_EXISTS k lines'.
STDIO (forwardFD fs fd k) * INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k) *
&(Fail_exn e ∧ parse_lsteps_aux fns (MAP toks_fast lines) [] = NONE))
)`
>- (
xapp>>xsimpl>>
simp[LIST_TYPE_def]>>
metis_tac[])
>- (
xsimpl>>
rw[]>>
simp[Once parse_red_aux_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl])>>
simp[]>>
fs[PAIR_TYPE_def]>>
xmatch>>
rpt xlet_autop>>
Cases_on`parse_red_body s`>>fs[OPTION_TYPE_def]
>- (
xmatch>>
xlet_autop>>
xlet_autop>>
xraise>>xsimpl>>
simp[Once parse_red_aux_def,Fail_exn_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
TOP_CASE_TAC>>fs[SUM_TYPE_def]
>- ( (* INL*)
xmatch>>
rpt xlet_autop>>
xcon>>xsimpl
>- metis_tac[STDIO_INSTREAM_LINES_refl_gc]>>
rw[]>>
gs[Once parse_red_aux_def])
>- ( (* INR*)
xmatch>>
xlet_autop>>
xlet`(POSTve
(λv.
SEP_EXISTS k lines'' acc' fns'' s lno' rest'.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv lines'' (forwardFD fs fd k) *
&(
PAIR_TYPE (LIST_TYPE (NPBC_CHECK_LSTEP_TYPE))
(PAIR_TYPE (fns_TYPE a)
(PAIR_TYPE (LIST_TYPE (SUM_TYPE STRING_TYPE INT))
NUM))
(acc',fns'',s,lno') v ∧
parse_lsteps_aux fns' rest [] = SOME(acc',fns'',s,rest') ∧
MAP toks_fast lines'' = rest'))
(λe.
SEP_EXISTS k lines''.
STDIO (forwardFD fs fd k) * INSTREAM_LINES #"\n" fd fdv lines'' (forwardFD fs fd k) *
&(Fail_exn e ∧ parse_lsteps_aux fns' rest [] = NONE))
)`
>- (
xapp>>xsimpl>>
asm_exists_tac>>xsimpl>>
qexists_tac`emp`>>qexists_tac`lines'`>>
qexists_tac`forwardFD fs fd k`>>
first_x_assum (irule_at Any)>>
qexists_tac`fd`>>xsimpl>>
qexists_tac`[]`>>simp[LIST_TYPE_def,PAIR_TYPE_def]>>
rw[]
>-(
simp[forwardFD_o]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
simp[forwardFD_o]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])
>- (
xsimpl>>rw[]>>
simp[Once parse_red_aux_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl])>>
fs[PAIR_TYPE_def]>>
xmatch>>
xlet_autop>>
Cases_on`check_end s'`>>
fs[OPTION_TYPE_def]>>xmatch
>- (
rpt xlet_autop>>
xraise>>
xsimpl>>
simp[Once parse_red_aux_def,Fail_exn_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
rpt xlet_autop>>
xapp>>
simp[LIST_TYPE_def,PAIR_TYPE_def,OPTION_TYPE_def]>>
first_x_assum (irule_at Any)>>
first_x_assum (irule_at Any)>>
xsimpl>>
qexists_tac`forwardFD fs fd k`>>
qexists_tac`fd`>>qexists_tac`emp`>>xsimpl>>
rw[]
>- (
simp[forwardFD_o]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
simp[Once parse_red_aux_def]>>
simp[forwardFD_o]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])
QED
Theorem is_empty_thm:
is_empty v ⇔
case v of [] => T | _ => F
Proof
EVAL_TAC>>Cases_on`v`>>fs[mlvectorTheory.length_def]
QED
val res = translate is_empty_thm;
val res = translate reduce_pf_def;
val parse_sstep = process_topdecs`
fun parse_sstep fns fd lno =
case TextIO.b_inputLineTokens #"\n" fd blanks tokenize_fast of
None =>
raise Fail (format_failure lno "Unexpected EOF when parsing proof steps")
| Some s =>
(case parse_lstep_aux fns s of
None => (Inl s, (fns, lno+1))
| Some (Inl step,fns') => (Inr (Lstep step),(fns',lno+1))
| Some (Inr (c,s),fns') =>
case parse_red_aux fns' fd (lno+1) [] of
(res,(pf,(fns'',lno'))) =>
(case reduce_pf s pf res of
None => (Inr (Red c s pf res),(fns'',lno'))
| Some ((pf,n)) => (Inr (Lstep (Con c pf n)), (fns'', lno')))
)` |> append_prog
Theorem parse_sstep_spec:
!ss fd fdv lines lno lnov fs fns fnsv.
fns_TYPE a fns fnsv ∧
NUM lno lnov ∧
MAP toks_fast lines = ss
⇒
app (p : 'ffi ffi_proj)
^(fetch_v "parse_sstep" (get_ml_prog_state()))
[fnsv; fdv; lnov]
(STDIO fs * INSTREAM_LINES #"\n" fd fdv lines fs)
(POSTve
(λv.
SEP_EXISTS k lines' lno'.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k) *
&(
case parse_sstep fns ss of
NONE => F
| SOME (res,fns',rest) =>
(PAIR_TYPE
(SUM_TYPE (LIST_TYPE (SUM_TYPE STRING_TYPE INT)) NPBC_CHECK_SSTEP_TYPE)
(PAIR_TYPE
(fns_TYPE a)
NUM)) (res,fns',lno') v ∧
MAP toks_fast lines' = rest))
(λe.
SEP_EXISTS k lines'.
STDIO (forwardFD fs fd k) * INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k) *
&(Fail_exn e ∧ parse_sstep fns ss = NONE))
)
Proof
rpt strip_tac>>
xcf "parse_sstep" (get_ml_prog_state ())>>
Cases_on`ss`>>simp[parse_sstep_def]
>- (
xlet ‘(POSTv v.
SEP_EXISTS k.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv [] (forwardFD fs fd k) *
&OPTION_TYPE (LIST_TYPE (SUM_TYPE STRING_TYPE INT)) NONE v)’
>- (
xapp_spec b_inputLineTokens_specialize
\\ qexists_tac ‘emp’
\\ qexists_tac ‘lines’
\\ qexists_tac ‘fs’
\\ qexists_tac ‘fd’ \\ xsimpl
\\ Cases_on`lines` \\ fs[OPTION_TYPE_def]
\\ metis_tac[STDIO_INSTREAM_LINES_refl_gc]) >>
fs[OPTION_TYPE_def]>>
xmatch>>
rpt xlet_autop>>
xraise>>
xsimpl>>
metis_tac[Fail_exn_def,STDIO_INSTREAM_LINES_refl_gc])>>
`?l ls. lines = l::ls` by
(Cases_on`lines`>>fs[])>>
rw[]>>
fs[]>>
xlet ‘(POSTv v.
SEP_EXISTS k.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv ls (forwardFD fs fd k) *
& OPTION_TYPE (LIST_TYPE (SUM_TYPE STRING_TYPE INT)) (SOME (toks_fast l)) v)’
THEN1 (
xapp_spec b_inputLineTokens_specialize
\\ qexists_tac ‘emp’
\\ qexists_tac ‘l::ls’
\\ qexists_tac ‘fs’
\\ qexists_tac ‘fd’ \\ xsimpl \\ fs []
\\ rw [] \\ qexists_tac ‘x’ \\ xsimpl
\\ simp[toks_fast_def]) >>
fs[OPTION_TYPE_def]>>
xmatch>>
xlet_autop>>
Cases_on`parse_lstep_aux fns h`>>fs[OPTION_TYPE_def]
>- (
xmatch>>
rpt xlet_autop>>
xcon>>
xsimpl>>
simp[PAIR_TYPE_def,SUM_TYPE_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
PairCases_on`x`>>
Cases_on`x0`>>
fs[PAIR_TYPE_def,SUM_TYPE_def]
>- ( (* INL*)
xmatch>>
rpt xlet_autop>>
xcon>>xsimpl>>
simp[PAIR_TYPE_def,NPBC_CHECK_SSTEP_TYPE_def]>>
metis_tac[ STDIO_INSTREAM_LINES_refl_gc])>>
(* INR *)
Cases_on`y`>>
fs[PAIR_TYPE_def]>>
xmatch>>
rpt xlet_autop>>
xlet`(POSTve
(λv.
SEP_EXISTS k lines' res acc' fns' s lno' rest.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k) *
&(
PAIR_TYPE (OPTION_TYPE NUM)
(PAIR_TYPE (LIST_TYPE (PAIR_TYPE (OPTION_TYPE (PAIR_TYPE (SUM_TYPE NUM NUM) NUM)) (LIST_TYPE (NPBC_CHECK_LSTEP_TYPE))))
(PAIR_TYPE (fns_TYPE a)
NUM)) (res,acc',fns',lno') v ∧
parse_red_aux (x1,x2) t [] = SOME(res,acc',fns',rest) ∧
MAP toks_fast lines' = rest))
(λe.
SEP_EXISTS k lines'.
STDIO (forwardFD fs fd k) * INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k) *
&(Fail_exn e ∧ parse_red_aux (x1,x2) t [] = NONE))
)`
>- (
xapp>>xsimpl>>
asm_exists_tac>>xsimpl>>
qexists_tac`emp`>>qexists_tac`ls`>>
qexists_tac`forwardFD fs fd k`>>
qexists_tac`(x1,x2)`>>xsimpl>>
qexists_tac`fd`>>xsimpl>>
qexists_tac`[]`>>simp[LIST_TYPE_def,PAIR_TYPE_def]>>
asm_exists_tac>>xsimpl>>
rw[]
>-(
simp[forwardFD_o]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])>>
simp[forwardFD_o]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])
>- (
xsimpl>>
metis_tac[STDIO_INSTREAM_LINES_refl] )>>
fs[PAIR_TYPE_def]>>
xmatch>>
rpt xlet_autop>>
every_case_tac>>fs[OPTION_TYPE_def,PAIR_TYPE_def]>>xmatch
>- (
rpt xlet_autop>>
xcon>>xsimpl>>
simp[NPBC_CHECK_SSTEP_TYPE_def,NPBC_CHECK_LSTEP_TYPE_def,SUM_TYPE_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])
>- (
rpt xlet_autop>>
xcon>>xsimpl>>
simp[NPBC_CHECK_SSTEP_TYPE_def,NPBC_CHECK_LSTEP_TYPE_def,SUM_TYPE_def]>>
metis_tac[STDIO_INSTREAM_LINES_refl_gc])
QED
val res = translate pb_parseTheory.parse_vars_line_aux_def;
val res = translate parse_load_order_def;
val res = translate hashString_nf_def;
val res = translate parse_vars_line_def;
val res = translate parse_vars_aux_def;
val read_n_lines = process_topdecs`
fun read_n_lines n fd lno =
if n = 0 then []
else
let val l = TextIO.b_inputLineTokens #"\n" fd blanks tokenize_fast in
case l of None =>
raise Fail (format_failure lno "Unexpected EOF when reading lines")
| Some l =>
l :: read_n_lines (n-1) fd (lno+1)
end` |> append_prog
Theorem read_n_lines_spec:
!n nv fs fd fdv lines lno lnov.
NUM n nv ∧
NUM lno lnov
⇒
app (p : 'ffi ffi_proj)
^(fetch_v "read_n_lines" (get_ml_prog_state()))
[nv; fdv;lnov]
(STDIO fs * INSTREAM_LINES #"\n" fd fdv lines fs)
(POSTve
(λv.
SEP_EXISTS k.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv (DROP n lines) (forwardFD fs fd k) *
&(
n ≤ LENGTH lines ∧
LIST_TYPE
(LIST_TYPE (SUM_TYPE STRING_TYPE INT))
(MAP