forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lprScript.sml
1303 lines (1202 loc) · 35.1 KB
/
lprScript.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
(*
Basic specification of an LPR checker (minimal optimization)
*)
open preamble miscTheory mlstringTheory satSemTheory;
val _ = new_theory "lpr";
(*
Bridging implementation and semantics
Clauses are lists of integers where positive (>0) integers map to INL and negative (<0) map to INR
In implementation, clauses must not contain 0
The ccnf type is the concrete "list"-based CNF representation
The tccnf type is the concrete "sptree"-based CNF representation used by LRAT/LPR checking
which adds an id to each clause
*)
Type lit = ``:int``;
Type cclause = ``:lit list``;
Type ccnf = ``:cclause list``;
Type tccnf = ``:cclause spt``;
val interp_lit_def = Define`
interp_lit (l:lit) =
if l > 0 then INL (Num (ABS l))
else INR (Num (ABS l))`
val interp_cclause_def = Define`
interp_cclause (ls:cclause) =
IMAGE interp_lit (set ls DIFF {0})`
val interp_def = Define`
interp (fml:ccnf) =
IMAGE interp_cclause (set fml)`
val interp_spt_def = Define`
interp_spt (fml:tccnf) = IMAGE interp_cclause (range fml)`
(* Implementation *)
val _ = Datatype`
lprstep =
| Delete (num list) (* Clauses to delete *)
| PR num cclause (cclause option) (num list) ((num,(num list)) alist)`
(* PR step:
PR n C wopt i0 (ik id ~> ik)
n is the new id of the clause C
wopt is a witnessing assignment (represented as a list of literals)
if w is NONE, then this reduces to RAT
i0 is a list of clause IDs for the AT part
ik is a alist mapping clause IDs to their hints
*)
Type lpr = ``:lprstep list``
val delete_literals_def = Define`
delete_literals (C:cclause) (D:cclause) =
FILTER (λx. ¬MEM x D) C`
(*
Checking for asymmetric tautology via unit propagation using the given hints.
Returns:
NONE => Error
SOME (INL ()) => C is an AT
SOME (INR C) => hints were insufficient, but C is now extended with units
*)
val is_AT_def = Define`
(is_AT fml [] (C:cclause) = SOME (INR C)) ∧
(is_AT fml (i::is) C =
case lookup i fml of
NONE => NONE
| SOME Ci =>
case delete_literals Ci C of
[] => SOME (INL ())
| [l] => is_AT fml is (-l :: C)
| _ => NONE)`
(* Check if Ci overlaps with a list of assignments *)
val check_overlap_def = Define`
(check_overlap Ci [] = F) ∧
(check_overlap Ci (a::as) =
(MEM a Ci ∨ check_overlap Ci as))`
(* flips a clause/assignment.
for a clause, this yields its blocked assignment *)
val flip_def = Define`
flip (C:cclause) = MAP (λi. -i) C`
(* Construct the overlapping assignment
{ w ∪ negate (C) }
where w overrides everything in negate(C)
*)
val overlap_assignment_def = Define`
overlap_assignment w C =
w ++ flip (delete_literals C w)`
(* The (L)RAT check (no witnesses) *)
val check_RAT_def = Define`
check_RAT fml p C ik (i,Ci) =
(* Step 5.1: if Ci contains -p do work, else skip *)
if check_overlap Ci [-p] then
(* Lookup the hint *)
case ALOOKUP ik i of
NONE => F
| SOME is =>
case is of
(* Step 5.2: Ci is satisfied by p ∪ negate (C) *)
[] => check_overlap Ci (overlap_assignment [p] C)
| _ =>
(* Step 5.3-5.5: Otherwise, use full hints *)
is_AT fml is (C ++ (delete_literals Ci [-p])) = SOME (INL ())
else
T`
(* Adding debug messages
open mlintTheory
val guard_def = Define`
guard P s =
if P then P else
(let _ = empty_ffi s in F)`
guard (check_overlap Ci w) (strlit "5.2.1 failed: " ^ mlint$toString (&i))
guard (check_overlap Ci (overlap_assignment w C)) (strlit "5.2.2 failed: " ^ mlint$toString (&i))
guard (is_AT fml is (C ++ (delete_literals Ci (flip (overlap_assignment w C)))) = SOME (INL ()))
*)
(* The (L)PR check (witness given) *)
val check_PR_def = Define`
check_PR fml w C ik (i,Ci) =
(* Step 5.1: if Ci is touched by w do work, else skip *)
if check_overlap Ci (flip w) then
(* Lookup the hint *)
case ALOOKUP ik i of
NONE =>
(* Step 5.2.1: Ci is satisfied by w *)
check_overlap Ci w
| SOME is =>
case is of
(* Step 5.2.2: Ci is satisfied by w ∪ negate (C) *)
[] =>
check_overlap Ci (overlap_assignment w C)
| _ =>
(* Step 5.3-5.5: Otherwise use full hints *)
is_AT fml is (C ++ (delete_literals Ci (flip (overlap_assignment w C)))) = SOME (INL ())
else
T`
val is_PR_def = Define`
is_PR fml p (C:cclause) wopt i0 ik =
(* First, do the asymmetric tautology check *)
case is_AT fml i0 C of
NONE => F
| SOME (INL ()) => T
| SOME (INR D) =>
if p ≠ 0 then
let iCs = toAList fml in
case wopt of
NONE => EVERY (check_RAT fml p D ik) iCs
| SOME w => ¬(check_overlap w (flip w)) ∧ EVERY (check_PR fml w D ik) iCs
else
F`
(*
Deletions and updates can only happen above position index mindel
By convention, setting mindel = 0 enables all deletions
(clauses are 1-indexed by the parser)
*)
val check_lpr_step_def = Define`
check_lpr_step mindel step fml =
case step of
Delete cl =>
if EVERY ($< mindel) cl then
SOME (FOLDL (\a b. delete b a) fml cl)
else
NONE
| PR n C w i0 ik =>
let p = case C of [] => 0 | (x::xs) => x in
if is_PR fml p C w i0 ik ∧ mindel < n then
SOME (insert n C fml)
else NONE`
(* Run the LPR checker on fml, returning an option *)
val check_lpr_def = Define`
(check_lpr mindel [] fml = SOME fml) ∧
(check_lpr mindel (step::steps) fml =
case check_lpr_step mindel step fml of
NONE => NONE
| SOME fml' => check_lpr mindel steps fml')`
(* Checking that the final formula contains a list of clauses *)
(* Canonical form for a clause, making transformation proofs easier to check *)
val sorted_dup_def = Define`
(sorted_dup (x::y::xs) =
if x = (y:int) then sorted_dup (x::xs)
else x::(sorted_dup (y::xs))) ∧
(sorted_dup ls = ls)`
val canon_clause_def = Define`
canon_clause cl = sorted_dup (QSORT (λi j. i ≤ (j:int)) cl)`
Theorem set_sorted_dup:
∀cl.
set (sorted_dup cl) = set cl
Proof
ho_match_mp_tac (fetch "-" "sorted_dup_ind")>>
rw[sorted_dup_def]
QED
Theorem set_QSORT:
set (QSORT R ls) = set ls
Proof
rw[EXTENSION,QSORT_MEM]
QED
Theorem canon_clause_interp:
interp_cclause (canon_clause cl) = interp_cclause cl
Proof
rw[canon_clause_def,interp_cclause_def]>>
simp[set_sorted_dup,set_QSORT]
QED
val contains_clauses_def = Define`
contains_clauses fml cls =
let ls = MAP (canon_clause o SND) (toAList fml) in
EVERY (λcl. MEM (canon_clause cl) ls) cls`
(* Checking unsatisfiability *)
val check_lpr_unsat_def = Define`
check_lpr_unsat lpr fml =
case check_lpr 0 lpr fml of
NONE => F
| SOME fml' => contains_clauses fml' [[]]`
(* Checking satisfiability equivalence after adding clauses *)
val check_lpr_sat_equiv_def = Define`
check_lpr_sat_equiv lpr fml mindel cls =
case check_lpr mindel lpr fml of
NONE => F
| SOME fml' => contains_clauses fml' cls`
(* Proofs *)
val wf_clause_def = Define`
wf_clause (C:cclause) ⇔ ¬ MEM 0 C`
val wf_fml_def = Define`
wf_fml (fml:tccnf) ⇔
∀C. C ∈ range fml ⇒ wf_clause C`
val wf_lpr_def = Define`
(wf_lpr (Delete _) = T) ∧
(wf_lpr (PR n C wopt i0 ik) =
(wf_clause C ∧
case C of [] => T
| h::t => case wopt of SOME w => MEM h w | _ => T)
)`
Theorem filter_unit_preserves_satisfies:
∀C.
(IMAGE (λl. {negate_literal l}) C) ⊆ fml ∧
satisfies w (D INSERT fml) ⇒
satisfies w ( (D DIFF C) INSERT fml)
Proof
rw[satisfies_def]
>-
(first_assum(qspec_then`D` assume_tac)>>fs[satisfies_clause_def]>>
`l ∉ C` by
(CCONTR_TAC>>
fs[SUBSET_DEF]>>
last_x_assum(qspec_then`{negate_literal l}` mp_tac)>>
impl_tac >-
metis_tac[]>>
strip_tac>>
first_x_assum(qspec_then`{negate_literal l}` assume_tac)>>rfs[]>>
metis_tac[satisfies_literal_exclusive])>>
metis_tac[])
>>
metis_tac[]
QED
Theorem filter_unit_preserves_unsatisfiable:
∀C.
(IMAGE (λl. {negate_literal l}) C) ⊆ fml ∧
unsatisfiable ((D DIFF C) INSERT fml) ⇒
unsatisfiable (D INSERT fml)
Proof
rw[unsatisfiable_def,satisfiable_def]>>
metis_tac[filter_unit_preserves_satisfies]
QED
Theorem interp_lit_eq:
x ≠ 0 ∧
interp_lit x = interp_lit y ⇒
x = y
Proof
rw[interp_lit_def]>>
intLib.ARITH_TAC
QED
Theorem interp_cclause_delete_literals:
interp_cclause (delete_literals C D) =
interp_cclause C DIFF interp_cclause D
Proof
simp[delete_literals_def]>>
fs[interp_cclause_def]>>
fs[EXTENSION]>>
rw[EQ_IMP_THM]>>
fs[MEM_FILTER]>>
metis_tac[interp_lit_eq]
QED
Theorem negate_literal_interp_lit:
h ≠ 0 ⇒
negate_literal (interp_lit h) = interp_lit (-h)
Proof
rw[negate_literal_def,interp_lit_def]>>
intLib.ARITH_TAC
QED
Theorem interp_cclause_negate_literal:
h ≠ 0 ⇒
IMAGE (λl. {negate_literal l}) (interp_cclause [-h]) =
{interp_cclause [h]}
Proof
simp[interp_cclause_def]>>
simp[negate_literal_interp_lit]
QED
Theorem interp_cclause_empty[simp]:
interp_cclause [] = {}
Proof
fs[interp_cclause_def]
QED
Theorem wf_cclause_empty[simp]:
wf_clause []
Proof
fs[wf_clause_def]
QED
Theorem interp_cclause_append:
interp_cclause (A++B) = interp_cclause A ∪ interp_cclause B
Proof
fs[interp_cclause_def,EXTENSION]>>
metis_tac[]
QED
Theorem interp_cclause_cons:
interp_cclause (h::C) =
interp_cclause [h] ∪ interp_cclause C
Proof
rw[interp_cclause_def]>>
metis_tac[INSERT_SING_UNION]
QED
(* is_AT is correct in the INL case *)
Theorem is_AT_imp_asymmetric_tautology:
∀is fml C.
wf_fml fml ∧ wf_clause C ∧
is_AT fml is C = SOME (INL ()) ⇒
asymmetric_tautology (interp_spt fml) (interp_cclause C)
Proof
Induct>>fs[is_AT_def]>>
ntac 4 strip_tac>>
every_case_tac>>fs[]>>
`interp_cclause x DIFF interp_cclause C = interp_cclause (delete_literals x C)` by
fs[interp_cclause_delete_literals]
>-
(fs[asymmetric_tautology_def]>>
qmatch_goalsub_abbrev_tac`unsatisfiable fml'`>>
`fml' = (interp_cclause x) INSERT fml'` by
(match_mp_tac (GSYM ABSORPTION_RWT)>>
fs[Abbr`fml'`,range_def,interp_spt_def]>>
metis_tac[])>>
pop_assum SUBST1_TAC>>
match_mp_tac filter_unit_preserves_unsatisfiable>>
qexists_tac`interp_cclause C`>>rw[]
>-
fs[Abbr`fml'`]
>>
match_mp_tac empty_clause_imp_unsatisfiable>>
simp[interp_cclause_def])
>>
`wf_clause x` by
(fs[wf_fml_def,range_def]>>
metis_tac[])>>
`MEM h' (delete_literals x C)` by fs[]>>
`-h' ≠ 0` by
(fs[wf_clause_def,delete_literals_def,MEM_FILTER]>>
metis_tac[])>>
last_x_assum drule>>
disch_then (qspec_then`(-h')::C` mp_tac)>>simp[]>>
impl_tac >-
(fs[wf_clause_def]>>
intLib.ARITH_TAC)>>
simp[asymmetric_tautology_def]>>
rw[]>>fs[]>>
qmatch_goalsub_abbrev_tac`unsatisfiable fml'`>>
`(interp_cclause x) INSERT fml' = fml'` by
(match_mp_tac ABSORPTION_RWT>>
fs[Abbr`fml'`,range_def,interp_spt_def]>>
metis_tac[])>>
pop_assum (SUBST1_TAC o SYM)>>
match_mp_tac filter_unit_preserves_unsatisfiable>>
qexists_tac`interp_cclause C`>>rw[]
>-
simp[Abbr`fml'`]
>>
fs[Abbr`fml'`]>>
pop_assum mp_tac>>
simp[Once interp_cclause_cons]>>
simp[interp_cclause_negate_literal]>>
metis_tac[UNION_INSERT_EQ,INSERT_SING_UNION]
QED
Theorem satisfies_clause_DIFF:
satisfies_clause w C ∧
¬satisfies_clause w D ⇒
satisfies_clause w (C DIFF D)
Proof
fs[satisfies_clause_def]>>
metis_tac[]
QED
Theorem is_AT_imp_sat_implies:
∀is fml C D.
wf_fml fml ∧ wf_clause C ⇒
is_AT fml is C = SOME (INR D) ⇒
∃E.
interp_cclause D = E ∪ interp_cclause C ∧
wf_clause D ∧
sat_implies
(interp_cclause D INSERT interp_spt fml)
(interp_cclause C INSERT interp_spt fml)
Proof
Induct>>fs[is_AT_def]>>
rw[]
>-
(qexists_tac`{}`>>simp[sat_implies_def])>>
every_case_tac>>fs[]>>
`interp_cclause x DIFF interp_cclause C = interp_cclause (delete_literals x C)` by
fs[interp_cclause_delete_literals]>>
`wf_clause x` by
(fs[wf_fml_def,range_def]>>
metis_tac[])>>
`MEM h' (delete_literals x C)` by fs[]>>
`-h' ≠ 0` by
(fs[wf_clause_def,delete_literals_def,MEM_FILTER]>>
metis_tac[])>>
first_x_assum drule>>
disch_then(qspecl_then [`-h'::C`,`D`] mp_tac)>>simp[]>>
impl_tac >-
(fs[wf_clause_def]>>
intLib.ARITH_TAC)>>
strip_tac>>
qexists_tac`E ∪ interp_cclause [-h']`>> simp[]>>
simp[Once interp_cclause_cons,UNION_ASSOC]>>
rw[sat_implies_def]>>fs[sat_implies_def]>>
qpat_x_assum` _ D = _` SUBST_ALL_TAC>>
first_x_assum drule>>
strip_tac>>
fs[satisfies_INSERT]>>
`satisfies_clause w (interp_cclause x)` by
(fs[satisfies_def,interp_spt_def,range_def]>>
metis_tac[])>>
CCONTR_TAC >> fs[]>>
`interp_cclause x DIFF interp_cclause C = interp_cclause [h']` by
fs[]>>
`satisfies_clause w (interp_cclause [h'])` by
metis_tac[satisfies_clause_DIFF]>>
fs[satisfies_clause_union]>>
qpat_x_assum`h' ≠ 0` mp_tac>>
rpt (qpat_x_assum`satisfies_clause w _` mp_tac)>>
PURE_REWRITE_TAC [Once interp_cclause_cons,Once satisfies_clause_union]>>
strip_tac>>
pop_assum mp_tac>>
rpt( pop_assum kall_tac)>>
rw[interp_cclause_def]>>
fs[satisfies_clause_def]>>
metis_tac[negate_literal_interp_lit,satisfies_literal_exclusive]
QED
Theorem check_overlap_eq:
∀D.
check_overlap C D ⇔
∃x. MEM x C ∧ MEM x D
Proof
Induct>>rw[check_overlap_def]>>
metis_tac[]
QED
(* Some sanity checks:
is_AT in INL() case is independent of ordering
*)
(* TODO: these antitone theorems can be stated more precisely *)
Theorem delete_literals_antitone:
∀ls.
(∀x. MEM x D ⇒ MEM x C) ⇒
(∀y. MEM y (delete_literals ls C) ⇒ MEM y (delete_literals ls D))
Proof
rw[delete_literals_def,MEM_FILTER]>>
metis_tac[]
QED
Theorem delete_literals_antitone_LENGTH:
∀ls.
(∀x. MEM x D ⇒ MEM x C) ⇒
LENGTH (delete_literals ls C) ≤ LENGTH (delete_literals ls D)
Proof
rw[delete_literals_def]>>
match_mp_tac LENGTH_FILTER_LEQ_MONO>>
metis_tac[]
QED
Theorem is_AT_INL_reorder:
∀is C D.
(∀x. MEM x D ⇒ MEM x C) /\ is_AT fml is D = SOME (INL ()) ⇒
is_AT fml is C = SOME (INL ())
Proof
Induct>> rw[is_AT_def]>>
TOP_CASE_TAC>>fs[]>>
TOP_CASE_TAC>>fs[]>>
TOP_CASE_TAC>>fs[]
>- (
drule delete_literals_antitone_LENGTH>>
disch_then(qspec_then`x` assume_tac)>>rfs[]>>
every_case_tac>>fs[]>>
`h'=h''` by
(drule delete_literals_antitone>>
disch_then(qspec_then`x` assume_tac)>>rfs[])>>
rw[]>>
metis_tac[MEM])
>>
drule delete_literals_antitone_LENGTH>>
disch_then(qspec_then`x` assume_tac)>>rfs[]>>
every_case_tac>>fs[]
QED
(* Converse is true assuming p and ¬p are not both in Ci *)
Theorem check_RAT_imp_check_PR:
check_RAT fml p C ik (i,Ci) ==>
check_PR fml [p] C ik (i,Ci)
Proof
rw[check_RAT_def,check_PR_def]>>
fs[flip_def,overlap_assignment_def]>>
every_case_tac>>fs[]>>
fs[check_overlap_eq]>>
rw[]>>fs[]>>
match_mp_tac is_AT_INL_reorder>>
HINT_EXISTS_TAC>>fs[]>>
simp[MAP_MAP_o,o_DEF]>>
rw[delete_literals_def,MEM_FILTER]>>
metis_tac[]
QED
(*
The witnessing assignment is
interp_cclause w ∪ (IMAGE negate_literal (interp_cclause C DIFF interp_cclause w))
*)
Theorem DIFF_ID:
A DIFF B = A ⇔ A ∩ B = {}
Proof
rw[EXTENSION]>>
metis_tac[]
QED
Theorem INTER_UNION_EMPTY:
A ∩ (B ∪ C) = {} ⇔
A ∩ B = {} ∧ A ∩ C = {}
Proof
rw[EXTENSION]>>
metis_tac[]
QED
Theorem MEM_overlap_assignment:
MEM x (overlap_assignment w C) =
(MEM x w ∨ MEM (-x) C ∧ ¬MEM (-x) w)
Proof
rw[overlap_assignment_def,flip_def,MEM_MAP,delete_literals_def,MEM_FILTER]>>
Cases_on`MEM x w`>>simp[]>>
rw[EQ_IMP_THM]>>fs[]>>
qexists_tac`-x`>> simp[]
QED
Theorem interp_cclause_flip:
interp_cclause (flip C) = IMAGE negate_literal (interp_cclause C)
Proof
rw[interp_cclause_def,flip_def,EXTENSION,MEM_MAP,EQ_IMP_THM]
>- (
qexists_tac`interp_lit i`>>
`i ≠ 0` by fs[]>>
simp[negate_literal_interp_lit]>>
metis_tac[])
>>
qexists_tac`-x''`>>
simp[negate_literal_interp_lit]
QED
Theorem check_PR_sat_implies:
check_PR fml w C ik (i,Ci) ∧ Ci ∈ range fml ∧
wf_fml fml ∧ wf_clause C ∧ consistent_par (interp_cclause C) ⇒
sat_implies (par (IMAGE negate_literal (interp_cclause C)) (interp_spt fml))
(par
(interp_cclause w ∪ (IMAGE negate_literal (interp_cclause C DIFF interp_cclause w)))
{interp_cclause Ci})
Proof
simp[check_PR_def]>>
reverse (Cases_on`check_overlap Ci (flip w)`)
>- (
(* 5.1: Ci not touched by w *)
rw[sat_implies_def,par_def,satisfies_def]>>
simp[DIFF_UNION]>>
`interp_cclause Ci DIFF IMAGE negate_literal (interp_cclause w) = interp_cclause Ci` by
(simp[DIFF_ID,interp_cclause_def,EXTENSION]>>
rw[]>>CCONTR_TAC>>fs[check_overlap_eq,flip_def,MEM_MAP]>>
metis_tac[interp_lit_eq,negate_literal_interp_lit])>>
fs[INTER_UNION_EMPTY,IMAGE_IMAGE]>>
first_x_assum match_mp_tac>>
simp[PULL_EXISTS,interp_spt_def]>>
fs[EXTENSION]>>
metis_tac[]) >>
simp[]>>
strip_tac>>
`wf_clause Ci` by
fs[wf_fml_def]>>
Cases_on`ALOOKUP ik i`>>
simp[]
>- (
(* 5.2.1: Ci already satisfied by w *)
rw[sat_implies_def,par_def,satisfies_def]>>
fs[INTER_UNION_EMPTY,check_overlap_eq]>>
`interp_lit x' ∈ interp_cclause Ci ∩ interp_cclause w` by
(fs[wf_clause_def]>>
simp[interp_cclause_def]>>
metis_tac[])>>
fs[EXTENSION]>>
metis_tac[])>>
Cases_on`x`>>fs[]
>- (
(* 5.2.2: Ci already satisfied by w ∪ alpha+ *)
rw[sat_implies_def,par_def,satisfies_def]>>
fs[INTER_UNION_EMPTY,check_overlap_eq]>>
qmatch_asmsub_abbrev_tac `A = {}`>>
qsuff_tac`interp_lit x' ∈ A`
>- fs[EXTENSION]>>
simp[Abbr`A`]>>
CONJ_ASM1_TAC >- (
fs[interp_cclause_def,wf_clause_def]>>
metis_tac[])>>
fs[MEM_overlap_assignment]
>- (
`interp_lit x' ∈ interp_cclause w` by
(fs[wf_clause_def]>>
simp[interp_cclause_def]>>
metis_tac[])>>
fs[EXTENSION]>>
metis_tac[])>>
qexists_tac`interp_lit (-x')`>>
`-x' ≠ 0` by (fs[wf_clause_def] >> metis_tac[])>>
simp[negate_literal_interp_lit]>>
fs[wf_clause_def,interp_cclause_def]>>
metis_tac[interp_lit_eq])>>
(* 5.3 *)
rw[sat_implies_def]>>
simp[satisfies_def,par_def]>>
strip_tac>>
drule is_AT_imp_asymmetric_tautology>>
qmatch_asmsub_abbrev_tac`is_AT _ _ D = SOME _`>>
disch_then(qspecl_then[`h::t`,`D`] mp_tac)>>
simp[]>>
impl_tac >-
(simp[Abbr`D`,delete_literals_def]>>
fs[wf_clause_def,MEM_FILTER])>>
simp[Abbr`D`,interp_cclause_append]>>
simp[overlap_assignment_def,interp_cclause_delete_literals,interp_cclause_flip,interp_cclause_append,IMAGE_IMAGE]>>
strip_tac>>
drule asymmetric_tautology_union_clause2 >>
disch_then drule>>
impl_tac >-
(fs[EXTENSION]>> metis_tac[])>>
strip_tac>>
drule asymmetric_tautology_satisfies>>
disch_then drule>>
simp[satisfies_INSERT,interp_cclause_delete_literals,interp_cclause_flip]>>
simp[overlap_assignment_def,interp_cclause_append,interp_cclause_flip,IMAGE_IMAGE,interp_cclause_delete_literals]>>
qmatch_goalsub_abbrev_tac`_ _ A ==> _ _ B`>>
qsuff_tac`A=B` >- fs[]>>
unabbrev_all_tac>>fs[EXTENSION]>>
metis_tac[]
QED
Theorem redundant_sat_implies:
sat_implies (D INSERT fml) (C INSERT fml) ∧
redundant fml D
⇒
redundant fml C
Proof
rw[sat_implies_def,redundant_def]>>
fs[]>>
metis_tac[satisfiable_def]
QED
Theorem sat_implies_range:
(∀C. C ∈ range fml ⇒ sat_implies A (par w {interp_cclause C})) ⇒
sat_implies A (par w (interp_spt fml))
Proof
rw[sat_implies_def,satisfies_def,interp_spt_def,par_def]>>
metis_tac[]
QED
Theorem consistent_par_union:
consistent_par A ∧
consistent_par B ∧
A ∩ B = {} ⇒
consistent_par (A ∪ IMAGE negate_literal B)
Proof
rw[consistent_par_def,IMAGE_IMAGE]>>
simp[INTER_UNION_EMPTY,INTER_UNION]>>
fs[EXTENSION]>>
metis_tac[negate_literal_11]
QED
Theorem consistent_par_DIFF:
consistent_par A ⇒ consistent_par (A DIFF B)
Proof
rw[consistent_par_def,EXTENSION]>>
metis_tac[]
QED
Theorem check_overlap_flip_consistent_par:
¬check_overlap x (flip x) ⇒
consistent_par (interp_cclause x)
Proof
rw[consistent_par_def,interp_cclause_def,EXTENSION,check_overlap_eq,flip_def,MEM_MAP]>>
CCONTR_TAC>>fs[]>>
rename1 `MEM A x`>>
qpat_x_assum`MEM _ _` mp_tac>>
rename1 `MEM B x`>>
first_x_assum(qspec_then`A` assume_tac)>>rfs[]>>
first_x_assum(qspec_then`B` assume_tac)>>rfs[]>>
`B ≠ -A` by intLib.ARITH_TAC>>
metis_tac[interp_lit_eq,negate_literal_interp_lit]
QED
Theorem is_PR_redundant:
wf_clause C ∧ wf_fml fml ∧
(p ≠ 0 ⇒ MEM p C ∧
case w of SOME ww =>
MEM p ww
| NONE => T) ∧
is_PR fml p C w i0 ik ⇒
redundant (interp_spt fml) (interp_cclause C)
Proof
simp[is_PR_def]>>
strip_tac>>
pop_assum mp_tac>>
ntac 2 (TOP_CASE_TAC>>fs[])
>-
metis_tac[is_AT_imp_asymmetric_tautology,asymmetric_tautology_redundant]
>>
strip_tac>>
drule is_AT_imp_sat_implies>>
disch_then drule>>
disch_then drule>>
strip_tac>>
drule redundant_sat_implies>>
disch_then match_mp_tac>>
reverse (Cases_on`consistent_par (E ∪ interp_cclause C)`)
>-
metis_tac[not_consistent_par_redundant]>>
reverse (Cases_on`w`)>>fs[]
>- (
(* PR *)
match_mp_tac par_redundant>>
qexists_tac`(interp_cclause x ∪
IMAGE negate_literal (E ∪ interp_cclause C DIFF interp_cclause x))`>>
simp[]>>
CONJ_TAC >- (
match_mp_tac consistent_par_union>>
rw[]
>-
metis_tac[check_overlap_flip_consistent_par]
>-
metis_tac[consistent_par_DIFF]
>>
simp[EXTENSION]>>
metis_tac[])>>
CONJ_TAC >- (
`interp_lit p ∈ interp_cclause x ∧ interp_lit p ∈ interp_cclause C` by
fs[interp_cclause_def]>>
simp[EXTENSION]>>
metis_tac[])>>
match_mp_tac sat_implies_range>>
rw[range_def]>>fs[EVERY_MEM,MEM_toAList,FORALL_PROD]>>
first_x_assum drule>>
strip_tac>>
drule check_PR_sat_implies>>
simp[]>>
impl_tac >-
(fs[range_def]>>
metis_tac[])>>
simp[])
>>
(* RAT *)
match_mp_tac par_redundant>>
qexists_tac`({interp_lit p} ∪
IMAGE negate_literal (E ∪ interp_cclause C DIFF {interp_lit p}))`>>
simp[]>>
CONJ_TAC >- (
match_mp_tac consistent_par_union>>
rw[]
>-
metis_tac[consistent_par_DIFF]
>>
simp[EXTENSION]>>
metis_tac[])>>
CONJ_TAC >- (
`interp_lit p ∈ interp_cclause C` by
fs[interp_cclause_def]>>
simp[EXTENSION]>>
metis_tac[])>>
match_mp_tac sat_implies_range>>
rw[range_def]>>fs[EVERY_MEM,MEM_toAList,FORALL_PROD]>>
first_x_assum drule>>
strip_tac>>
drule check_RAT_imp_check_PR>>
rw[]>>
drule check_PR_sat_implies>>
simp[]>>
impl_tac >-
(fs[range_def]>>
metis_tac[])>>
simp[interp_cclause_def]
QED
(* Deletion preserves sat: if C is satisfiable, then deleting clauses from C keeps satisfiability *)
Theorem delete_preserves_satisfiable:
satisfiable (interp_spt C) ⇒ satisfiable (interp_spt (delete n C))
Proof
match_mp_tac satisfiable_SUBSET>>
simp[interp_spt_def]>>
match_mp_tac IMAGE_SUBSET>>
metis_tac[range_delete]
QED
Theorem delete_clauses_sound:
∀l fml. satisfiable (interp_spt fml) ⇒
satisfiable (interp_spt (FOLDL (λa b. delete b a) fml l))
Proof
Induct>>simp[]>>
rw[]>>metis_tac[delete_preserves_satisfiable]
QED
Theorem interp_insert:
interp_spt (insert n p fml) ⊆ interp_cclause p INSERT interp_spt fml
Proof
simp[interp_spt_def,SUBSET_DEF,PULL_EXISTS]>>
rw[]>>
drule range_insert_2>>
rw[]>>
metis_tac[]
QED
Theorem check_lpr_step_sound:
∀mindel lpr fml fml'.
wf_fml fml ∧ wf_lpr lpr ∧
check_lpr_step mindel lpr fml = SOME fml' ⇒
(satisfiable (interp_spt fml) ⇒ satisfiable (interp_spt fml'))
Proof
rw[check_lpr_step_def]>>
qpat_x_assum `_ = SOME _`mp_tac>>
TOP_CASE_TAC>>fs[]
>-
(simp[]>>
metis_tac[delete_clauses_sound])
>>
rw[]>>
`redundant (interp_spt fml) (interp_cclause l)` by
(match_mp_tac (GEN_ALL is_PR_redundant)>>
simp[]>>
PURE_REWRITE_TAC[Once CONJ_ASSOC]>>
PURE_REWRITE_TAC[Once CONJ_COMM]>>
asm_exists_tac>>
simp[]>>
Cases_on`l`>>
fs[wf_lpr_def])>>
fs[redundant_def]>>
first_x_assum drule>>
match_mp_tac satisfiable_SUBSET>>
simp[interp_insert]
QED
Theorem wf_fml_delete_clauses:
∀l fml.
wf_fml fml ⇒
wf_fml (FOLDL (λa b. delete b a) fml l)
Proof
simp[FOLDL_FOLDR_REVERSE]>>
strip_tac>>
qabbrev_tac`ll= REVERSE l`>>
pop_assum kall_tac>>
Induct_on`ll`>>
rw[]>>first_x_assum drule>>
rw[wf_fml_def]>>
`C ∈ range (FOLDR (\b a . delete b a) fml ll)` by
metis_tac[range_delete,SUBSET_DEF]>>
fs[]
QED
Theorem wf_fml_insert:
wf_fml fml ∧ wf_clause l ⇒
wf_fml (insert n l fml)
Proof
fs[wf_fml_def]>>rw[]>>
drule range_insert_2>>
metis_tac[]
QED
Theorem check_lpr_step_wf_fml:
∀mindel lpr fml fml'.
wf_fml fml ∧ wf_lpr lpr ∧
check_lpr_step mindel lpr fml = SOME fml' ⇒
wf_fml fml'
Proof
rw[check_lpr_step_def]>>
qpat_x_assum `_ = SOME _`mp_tac>>
TOP_CASE_TAC>>fs[]
>-
(simp[]>>
metis_tac[wf_fml_delete_clauses])
>>
strip_tac>>
rveq>>fs[]>>
match_mp_tac wf_fml_insert>>fs[wf_lpr_def]
QED
(* The main operational theorem about check_lpr *)
Theorem check_lpr_sound:
∀mindel lpr fml.
wf_fml fml ∧ EVERY wf_lpr lpr ⇒
check_lpr mindel lpr fml = SOME fml' ⇒
wf_fml fml' ∧
(satisfiable (interp_spt fml) ⇒ satisfiable (interp_spt fml'))
Proof
Induct_on`lpr` >> simp[check_lpr_def]>>
ntac 3 strip_tac>>
every_case_tac>>fs[]>>
strip_tac>>
drule check_lpr_step_sound>>
rpt (disch_then drule)>>
drule check_lpr_step_wf_fml>>
rpt (disch_then drule)>>
strip_tac>>
strip_tac>>
strip_tac>>
first_x_assum drule>> simp[]>>
metis_tac[]
QED
(* Theorems about mindel *)
Theorem lookup_FOLDL_delete':
∀l fml.
lookup n fml = SOME c ∧
EVERY ($< mindel) l ∧ n ≤ mindel ⇒
lookup n (FOLDL (λa b. delete b a) fml l) = SOME c
Proof
Induct>>rw[]>>fs[]>>
first_x_assum match_mp_tac>>
rw[lookup_delete]>>
CCONTR_TAC>>fs[]
QED
Theorem check_lpr_step_mindel:
check_lpr_step mindel lpr fml = SOME fml' ⇒
∀n c. n ≤ mindel ∧
lookup n fml = SOME c ⇒
lookup n fml' = SOME c
Proof
rw[check_lpr_step_def]>>
every_case_tac>>fs[]>>rw[lookup_insert]>>
match_mp_tac lookup_FOLDL_delete'>>
fs[EVERY_MEM]
QED
Theorem check_lpr_mindel:
∀mindel lpr fml.
check_lpr mindel lpr fml = SOME fml' ⇒
∀n c. n ≤ mindel ∧
lookup n fml = SOME c ⇒
lookup n fml' = SOME c
Proof
Induct_on`lpr`>> rw[check_lpr_def]>>
every_case_tac>>fs[]>>
first_x_assum drule>>
disch_then drule>>
disch_then match_mp_tac>>
drule check_lpr_step_mindel>>