-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathlpr_arrayFullProgScript.sml
1797 lines (1707 loc) · 56.3 KB
/
lpr_arrayFullProgScript.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
(*
This builds the cake_lpr proof checker
*)
open preamble basis md5ProgTheory lpr_composeProgTheory UnsafeProofTheory lprTheory lpr_listTheory lpr_parsingTheory HashtableProofTheory lpr_arrayProgTheory lpr_arrayParsingProgTheory;
val _ = new_theory "lpr_arrayFullProg"
val _ = temp_delsimps ["NORMEQ_CONV"]
val _ = diminish_srw_ss ["ABBREV"]
val _ = set_trace "BasicProvers.var_eq_old" 1
val _ = translation_extends"lpr_arrayParsingProg";
val xlet_autop = xlet_auto >- (TRY( xcon) >> xsimpl)
val _ = translate parse_header_line_def;
val parse_header_line_side = Q.prove(`
∀x. parse_header_line_side x= T`,
rw[definition"parse_header_line_side_def"]>>
intLib.ARITH_TAC)
|> update_precondition;
val _ = translate parse_clause_aux_def;
val _ = translate parse_clause_def;
val _ = translate nocomment_line_def;
Definition format_dimacs_failure_def:
format_dimacs_failure (lno:num) s =
strlit "c DIMACS parse failed at line: " ^ toString lno ^ strlit ". Reason: " ^ s ^ strlit"\n"
End
val _ = translate format_dimacs_failure_def;
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`
|> Q.GEN `gv` |> Q.ISPEC`tokenize_v`
|> Q.GEN `a` |> Q.ISPEC`SUM_TYPE STRING_TYPE INT`
|> SIMP_RULE std_ss [blanks_v_thm,tokenize_v_thm,blanks_def] ;
val parse_dimacs_body_arr = process_topdecs`
fun parse_dimacs_body_arr lno maxvar fd acc =
case TextIO.b_inputLineTokens #"\n" fd blanks tokenize of
None => Inr (List.rev acc)
| Some l =>
if nocomment_line l then
(case parse_clause maxvar l of
None => Inl (format_dimacs_failure lno "failed to parse line")
| Some cl => parse_dimacs_body_arr (lno+1) maxvar fd (cl::acc))
else parse_dimacs_body_arr (lno+1) maxvar fd acc` |> append_prog;
Theorem parse_dimacs_body_arr_spec:
!lines fd fdv fs maxvar maxvarv acc accv lno lnov.
NUM lno lnov ∧
NUM maxvar maxvarv ∧
LIST_TYPE (LIST_TYPE INT) acc accv
⇒
app (p : 'ffi ffi_proj)
^(fetch_v "parse_dimacs_body_arr" (get_ml_prog_state()))
[lnov; maxvarv; fdv; accv]
(STDIO fs * INSTREAM_LINES #"\n" fd fdv lines fs)
(POSTv v.
& (∃err. SUM_TYPE STRING_TYPE (LIST_TYPE (LIST_TYPE INT))
(case parse_dimacs_body maxvar (FILTER nocomment_line (MAP toks lines)) acc of
NONE => INL err
| SOME x => INR x) v) *
SEP_EXISTS k lines'.
STDIO (forwardFD fs fd k) * INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k))
Proof
Induct
\\ simp []
\\ rpt strip_tac
\\ xcf "parse_dimacs_body_arr" (get_ml_prog_state ())
THEN1 (
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 ‘[]’
\\ qexists_tac ‘fs’
\\ qexists_tac ‘fd’ \\ xsimpl \\ fs [])
\\ fs [std_preludeTheory.OPTION_TYPE_def] \\ rveq \\ fs []
\\ xmatch \\ fs []
\\ simp[parse_dimacs_body_def]
\\ xlet_autop
\\ xcon \\ xsimpl
\\ simp[SUM_TYPE_def]
\\ qexists_tac ‘k’ \\ xsimpl
\\ qexists_tac `[]` \\ xsimpl)
\\ xlet ‘(POSTv v.
SEP_EXISTS k.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv lines (forwardFD fs fd k) *
& OPTION_TYPE (LIST_TYPE (SUM_TYPE STRING_TYPE INT)) (SOME (toks h)) v)’
THEN1 (
xapp_spec b_inputLineTokens_specialize
\\ qexists_tac `emp`
\\ qexists_tac ‘h::lines’
\\ qexists_tac ‘fs’
\\ qexists_tac ‘fd’ \\ xsimpl \\ fs []
\\ rw [] \\ qexists_tac ‘x’ \\ xsimpl
\\ simp[toks_def])
\\ fs [std_preludeTheory.OPTION_TYPE_def] \\ rveq \\ fs []
\\ xmatch \\ fs []
\\ xlet_autop
\\ reverse IF_CASES_TAC
>- (
xif >> asm_exists_tac>>xsimpl>>
xlet_autop>>
xapp>> xsimpl>>
asm_exists_tac>> simp[]>>
asm_exists_tac>> simp[]>>
qexists_tac`emp`>>xsimpl>>
qexists_tac`forwardFD fs fd k`>>
qexists_tac`fd`>>xsimpl>>
qexists_tac`acc`>>xsimpl>>
rw[]>>
qexists_tac`k+x`>>
simp[GSYM fsFFIPropsTheory.forwardFD_o]>>
qexists_tac`x'`>>xsimpl>>
metis_tac[])>>
xif>> asm_exists_tac>>simp[]>>
xlet_autop>>
simp[parse_dimacs_body_def]>>
Cases_on`parse_clause maxvar (toks h)`>>fs[OPTION_TYPE_def]
>- (
xmatch>>
xlet_autop>>
xcon>>
xsimpl>>
qexists_tac`k`>> qexists_tac`lines`>>xsimpl>>
simp[SUM_TYPE_def]>>
metis_tac[])>>
xmatch>>
xlet_autop>>
xlet_autop>>
xapp>>
xsimpl>>
asm_exists_tac>>simp[]>>
asm_exists_tac>>simp[]>>
qexists_tac`emp`>>
qexists_tac`forwardFD fs fd k`>>
qexists_tac`fd`>>
qexists_tac`x::acc`>>
xsimpl>>
simp[LIST_TYPE_def]>>rw[]>>
qexists_tac`k+x'`>>
qexists_tac`x''`>>
simp[GSYM fsFFIPropsTheory.forwardFD_o]>>
xsimpl>>
metis_tac[]
QED
val parse_dimacs_toks_arr = process_topdecs`
fun parse_dimacs_toks_arr lno fd =
case TextIO.b_inputLineTokens #"\n" fd blanks tokenize of
None => Inl (format_dimacs_failure lno "failed to find header")
| Some l =>
if nocomment_line l then
(case parse_header_line l of
None => Inl (format_dimacs_failure lno "failed to parse header")
| Some res => case res of (vars,clauses) =>
(case parse_dimacs_body_arr lno vars fd [] of
Inl fail => Inl fail
| Inr acc =>
if List.length acc = clauses then
Inr (vars,(clauses,acc))
else
Inl (format_dimacs_failure lno "incorrect number of clauses")))
else parse_dimacs_toks_arr (lno+1) fd` |> append_prog;
Theorem parse_dimacs_toks_arr_spec:
!lines fd fdv fs lno lnov.
NUM lno lnov
⇒
app (p : 'ffi ffi_proj)
^(fetch_v "parse_dimacs_toks_arr" (get_ml_prog_state()))
[lnov; fdv]
(STDIO fs * INSTREAM_LINES #"\n" fd fdv lines fs)
(POSTv v.
& (∃err. SUM_TYPE STRING_TYPE (PAIR_TYPE NUM (PAIR_TYPE NUM (LIST_TYPE (LIST_TYPE INT))))
(case parse_dimacs_toks (MAP toks lines) of
NONE => INL err
| SOME x => INR x) v) *
SEP_EXISTS k lines'.
STDIO (forwardFD fs fd k) * INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k))
Proof
Induct
\\ simp []
\\ rpt strip_tac
\\ xcf "parse_dimacs_toks_arr" (get_ml_prog_state ())
THEN1 (
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 ‘[]’
\\ qexists_tac ‘fs’
\\ qexists_tac ‘fd’ \\ xsimpl \\ fs [])
\\ fs [std_preludeTheory.OPTION_TYPE_def] \\ rveq \\ fs []
\\ xmatch \\ fs []
\\ simp[parse_dimacs_toks_def]
\\ xlet_autop
\\ xcon \\ xsimpl
\\ simp[SUM_TYPE_def]
\\ qexists_tac ‘k’ \\ xsimpl
\\ qexists_tac `[]` \\ xsimpl
\\ metis_tac[])
\\ xlet ‘(POSTv v.
SEP_EXISTS k.
STDIO (forwardFD fs fd k) *
INSTREAM_LINES #"\n" fd fdv lines (forwardFD fs fd k) *
& OPTION_TYPE (LIST_TYPE (SUM_TYPE STRING_TYPE INT)) (SOME (toks h)) v)’
THEN1 (
xapp_spec b_inputLineTokens_specialize
\\ qexists_tac `emp`
\\ qexists_tac ‘h::lines’
\\ qexists_tac ‘fs’
\\ qexists_tac ‘fd’ \\ xsimpl \\ fs []
\\ rw [] \\ qexists_tac ‘x’ \\ xsimpl
\\ simp[toks_def])
\\ fs [std_preludeTheory.OPTION_TYPE_def] \\ rveq \\ fs []
\\ xmatch \\ fs []
\\ xlet_autop
\\ simp[parse_dimacs_toks_def]
\\ reverse IF_CASES_TAC
>- (
xif >> asm_exists_tac>>xsimpl>>
xlet_autop>>
xapp>> xsimpl>>
asm_exists_tac>> simp[]>>
qexists_tac`emp`>>xsimpl>>
qexists_tac`forwardFD fs fd k`>>
qexists_tac`fd`>>xsimpl>>
rw[]>>
fs[parse_dimacs_toks_def]>>
qexists_tac`k+x`>>
simp[GSYM fsFFIPropsTheory.forwardFD_o]>>
qexists_tac`x'`>>xsimpl>>
metis_tac[])>>
xif>> asm_exists_tac>>simp[]>>
xlet_autop>>
Cases_on`parse_header_line (toks h)`>>fs[OPTION_TYPE_def]
>- (
xmatch>>
xlet_autop>>
xcon>>
xsimpl>>
qexists_tac`k`>> qexists_tac`lines`>>xsimpl>>
simp[SUM_TYPE_def]>>
metis_tac[])>>
xmatch>>
Cases_on`x`>>fs[PAIR_TYPE_def]>>
xmatch>>
xlet_autop>>
xlet `(POSTv v.
& (∃err. SUM_TYPE STRING_TYPE (LIST_TYPE (LIST_TYPE INT))
(case parse_dimacs_body q (FILTER nocomment_line (MAP toks lines)) [] of
NONE => INL err
| SOME x => INR x) v) *
SEP_EXISTS k lines'.
STDIO (forwardFD fs fd k) * INSTREAM_LINES #"\n" fd fdv lines' (forwardFD fs fd k))`
>- (
xapp>>xsimpl>>
qexists_tac`emp`>>
asm_exists_tac>>simp[]>>
asm_exists_tac>>simp[]>>
qexists_tac`lines`>>
qexists_tac`forwardFD fs fd k`>>
qexists_tac`fd`>>xsimpl>>
qexists_tac`[]`>>simp[LIST_TYPE_def]>>
rw[]>>
qexists_tac`k+x`>>
simp[GSYM fsFFIPropsTheory.forwardFD_o]>>
qexists_tac`x'`>>xsimpl>>
metis_tac[])>>
pop_assum mp_tac>> TOP_CASE_TAC>>fs[OPTION_TYPE_def]
>- (
rw[]>>fs[SUM_TYPE_def]>>
xmatch>>
xcon>>
xsimpl>>
qexists_tac`k`>>qexists_tac`lines'`>>xsimpl>>
metis_tac[])>>
strip_tac>>fs[SUM_TYPE_def]>>
xmatch>>
xlet_autop>>
xlet_autop>>
drule LENGTH_parse_dimacs_body>>
strip_tac>>fs[]>>
rw[]>> xif
>- (
asm_exists_tac>>simp[]>>
xlet_autop>>
xlet_autop>>
xcon>>xsimpl>>
simp[SUM_TYPE_def,PAIR_TYPE_def]>>
qexists_tac`k`>>qexists_tac`lines'`>>xsimpl)>>
asm_exists_tac>>simp[]>>
xlet_autop>>
xcon>>
xsimpl>>
qexists_tac`k`>>
qexists_tac`lines'`>>
simp[SUM_TYPE_def,PAIR_TYPE_def]>>
xsimpl>>
metis_tac[]
QED
(* parse_dimacs_toks with simple wrapper *)
val parse_dimacs_full = (append_prog o process_topdecs) `
fun parse_dimacs_full fname =
let
val fd = TextIO.b_openIn fname
val res = parse_dimacs_toks_arr 0 fd
val close = TextIO.b_closeIn fd;
in
res
end
handle TextIO.BadFileName => Inl (notfound_string fname)`;
Definition get_fml_def:
get_fml fs f =
if inFS_fname fs f then
parse_dimacs_toks (MAP toks (all_lines fs f))
else NONE
End
Theorem parse_dimacs_full_spec:
STRING_TYPE f fv ∧
validArg f ∧
hasFreeFD fs
⇒
app (p:'ffi ffi_proj) ^(fetch_v"parse_dimacs_full"(get_ml_prog_state()))
[fv]
(STDIO fs)
(POSTv v.
& (∃err. (SUM_TYPE STRING_TYPE (PAIR_TYPE NUM (PAIR_TYPE NUM (LIST_TYPE (LIST_TYPE INT))))
(case get_fml fs f of
NONE => INL err
| SOME x => INR x)) v) * STDIO fs)
Proof
rw[]>>
xcf"parse_dimacs_full"(get_ml_prog_state()) >>
fs[validArg_def,get_fml_def]>>
reverse (Cases_on `STD_streams fs`)
>- (fs [TextIOProofTheory.STDIO_def] \\ xpull) >>
reverse (Cases_on`consistentFS fs`)
>- (fs [STDIO_def,IOFS_def,wfFS_def,consistentFS_def] \\ xpull \\ metis_tac[]) >>
reverse (Cases_on `inFS_fname fs f`) >> simp[]
>- (
xhandle`POSTe ev.
&BadFileName_exn ev *
&(~inFS_fname fs f) *
STDIO fs`
>-
(xlet_auto_spec (SOME b_openIn_STDIO_spec) \\ xsimpl)
>>
fs[BadFileName_exn_def]>>
xcases>>rw[]>>
xlet_auto>>xsimpl>>
xcon>>xsimpl>>
simp[SUM_TYPE_def]>>metis_tac[])>>
qmatch_goalsub_abbrev_tac`$POSTv Qval`>>
xhandle`$POSTv Qval` \\ xsimpl >>
qunabbrev_tac`Qval`>>
xlet_auto_spec (SOME (b_openIn_spec_lines |> Q.GEN`c0` |> Q.SPEC`#"\n"`)) \\ xsimpl >>
qmatch_goalsub_abbrev_tac`STDIO fss`>>
qmatch_goalsub_abbrev_tac`INSTREAM_LINES _ fdd fddv lines fss`>>
xlet`(POSTv v.
& (∃err. SUM_TYPE STRING_TYPE (PAIR_TYPE NUM (PAIR_TYPE NUM (LIST_TYPE (LIST_TYPE INT))))
(case parse_dimacs_toks (MAP toks lines) of
NONE => INL err
| SOME x => INR x) v) *
SEP_EXISTS k lines'.
STDIO (forwardFD fss fdd k) * INSTREAM_LINES #"\n" fdd fddv lines' (forwardFD fss fdd k))`
>- (
xapp>>xsimpl>>
qexists_tac`emp`>>qexists_tac`lines`>>
qexists_tac`fss`>>qexists_tac`fdd`>>xsimpl>>
rw[]>>
qexists_tac`x`>>qexists_tac`x'`>>xsimpl>>
metis_tac[])>>
xlet `POSTv v. STDIO fs`
>- (
xapp_spec b_closeIn_spec_lines >>
qexists_tac `emp`>>
qexists_tac `lines'` >>
qexists_tac `forwardFD fss fdd k` >>
qexists_tac `fdd` >>
qexists_tac `#"\n"` >>
conj_tac THEN1
(unabbrev_all_tac
\\ imp_res_tac fsFFIPropsTheory.nextFD_ltX \\ fs []
\\ imp_res_tac fsFFIPropsTheory.STD_streams_nextFD \\ fs []) >>
xsimpl>>
`validFileFD fdd (forwardFD fss fdd k).infds` by
(unabbrev_all_tac>> simp[validFileFD_forwardFD]
\\ imp_res_tac fsFFIPropsTheory.nextFD_ltX \\ fs []
\\ match_mp_tac validFileFD_nextFD \\ fs []) >>
xsimpl >> rw [] >>
imp_res_tac (DECIDE ``n<m:num ==> n <= m``) >>
imp_res_tac fsFFIPropsTheory.nextFD_leX \\ fs [] >>
drule fsFFIPropsTheory.openFileFS_ADELKEY_nextFD >>
fs [Abbr`fss`] \\ xsimpl)>>
xvar>>
xsimpl>>
metis_tac[]
QED
val usage_string = ‘
cake_lpr can be invoked in several ways from the command line.
The LPR proof file can optionally be in binary format.
Usage: cake_lpr <DIMACS formula file>
Parses the DIMACS file and prints the parsed formula.
Usage: cake_lpr <DIMACS formula file> <LPR proof file>
Run LPR unsatisfiability proof checking
Usage: cake_lpr <DIMACS formula file> <LPR proof file> <DIMACS transformation file>
Run LPR transformation proof checking
Usage: cake_lpr <DIMACS formula file> <summary proof file> i-j <LPR proof file>
Run two-level transformation proof checking for lines i-j
Usage: cake_lpr <DIMACS formula file> <summary proof file> -check <output file>
Check that output intervals cover all lines of summary proof file
’
fun drop_until p [] = []
| drop_until p (x::xs) = if p x then x::xs else drop_until p xs;
val usage_string_tm =
usage_string |> hd |> (fn QUOTE s => s) |> explode
|> drop_until (fn c => c = #"\n") |> tl |> implode
|> stringSyntax.fromMLstring;
Definition usage_string_def:
usage_string = strlit ^usage_string_tm
End
val r = translate usage_string_def;
val _ = register_type``:step``;
val LPR_STEP_TYPE_def = fetch "-" "LPR_STEP_TYPE_def";
val run_proof_arr = (append_prog o process_topdecs) `
fun run_proof_arr fml inds earr hm n mv steps =
case steps of [] => (fml,inds,earr,n,mv)
| step::rest =>
(case step of
Del c =>
(case Hashtable.lookup hm c of
None => run_proof_arr fml inds earr hm n mv rest
| Some cls =>
(list_delete_arr cls fml;
Hashtable.delete hm c;
run_proof_arr fml inds earr hm n mv rest))
| Add c =>
let val earr = update_earliest_arr earr n c
val mv = max mv (list_max_index c + 1)
val u = hash_ins hm c n
in
run_proof_arr (Array.updateResize fml None n (Some c))
(sorted_insert n inds) earr hm (n+1) mv rest
end)`
Theorem run_proof_arr_spec:
∀sts stsv ls lsv fmlls fmllsv earliest earliestv n nv fmlv Earrv h hv mv mvv.
(LIST_TYPE NUM) ls lsv ∧
LIST_REL (OPTION_TYPE (LIST_TYPE INT)) fmlls fmllsv ∧
LIST_REL (OPTION_TYPE NUM) earliest earliestv ∧
NUM n nv ∧
NUM mv mvv ∧
(LIST_TYPE LPR_STEP_TYPE) sts stsv
⇒
app (p : 'ffi ffi_proj)
^(fetch_v "run_proof_arr" (get_ml_prog_state()))
[fmlv; lsv; Earrv; hv; nv; mvv; stsv]
(ARRAY fmlv fmllsv * ARRAY Earrv earliestv *
HASHTABLE (LIST_TYPE INT) (LIST_TYPE NUM) hash_func order_lists h hv)
(POSTv v.
SEP_EXISTS v1 v2 v3 v4 v5.
&(v = Conv NONE [v1; v2; v3; v4; v5]) *
SEP_EXISTS fmllsv' earliestv'.
let (fmlls',ls',earliest',h',n',mv') = run_proof_list (fmlls,ls,earliest,h,n,mv) sts in
ARRAY v1 fmllsv' *
ARRAY v3 earliestv' *
HASHTABLE (LIST_TYPE INT) (LIST_TYPE NUM) hash_func order_lists h' hv *
&(LIST_REL (OPTION_TYPE (LIST_TYPE INT)) fmlls' fmllsv' ∧
(LIST_TYPE NUM) ls' v2 ∧
LIST_REL (OPTION_TYPE NUM) earliest' earliestv' ∧
NUM n' v4 ∧
NUM mv' v5
)
)
Proof
simp[run_proof_list_def]>>
Induct>>rw[]>>
xcf "run_proof_arr" (get_ml_prog_state ())>>
fs[LIST_TYPE_def]>>
xmatch
>- (simp[]>>xcon>>xsimpl)>>
Cases_on`h`>>fs[LPR_STEP_TYPE_def]>>
xmatch>>simp[run_proof_step_list_def]
>- (
(* Del case *)
xlet_auto >- (
qexists_tac`ARRAY fmlv fmllsv * ARRAY Earrv earliestv`>>qexists_tac`h'`>>xsimpl)>>
TOP_CASE_TAC>>fs[OPTION_TYPE_def]>>xmatch
>- (
(* Key not present *)
xapp>>
xsimpl>>
rpt(asm_exists_tac>>simp[])>>
qexists_tac`emp`>>qexists_tac`h'`>>xsimpl>>
pairarg_tac>>simp[]>>
xsimpl)>>
rpt xlet_autop>>
xapp>>xsimpl>>
rpt(asm_exists_tac>>simp[])>>
qexists_tac`emp`>>qexists_tac`h' \\ l`>>xsimpl>>
pairarg_tac>>simp[]>>
xsimpl)>>
(* "Add" case -- annoying... *)
rpt xlet_autop>>
xlet`POSTv uv.
ARRAY fmlv fmllsv *
ARRAY Earrv' earliestv' *
HASHTABLE (LIST_TYPE INT) (LIST_TYPE NUM) hash_func order_lists (hash_insert h' l n) hv`
>- (
xapp>>asm_exists_tac>>simp[]>>
asm_exists_tac>>simp[]>>
qexists_tac`ARRAY Earrv' earliestv' * ARRAY fmlv fmllsv `>>
qexists_tac`h'`>>
xsimpl)>>
rpt xlet_autop>>
xapp>> xsimpl>>
match_mp_tac LIST_REL_update_resize>>fs[OPTION_TYPE_def]
QED
(* Only run proof on the hash table *)
val run_proof_hash_arr = (append_prog o process_topdecs) `
fun run_proof_hash_arr hm n steps =
case steps of [] => ()
| step::rest =>
(case step of
Del c =>
(Hashtable.delete hm c;
run_proof_hash_arr hm n rest)
| Add c =>
(hash_ins hm c n;
run_proof_hash_arr hm (n+1) rest))`
Theorem run_proof_hash_arr_spec:
∀sts stsv n nv h hv a b c d.
NUM n nv ∧
(LIST_TYPE LPR_STEP_TYPE) sts stsv
⇒
app (p : 'ffi ffi_proj)
^(fetch_v "run_proof_hash_arr" (get_ml_prog_state()))
[hv; nv; stsv]
(HASHTABLE (LIST_TYPE INT) (LIST_TYPE NUM) hash_func order_lists h hv)
(POSTv uv.
HASHTABLE (LIST_TYPE INT) (LIST_TYPE NUM) hash_func order_lists
(FST(SND(SND(SND(run_proof_list (a,b,c,h,n,d) sts)))))
hv)
Proof
simp[run_proof_list_def]>>
Induct>>rw[]>>
xcf "run_proof_hash_arr" (get_ml_prog_state ())>>
fs[LIST_TYPE_def]>>
xmatch
>- (simp[]>>xcon>>xsimpl)>>
Cases_on`h`>>fs[LPR_STEP_TYPE_def]>>
xmatch>>simp[run_proof_step_list_def]
>- (
(* Del case *)
xlet_auto >- (
qexists_tac`emp`>>qexists_tac`h'`>>xsimpl)>>
xapp>>
asm_exists_tac>>simp[]>>
qexists_tac`emp`>>xsimpl>>
qexists_tac`h' \\l`>>xsimpl>>
every_case_tac>>simp[]>>
qmatch_goalsub_abbrev_tac`FOLDL _ (aa,bb,cc,_,_,dd)`>>
map_every qexists_tac [`dd`,`cc`,`bb`,`aa`]>>
xsimpl>>
DEP_REWRITE_TAC[DOMSUB_NOT_IN_DOM]>>
fs[FDOM_FLOOKUP]>>
xsimpl)>>
(* "Add" case -- annoying... *)
xlet`POSTv uv.
HASHTABLE (LIST_TYPE INT) (LIST_TYPE NUM) hash_func order_lists (hash_insert h' l n) hv`
>- (
xapp>>asm_exists_tac>>simp[]>>
asm_exists_tac>>simp[]>>
qexists_tac`emp`>>qexists_tac`h'`>>
xsimpl)>>
xlet_autop>>
xapp>>simp[]
QED
Definition mapf_def:
mapf ls = MAP FST (ls: (int list # num list) list)
End
val _ = translate mapf_def;
val check_lpr_range_arr = (append_prog o process_topdecs) `
fun check_lpr_range_arr fname fml inds earr mv n pf i j =
let
val hm = (Hashtable.empty (2 * n) hash_func order_lists)
val u = reindex_hash fml False inds hm
val pfij = List.take pf j
val pfi = List.take pfij i
val pfj = List.drop pfij i
val ri = run_proof_arr fml inds earr hm n mv pfi
in
case ri of (fml',inds',earr',n',mv') =>
let val rj = run_proof_hash_arr hm n' pfj
val cls = mapf (Hashtable.toAscList hm)
in
check_unsat' 0 fml' inds' earr' fname mv' cls
end
end`
Theorem bounded_fml_run_proof_list:
∀pf fmlls ls earliest fm n mv fmlls' ls' earliest' fm' n' mv'.
run_proof_list (fmlls,ls,earliest,fm,n,mv) pf = (fmlls',ls',earliest',fm',n',mv') ∧
bounded_fml mv fmlls ⇒
bounded_fml mv' fmlls'
Proof
Induct>>fs[run_proof_list_def]>>
Cases>>rw[run_proof_step_list_def]>>
every_case_tac>>fs[]>>
first_x_assum drule>>
disch_then match_mp_tac>>
fs[bounded_fml_list_delete_list]>>
DEP_REWRITE_TAC [bounded_fml_update_resize]>>
CONJ_TAC>- (
match_mp_tac (GEN_ALL bounded_fml_leq)>>
asm_exists_tac>>simp[])>>
match_mp_tac list_max_index_bounded_clause>>
simp[]
QED
Theorem contains_clauses_list_eq:
set (ls:int list list) = set ls' ⇒
contains_clauses_list fml inds ls = contains_clauses_list fml inds ls'
Proof
rw[contains_clauses_list_def]>>
every_case_tac>>fs[EVERY_MEM,EXTENSION]
QED
Definition parse_lpr_def:
parse_lpr x = NONE
End
Theorem check_lpr_range_arr_spec:
FILENAME f fv ∧
hasFreeFD fs ∧
LIST_REL (OPTION_TYPE (LIST_TYPE INT)) fmlls fmllsv ∧
LIST_REL (OPTION_TYPE NUM) earliest earliestv ∧
(LIST_TYPE NUM) ls lsv ∧
(LIST_TYPE LPR_STEP_TYPE) sts stsv ∧
NUM i iv ∧
NUM j jv ∧
NUM n nv ∧
NUM mv mvv ∧
i ≤ j ∧
bounded_fml mv fmlls
⇒
app (p : 'ffi ffi_proj)
^(fetch_v "check_lpr_range_arr" (get_ml_prog_state()))
[fv; fmlv; lsv; Earrv; mvv; nv; stsv; iv; jv]
(STDIO fs * ARRAY fmlv fmllsv * ARRAY Earrv earliestv)
(POSTv v.
STDIO fs *
SEP_EXISTS res.
&(SUM_TYPE STRING_TYPE (OPTION_TYPE (LIST_TYPE INT)) res v ∧
case res of
INL err => T
| INR opt =>
inFS_fname fs f ∧
∃lpr fml' inds' err2.
EVERY wf_lpr lpr ∧
let fm = hash_clauses_list fmlls ls in
let stsij = TAKE j sts in
let stsi = TAKE i stsij in
let stsj = DROP i stsij in
let (fmlls',ls',earliest',fm',n',mv') = run_proof_list (fmlls,ls,earliest,fm,n,mv) stsi in
let (fmlls'',ls'',earliest'',fm'',n'',mv'') = run_proof_list (fmlls',ls',earliest',fm',n',mv') stsj in
let cls = MAP FST (fmap_to_alist fm'') in
check_lpr_list 0 lpr fmlls' ls' (REPLICATE mv' w8z) earliest' = SOME (fml',inds') ∧
(opt =
if contains_clauses_list fml' inds' cls then NONE
else SOME err2)
))
Proof
rw[]>>
xcf "check_lpr_range_arr" (get_ml_prog_state ())>>
rpt xlet_autop>>
xlet`POSTv v. HASHTABLE (LIST_TYPE INT) (LIST_TYPE NUM) hash_func order_lists FEMPTY v * STDIO fs * ARRAY fmlv fmllsv * ARRAY Earrv earliestv`
>- (
xapp_spec (HashtableProofTheory.hashtable_empty_spec|>INST_TYPE[alpha|->``:int list``,beta |-> ``:num list``])>>
assume_tac order_lists_TotOrd>>
asm_exists_tac>>simp[]>>
simp[PULL_EXISTS]>>
asm_exists_tac>>simp[]>>
assume_tac order_lists_v_thm>>
asm_exists_tac>>simp[]>>
qexists_tac`STDIO fs * ARRAY fmlv fmllsv * ARRAY Earrv earliestv`>>xsimpl>>
qexists_tac`hash_func`>>
qexists_tac`LIST_TYPE NUM`>>xsimpl>>
simp[hash_func_v_thm])>>
assume_tac (ListProgTheory.take_v_thm |> Q.GEN `a` |> Q.ISPEC `LPR_STEP_TYPE`)>>
rpt xlet_autop>>
xlet`(POSTv uv.
&(UNIT_TYPE () uv) *
HASHTABLE (LIST_TYPE INT) (LIST_TYPE NUM) hash_func order_lists
((λ(xs,ys). hash_clauses_aux ys xs) (reindex fmlls ls) FEMPTY) v *
STDIO fs * ARRAY fmlv fmllsv * ARRAY Earrv earliestv)`
>- (
xapp>>xsimpl>>
qexists_tac`STDIO fs * ARRAY Earrv earliestv`>>xsimpl>>
`BOOL F (Conv (SOME (TypeStamp "False" 0)) [])` by EVAL_TAC>>
rpt (asm_exists_tac>>simp[])>>
qexists_tac`FEMPTY`>>xsimpl)>>
rpt xlet_autop>>
xlet_auto
>- (
xsimpl>>
pairarg_tac>>simp[]>>
xsimpl)>>
simp[hash_clauses_list_def,mllistTheory.take_def]>>
rpt(pairarg_tac>>fs[])>>rw[]>>
xpull>>
xmatch >>
xlet`POSTv uv.
ARRAY v1 fmllsv' * ARRAY v3 earliestv' *
HASHTABLE (LIST_TYPE INT) (LIST_TYPE NUM) hash_func order_lists
(FST(SND(SND(SND(run_proof_list (fmlls',ls',earliest',fm',n',mv') (drop (take sts j) i)))))) v * STDIO fs`
>- (
xapp>>xsimpl >>
rpt(asm_exists_tac>>simp[])>>
qexists_tac` ARRAY v1 fmllsv' * ARRAY v3 earliestv' * STDIO fs`>>
qexists_tac`fm'`>>
xsimpl>>
map_every qexists_tac [`mv'`,`earliest'`,`ls'`,`fmlls'`]>>
xsimpl)>>
fs[mllistTheory.drop_def,mllistTheory.take_def]>>
xlet`POSTv listv. SEP_EXISTS bsalist asclist.
&(LIST_TYPE (PAIR_TYPE (LIST_TYPE INT) (LIST_TYPE NUM)) bsalist listv ∧ FEMPTY |++ bsalist = fm'') *
ARRAY v1 fmllsv' * ARRAY v3 earliestv' * STDIO fs`
>- (
xapp_spec (HashtableProofTheory.hashtable_toAscList_spec|>INST_TYPE[alpha|->``:int list``,beta |-> ``:num list``])>>
qexists_tac`ARRAY v1 fmllsv' * ARRAY v3 earliestv' * STDIO fs`>>xsimpl>>
metis_tac[SEP_IMP_REFL])>>
xlet_autop>>
xapp>>
xsimpl>>
`bounded_fml mv' fmlls'` by
metis_tac[bounded_fml_run_proof_list]>>
rpt(asm_exists_tac>>simp[])>>
qexists_tac`emp`>>xsimpl>>
simp[mapf_def]>>rveq>>
`set (MAP FST bsalist) = set (MAP FST (fmap_to_alist (FEMPTY |++ bsalist)))` by
fs[EXTENSION,MEM_MAP,MEM_fmap_to_alist,EXISTS_PROD,FDOM_FUPDATE_LIST]>>
drule contains_clauses_list_eq>>
rw[]>>
every_case_tac>>gvs[SUM_TYPE_def]
>- (
rename1`STRING_TYPE xxx _`>>
qexists_tac`INL xxx`>>
simp[SUM_TYPE_def])>>
qmatch_asmsub_abbrev_tac`OPTION_TYPE _ xxx _`>>
qexists_tac`INR xxx`>>
simp[SUM_TYPE_def]>>
first_x_assum (irule_at Any)>>
unabbrev_all_tac>>
gvs[contains_clauses_list_err]>>
TOP_CASE_TAC>> gvs[]>>
metis_tac[GSYM quantHeuristicsTheory.IS_SOME_EQ_NOT_NONE,IS_SOME_EXISTS]
QED
(*
Checker takes up to 4 arguments:
1 arg (CNF file): parse and print the CNF
2 args (CNF file, proof file): parse CNF, run proof, report UNSAT (or error)
3 args (CNF file, proof file, CNF file (transformation)):
parse CNF, run proof, check that the proof transforms the CNF correctly to the latter CNF
4 args (CNF file, top-level proof, range a-b, LPR proof file)
*)
val _ = translate parse_proofstep_def;
val _ = translate parse_proof_toks_aux_def;
val _ = translate parse_proof_toks_def;
Definition noparse_string_def:
noparse_string f s = concat[strlit"c Input file: ";f;strlit" unable to parse in format: "; s;strlit"\n"]
End
val r = translate noparse_string_def;
(* parse_proof with simple wrapper *)
val parse_proof_full = (append_prog o process_topdecs) `
fun parse_proof_full f =
(case TextIO.b_inputAllTokensFrom #"\n" f blanks tokenize of
None => Inl (notfound_string f)
| Some lines =>
(case parse_proof_toks lines of
None => Inl (noparse_string f "Proof")
| Some x => Inr x))`
val check_unsat_1 = (append_prog o process_topdecs) `
fun check_unsat_1 f1 =
case parse_dimacs_full f1 of
Inl err => TextIO.output TextIO.stdErr err
| Inr (mv,(ncl,fml)) => TextIO.print_list (print_dimacs fml)`
val check_unsat_2 = (append_prog o process_topdecs) `
fun check_unsat_2 f1 f2 =
case parse_dimacs_full f1 of
Inl err => TextIO.output TextIO.stdErr err
| Inr (mv,(ncl,fml)) =>
let val one = 1
val arr = Array.array (2*ncl) None
val arr = fill_arr arr one fml
val bnd = 2*mv + 3
val earr = Array.array bnd None
val earr = fill_earliest earr one fml
val rls = rev_enum_full 1 fml
in
case check_unsat' 0 arr rls earr f2 bnd [[]] of
Inl err => TextIO.output TextIO.stdErr err
| Inr None => TextIO.print "s VERIFIED UNSAT\n"
| Inr (Some l) => TextIO.output TextIO.stdErr "c empty clause not derived at end of proof\n"
end`
Definition transformation_err_def:
transformation_err cl =
concat[strlit"c transformation clause: ";print_clause cl;strlit"c not derived at end of proof\n"]
End
val _ = translate transformation_err_def;
val check_unsat_3 = (append_prog o process_topdecs) `
fun check_unsat_3 f1 f2 f3 =
case parse_dimacs_full f1 of
Inl err => TextIO.output TextIO.stdErr err
| Inr (mv,(ncl,fml)) =>
case parse_dimacs_full f3 of
Inl err => TextIO.output TextIO.stdErr err
| Inr (mv2,(ncl2,fml2)) =>
let val one = 1
val arr = Array.array (2*ncl) None
val arr = fill_arr arr one fml
val bnd = 2*mv + 3
val earr = Array.array bnd None
val earr = fill_earliest earr one fml
val rls = rev_enum_full 1 fml
in
case check_unsat' 0 arr rls earr f2 bnd fml2 of
Inl err => TextIO.output TextIO.stdErr err
| Inr None => TextIO.print "s VERIFIED TRANSFORMATION\n"
| Inr (Some cl) => TextIO.output TextIO.stdErr (transformation_err cl)
end`
Definition check_cond_def:
check_cond i j pf = (i ≤ j ∧ j ≤ LENGTH pf)
End
val _ = translate check_cond_def;
Definition success_str_def:
success_str cnf_md5 proof_md5 rng = expected_prefix cnf_md5 proof_md5 ^ rng ^ strlit "\n"
End
val _ = translate success_str_def;
Definition parse_rng_or_check_def:
parse_rng_or_check rngc =
if rngc = strlit "-check" then SOME (INL ())
else OPTION_MAP INR (parse_rng rngc)
End
val _ = translate parse_rng_or_check_def;
val _ = translate print_rng_def;
val check_unsat_4 = (append_prog o process_topdecs) `
fun check_unsat_4 f1 f2 rng f3 =
case parse_dimacs_full f1 of
Inl err => TextIO.output TextIO.stdErr err
| Inr (mv,(ncl,fml)) =>
case parse_proof_full f2 of
Inl err => TextIO.output TextIO.stdErr err
| Inr pf =>
case parse_rng_or_check rng of
None => TextIO.output TextIO.stdErr "c Unable to parse range specification a-b\n"
| Some (Inl u) =>
check_compose f1 f2 f3
| Some (Inr (i,j)) =>
if check_cond i j pf
then
let val one = 1
val arr = Array.array (2*ncl) None
val arr = fill_arr arr one fml
val bnd = 2*mv + 3
val earr = Array.array bnd None
val earr = fill_earliest earr one fml
val rls = rev_enum_full 1 fml
in
case check_lpr_range_arr f3 arr rls earr bnd (ncl+1) pf i j of
Inl err => TextIO.output TextIO.stdErr err
| Inr None =>
(case md5_of (Some f1) of
None => TextIO.output TextIO.stdErr (notfound_string f1)
| Some cnf_md5 =>
case md5_of (Some f2) of
None => TextIO.output TextIO.stdErr (notfound_string f2)
| Some proof_md5 => TextIO.print (success_str cnf_md5 proof_md5 (print_rng i j)))
| Inr (Some cl) => TextIO.output TextIO.stdErr (transformation_err cl)
end
else TextIO.output TextIO.stdErr "c Invalid range specification: range a-b must satisfy a <= b <= num lines in proof file\n"`
val check_unsat = (append_prog o process_topdecs) `
fun check_unsat u =
case CommandLine.arguments () of
[f1] => check_unsat_1 f1
| [f1,f2] => check_unsat_2 f1 f2
| [f1,f2,f3] => check_unsat_3 f1 f2 f3
| [f1,f2,rng,f3] => check_unsat_4 f1 f2 rng f3
| _ => TextIO.output TextIO.stdErr usage_string`
(* We verify each argument type separately *)
val b_inputAllTokensFrom_spec_specialize =
b_inputAllTokensFrom_spec
|> Q.GEN `f` |> Q.SPEC`blanks`
|> Q.GEN `fv` |> Q.SPEC`blanks_v`
|> Q.GEN `g` |> Q.ISPEC`tokenize`
|> Q.GEN `gv` |> Q.ISPEC`tokenize_v`
|> Q.GEN `a` |> Q.ISPEC`SUM_TYPE STRING_TYPE INT`
|> REWRITE_RULE [blanks_v_thm,tokenize_v_thm,blanks_def] ;
Definition check_unsat_1_sem_def:
check_unsat_1_sem fs f1 out ⇔
case get_fml fs f1 of
NONE => out = strlit ""
| SOME (mv,ncl,fml) => out = concat (print_dimacs fml)
End
val err_tac =
xapp_spec output_stderr_spec \\ xsimpl>>
asm_exists_tac>>xsimpl>>
qexists_tac`emp`>>xsimpl>>
qexists_tac`fs`>>xsimpl>>
rw[]>>
qexists_tac`err`>>xsimpl>>
simp[STD_streams_add_stdout,STD_streams_add_stderr, STD_streams_stdout,add_stdo_nil]>>
xsimpl;
Theorem check_unsat_1_spec:
STRING_TYPE f1 f1v ∧
validArg f1 ∧
hasFreeFD fs
⇒
app (p:'ffi ffi_proj) ^(fetch_v"check_unsat_1"(get_ml_prog_state()))
[f1v]
(STDIO fs)
(POSTv uv. &UNIT_TYPE () uv *
SEP_EXISTS out err.
STDIO (add_stdout (add_stderr fs err) out) *
&(check_unsat_1_sem fs f1 out))
Proof
rw[]>>
xcf "check_unsat_1" (get_ml_prog_state ())>>
reverse (Cases_on `STD_streams fs`) >- (fs [TextIOProofTheory.STDIO_def] \\ xpull) >>
xlet_autop>>
simp[check_unsat_1_sem_def]>>
TOP_CASE_TAC>>fs[SUM_TYPE_def]