-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathnpbc_checkScript.sml
4393 lines (4139 loc) · 122 KB
/
npbc_checkScript.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
(*
Pseudo-boolean constraints proof format and checker
*)
open preamble npbcTheory mlstringTheory mlintTheory mlvectorTheory spt_to_vecTheory;
val _ = new_theory "npbc_check";
val _ = numLib.temp_prefer_num();
(* Proof steps are classified in a hierachy of three descending levels
"Core" steps csteps
|
|--> "SAT" steps ssteps
|
|--> "Logical" steps lsteps
lstep: These steps preserve logical implication for F = C U D
Includes cutting planes and contradiction proof steps
Deletions allowed in D only
sstep: These steps preserve satisfiability of F = C U D
Includes redundancy, and redundancy subproofs can only use lsteps
cstep: These steps manipulate the core
Includes objective updates, core transfer, checked deletion, dominance
*)
(* TODO: move *)
Theorem lookup_spt_size:
∀n x a. lookup n x = SOME a ⇒ ∀f. f a < spt_size f x
Proof
recInduct lookup_ind \\ rw []
\\ gvs [lookup_def,spt_size_def,AllCaseEqs()]
\\ first_x_assum (qspec_then ‘f’ assume_tac) \\ fs []
QED
Theorem MEM_list_size:
∀xs x. MEM x xs ⇒ ∀f. f x < list_size f xs
Proof
Induct \\ fs [list_size_def] \\ rw [] \\ fs [] \\ res_tac
\\ first_x_assum (qspec_then ‘f’ assume_tac) \\ fs []
QED
(* pbp = pseudo-boolean proof format
Below, nums represent ConstraintIds, which are 1-indexed (although 0s internally work fine)
*)
(* A constraint to be added by a cutting planes proof *)
Datatype:
constr =
| Id num (* Constraints can refer to earlier IDs *)
| Add constr constr (* Add two constraints *)
| Mul constr num (* Multiply by a constant factor *)
| Div constr num (* Divide by a constant factor *)
| Sat constr (* Saturation *)
| Lit (num lit) (* Literal axiom lit ≥ 0 *)
| Weak constr var (* Addition of literal axioms until "var" disappears *)
End
(* Steps that preserve logical implication *)
Datatype:
lstep =
| Delete (num list) (* Ids to delete *)
| Cutting constr (* Adds a constraint using cutting planes, written in reverse polish notation *)
| Rup npbc (num list) (* Add a constraint by RUP *)
| Con npbc (lstep list) num
(* Prove constraint by contradiction, indicated by the id *)
| Check num npbc
(* Debugging / other steps are parsed as no ops *)
| NoOp (* Other steps are parsed as no ops *)
End
Definition lstep_ok_def[simp]:
(lstep_ok (Con c pfs n) ⇔
compact c ∧ (EVERY lstep_ok pfs)) ∧
(lstep_ok (Rup c ls) ⇔ compact c) ∧
(lstep_ok _ ⇔ T)
Termination
WF_REL_TAC ‘measure lstep_size’ \\ rw []
End
(*
The type of PB formulas represented as a finite map
num -> pbc
*)
Type pbf = ``:(npbc # bool) spt``;
(* b = T forces all operations to respect/use the core directly *)
Definition lookup_core_only_def:
lookup_core_only b fml n =
case lookup n fml of
NONE => NONE
| SOME (x,b') =>
if ¬b ∨ b' then SOME x
else NONE
End
Definition check_cutting_def:
(check_cutting b (fml:pbf) (Id n) =
lookup_core_only b fml n) ∧
(check_cutting b fml (Add c1 c2) =
OPTION_MAP2 add (check_cutting b fml c1) (check_cutting b fml c2)) ∧
(check_cutting b fml (Mul c k) =
OPTION_MAP (λc. multiply c k) (check_cutting b fml c)) ∧
(check_cutting b fml (Div c k) =
if k ≠ 0 then
OPTION_MAP (λc. divide c k) (check_cutting b fml c)
else NONE) ∧
(check_cutting b fml (Sat c) =
OPTION_MAP saturate (check_cutting b fml c)) ∧
(check_cutting b fml (Lit (Pos v)) = SOME ([(1,v)],0)) ∧
(check_cutting b fml (Lit (Neg v)) = SOME ([(-1,v)],0)) ∧
(check_cutting b fml (Weak c var) =
OPTION_MAP (λc. weaken c var) (check_cutting b fml c))
End
Definition check_contradiction_fml_def:
check_contradiction_fml b (fml:pbf) n =
case lookup_core_only b fml n of
NONE => F
| SOME c =>
check_contradiction c
End
(* Insertion into derived set, unless we're in core only mode *)
Definition insert_fml_def:
insert_fml b fml id c =
(insert id (c,b) fml,
id+1)
End
Definition rup_pass1_def:
rup_pass1 assg [] acc ys = (acc,ys) ∧
rup_pass1 assg ((i:int,n:num)::xs) acc ys =
let k = Num (ABS i) in
case FLOOKUP assg n of
| NONE => rup_pass1 assg xs (acc + k) ((k,i,n)::ys)
| SOME T => rup_pass1 assg xs (if i < 0 then acc else acc + k) ys
| SOME F => rup_pass1 assg xs (if i < 0 then acc + k else acc) ys
End
Definition rup_pass2_def:
rup_pass2 assg max [] l = SOME assg ∧
rup_pass2 assg max ((k:num,i:int,n:num)::ys) l =
if max < l + k then
rup_pass2 (assg |+ (n,0 ≤ i)) max ys l
else
rup_pass2 assg max ys l
End
(* Reduce c according to the assignment,
return NONE if it gives a contradiction
otherwise return the updated assignment using units in (ls,n) *)
Definition update_assg_def:
update_assg assg (ls,n) do_check =
let (max,ls1) = rup_pass1 assg ls 0 [] in
if max < n ∧ do_check then NONE else
rup_pass2 assg max ls1 n
End
(* Here, assg could be a finite map of variables to T/F
assg' should be assg updated with all units under assg
id = 0 is special for nc
*)
Definition get_rup_constraint_def:
get_rup_constraint b fml n nc =
if n = 0 then SOME nc
else
lookup_core_only b fml n
End
Definition check_rup_def:
(check_rup b nc fml (assg: num |-> bool) [] = F) ∧
(check_rup b nc fml assg (n::ns) =
case get_rup_constraint b fml n nc of
NONE => F
| SOME c =>
case update_assg assg c (NULL ns) of
| NONE => T
| SOME assg' => check_rup b nc fml assg' ns)
End
Definition check_lstep_def:
(check_lstep lstep b (fml:pbf) (id:num) =
case lstep of
| Delete ls =>
if EVERY (λid. lookup_core_only T fml id = NONE) ls
then SOME (FOLDL (λa b. delete b a) fml ls,id)
else NONE
| Cutting constr =>
(case check_cutting b fml constr of
NONE => NONE
| SOME c =>
SOME (insert_fml b fml id c))
| Rup c ls =>
if check_rup b (not c) fml FEMPTY ls then
SOME (insert_fml b fml id c)
else NONE
| Con c pf n =>
let (fml_not_c,id1) = insert_fml b fml id (not c) in
(case check_lsteps pf b fml_not_c id1 of
SOME (fml',id') =>
if check_contradiction_fml b fml' n
then SOME (insert_fml b fml id' c)
else NONE
| _ => NONE)
| Check n c =>
(case lookup n fml of NONE => NONE
| SOME (c',b) =>
if c = c' then SOME(fml,id)
else NONE)
| NoOp => SOME(fml,id)) ∧
(check_lsteps [] b fml id = SOME (fml,id)) ∧
(check_lsteps (step::steps) b fml id =
case check_lstep step b fml id of
SOME(fml',id') =>
check_lsteps steps b fml' id'
| res => res)
Termination
WF_REL_TAC ‘measure (
sum_size (lstep_size o FST)
(list_size lstep_size o FST) )’
End
Theorem satisfies_SUBSET:
Y ⊆ X ⇒
(satisfies w X ⇒ satisfies w Y)
Proof
rw[satisfies_def]>>
metis_tac[SUBSET_DEF]
QED
Definition core_only_fml_def:
core_only_fml b fml =
if b then
{v | ∃n. lookup n fml = SOME(v,b)}
else
{v | ∃n b. lookup n fml = SOME(v,b)}
End
Theorem check_cutting_correct:
∀fml n c w.
check_cutting b fml n = SOME c ∧
satisfies w (core_only_fml b fml) ⇒
satisfies_npbc w c
Proof
Induct_on`n`>>rw[check_cutting_def]
>- (
(*Id case*)
fs[lookup_core_only_def,core_only_fml_def]>>
every_case_tac>>
fs[satisfies_def,satisfies_npbc_def,range_def,PULL_EXISTS]>>
metis_tac[])
>- (
(* add case *)
match_mp_tac add_thm>>
metis_tac[])
>- (
(* multiply case *)
match_mp_tac multiply_thm>>
metis_tac[])
>- (
(* divide case *)
match_mp_tac divide_thm>>
metis_tac[])
>- (
(* saturate case *)
match_mp_tac saturate_thm>>
metis_tac[])
>- (
(* literal case *)
Cases_on`l`>>fs[check_cutting_def]>>rveq>>
EVAL_TAC)
>- (
(* weaken case *)
match_mp_tac weaken_thm>>
metis_tac[])
QED
Theorem check_cutting_compact:
∀n c.
(∀c. c ∈ core_only_fml b fml ⇒ compact c) ∧
check_cutting b fml n = SOME c ⇒
compact c
Proof
Induct_on`n`>>rw[check_cutting_def]
>- (
(*Id case*)
fs[core_only_fml_def,lookup_core_only_def]>>
every_case_tac>>gvs[]>>
metis_tac[])
>- (
(* add case *)
metis_tac[compact_add])
>- (
(* multiply case *)
metis_tac[compact_multiply])
>- (
metis_tac[compact_divide])
>- (
metis_tac[compact_saturate])
>- (
(* literal case *)
Cases_on`l`>>fs[check_cutting_def]>>rveq>>
EVAL_TAC)
>- (
(* weaken case *)
metis_tac[compact_weaken])
QED
Definition id_ok_def:
id_ok fml id = ∀n. id ≤ n ⇒ n ∉ domain fml
End
Theorem sat_implies_refl[simp]:
fml ⊨ fml
Proof
rw[sat_implies_def]
QED
Theorem range_FOLDL_delete:
∀ls v.
range (FOLDL (λa b. delete b a) v ls) ⊆ range v
Proof
Induct>>rw[]>>
first_x_assum (qspec_then`delete h v` mp_tac)>>rw[]>>
match_mp_tac SUBSET_TRANS>>
asm_exists_tac>>simp[]>>
simp[range_delete]
QED
Theorem sat_implies_subset:
s ⊆ t ⇒
t ⊨ s
Proof
rw[sat_implies_def,satisfies_def,SUBSET_DEF]>>
metis_tac[]
QED
Theorem satisfiable_subset:
satisfiable t ∧ s ⊆ t ⇒
satisfiable s
Proof
rw[satisfiable_def,SUBSET_DEF,satisfies_def]>>
metis_tac[]
QED
Theorem id_ok_insert_1:
id_ok fml id ⇒
id_ok (insert id c fml) (id+1)
Proof
rw[id_ok_def]
QED
Theorem domain_FOLDL_delete:
∀ls v.
domain (FOLDL (λa b. delete b a) v ls) =
(domain v) DIFF set ls
Proof
Induct>>rw[]>>
simp[DIFF_INSERT]
QED
Theorem id_ok_FOLDL_delete:
∀l fml.
id_ok fml id ⇒
id_ok (FOLDL (λa b. delete b a) fml l) id
Proof
Induct \\ fs[] \\ rw[]
\\ first_x_assum irule
\\ fs [id_ok_def,domain_delete]
QED
Theorem lookup_FOLDL_delete:
∀ls v.
lookup n (FOLDL (λa b. delete b a) v ls) =
if MEM n ls then NONE else lookup n v
Proof
Induct>>rw[]>>
first_x_assum (qspec_then`delete h v` mp_tac)>>rw[]>>
fs[lookup_delete]
QED
Theorem check_contradiction_fml_unsat:
check_contradiction_fml b fml n ⇒
unsatisfiable (core_only_fml b fml)
Proof
rw[check_contradiction_fml_def,core_only_fml_def,lookup_core_only_def]>>
every_case_tac>>fs[]>>
drule check_contradiction_unsat>>
rw[unsatisfiable_def,satisfiable_def,range_def,satisfies_def]>>
metis_tac[]
QED
Theorem core_only_fml_insert:
id ∉ domain fml ⇒
core_only_fml b (insert id (c,b) fml) =
c INSERT core_only_fml b fml
Proof
rw[core_only_fml_def,EXTENSION,lookup_insert]>>
eq_tac>>rw[]>>
every_case_tac>>fs[domain_lookup]>>
metis_tac[]
QED
Definition agree_assg_def:
agree_assg assg w ⇔
∀x b.
FLOOKUP assg x = SOME b ⇒
w x = b
End
Theorem rup_pass1_thm:
∀assg xs acc ys acc1 ys1 w.
rup_pass1 assg xs acc ys = (acc1,ys1) ∧ agree_assg assg w ⇒
SUM (MAP (eval_term w) xs) + acc ≤ acc1 ∧
(∀n i k. MEM (n,i,k) ys1 ⇒ MEM (n,i,k) ys ∨ n = Num (ABS i)) ∧
∃xs1.
SUM (MAP (eval_term w) xs1) + lslack (MAP SND ys1) + acc ≤ acc1 + lslack (MAP SND ys) ∧
SUM (MAP (eval_term w) xs) + SUM (MAP (eval_term w) (MAP SND ys)) =
SUM (MAP (eval_term w) xs1) + SUM (MAP (eval_term w) (MAP SND ys1))
Proof
Induct_on ‘xs’ \\ fs [rup_pass1_def]
>- (rw [] \\ qexists_tac ‘[]’ \\ gvs [])
\\ PairCases \\ fs [rup_pass1_def]
\\ rpt gen_tac \\ gvs [AllCaseEqs()] \\ strip_tac \\ gvs []
>- (last_x_assum dxrule_all \\ fs [lslack_def] \\ strip_tac \\ gvs []
\\ conj_tac >- (Cases_on ‘w h1’ \\ gvs [] \\ metis_tac [])
\\ conj_tac >- metis_tac []
\\ qexists_tac ‘xs1’ \\ gvs [])
\\ last_x_assum drule_all \\ strip_tac \\ fs []
\\ gvs [agree_assg_def] \\ res_tac \\ gvs []
\\ IF_CASES_TAC \\ gvs []
>- (qexists_tac ‘xs1’ \\ gvs [])
>- (qexists_tac ‘(h0,h1)::xs1’ \\ gvs [])
>- (qexists_tac ‘(h0,h1)::xs1’ \\ gvs [])
>- (qexists_tac ‘xs1’ \\ gvs [])
QED
Theorem rup_pass2_thm:
∀assg d1 slack ls1 d hc.
agree_assg assg w ∧
c1 ≤ d + SUM (MAP (eval_term w) (MAP SND ls1)) ∧
d + lslack (MAP SND ls1) ≤ max ∧
(∀n i k. MEM (n,i,k) ls1 ⇒ n = Num (ABS i)) ∧
rup_pass2 assg max ls1 c1 = SOME assg1
⇒
agree_assg assg1 w
Proof
Induct_on ‘ls1’ \\ fs [rup_pass2_def] \\ PairCases
\\ gvs [rup_pass2_def] \\ rpt gen_tac \\ strip_tac
\\ qabbrev_tac ‘x = if h1 < 0 then 1 − b2n (w h2) else b2n (w h2)’
\\ ‘x ≤ 1’ by (rw [Abbr‘x’] \\ Cases_on ‘w h2’ \\ gvs [])
\\ reverse (Cases_on ‘max < c1 + h0’) \\ gvs []
\\ last_x_assum irule \\ fs []
\\ gvs [SF DNF_ss,SF SFY_ss]
\\ fs [lslack_def] \\ fs [GSYM lslack_def]
\\ qexists_tac ‘d + Num (ABS h1)’ \\ gvs []
\\ ‘c1 ≤ d + (Num (ABS h1) + SUM (MAP (eval_term w) (MAP SND ls1)))’ by
(Cases_on ‘x’ \\ gvs [ADD1] \\ Cases_on ‘n’ \\ gvs []) \\ fs []
\\ first_x_assum $ irule_at $ Pos hd \\ fs []
\\ gvs [agree_assg_def,FLOOKUP_SIMP] \\ rw [] \\ gvs []
\\ rpt $ qpat_x_assum ‘∀x._’ kall_tac
\\ dxrule_all LESS_EQ_LESS_TRANS \\ strip_tac
\\ ‘d + lslack (MAP SND ls1) < c1’ by gvs []
\\ Cases_on ‘x = 1’ >- (Cases_on ‘w h2’ \\ gvs [AllCaseEqs()] \\ intLib.COOPER_TAC)
\\ ‘x = 0’ by gvs [] \\ gvs []
\\ ‘SUM (MAP (eval_term w) (MAP SND ls1)) ≤ lslack (MAP SND ls1)’ by fs [lslack_thm]
\\ gvs []
QED
Theorem update_assg_NONE:
update_assg assg c flag = NONE ⇒
∀w. agree_assg assg w ⇒
¬ satisfies_npbc w c
Proof
PairCases_on ‘c’
\\ rw[update_assg_def]
\\ pairarg_tac \\ gvs []
\\ drule_all rup_pass1_thm \\ strip_tac
\\ gvs [AllCaseEqs()]
\\ gvs [satisfies_npbc_def,GREATER_EQ,GSYM NOT_LESS]
\\ CCONTR_TAC \\ gvs [lslack_def]
\\ qsuff_tac
‘∀assg m ls1 c1. rup_pass2 assg m ls1 c1 ≠ NONE’
>- (strip_tac \\ gvs [])
\\ rpt $ pop_assum kall_tac
\\ Induct_on ‘ls1’ \\ gvs [rup_pass2_def]
\\ gvs [FORALL_PROD]
\\ gvs [rup_pass2_def] \\ rw []
QED
Theorem update_assg_SOME:
update_assg assg c flag = SOME assg' ⇒
∀w. agree_assg assg w ∧ satisfies_npbc w c ⇒
agree_assg assg' w
Proof
PairCases_on ‘c’
\\ rw[update_assg_def]
\\ pairarg_tac \\ gvs []
\\ drule_all rup_pass1_thm \\ strip_tac
\\ gvs [AllCaseEqs()]
\\ gvs [satisfies_npbc_def,GREATER_EQ,EVAL “lslack []”]
\\ irule rup_pass2_thm
\\ last_x_assum $ irule_at Any
\\ gvs [SF SFY_ss]
QED
Theorem check_rup_unsat:
∀ns assg.
check_rup b nc fml assg ns ∧
agree_assg assg w ⇒
¬(satisfies_npbc w nc ∧
satisfies w (core_only_fml b fml))
Proof
Induct>>rw[check_rup_def]>>
gvs[get_rup_constraint_def,AllCasePreds(),AllCaseEqs()]
>- (
drule update_assg_NONE>>
disch_then drule>>
fs[lookup_core_only_def,core_only_fml_def]>>
every_case_tac>>fs[satisfies_def,PULL_EXISTS]>>
metis_tac[])
>- (
drule update_assg_SOME>>
disch_then drule>>
Cases_on`satisfies_npbc w x`>>rw[]
>-
metis_tac[]>>
fs[lookup_core_only_def,core_only_fml_def]>>
every_case_tac>>fs[satisfies_def,PULL_EXISTS]>>
metis_tac[])
>- (
drule update_assg_NONE>>
disch_then drule>>
fs[lookup_core_only_def,core_only_fml_def]>>
every_case_tac>>fs[satisfies_def,PULL_EXISTS]>>
metis_tac[])
>- (
drule update_assg_SOME>>
disch_then drule>>
Cases_on`satisfies_npbc w c`>>rw[]
>-
metis_tac[]>>
fs[lookup_core_only_def,core_only_fml_def]>>
every_case_tac>>fs[satisfies_def,PULL_EXISTS]>>
metis_tac[])
QED
(* if b = F, then the core should be untouched,
otherwise the core can grow *)
Theorem check_lstep_correct:
(∀step b fml id.
id_ok fml id ⇒
case check_lstep step b fml id of
SOME (fml',id') =>
id ≤ id' ∧
id_ok fml' id' ∧
(∀n x.
(lookup n fml = SOME (x,T) ⇒
lookup n fml' = SOME (x,T))) ∧
(¬b ⇒
(∀n x.
(lookup n fml' = SOME (x,T) ⇒
lookup n fml = SOME (x,T)))) ∧
(core_only_fml b fml) ⊨ (core_only_fml b fml')
| NONE => T) ∧
(∀steps b fml id.
id_ok fml id ⇒
case check_lsteps steps b fml id of
| SOME (fml',id') =>
id ≤ id' ∧
id_ok fml' id' ∧
(∀n x.
lookup n fml = SOME (x,T) ⇒
lookup n fml' = SOME (x,T)) ∧
(¬b ⇒
(∀n x.
(lookup n fml' = SOME (x,T) ⇒
lookup n fml = SOME (x,T)))) ∧
(core_only_fml b fml) ⊨ (core_only_fml b fml')
| NONE => T)
Proof
ho_match_mp_tac check_lstep_ind \\
reverse (rpt strip_tac)
>- (
simp[Once check_lstep_def]>>
every_case_tac>>gs[]>>
fs[sat_implies_def,satisfiable_def]>>
metis_tac[])
>- simp[Once check_lstep_def]
\\ Cases_on ‘∃n c. step = Check n c’
>- (
rw[check_lstep_def] \\ every_case_tac \\ fs [] \\ metis_tac[sat_implies_refl])
\\ Cases_on ‘∃n. step = Delete n’
THEN1 (
simp[check_lstep_def] \\
Cases_on`step` \\ fs[] \\
every_case_tac \\ rw []
>- metis_tac[id_ok_FOLDL_delete]
>- (
fs[lookup_FOLDL_delete,EVERY_MEM,lookup_core_only_def,AllCaseEqs()]>>
metis_tac[option_CLAUSES,PAIR,SND])
>- fs[lookup_FOLDL_delete]
>- (
match_mp_tac sat_implies_subset>>
simp[core_only_fml_def,SUBSET_DEF]>>rw[]>>
fs[lookup_FOLDL_delete]>>
metis_tac[]))
\\ Cases_on ‘step = NoOp’ >- simp[check_lstep_def]
\\ Cases_on ‘∃c. step = Cutting c’
THEN1 (
simp[check_lstep_def] \\
Cases_on`step` \\ fs[] \\
every_case_tac \\ simp [] \\
gvs[insert_fml_def]>>
drule check_cutting_correct>>
DEP_REWRITE_TAC[core_only_fml_insert] >> fs[id_ok_def]>>
rw[]>>fs[sat_implies_def,core_only_fml_def]
>- (
rw[lookup_insert]>>fs[]>>
first_x_assum(qspec_then`id` mp_tac)>>
fs[domain_lookup])>>
gvs[lookup_insert,AllCaseEqs()])
\\ Cases_on `∃c ls. step = Rup c ls`
THEN1 (
fs[check_lstep_def] \\
every_case_tac \\
gvs[insert_fml_def]
\\ CONJ_TAC >- fs[id_ok_def]
\\ CONJ_TAC >- (
rw[lookup_insert]>>fs[id_ok_def]>>
last_x_assum(qspec_then`id` mp_tac)>>simp[]>>
metis_tac[domain_lookup])
\\ CONJ_TAC >- (
rw[]>>gvs[lookup_insert,AllCaseEqs()])
\\ drule check_rup_unsat
\\ simp[agree_assg_def]
\\ rw[] \\ fs[sat_implies_def]
\\ rw[]
\\ gvs[id_ok_def,core_only_fml_insert]
\\ metis_tac[not_thm])
(* Proof by contradiction *)
\\ Cases_on ‘∃c steps n. step = Con c steps n’
THEN1 (
gvs[check_lstep_def,insert_fml_def]
\\ last_x_assum mp_tac
\\ impl_tac >- (
fs[id_ok_def,SUBSET_DEF]>>rw[]>>
metis_tac[])
\\ TOP_CASE_TAC \\ gvs[AllCaseEqs()]
\\ TOP_CASE_TAC \\ fs[]
\\ strip_tac
\\ rpt(TOP_CASE_TAC \\ fs[])
\\ gvs[]
\\ CONJ_TAC >- fs[id_ok_def]
\\ CONJ_TAC >- (
rw[lookup_insert]>>fs[id_ok_def]>>
last_x_assum(qspec_then`n'` mp_tac)>>simp[]>>
metis_tac[domain_lookup])
\\ CONJ_TAC >- (
rw[]>>gvs[lookup_insert,AllCaseEqs()])
\\ drule check_contradiction_fml_unsat
\\ rw[] \\ fs[unsatisfiable_def,sat_implies_def]
\\ rw[]
\\ gvs[id_ok_def,core_only_fml_insert]
\\ CCONTR_TAC \\ fs[GSYM not_thm]
\\ first_x_assum(qspec_then`w` mp_tac)
\\ impl_tac >- metis_tac[not_thm]
\\ metis_tac[satisfies_def,satisfiable_def])
THEN1 (
rw[Once check_lstep_def]
\\ every_case_tac \\ gvs [])
QED
(* TODO
Theorem check_lstep_compact:
(∀step b core fml id fml' core' id'.
(∀c. c ∈ range fml ⇒ compact c) ∧ lstep_ok step ∧
check_lstep step b core fml id = SOME(fml',core',id') ⇒
(∀c. c ∈ range fml' ⇒ compact c)) ∧
(∀steps b core fml id fml' core' id'.
(∀c. c ∈ range fml ⇒ compact c) ∧ EVERY lstep_ok steps ∧
check_lsteps steps b core fml id = SOME(fml',core',id') ⇒
(∀c. c ∈ range fml' ⇒ compact c))
Proof
ho_match_mp_tac check_lstep_ind
\\ rpt strip_tac
>- (
Cases_on`step`>>fs[check_lstep_def]
>- metis_tac[range_FOLDL_delete,SUBSET_DEF]
>- (
gvs[insert_fml_def,AllCaseEqs()]>>
drule range_insert_2>>rw[]>>
metis_tac[check_cutting_compact])
\\ gvs[AllCaseEqs(),insert_fml_def]
\\ imp_res_tac range_insert_2 \\ gvs [] )
>- fs[Once check_lstep_def]
>- (
qpat_x_assum`_ = SOME _` mp_tac>>
simp[Once check_lstep_def]>>
every_case_tac>>fs[])
QED
*)
Type subst_raw = ``:(num , bool + num lit) alist``;
(* Steps that preserve satisfiability modulo
a preserved set of variables *)
Datatype:
sstep =
| Lstep lstep (* Id representing a contradiction *)
| Red npbc subst_raw
(( ((num + num) # num) option, (lstep list)) alist)
(num option)
(* the alist represents a subproof
NONE -> top level step
SOME (INL n,id) -> database proofgoals, contradiction at id
SOME (INR n,id) -> # proofgoals, contradiction at id *)
End
(* The list of subgoals for redundancy
numbered #0 ... *)
Definition red_subgoals_def:
red_subgoals ord s def obj =
let c0 = subst s def in (**)
let cobj =
case obj of NONE => []
| SOME l => [[not (obj_constraint s l)]] in
[not c0]::(MAP (λc. [not c]) (dom_subst s ord)) ++ cobj
End
(* Apply a substitution where needed *)
Definition extract_clauses_def:
(extract_clauses f b fml rsubs [] acc =
SOME (REVERSE acc)) ∧
(extract_clauses f b fml rsubs (cpf::pfs) acc =
case cpf of
(NONE,pf) =>
extract_clauses f b fml rsubs pfs ((NONE,pf)::acc)
| (SOME (INL n,i),pf) =>
(case lookup_core_only b fml n of
NONE => NONE
| SOME c =>
extract_clauses f b fml rsubs pfs
((SOME ([not (subst f c)],i),pf)::acc))
| (SOME (INR u,i),pf) =>
if u < LENGTH rsubs then
extract_clauses f b fml rsubs pfs ((SOME (EL u rsubs,i),pf)::acc)
else
NONE)
End
Theorem extract_clauses_MAP_SND:
∀f b fml rsubs pfs acc res.
extract_clauses f b fml rsubs pfs acc = SOME res ⇒
MAP SND res = REVERSE (MAP SND acc) ++ MAP SND pfs
Proof
Induct_on`pfs`>>rw[extract_clauses_def] >> simp[MAP_REVERSE]>>
every_case_tac>>fs[]>>
first_x_assum drule>>
rw[]
QED
Definition list_insert_fml_def:
(list_insert_fml b fml id [] =
(fml,id)) ∧
(list_insert_fml b fml id (c::cs) =
case insert_fml b fml id c of
(fml',id') =>
list_insert_fml b fml' id' cs)
End
(* Check a list of subproofs *)
Definition check_subproofs_def:
(check_subproofs [] b fml id = SOME (fml,id)) ∧
(check_subproofs ((cnopt,pf)::pfs) b fml id =
case cnopt of
NONE => (* no clause given *)
(case check_lsteps pf b fml id of
SOME (fml',id') =>
check_subproofs pfs b fml' id'
| res => NONE)
| SOME (cs,n) =>
(let (cfml,cid) = list_insert_fml b fml id cs in
case check_lsteps pf b cfml cid of
SOME(fml',id') =>
if check_contradiction_fml b fml' n
then check_subproofs pfs b fml id'
else NONE
| res => NONE))
End
Type subst = ``:(num # (bool + num lit)) + (bool + num lit) option vector``;
Definition subst_fun_def:
subst_fun (s:subst) n =
case s of
INL (m,v) => if n = m then SOME v else NONE
| INR s =>
if n < length s then
sub s n
else NONE
End
Definition map_opt_def:
map_opt f LN = LN ∧
map_opt f (LS x) = (case f x of NONE => LN | SOME a => LS a) ∧
map_opt f (BN t1 t2) = mk_BN (map_opt f t1) (map_opt f t2) ∧
map_opt f (BS t1 x t2) =
case f x of
| NONE => mk_BN (map_opt f t1) (map_opt f t2)
| SOME a => mk_BS (map_opt f t1) a (map_opt f t2)
End
(* Extract the INL and INR
ids which were proved by a list of proofs *)
Definition extract_pids_def:
(extract_pids [] l r = (l,r)) ∧
(extract_pids (cpf::pfs) l r =
case FST cpf of
NONE => extract_pids pfs l r
| SOME (i,n) =>
(case i of
INL i => extract_pids pfs (insert i () l) r
| INR i => extract_pids pfs l (insert i () r)))
End
Definition list_pair_eq_def:
list_pair_eq xs ys =
case xs of
| [] => (case ys of [] => T | _ => F)
| (x1,x2)::xs => (case ys of
| [] => F
| ((y1,y2)::ys) =>
if x1 = y1 ∧ x2 = y2 then list_pair_eq xs ys else F)
End
Definition equal_constraint_def:
equal_constraint (x2,x3) ((y2,y3):(int # num) list # num) ⇔
if x3 = y3 then
list_pair_eq x2 y2
else F
End
Definition mem_constraint_def:
mem_constraint (c:(int # num) list # num) [] = F ∧
mem_constraint c (x::xs) =
if equal_constraint c x then T else mem_constraint c xs
End
(* Checking triviality for a negated target c *)
Definition check_triv_def:
check_triv extra nc =
(check_contradiction (add extra nc) ∨
check_contradiction nc)
End
(* Partition the formula goals into proved and non-proved
For each non-proved goal, check if
0) it is implied by extra (= ¬ C) or trivial
1) it was already in the formula
2) it was already proved by another proofgoal (excluding #)
*)
Definition split_goals_def:
split_goals (fml:npbc num_map)
(extra:npbc) (proved:num_set)
(goals:(num # (int # num) list # num) list) =
let (lp,lf) =
PARTITION (λ(i,c). sptree$lookup i proved ≠ NONE) goals in
let proved = MAP SND lp in
let f = range fml in
EVERY (λ(i,c). c ∈ f ∨ check_triv extra (not c) ∨ mem_constraint c proved) lf
End
Triviality list_pair_eq_thm:
∀xs ys. list_pair_eq xs ys ⇔ xs = ys
Proof
Induct \\ rw []
\\ Cases_on ‘ys’
\\ simp [Once list_pair_eq_def]
\\ PairCases_on ‘h’ \\ gvs []
\\ CASE_TAC \\ fs []
QED
Theorem mem_constraint_thm:
∀xs c. mem_constraint c xs = MEM c xs
Proof
Induct \\ fs [mem_constraint_def] \\ rw []
\\ qsuff_tac ‘equal_constraint c h ⇔ c = h’ >- fs []
\\ PairCases_on ‘c’
\\ PairCases_on ‘h’
\\ fs [equal_constraint_def,list_pair_eq_thm]
\\ rw [] \\ eq_tac \\ rw []
QED
(* The core formula, not executable *)
Definition mk_core_fml_def:
mk_core_fml b fml =
map_opt (λ(x,b').
if b ⇒ b' then SOME x else NONE) fml
End
Definition mk_subst_def:
(mk_subst [(n,v)] = INL (n,v)) ∧
(mk_subst xs = INR (spt_to_vec (fromAList xs)))
End
Definition check_hash_triv_def:
check_hash_triv extra ncs =
EXISTS (check_triv extra) ncs
End
(* pres : num_set -- forces all LHS of the
substitution to not contain pres *)
Definition check_pres_def:
check_pres pres s =
case pres of NONE => T
| SOME pres => EVERY (λx. lookup (FST x) pres = NONE) s
End
(* The tcb flag indicates we're in to-core mode
where it is guaranteed that the core formula implies derived *)
Definition check_red_def:
check_red pres ord obj b tcb fml id c s pfs idopt =
if check_pres pres s then
( let nc = not c in
let (fml_not_c,id1) = insert_fml b fml id (not c) in
let s = mk_subst s in
let w = subst_fun s in
let rsubs = red_subgoals ord w c obj in
case extract_clauses w b fml rsubs pfs [] of
NONE => NONE
| SOME cpfs =>
(case check_subproofs cpfs b fml_not_c id1 of
NONE => NONE
| SOME (fml',id') =>
let chk =
(case idopt of NONE =>
(
let gfml = mk_core_fml (b ∨ tcb) fml in
let goals = toAList (map_opt (subst_opt w) gfml) in
let (l,r) = extract_pids pfs LN LN in
split_goals gfml nc l goals ∧
EVERY (λ(id,cs).
lookup id r ≠ NONE ∨
check_hash_triv nc cs
)
(enumerate 0 rsubs))
| SOME cid =>
check_contradiction_fml b fml' cid) in
if chk then
SOME id'
else NONE) )
else NONE
End
Definition check_sstep_def:
check_sstep sstep (pres : num_set option) ord obj tcb
(fml:pbf) (id:num) =
case sstep of
Lstep lstep => check_lstep lstep F fml id
| Red c s pfs idopt =>
case check_red pres ord obj F tcb fml id c s pfs idopt of
SOME id' => SOME (insert_fml tcb fml id' c)
| NONE => NONE
End
Theorem sat_implies_transitive:
fml ⊨ fml' ∧ fml' ⊨ fml'' ⇒
fml ⊨ fml''
Proof
rw[sat_implies_def]
QED
Theorem unsat_iff_implies:
¬satisfiable (not x INSERT fml) ⇔ fml ⊨ {x}
Proof
fs [sat_implies_def,satisfiable_def,not_thm]
\\ metis_tac []
QED
Theorem list_insert_fml_ok:
∀xs fml id fml' id'.
list_insert_fml b fml id xs = (fml',id') ∧
id_ok fml id ⇒
id ≤ id' ∧
id_ok fml' id' ∧
core_only_fml b fml' =
set xs ∪ (core_only_fml b fml)
Proof
Induct>>simp[list_insert_fml_def]>>
ntac 6 strip_tac>>
gvs[AllCaseEqs(),insert_fml_def]>>
simp[]>>
first_x_assum drule>>
impl_tac >-
fs[id_ok_def]>>
rw[]>>