-
Notifications
You must be signed in to change notification settings - Fork 85
/
lpr_listScript.sml
2174 lines (2050 loc) · 61.4 KB
/
lpr_listScript.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 refines the LPR checker to a fixed-size, list-based implementation
These fixed-size lists (later refined to arrays) are used in three places:
1) Storing the formula
2) Marking clauses (in the is_AT step)
3) Tracking earliest occurences of pivots
*)
open preamble basis lprTheory;
val _ = new_theory "lpr_list"
Definition w8z_def:
w8z = (0w:word8)
End
Definition w8o_def:
w8o = (1w:word8)
End
Definition index_def:
index (i:int) =
if i ≤ 0 then
2 * Num(-i)
else
2 * Num(i) - 1
End
(* optimized for is_AT step *)
Definition delete_literals_sing_list_def:
(delete_literals_sing_list Clist [] = SOME 0) ∧
(delete_literals_sing_list Clist (c::cs) =
if any_el (index c) Clist w8z = w8o
then delete_literals_sing_list Clist cs
else (* c should be the only literal left *)
if EVERY (λi. any_el (index i) Clist w8z = w8o) cs
then SOME (~c)
else NONE)
End
Definition is_AT_list_aux_def:
(is_AT_list_aux fml [] C Clist = SOME (INR C, Clist)) ∧
(is_AT_list_aux fml (i::is) C Clist =
case any_el i fml NONE of
NONE => NONE
| SOME Ci =>
case delete_literals_sing_list Clist Ci of
NONE => NONE
| SOME nl =>
if nl = 0 then SOME (INL C, Clist)
else is_AT_list_aux fml is (nl::C) (update_resize Clist w8z w8o (index nl)))
End
Definition set_list_def:
(set_list Clist v [] = Clist) ∧
(set_list Clist v (c::cs) =
set_list (update_resize Clist w8z v (index c)) v cs)
End
Definition is_AT_list_def:
is_AT_list fml ls c Clist =
let Clist = set_list Clist w8o c in
case is_AT_list_aux fml ls c Clist of
NONE => NONE
| SOME (INL c, Clist) => SOME (INL (), set_list Clist w8z c)
| SOME (INR c, Clist) => SOME (INR c, set_list Clist w8z c)
End
Definition check_RAT_list_def:
check_RAT_list fml Clist np C ik (i:num) Ci =
if MEM np Ci then
case ALOOKUP ik i of
NONE => NONE
| SOME is =>
case is of
[] =>
if check_overlap Ci (overlap_assignment [-np] C)
then SOME Clist
else NONE
| _ =>
case is_AT_list fml is (C ++ (delete_literals Ci [np])) Clist of
SOME (INL (), Clist) => SOME Clist
| _ => NONE
else SOME Clist
End
Definition check_PR_list_def:
check_PR_list fml Clist nw C ik (i:num) Ci =
if check_overlap Ci nw then
case ALOOKUP ik i of
NONE =>
if check_overlap Ci (flip nw)
then SOME Clist
else NONE
| SOME is =>
case is of
[] =>
if check_overlap Ci (overlap_assignment (flip nw) C)
then SOME Clist
else NONE
| _ =>
case is_AT_list fml is (C ++ (delete_literals Ci (flip (overlap_assignment (flip nw) C)))) Clist of
SOME (INL (), Clist) => SOME Clist
| _ => NONE
else SOME Clist
End
Definition every_check_RAT_list_def:
(every_check_RAT_list fml Clist np C ik [] [] = SOME Clist) ∧
(every_check_RAT_list fml Clist np C ik (i::is) (Ci::Cis) =
case check_RAT_list fml Clist np C ik i Ci of
NONE => NONE
| SOME Clist => every_check_RAT_list fml Clist np C ik is Cis) ∧
(every_check_RAT_list fml Clist np C ik _ _ = NONE)
End
Definition every_check_PR_list_def:
(every_check_PR_list fml Clist nw C ik [] [] = SOME Clist) ∧
(every_check_PR_list fml Clist nw C ik (i::is) (Ci::Cis) =
case check_PR_list fml Clist nw C ik i Ci of
NONE => NONE
| SOME Clist => every_check_PR_list fml Clist nw C ik is Cis) ∧
(every_check_PR_list fml Clist nw C ik _ _ = NONE)
End
Definition min_opt_def:
min_opt i j =
case i of NONE => j
| SOME ii =>
(case j of
NONE => SOME ii
| SOME jj => SOME (MIN ii jj))
End
Definition list_min_opt_def:
(list_min_opt min [] = min) ∧
(list_min_opt min (i::is) =
list_min_opt (min_opt min i) is)
End
(* Clean up the index list *)
Definition reindex_def:
(reindex fml [] = ([],[])) ∧
(reindex fml (i::is) =
case any_el i fml NONE of
NONE => reindex fml is
| SOME v =>
let (l,r) = reindex fml is in
(i::l, v::r))
End
Definition reindex_partial_def:
(reindex_partial fml mini [] = ([],[],[])) ∧
(reindex_partial fml mini (i::is) =
if i ≥ mini then
case any_el i fml NONE of
NONE => reindex_partial fml mini is
| SOME v =>
let (l,r,rest) = reindex_partial fml mini is in
(i::l, v::r,rest)
else
([],[],i::is))
End
Definition every_check_RAT_inds_list_def:
(every_check_RAT_inds_list fml Clist np C ik mini [] acc = SOME (REVERSE acc, Clist)) ∧
(every_check_RAT_inds_list fml Clist np C ik mini (i::is) acc =
if i ≥ mini then
case any_el i fml NONE of
NONE => every_check_RAT_inds_list fml Clist np C ik mini is acc
| SOME Ci =>
case check_RAT_list fml Clist np C ik i Ci of
NONE => NONE
| SOME Clist => every_check_RAT_inds_list fml Clist np C ik mini is (i::acc)
else
SOME(REV acc (i::is), Clist))
End
(* rewrite into a simpler form without accumulator *)
Theorem every_check_RAT_inds_list_eq:
∀inds fml Clist np C ik mini acc.
every_check_RAT_inds_list fml Clist np C ik mini inds acc =
let (inds,vs,rest) = reindex_partial fml mini inds in
case every_check_RAT_list fml Clist np C ik inds vs of
NONE => NONE
| SOME Clist => SOME(REVERSE acc ++ inds ++ rest, Clist)
Proof
Induct>>rw[every_check_RAT_inds_list_def,reindex_partial_def,every_check_RAT_list_def]
>- (
TOP_CASE_TAC>>simp[]>>
pairarg_tac>>fs[]>>
simp[every_check_RAT_list_def]>>
TOP_CASE_TAC>>simp[]>>
metis_tac[APPEND_ASSOC,APPEND])
>>
simp[REV_REVERSE_LEM]
QED
Definition every_check_PR_inds_list_def:
(every_check_PR_inds_list fml Clist np C ik mini [] acc = SOME (REVERSE acc, Clist)) ∧
(every_check_PR_inds_list fml Clist np C ik mini (i::is) acc =
if i ≥ mini then
case any_el i fml NONE of
NONE => every_check_PR_inds_list fml Clist np C ik mini is acc
| SOME Ci =>
case check_PR_list fml Clist np C ik i Ci of
NONE => NONE
| SOME Clist => every_check_PR_inds_list fml Clist np C ik mini is (i::acc)
else
SOME(REV acc (i::is), Clist))
End
Theorem every_check_PR_inds_list_eq:
∀inds fml Clist np C ik mini acc.
every_check_PR_inds_list fml Clist np C ik mini inds acc =
let (inds,vs,rest) = reindex_partial fml mini inds in
case every_check_PR_list fml Clist np C ik inds vs of
NONE => NONE
| SOME Clist => SOME(REVERSE acc ++ inds ++ rest, Clist)
Proof
Induct>>rw[every_check_PR_inds_list_def,reindex_partial_def,every_check_PR_list_def]
>- (
TOP_CASE_TAC>>simp[]>>
pairarg_tac>>fs[]>>
simp[every_check_PR_list_def]>>
TOP_CASE_TAC>>simp[]>>
metis_tac[APPEND_ASSOC,APPEND])
>>
simp[REV_REVERSE_LEM]
QED
Definition is_PR_list_def:
is_PR_list fml inds Clist earliest p (C:cclause) wopt i0 ik =
(* First, do the asymmetric tautology check *)
case is_AT_list fml i0 C Clist of
NONE => NONE
| SOME (INL (), Clist) => SOME (inds, Clist)
| SOME (INR D, Clist) =>
if p ≠ 0 then
case wopt of NONE =>
(let miniopt = any_el (index (~p)) earliest NONE in
case miniopt of NONE => SOME (inds,Clist)
| SOME mini => every_check_RAT_inds_list fml Clist (~p) D ik mini inds [])
| SOME w =>
if check_overlap w (flip w) then NONE (* error *)
else
let miniopt = list_min_opt NONE (MAP (λw. any_el (index w) earliest NONE) (flip w)) in
case miniopt of NONE => SOME (inds,Clist)
| SOME mini => every_check_PR_inds_list fml Clist (flip w) D ik mini inds []
else
NONE
End
(* easier to reason about later *)
Theorem is_PR_list_eq:
is_PR_list fml inds Clist earliest p (C:cclause) wopt i0 ik =
(* First, do the asymmetric tautology check *)
case is_AT_list fml i0 C Clist of
NONE => NONE
| SOME (INL (), Clist) => SOME (inds, Clist)
| SOME (INR D, Clist) =>
if p ≠ 0 then
case wopt of NONE =>
(let miniopt = any_el (index (~p)) earliest NONE in
case miniopt of NONE => SOME (inds,Clist)
| SOME mini =>
let (inds,vs,rest) = reindex_partial fml mini inds in
(case every_check_RAT_list fml Clist (~p) D ik inds vs of
NONE => NONE
| SOME Clist => SOME (inds ++ rest, Clist)))
| SOME w =>
if check_overlap w (flip w) then NONE (* error *)
else
let miniopt = list_min_opt NONE (MAP (λw. any_el (index w) earliest NONE) (flip w)) in
case miniopt of NONE => SOME (inds,Clist)
| SOME mini =>
let (inds,vs,rest) = reindex_partial fml mini inds in
(case every_check_PR_list fml Clist (flip w) D ik inds vs of
NONE => NONE
| SOME Clist => SOME (inds ++ rest, Clist))
else
NONE
Proof
simp[is_PR_list_def]>>
ntac 6 (TOP_CASE_TAC>>simp[])>>
rpt(pairarg_tac>>fs[])
>- simp[every_check_RAT_inds_list_eq]>>
TOP_CASE_TAC>>fs[]>>
pairarg_tac>>fs[]>>
simp[every_check_PR_inds_list_eq]
QED
Definition list_delete_list_def:
(list_delete_list [] fml = fml) ∧
(list_delete_list (i::is) fml =
if LENGTH fml ≤ i
then list_delete_list is fml
else list_delete_list is (LUPDATE NONE i fml))
End
Definition safe_hd_def:
safe_hd ls = case ls of [] => (0:int) | (x::xs) => x
End
Definition list_max_index_def:
list_max_index C = 2*list_max (MAP (λc. Num (ABS c)) C) + 1
End
(* bump up the length to a large number *)
Definition resize_Clist_def:
resize_Clist C Clist =
if LENGTH Clist ≤ list_max_index C then
REPLICATE (2 * (list_max_index C )) w8z
else Clist
End
(* v is the clause index *)
Definition update_earliest_def:
(update_earliest ls v [] = ls) ∧
(update_earliest ls v (n::ns) =
let ind = index n in
let minn = any_el ind ls NONE in
let updmin = min_opt minn (SOME v) in
update_earliest (update_resize ls NONE updmin ind) v ns)
End
(* ensure list remains ≥ sorted -- common case: will always just insert at the front *)
Definition sorted_insert_def:
(sorted_insert (x:num) [] = [x]) ∧
(sorted_insert x (y::ys) =
if x ≥ y then x::y::ys
else y::(sorted_insert x ys))
End
Definition check_earliest_def:
(check_earliest fml x old new [] = T) ∧
(check_earliest fml x old new (i::is) =
if i ≥ old then
if i < new
then
case any_el i fml NONE of
NONE => check_earliest fml x old new is
| SOME Ci =>
¬ (MEM x Ci) ∧ check_earliest fml x old new is
else
check_earliest fml x old new is
else T)
End
Definition list_min_aux_def:
(list_min_aux min [] = min) ∧
(list_min_aux min ((i,_)::is) =
list_min_aux (MIN min i) is)
End
(* Note that clauses are 1 indexed *)
Definition list_min_def:
list_min ls =
case ls of [] => 0
| (x::xs) => list_min_aux (FST x) xs
End
Definition hint_earliest_def:
hint_earliest C (w:int list option) (ik:(num # num list) list) fml inds earliest =
case w of
NONE =>
(let lm = list_min ik in
if lm = 0 then earliest
else
(* RAT *)
let p = safe_hd C in
case any_el (index (~p)) earliest NONE of
NONE => earliest
| SOME mini => (* The current mini index of ~p *)
if check_earliest fml (~p) mini lm inds
then update_resize earliest NONE (SOME lm) (index (~p))
else earliest)
| SOME _ => earliest
End
Definition check_lpr_step_list_def:
check_lpr_step_list mindel step fml inds Clist earliest =
case step of
Delete cl =>
if EVERY ($< mindel) cl then
SOME (list_delete_list cl fml, inds, Clist, earliest)
else
NONE
| PR n C w i0 ik =>
let p = safe_hd C in
let Clist = resize_Clist C Clist in
let earliest = hint_earliest C w ik fml inds earliest in
case is_PR_list fml inds Clist earliest p C w i0 ik of
NONE => NONE
| SOME (inds, Clist) =>
if mindel < n then
SOME (update_resize fml NONE (SOME C) n, sorted_insert n inds, Clist,
update_earliest earliest n C)
else NONE
End
Definition check_lpr_list_def:
(check_lpr_list mindel [] fml inds Clist earliest = SOME (fml, inds)) ∧
(check_lpr_list mindel (step::steps) fml inds Clist earliest =
case check_lpr_step_list mindel step fml inds Clist earliest of
NONE => NONE
| SOME (fml', inds', Clist',earliest') => check_lpr_list mindel steps fml' inds' Clist' earliest')
End
Definition contains_clauses_list_def:
contains_clauses_list fml inds cls =
case reindex fml inds of
(_,inds') =>
let inds'' = MAP canon_clause inds' in
EVERY (λcl. MEM (canon_clause cl) inds'') cls
End
Definition check_lpr_unsat_list_def:
check_lpr_unsat_list lpr fml inds Clist earliest =
case check_lpr_list 0 lpr fml inds Clist earliest of
NONE => F
| SOME (fml', inds') => contains_clauses_list fml' inds' [[]]
End
(* Checking satisfiability equivalence *)
Definition check_lpr_sat_equiv_list_def:
check_lpr_sat_equiv_list lpr fml inds Clist earliest mindel cls =
case check_lpr_list mindel lpr fml inds Clist earliest of
NONE => F
| SOME (fml', inds') => contains_clauses_list fml' inds' cls
End
(* prove that check_lpr_step_list implements check_lpr_step *)
Definition fml_rel_def:
fml_rel fml fmlls ⇔
∀x.
lookup x fml = any_el x fmlls NONE
End
(* Require that the lookup table matches a clause exactly *)
Definition lookup_rel_def:
lookup_rel C Clist ⇔
(* elements are either 0 or 1 *)
(∀i. MEM i Clist ⇒ i = w8z ∨ i = w8o) ∧
(* where 1 indicates membership in C *)
(∀i. any_el (index i) Clist w8z = w8o ⇔ MEM i C)
End
Theorem delete_literals_sing_list_correct:
∀ls.
lookup_rel C Clist ∧ wf_clause ls ⇒
case delete_literals_sing_list Clist ls of
NONE => LENGTH (delete_literals ls C) > 1
| SOME 0 => delete_literals ls C = []
| SOME l => delete_literals ls C = [-l]
Proof
Induct>>simp[delete_literals_sing_list_def,delete_literals_def]>>
ntac 2 strip_tac>>fs[lookup_rel_def,wf_clause_def]>>
IF_CASES_TAC>>simp[]
>-
fs[delete_literals_def]
>>
IF_CASES_TAC>>simp[]
>-
simp[FILTER_EQ_NIL]
>>
Cases_on`FILTER (λx. ¬MEM x C) ls` >>
pop_assum mp_tac>> simp[FILTER_EQ_NIL,o_DEF]
QED
Theorem MEM_update_resize:
MEM i (update_resize ls def v x) ⇒
i = def ∨ MEM i ls ∨ i = v
Proof
rw[update_resize_def,MEM_LUPDATE]
>- metis_tac[MEM_EL]>>
rw[EL_APPEND_EQN]>- metis_tac[MEM_EL]>>
simp[EL_REPLICATE]
QED
Theorem any_el_update_resize:
any_el y (update_resize ls def v x) def =
if y = x then v
else
any_el y ls def
Proof
simp[update_resize_def]>>
IF_CASES_TAC
>- (
simp[any_el_ALT,EL_LUPDATE]>>
IF_CASES_TAC>>simp[])>>
simp[any_el_ALT,EL_LUPDATE,EL_APPEND_EQN,REPLICATE]>>
IF_CASES_TAC>>simp[]>>
IF_CASES_TAC>>simp[]>>
IF_CASES_TAC>>simp[]>>
simp[EL_REPLICATE]
QED
Theorem index_11:
index i = index x ⇔ i = x
Proof
rw[index_def,EQ_IMP_THM]>>
intLib.ARITH_TAC
QED
Theorem index_onto:
∃i. index i = k
Proof
rw[index_def]>>
qexists_tac`if k MOD 2 = 0 then -&(k DIV 2) else &((k+1) DIV 2)`>>
rw[]>>fs[]>>simp[bitTheory.DIV_MULT_THM2]>>
intLib.ARITH_TAC
QED
Theorem lookup_rel_cons:
lookup_rel C Clist ⇒
lookup_rel (x::C) (update_resize Clist w8z w8o (index x))
Proof
rw[lookup_rel_def]
>- (
drule MEM_update_resize >>
metis_tac[])>>
simp[any_el_update_resize,index_11]>>
IF_CASES_TAC>>metis_tac[]
QED
Theorem lookup_rel_REVERSE:
lookup_rel (REVERSE C) Clist ⇔ lookup_rel C Clist
Proof
rw[lookup_rel_def]
QED
Theorem fml_rel_is_AT_list_aux:
∀ls C Clist.
fml_rel fml fmlls ∧ wf_fml fml ∧
lookup_rel C Clist ⇒
case is_AT_list_aux fmlls ls C Clist of
SOME (INL C', Clist') => is_AT fml ls C = SOME (INL ()) ∧ lookup_rel C' Clist'
| SOME (INR C', Clist') => is_AT fml ls C = SOME (INR C') ∧ lookup_rel C' Clist'
| NONE => is_AT fml ls C = NONE (* Not required but should be true *)
Proof
Induct>>fs[is_AT_list_aux_def,is_AT_def]>>rw[]>>
fs[fml_rel_def,any_el_ALT]>>
first_x_assum(qspec_then`h` mp_tac)>>IF_CASES_TAC>>fs[]>>
strip_tac>>
Cases_on`EL h fmlls`>>simp[]>>
`wf_clause x` by
(fs[wf_fml_def,range_def]>>metis_tac[])>>
drule delete_literals_sing_list_correct>>
disch_then drule>>
TOP_CASE_TAC>>simp[]
>-
(every_case_tac>>fs[])
>>
IF_CASES_TAC>>simp[]>>
qmatch_goalsub_abbrev_tac`is_AT_list_aux _ _ aaa bbb`>>
first_x_assum(qspecl_then[`aaa`,`bbb`] mp_tac)>>
impl_tac >-
(unabbrev_all_tac>>simp[lookup_rel_cons])>>
TOP_CASE_TAC>>simp[]
QED
Theorem lookup_rel_set_list_lookup_rel:
∀D ls C.
lookup_rel C ls ⇒
lookup_rel (C++D) (set_list ls w8o D)
Proof
Induct>>rw[set_list_def]>>
`C ++ h::D = (C++[h])++D` by simp[]>>
pop_assum SUBST_ALL_TAC>>
first_x_assum match_mp_tac>>
`C++[h] = REVERSE (h::REVERSE C)` by fs[]>>
metis_tac[lookup_rel_REVERSE,lookup_rel_cons]
QED
Theorem empty_set_list_lookup_rel:
EVERY ($= w8z) Clist ⇒
lookup_rel C (set_list Clist w8o C)
Proof
rw[]>>
`lookup_rel [] Clist` by
(fs[lookup_rel_def,EVERY_MEM,any_el_ALT]>>
rw[]>>fs[w8z_def,w8o_def]>>
first_x_assum(qspec_then`EL (index i) Clist` mp_tac)>>
impl_tac>-
simp[EL_MEM]>>
simp[])>>
drule lookup_rel_set_list_lookup_rel>>
simp[]
QED
Theorem any_el_set_list:
∀is ls.
any_el x (set_list ls v is) w8z =
if ∃y. x = index y ∧ MEM y is then v
else any_el x ls w8z
Proof
Induct>>simp[set_list_def]>>
ntac 2 strip_tac>>
IF_CASES_TAC>-
(fs[]>>
metis_tac[])>>
simp[any_el_update_resize]>>
fs[]>>
metis_tac[]
QED
Theorem lookup_rel_set_list_empty:
∀C.
lookup_rel C Clist ⇒
EVERY ($= w8z) (set_list Clist w8z C)
Proof
rw[EVERY_EL]>>
`any_el n (set_list Clist w8z C) w8z = w8z` by
(simp[any_el_set_list]>>
rw[]>>fs[lookup_rel_def,PULL_EXISTS]>>
`?k. index k = n` by fs[index_onto]>>
first_x_assum(qspec_then`k` assume_tac)>>rfs[]>>
first_x_assum(qspec_then`k` assume_tac)>>rfs[]>>
fs[any_el_ALT]>>
rw[]>>fs[]>>
first_x_assum(qspec_then `EL (index k) Clist` mp_tac)>>
impl_tac>-
(simp[MEM_EL]>>
qexists_tac`index k`>>simp[])>>
metis_tac[])>>
rfs[any_el_ALT]
QED
Theorem fml_rel_is_AT_list:
EVERY ($= w8z) Clist ∧ (* the array is always zero-ed before and after *)
wf_fml fml ∧
fml_rel fml fmlls ⇒
(case is_AT_list fmlls ls (C:cclause) Clist of
SOME (INL (), Clist') => is_AT fml ls C = SOME (INL ()) ∧ EVERY ($= w8z) Clist'
| SOME (INR C', Clist') => is_AT fml ls C = SOME (INR C') ∧ EVERY ($= w8z) Clist'
| NONE => is_AT fml ls C = NONE)
Proof
rw[is_AT_list_def]>>
drule fml_rel_is_AT_list_aux>>
simp[]>>
drule empty_set_list_lookup_rel>>
disch_then(qspec_then`C` assume_tac)>>
disch_then drule>>
disch_then(qspec_then`ls` assume_tac)>>
every_case_tac>>fs[]>>
metis_tac[lookup_rel_set_list_empty]
QED
Theorem fml_rel_check_RAT_list:
EVERY ($= w8z) Clist ∧ wf_fml fml ∧ fml_rel fml fmlls ⇒
case check_RAT_list fmlls Clist (-p) C ik i Ci of
SOME Clist' => check_RAT fml p C ik (i,Ci) ∧ EVERY ($= w8z) Clist'
| NONE => T (* not needed but can probably show it's ¬ check_RAT *)
Proof
simp[check_RAT_list_def,check_RAT_def]>>
simp[check_overlap_def]>>
strip_tac>> IF_CASES_TAC>> simp[]>>
TOP_CASE_TAC>>simp[]>>
TOP_CASE_TAC>>fs[]>>
TOP_CASE_TAC>>fs[]>>
every_case_tac>>fs[]>>
drule fml_rel_is_AT_list>>
disch_then drule>>
disch_then drule>>
qmatch_asmsub_abbrev_tac`is_AT_list _ aaa bbb`>>
disch_then(qspecl_then[`aaa`,`bbb`] mp_tac)>>
every_case_tac>>fs[]
QED
Theorem fml_rel_every_check_RAT_list:
∀is Cis Clist.
EVERY ($= w8z) Clist ∧ wf_fml fml ∧ fml_rel fml fmlls ⇒
case every_check_RAT_list fmlls Clist (-p) C ik is Cis of
SOME Clist' => EVERY (check_RAT fml p C ik) (ZIP (is,Cis))∧ EVERY ($= w8z) Clist'
| NONE => T (* not needed but can probably show it's ¬ check_RAT *)
Proof
Induct>>rw[]
>-
(Cases_on`Cis`>>simp[every_check_RAT_list_def])
>>
Cases_on`Cis`>>simp[every_check_RAT_list_def]>>
drule fml_rel_check_RAT_list>>
rpt (disch_then drule)>>
disch_then (qspecl_then [`p`,`ik`,`h`,`h'`,`C`] mp_tac)>>
TOP_CASE_TAC>>simp[]
QED
Theorem flip_flip[simp]:
flip(flip w) = w
Proof
rw[flip_def,MAP_MAP_o,o_DEF]
QED
Theorem fml_rel_check_PR_list:
EVERY ($= w8z) Clist ∧ wf_fml fml ∧ fml_rel fml fmlls ⇒
case check_PR_list fmlls Clist (flip w) C ik i Ci of
SOME Clist' => check_PR fml w C ik (i,Ci) ∧ EVERY ($= w8z) Clist'
| NONE => T (* see above *)
Proof
simp[check_PR_list_def,check_PR_def]>>
IF_CASES_TAC>> simp[]>>
TOP_CASE_TAC>>simp[]>>
TOP_CASE_TAC>>fs[]>>
TOP_CASE_TAC>>fs[]>>
strip_tac>>
every_case_tac>>fs[]>>
drule fml_rel_is_AT_list>>
disch_then drule>>
disch_then drule>>
qmatch_asmsub_abbrev_tac`is_AT_list _ aaa bbb`>>
disch_then(qspecl_then[`aaa`,`bbb`] mp_tac)>>
every_case_tac>>fs[]
QED
Theorem fml_rel_every_check_PR_list:
∀is Cis Clist.
EVERY ($= w8z) Clist ∧ wf_fml fml ∧ fml_rel fml fmlls ⇒
case every_check_PR_list fmlls Clist (flip w) C ik is Cis of
SOME Clist' => EVERY (check_PR fml w C ik) (ZIP (is,Cis)) ∧ EVERY ($= w8z) Clist'
| NONE => T
Proof
Induct>>rw[]
>-
(Cases_on`Cis`>>simp[every_check_PR_list_def])
>>
Cases_on`Cis`>>simp[every_check_PR_list_def]>>
drule fml_rel_check_PR_list>>
rpt (disch_then drule)>>
disch_then (qspecl_then [`w`,`ik`,`h`,`h'`,`C`] mp_tac)>>
TOP_CASE_TAC>>simp[]
QED
(* It must be the case that everything that is SOME is in inds *)
Definition ind_rel_def:
ind_rel fmlls inds ⇔
∀x. x < LENGTH fmlls ∧
IS_SOME (EL x fmlls) ⇒
MEM x inds
End
Theorem reindex_characterize:
∀inds inds' vs.
reindex fmlls inds = (inds',vs) ⇒
inds' = FILTER (λx. IS_SOME (any_el x fmlls NONE)) inds ∧
vs = MAP (λx. THE (any_el x fmlls NONE )) inds'
Proof
Induct>>fs[reindex_def] >>
ntac 3 strip_tac>>fs[]>>
TOP_CASE_TAC>>fs[]>>
pairarg_tac>>fs[]>>rw[]>>
simp[]
QED
Theorem ind_rel_filter:
ind_rel fmlls inds ⇒
ind_rel fmlls (FILTER (λx. IS_SOME (any_el x fmlls NONE)) inds)
Proof
rw[ind_rel_def]>>
simp[MEM_FILTER,any_el_ALT]
QED
Theorem ind_rel_reindex:
fml_rel fml fmlls ∧
ind_rel fmlls inds ∧
reindex fmlls inds = (inds',vs) ⇒
LENGTH inds' = LENGTH vs ∧
(∀x. MEM x (toAList fml) ⇔ MEM x (ZIP (inds',vs))) ∧
ind_rel fmlls inds'
Proof
strip_tac>> drule reindex_characterize>> simp[]>>
simp[FORALL_PROD,MEM_toAList]>>rw[]
>- (
simp[ZIP_MAP,MEM_MAP,MEM_FILTER]>>
fs[fml_rel_def,any_el_ALT]>>
first_x_assum(qspec_then`p_1` mp_tac)>>fs[]>>
IF_CASES_TAC>>simp[any_el_ALT]>>
rw[EQ_IMP_THM]>>fs[IS_SOME_EXISTS]>>
fs[ind_rel_def])
>>
metis_tac[ind_rel_filter]
QED
Theorem SORTED_HEAD_LESS:
¬(h ≥ mini:num) ∧
SORTED $>= (h::inds) ⇒
EVERY (λx. x < mini) inds
Proof
DEP_REWRITE_TAC [SORTED_EQ]>>
simp[transitive_def,EVERY_MEM]>>
rw[]>>
first_x_assum drule>>
fs[]
QED
Theorem reindex_partial_characterize:
∀inds inds' vs rest.
SORTED $>= inds ∧
reindex_partial (fmlls:int list option list) mini inds = (inds',vs, rest) ⇒
∃f.
inds = f ++ rest ∧
f = FILTER (λx. x ≥ mini) inds ∧
inds' = FILTER (λx. IS_SOME (any_el x fmlls NONE)) f ∧
vs = MAP (λx. THE (any_el x fmlls NONE)) inds'
Proof
Induct>>fs[reindex_partial_def] >>
ntac 4 strip_tac>>fs[]>>
reverse IF_CASES_TAC>>
simp[]
>- (
strip_tac>>simp[]>>
CONJ_ASM1_TAC>>simp[]>>
drule SORTED_HEAD_LESS>>
disch_then drule>>
rw[FILTER_EQ_NIL,EVERY_MEM]>>
first_x_assum drule>>simp[])>>
strip_tac>>
IF_CASES_TAC>>fs[]
>- (
fs[IS_SOME_EXISTS]>>fs[]>>
drule SORTED_TL>>strip_tac>>fs[]>>
pairarg_tac>>fs[]>>
rw[]>>simp[])
>>
drule SORTED_TL>>
strip_tac>>fs[]
QED
Theorem ind_rel_filter_partial:
ind_rel fmlls (inds++rest) ⇒
ind_rel fmlls (FILTER (λx. IS_SOME (any_el x fmlls NONE)) inds ++ rest)
Proof
rw[ind_rel_def]>>
simp[MEM_FILTER,any_el_ALT]
QED
Theorem SORTED_FILTER_part:
transitive R ⇒
SORTED R (a++b) ⇒
SORTED R (FILTER P a ++ b)
Proof
strip_tac>>
DEP_REWRITE_TAC [SORTED_APPEND]>>
metis_tac[SORTED_FILTER,MEM_FILTER]
QED
Theorem ind_rel_reindex_partial:
fml_rel fml (fmlls:int list option list) ∧
ind_rel fmlls inds ∧
SORTED $>= inds ∧
reindex_partial fmlls x inds = (inds',vs,rest) ⇒
ind_rel fmlls (inds'++rest) ∧
SORTED $>= (inds'++rest)
Proof
strip_tac>>
drule reindex_partial_characterize>>
disch_then drule>>
strip_tac>>
CONJ_TAC>-
metis_tac[ind_rel_filter_partial]>>
qpat_x_assum`SORTED _ _` mp_tac>>
qpat_x_assum`_ = _ ++_` SUBST_ALL_TAC>>
qpat_x_assum`inds' = _` SUBST_ALL_TAC>>
match_mp_tac SORTED_FILTER_part>>
simp[transitive_def]
QED
(* earliest correctly tracks earliest occurrence of a literal *)
Definition earliest_rel_def:
earliest_rel fmlls earliest ⇔
∀x.
case any_el x earliest NONE of
NONE =>
(∀pos z.
pos < LENGTH fmlls ⇒
case EL pos fmlls of
NONE => T
| SOME ls => MEM z ls ⇒ index z ≠ x)
| SOME i =>
(∀pos z.
pos < i ∧
pos < LENGTH fmlls ⇒
case EL pos fmlls of
NONE => T
| SOME ls => MEM z ls ⇒ index z ≠ x)
End
(* Trivial case when the earliest index is NONE *)
Theorem earliest_rel_RAT_NONE:
∀fmlls Clist np ik is inds vs inds' vs' Clist' earliest.
earliest_rel fmlls earliest ∧
EVERY (λx. IS_SOME (any_el x fmlls NONE)) inds ∧
vs = MAP (λx. THE (any_el x fmlls NONE)) inds ∧
any_el (index np) earliest NONE = NONE ⇒
every_check_RAT_list fmlls Clist np ik is inds vs = SOME Clist
Proof
ho_match_mp_tac (fetch "-" "every_check_RAT_list_ind")>>
rw[]>>
simp[every_check_RAT_list_def]>>
qpat_x_assum`!Clist'. _` (qspec_then `Clist` mp_tac)>>
impl_keep_tac>- (
simp[check_RAT_list_def]>>
fs[earliest_rel_def]>>
first_x_assum (qspec_then`index np` mp_tac)>>
simp[]>>
fs[any_el_ALT,IS_SOME_EXISTS]>>
disch_then(qspec_then`i` mp_tac)>>simp[]>>
disch_then(qspec_then`np` mp_tac)>>simp[])>>
simp[]>>
rpt (disch_then drule)>>
metis_tac[]
QED
Theorem earliest_rel_RAT_NONE_alt:
∀fmlls Clist np ik is inds earliest.
earliest_rel fmlls earliest ∧
any_el (index np) earliest NONE = NONE ⇒
let (aaa,bbb) = reindex fmlls inds in
every_check_RAT_list fmlls Clist np ik is aaa bbb = SOME Clist
Proof
rw[]>>
pairarg_tac>>simp[]>>
match_mp_tac earliest_rel_RAT_NONE>>
drule reindex_characterize>>
simp[EVERY_FILTER]>>
metis_tac[]
QED
(* Trivial case when the earliest index is beyond any index *)
Theorem earliest_rel_RAT_skip:
∀inds fmlls Clist np ik is vs Clist earliest pos.
earliest_rel fmlls earliest ∧
any_el (index np) earliest NONE = SOME pos ∧
EVERY (λx. x < pos) inds ∧
EVERY (λx. IS_SOME (any_el x fmlls NONE)) inds ∧
vs = MAP (λx. THE (any_el x fmlls NONE)) inds ⇒
every_check_RAT_list fmlls Clist np ik is inds vs = SOME Clist
Proof
Induct>>rw[every_check_RAT_list_def]>>
qpat_x_assum`IS_SOME _` mp_tac>>
simp[IS_SOME_EXISTS]>>
rw[]>> fs[]>>
simp[check_RAT_list_def]>>
`¬MEM np x` by (
fs[earliest_rel_def]>>
first_x_assum (qspec_then`index np` mp_tac)>>
simp[]>>
fs[any_el_ALT,IS_SOME_EXISTS]>>
disch_then(qspec_then`h` mp_tac)>>simp[]>>
disch_then(qspec_then`np` mp_tac)>>simp[])>>
simp[]>>
first_x_assum drule>>
disch_then drule>>
fs[]
QED
Theorem earliest_rel_reindex_partial_RAT_FILTER_min:
∀inds fmlls Clist np ik is aaa bbb Clist' earliest pos.
earliest_rel fmlls earliest ∧
SORTED ($>=) inds ∧
any_el (index np) earliest NONE = SOME pos ∧
reindex fmlls (FILTER (λx. x ≥ pos) inds) = (aaa,bbb) ∧
every_check_RAT_list fmlls Clist np ik is aaa bbb = SOME Clist' ⇒
let (aaa,bbb) = reindex fmlls inds in
every_check_RAT_list fmlls Clist np ik is aaa bbb = SOME Clist'
Proof
Induct>>rw[every_check_RAT_list_def,reindex_partial_def]
>- (
fs[reindex_def]>>
TOP_CASE_TAC>>fs[]
>- (
drule SORTED_TL>>
strip_tac>>
first_x_assum drule>>
simp[])>>
pairarg_tac>>fs[]>>
pairarg_tac>>fs[]>>
pairarg_tac>>fs[]>>
rw[]>>fs[]>>
fs[every_check_RAT_list_def]>>
TOP_CASE_TAC>>fs[]>>
drule SORTED_TL>>
strip_tac>>
first_x_assum drule>>
simp[])>>
`EVERY (λx. x < pos) inds` by
metis_tac[SORTED_HEAD_LESS]>>
`aaa = [] ∧ bbb = []` by
(`FILTER (λx. x ≥ pos) inds = []` by
(fs[FILTER_EQ_NIL,EVERY_MEM]>>
rw[]>>
first_x_assum drule>>simp[])>>
fs[reindex_def])>>
pairarg_tac>>fs[]>>
fs[every_check_RAT_list_def]>>
drule earliest_rel_RAT_skip>>
disch_then match_mp_tac>>
asm_exists_tac>>simp[]>>
drule reindex_characterize>>
rw[]>>simp[EVERY_FILTER]>>