-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathcnfProgScript.sml
904 lines (844 loc) · 25.6 KB
/
cnfProgScript.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
(*
CNF encoder and checker
*)
open preamble basis lpr_parsingTheory cnf_to_pbTheory npbc_parseProgTheory;
open cfLib basisFunctionsLib;
val _ = new_theory "cnfProg";
val _ = translation_extends "npbc_parseProg";
val xlet_autop = xlet_auto >- (TRY( xcon) >> xsimpl)
(* TODO: COPIED from lpr_arrayFullProgScript.sml *)
Theorem fastForwardFD_ADELKEY_same[simp]:
forwardFD fs fd n with infds updated_by ADELKEY fd =
fs with infds updated_by ADELKEY fd
Proof
fs [forwardFD_def, IO_fs_component_equality]
QED
val _ = translate lpr_parsingTheory.blanks_def;
val _ = translate lpr_parsingTheory.tokenize_def;
val blanks_1_v_thm = theorem "blanks_1_v_thm";
val tokenize_1_v_thm = theorem "tokenize_1_v_thm";
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 lpr_parsingTheory.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`lpr_parsing$blanks`
|> Q.GEN `fv` |> Q.SPEC`blanks_1_v`
|> Q.GEN `g` |> Q.ISPEC`lpr_parsing$tokenize`
|> Q.GEN `gv` |> Q.ISPEC`tokenize_1_v`
|> Q.GEN `a` |> Q.ISPEC`SUM_TYPE STRING_TYPE INT`
|> SIMP_RULE std_ss [blanks_1_v_thm,tokenize_1_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_1 tokenize_1 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 lpr_parsing$nocomment_line (MAP lpr_parsing$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 (lpr_parsing$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[lpr_parsingTheory.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 (lpr_parsing$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_1 tokenize_1 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 lpr_parsing$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 []
\\ rw[]
\\ 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 (lpr_parsing$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 (lpr_parsing$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 lpr_parsing$nocomment_line (MAP lpr_parsing$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)`;
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))))
(if inFS_fname fs f then
(case parse_dimacs_toks (MAP lpr_parsing$toks (all_lines fs f)) of
NONE => INL err
| SOME x => INR x)
else INL err) v)) * STDIO fs)
Proof
rw[]>>
xcf"parse_dimacs_full"(get_ml_prog_state()) >>
fs[validArg_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 #"\n" 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 lpr_parsing$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
(* Translate the encoder *)
val res = translate clause_to_pbc_def;
val res = translate fml_to_pbf_def;
(* parse input from f1 and run encoder into npbc *)
val parse_and_enc = (append_prog o process_topdecs) `
fun parse_and_enc f1 =
case parse_dimacs_full f1 of
Inl err => Inl err
| Inr (a,(b,fml)) =>
Inr (fml_to_pbf fml,(a,b))`
Definition get_fml_def:
get_fml fs f =
if inFS_fname fs f then
parse_dimacs (all_lines fs f)
else NONE
End
Theorem parse_and_enc_spec:
STRING_TYPE f1 f1v ∧
validArg f1 ∧
hasFreeFD fs
⇒
app (p:'ffi ffi_proj) ^(fetch_v"parse_and_enc"(get_ml_prog_state()))
[f1v]
(STDIO fs)
(POSTv v.
STDIO fs *
& ∃res.
SUM_TYPE STRING_TYPE
(PAIR_TYPE
(LIST_TYPE constraint_TYPE)
(PAIR_TYPE NUM NUM)) res v ∧
case res of
INL err =>
get_fml fs f1 = NONE
| INR (pbf,nvars,ncl) =>
∃fml.
get_fml fs f1 = SOME fml ∧
fml_to_pbf fml = pbf)
Proof
rw[]>>
xcf"parse_and_enc"(get_ml_prog_state())>>
xlet_autop>>
pop_assum mp_tac>>
reverse IF_CASES_TAC
>- (
rw[SUM_TYPE_def]>>
xmatch>>
xcon>>
xsimpl>>
qexists_tac`INL err`>>
simp[SUM_TYPE_def]>>
fs[get_fml_def])>>
TOP_CASE_TAC
>- (
rw[SUM_TYPE_def]>>
xmatch>>
xcon>>
xsimpl>>
qexists_tac`INL err`>>
simp[SUM_TYPE_def]>>
fs[get_fml_def,parse_dimacs_def])>>
rw[SUM_TYPE_def]>>
PairCases_on`x`>>fs[PAIR_TYPE_def]>>
xmatch>>
rpt xlet_autop>>
xcon>>
xsimpl>>
qexists_tac`INR (fml_to_pbf x2,x0,x1)`>>
fs[SUM_TYPE_def,PAIR_TYPE_def]>>
simp[get_fml_def,parse_dimacs_def]
QED
(* NOTE: Reuse infrastructure from pbc_normalise *)
(* Variables x1 ... xl maps to 1 ... l respectively
everything else uses the mapping *)
Definition plainLim_nf_def:
plainLim_nf l s nhm =
if strlen s ≥ 1 ∧ strsub s 0 = #"x" then
case mlint$fromNatString (substring s 1 (strlen s - 1)) of
NONE => NONE
| SOME n =>
if n ≤ l then SOME (n,nhm)
else SOME(name_to_num_var s nhm)
else
SOME(name_to_num_var s nhm)
End
val res = translate plainLim_nf_def;
val res = translate pbc_normaliseTheory.mk_map_def;
val res = translate pbc_normaliseTheory.name_to_num_var_def;
Definition cnf_init_state_def:
cnf_init_state n =
<| to_num := LN; to_str := LN; hash_fun := hash_str; cmp_name := compare; next_num := n+1 |>
End
Definition plainLim_ns_def:
plainLim_ns n = (plainLim_nf n,
cnf_init_state n)
End
val res = translate cnf_init_state_def;
val res = translate plainLim_ns_def;
Definition UNSAT_string_def:
UNSAT_string = strlit "s VERIFIED UNSAT\n"
End
Definition SAT_string_def:
SAT_string = strlit "s VERIFIED SAT\n"
End
(* And empty formula *)
Definition default_nobjf_def:
default_nobjf = (NONE,[]):((int # num) list # int) option # npbc list
End
val res = translate default_nobjf_def;
(* Turn result into string *)
Definition ores_to_string_def:
(ores_to_string (INL s) = INL s) ∧
(ores_to_string (INR (out,bnd,h)) =
if out ≠ NoOutput then INL (strlit "c Unexpected output section.\n")
else
case h of
DUnsat => INR UNSAT_string
| DSat => INR SAT_string
| _ => INL (strlit "c Unexpected conclusion for decision problem.\n"))
End
val res = translate (ores_to_string_def |> SIMP_RULE std_ss [UNSAT_string_def,SAT_string_def]);
val check_unsat_2 = (append_prog o process_topdecs) `
fun check_unsat_2 f1 f2 =
case parse_and_enc f1 of
Inl err => TextIO.output TextIO.stdErr err
| Inr (fml,(nv,nc)) =>
let val n = None in
(case
ores_to_string (
check_unsat_top False (plainlim_ns nv) fml n n [] n n f2) of
Inl err => TextIO.output TextIO.stdErr err
| Inr s => TextIO.print s)
end`
Definition check_unsat_2_sem_def:
check_unsat_2_sem fs f1 out ⇔
(out ≠ strlit"" ⇒
∃fml.
get_fml fs f1 = SOME fml ∧
(
out = UNSAT_string ∧ unsatisfiable (interp fml) ∨
out = SAT_string ∧ satisfiable (interp fml)))
End
Theorem check_unsat_2_spec:
STRING_TYPE f1 f1v ∧ validArg f1 ∧
STRING_TYPE f2 f2v ∧ validArg f2 ∧
hasFreeFD fs
⇒
app (p:'ffi ffi_proj) ^(fetch_v"check_unsat_2"(get_ml_prog_state()))
[f1v; f2v]
(STDIO fs)
(POSTv uv. &UNIT_TYPE () uv *
SEP_EXISTS out err.
STDIO (add_stdout (add_stderr fs err) out) *
&(check_unsat_2_sem fs f1 out))
Proof
rw[check_unsat_2_sem_def]>>
xcf "check_unsat_2" (get_ml_prog_state ())>>
reverse (Cases_on `STD_streams fs`) >- (fs [TextIOProofTheory.STDIO_def] \\ xpull) >>
xlet_autop>>
Cases_on`res`
>- (
fs[SUM_TYPE_def]>>
xmatch>>
xapp_spec output_stderr_spec \\ xsimpl>>
asm_exists_tac>>xsimpl>>
qexists_tac`emp`>>xsimpl>>
qexists_tac`fs`>>xsimpl>>
rw[]>>
qexists_tac`x`>>
xsimpl>>rw[]>>
fs[STD_streams_add_stderr, STD_streams_stdout,add_stdo_nil]>>
xsimpl)>>
fs[SUM_TYPE_def]>>
PairCases_on`y`>>
gvs[PAIR_TYPE_def]>>
xmatch>>
xlet_autop>>
xlet_autop>>
xlet_autop>>
xlet`POSTv v. STDIO fs * &BOOL F v`
>-
(xcon>>xsimpl)>>
xlet`POSTv v.
STDIO fs *
SEP_EXISTS res.
&(
SUM_TYPE STRING_TYPE
(PAIR_TYPE PBC_OUTPUT_TYPE
(PAIR_TYPE (OPTION_TYPE INT) PBC_CONCL_TYPE))
res v ∧
case res of
INR (output,bound,concl) =>
sem_concl (set (fml_to_pbf fml)) NONE concl
| INL l => T)`
>- (
drule_at (Pos (el 2)) check_unsat_top_spec>>
disch_then drule>>
strip_tac>>
xapp>>
xsimpl>>
fs[FILENAME_def,validArg_def,OPTION_TYPE_def]>>
rpt (first_x_assum (irule_at Any))>>
qexists_tac`NONE`>>qexists_tac`NONE`>>
qexists_tac`NONE`>>qexists_tac`NONE`>>
qexists_tac`[]`>>
xsimpl>>
simp[OPTION_TYPE_def,LIST_TYPE_def]>>
rw[]>> asm_exists_tac>>simp[]>>
every_case_tac>>fs[])>>
xlet_autop>>
Cases_on`ores_to_string res`>>fs[SUM_TYPE_def]>>
xmatch
>- (
xapp_spec output_stderr_spec \\ xsimpl>>
asm_exists_tac>>xsimpl>>
qexists_tac`emp`>>xsimpl>>
qexists_tac`fs`>>xsimpl>>
rw[]>>
qexists_tac`strlit ""`>>
simp[]>>
qexists_tac`x`>>
fs[STD_streams_add_stderr, STD_streams_stdout,add_stdo_nil]>>
xsimpl)>>
xapp>>asm_exists_tac>>xsimpl>>
qexists_tac`emp`>>qexists_tac`fs`>>xsimpl>>
rw[]>>
qexists_tac`y`>>
qexists_tac`strlit ""`>>
simp[STD_streams_stderr,add_stdo_nil]>>
reverse CONJ_TAC>- xsimpl>>
rw[]>>
every_case_tac>>fs[ores_to_string_def,SUM_TYPE_def]>>
gvs[AllCaseEqs(),npbc_checkTheory.hconcl_concl_def,npbcTheory.sem_concl_def]
>- (
DISJ2_TAC>>
fs[get_fml_def]>>
drule fml_to_pbf_parse_dimacs>>
fs[npbcTheory.unsatisfiable_def,npbcTheory.satisfiable_def,satSemTheory.unsatisfiable_def,satSemTheory.satisfiable_def]>>
metis_tac[])>>
DISJ1_TAC>>
fs[get_fml_def]>>
drule fml_to_pbf_parse_dimacs>>
fs[npbcTheory.unsatisfiable_def,npbcTheory.satisfiable_def,satSemTheory.unsatisfiable_def,satSemTheory.satisfiable_def]>>
metis_tac[]
QED
(* TODO: Move to parse *)
Definition num_lit_string_def:
(num_lit_string (i,n:num) =
if i ≥ 0 then
toString (Num (ABS i)) ^ strlit" x" ^ toString n
else
toString (Num (ABS i)) ^ strlit" ~x" ^ toString n)
End
Definition num_lhs_string_def:
num_lhs_string xs =
concatWith (strlit" ") (MAP num_lit_string xs)
End
Definition npbc_string_def:
(npbc_string (xs,n:num) =
concat [
num_lhs_string xs;
strlit" >= ";toString n; strlit ";\n"])
End
Definition print_npbf_def:
print_npbf fml = MAP npbc_string fml
End
val res = translate num_lit_string_def;
val res = translate num_lhs_string_def;
val res = translate npbc_string_def;
val res = translate print_npbf_def;
val check_unsat_1 = (append_prog o process_topdecs) `
fun check_unsat_1 f1 =
case parse_and_enc f1 of
Inl err => TextIO.output TextIO.stdErr err
| Inr (fml,(nv,nc)) =>
TextIO.print_list (print_npbf fml)`
Definition check_unsat_1_sem_def:
check_unsat_1_sem fs f1 out ⇔
(out ≠ strlit"" ⇒
∃fml.
get_fml fs f1 = SOME fml ∧
out = concat (print_npbf (fml_to_pbf fml)))
End
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[check_unsat_1_sem_def]>>
xcf "check_unsat_1" (get_ml_prog_state ())>>
reverse (Cases_on `STD_streams fs`) >- (fs [TextIOProofTheory.STDIO_def] \\ xpull) >>
xlet_autop>>
pop_assum mp_tac>>
every_case_tac>>fs[SUM_TYPE_def,PAIR_TYPE_def]>>rw[]
>- (
xmatch>>
xapp_spec output_stderr_spec \\ xsimpl>>
asm_exists_tac>>xsimpl>>
qexists_tac`emp`>>qexists_tac`fs`>>xsimpl>>
fs[STD_streams_add_stderr, STD_streams_stdout,add_stdo_nil]>>
rw[]>>
qexists_tac`x`>>xsimpl)>>
Cases_on`r`>>fs[PAIR_TYPE_def]>>
xmatch>>
xlet_autop>>
xapp_spec print_list_spec>>xsimpl>>
asm_exists_tac>>xsimpl>>
qexists_tac`emp`>>qexists_tac`fs`>>xsimpl>>
rw[]>>
qexists_tac`concat(print_npbf(fml_to_pbf fml))`>>
qexists_tac`strlit ""`>>
simp[STD_streams_stderr,add_stdo_nil]>>
xsimpl
QED
Definition usage_string_def:
usage_string = strlit "Usage: cake_pb_cnf <DIMACS CNF file> <optional: PB proof file>\n"
End
val r = translate usage_string_def;
val main = (append_prog o process_topdecs) `
fun main u =
case CommandLine.arguments () of
[f1] => check_unsat_1 f1
| [f1,f2] => check_unsat_2 f1 f2
| _ => TextIO.output TextIO.stdErr usage_string`
Definition main_sem_def:
main_sem fs cl out =
if LENGTH cl = 2 then
check_unsat_1_sem fs (EL 1 cl) out
else if LENGTH cl = 3 then
check_unsat_2_sem fs (EL 1 cl) out
else out = strlit ""
End
Theorem STDIO_refl:
STDIO A ==>>
STDIO A * GC
Proof
xsimpl
QED
Theorem main_spec:
hasFreeFD fs
⇒
app (p:'ffi ffi_proj) ^(fetch_v"main"(get_ml_prog_state()))
[Conv NONE []]
(COMMANDLINE cl * STDIO fs)
(POSTv uv. &UNIT_TYPE () uv *
COMMANDLINE cl *
SEP_EXISTS out err.
STDIO (add_stdout (add_stderr fs err) out) *
&(main_sem fs cl out))
Proof
rw[main_sem_def]>>
xcf"main"(get_ml_prog_state())>>
reverse (Cases_on `STD_streams fs`) >- (fs [TextIOProofTheory.STDIO_def] \\ xpull) >>
reverse(Cases_on`wfcl cl`) >- (fs[COMMANDLINE_def] \\ xpull)>>
rpt xlet_autop >>
Cases_on `cl` >- fs[wfcl_def] >>
Cases_on`t`>>fs[LIST_TYPE_def]
>- (
xmatch>>
xapp_spec output_stderr_spec \\ xsimpl>>
rename1`COMMANDLINE cl`>>
qexists_tac`COMMANDLINE cl`>>xsimpl>>
qexists_tac `usage_string` >>
simp [theorem "usage_string_v_thm"] >>
qexists_tac`fs`>>xsimpl>>
rw[]>>
fs[STD_streams_add_stderr, STD_streams_stdout,add_stdo_nil]>>
metis_tac[STDIO_refl])>>
Cases_on`t'`>>fs[LIST_TYPE_def]
>- (
xmatch>>
xapp>>rw[]>>
rpt(first_x_assum (irule_at Any)>>xsimpl)>>
fs[wfcl_def]>>
rw[]>>metis_tac[STDIO_refl])>>
Cases_on`t`>>fs[LIST_TYPE_def]
>- (
xmatch>>
xapp>>rw[]>>
first_x_assum (irule_at Any)>>xsimpl>>
first_x_assum (irule_at Any)>>xsimpl>>
first_x_assum (irule_at Any)>>xsimpl>>
fs[wfcl_def]>>
rw[]>>metis_tac[STDIO_refl])>>
xmatch>>
xapp_spec output_stderr_spec \\ xsimpl>>
rename1`COMMANDLINE cl`>>
qexists_tac`COMMANDLINE cl`>>xsimpl>>
qexists_tac `usage_string` >>
simp [theorem "usage_string_v_thm"] >>
qexists_tac`fs`>>xsimpl>>
rw[]>>
fs[STD_streams_add_stderr, STD_streams_stdout,add_stdo_nil]>>
metis_tac[STDIO_refl]
QED
Theorem main_whole_prog_spec2:
hasFreeFD fs ⇒
whole_prog_spec2 main_v cl fs NONE
(λfs'. ∃out err.
fs' = add_stdout (add_stderr fs err) out ∧
main_sem fs cl out)
Proof
rw[basis_ffiTheory.whole_prog_spec2_def]
\\ match_mp_tac (MP_CANON (DISCH_ALL (MATCH_MP app_wgframe (UNDISCH main_spec))))
\\ xsimpl
\\ rw[PULL_EXISTS]
\\ qexists_tac`add_stdout (add_stderr fs x') x`
\\ xsimpl
\\ qexists_tac`x`
\\ qexists_tac`x'`
\\ xsimpl
\\ simp[GSYM add_stdo_with_numchars,with_same_numchars]
QED
local
val name = "main"
val (sem_thm,prog_tm) =
whole_prog_thm (get_ml_prog_state()) name (UNDISCH main_whole_prog_spec2)
Definition main_prog_def:
main_prog = ^prog_tm
End
in
Theorem main_semantics =
sem_thm
|> REWRITE_RULE[GSYM main_prog_def]
|> DISCH_ALL
|> SIMP_RULE(srw_ss())[GSYM CONJ_ASSOC,AND_IMP_INTRO];
end
val _ = export_theory();