-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathpbc_normaliseScript.sml
1536 lines (1432 loc) · 45.5 KB
/
pbc_normaliseScript.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
(*
Normalizes pbc into npbc
*)
open preamble pbcTheory npbcTheory mlmapTheory mergesortTheory;
val _ = new_theory "pbc_normalise";
val _ = numLib.temp_prefer_num();
(* Normalization proceeds in three steps (for string variables):
'a pbc -> string pbc (for graph encoders) ~> print out
string pbc -> int pbc
int pbc -> int pbc with ≥
int pbc with ≥ -> npbc
----
'a pbc -> 'a pbc with ≥ ~> print out
'a pbc with ≥ -> string pbc (for graph encoders)
There is a builtin string normalization using hashing for
the supported characters, but this is (should be) unused
*)
(*
Injective mapping from mlstring into num, supports
a-z, A-Z, 0-9, []{}_^-
*)
val non_list = (EVAL ``fromAList (MAP SWAP (enumerate 63 (MAP ORD (explode (strlit "[]{}_^-")))))``);
Definition non_list_def:
non_list = ^(rconc non_list)
End
Theorem non_list_eq = non_list_def |> SIMP_RULE std_ss [GSYM non_list];
Theorem lookup_non_list:
sptree$lookup n non_list = SOME v ⇔
SOME n = (ALOOKUP (enumerate 63 (MAP ORD (explode (strlit "[]{}_^-")))) v)
Proof
simp[non_list_eq,lookup_fromAList]>>
EVAL_TAC>>rw[]
QED
Definition hashNon_def:
hashNon n =
case sptree$lookup n non_list of
NONE => 0n
| SOME v => v
End
Definition hashChar_def:
hashChar c =
let oc = ORD c in
if 48 ≤ oc ∧ oc ≤ 57 (* char 0 to 9 hashes to 1-10 respectively *)
then oc - 47
else if 65 ≤ oc ∧ oc ≤ 90 (* char A to Z hashes to 11-36 *)
then oc - 54
else if 97 ≤ oc ∧ oc ≤ 122 (* char a to z hashes to 37-62 *)
then oc - 60
else hashNon oc
End
Definition hashChars_alt_def:
(hashChars_alt [] = 0) ∧
(hashChars_alt (c::cs) =
hashChar c + 70 * hashChars_alt cs)
End
Definition hashString_def:
hashString s = hashChars_alt (explode s)
End
(* Kind of a circular definition ... *)
Definition goodChar_def:
goodChar c ⇔ hashChar c ≠ 0
End
Definition goodChars_def:
(goodChars 0 str = T) ∧
(goodChars (SUC n) str =
(goodChar (strsub str n) ∧
goodChars n str))
End
Definition goodString_def:
goodString str = goodChars (strlen str) str
End
Theorem goodString_eq_EVERY_goodChar:
∀s. goodString s ⇔ EVERY goodChar (explode s)
Proof
Cases \\ fs [goodString_def]
\\ qsuff_tac ‘∀s t. goodChars (STRLEN s) (strlit (s ++ t)) ⇔ EVERY goodChar s’
>- metis_tac [APPEND_NIL]
\\ Induct using SNOC_INDUCT
>- (EVAL_TAC \\ fs [])
\\ fs [goodChars_def,EVERY_SNOC]
\\ rewrite_tac [SNOC_APPEND,GSYM APPEND_ASSOC,APPEND]
\\ fs [EL_LENGTH_APPEND]
\\ rw [] \\ eq_tac \\ rw []
QED
Theorem goodChar_toString:
EVERY goodChar (case toString n1 of strlit x => x)
Proof
Cases_on ‘toString n1’ \\ fs []
\\ imp_res_tac mlintTheory.num_to_str_imp_cons
\\ gvs [goodChar_def,EVERY_MEM]
\\ rw [] \\ res_tac \\ fs []
\\ fs [hashChar_def]
QED
Triviality hashChar_bound:
∀h. hashChar h < 70
Proof
rw [hashChar_def,hashNon_def,non_list_eq,lookup_fromAList]>>
EVAL_TAC>>
rw[]
QED
Triviality hashChar_11:
hashChar h <> 0 /\ hashChar h' <> 0 ==>
(hashChar h = hashChar h' <=> h = h')
Proof
rw [] \\ eq_tac \\ rw []
\\ Cases_on ‘h’
\\ Cases_on ‘h'’
\\ fs []
\\ rpt $ last_x_assum mp_tac
\\ once_rewrite_tac [hashChar_def]
\\ ntac 2 strip_tac
\\ imp_res_tac ORD_CHR
\\ asm_rewrite_tac []
\\ simp_tac std_ss [LET_THM,hashNon_def]
\\ rename [‘ORD (CHR m) = m’]
\\ reverse (Cases_on `lookup n non_list`)
>- (
FULL_SIMP_TAC std_ss [lookup_non_list]>>
pop_assum mp_tac>>
qmatch_goalsub_abbrev_tac`_ ⇒ P`>>
EVAL_TAC>>
ntac 7 (IF_CASES_TAC>- (
simp[]>>
unabbrev_all_tac>>
SIMP_TAC std_ss []>>
rw[]>>fs[AllCaseEqs()]>>
pop_assum mp_tac >> simp[lookup_non_list]>>
EVAL_TAC>>rw[]))>>
simp[])
\\ reverse (Cases_on `lookup m non_list`)
>- (
FULL_SIMP_TAC std_ss [lookup_non_list]>>
pop_assum mp_tac>>
qmatch_goalsub_abbrev_tac`_ ⇒ P`>>
EVAL_TAC>>
ntac 7 (IF_CASES_TAC>- (
simp[]>>
unabbrev_all_tac>>
SIMP_TAC std_ss []>>
rw[]>>fs[AllCaseEqs()]>>
pop_assum mp_tac >> simp[lookup_non_list]>>
EVAL_TAC>>rw[]))>>
simp[])
\\ reverse $ Cases_on ‘48 <= n’ \\ asm_rewrite_tac []
>-
(Cases_on ‘48 <= m’ \\ asm_rewrite_tac []
\\ rewrite_tac [AllCaseEqs()]
\\ strip_tac \\ fs [])
\\ reverse $ Cases_on ‘48 <= m’ \\ asm_rewrite_tac []
>- (rewrite_tac [AllCaseEqs()] \\ strip_tac \\ fs [])
\\ Cases_on ‘n <= 57’ \\ asm_rewrite_tac []
>-
(Cases_on ‘m <= 57’ \\ asm_rewrite_tac []
\\ rewrite_tac [AllCaseEqs()]
\\ strip_tac \\ fs [])
\\ reverse $ Cases_on ‘m <= 57’ \\ asm_rewrite_tac []
>- (rewrite_tac [AllCaseEqs()] \\ strip_tac \\ fs [])
\\ fs []
QED
Theorem hashString_INJ:
INJ hashString goodString UNIV
Proof
rw[INJ_DEF,SPECIFICATION,hashString_def]
\\ gs [goodString_eq_EVERY_goodChar]
\\ Cases_on ‘x’ \\ Cases_on ‘y’ \\ fs []
\\ rename [‘s = t’]
\\ rpt $ pop_assum mp_tac
\\ qid_spec_tac ‘t’
\\ qid_spec_tac ‘s’
\\ Induct
\\ Cases_on ‘t’ \\ fs []
>- fs [hashChars_alt_def,goodChar_def]
>- fs [hashChars_alt_def,goodChar_def]
\\ fs [hashChars_alt_def]
\\ rpt gen_tac
\\ rpt $ disch_then assume_tac
\\ gvs []
\\ rename [‘EVERY goodChar t’]
\\ Cases_on ‘h = h'’ \\ fs []
\\ qsuff_tac ‘hashChar h = hashChar h'’
>- fs [hashChar_11,goodChar_def]
\\ ‘(hashChar h' + 70 * hashChars_alt s) MOD 70 =
(hashChar h + 70 * hashChars_alt t) MOD 70’ by metis_tac []
\\ ‘0 < 70:num’ by fs []
\\ drule arithmeticTheory.MOD_TIMES
\\ once_rewrite_tac [ADD_COMM]
\\ once_rewrite_tac [MULT_COMM]
\\ strip_tac \\ full_simp_tac std_ss []
\\ fs [hashChar_bound]
QED
Definition convert_pbf_def:
convert_pbf pbf = MAP (map_pbc hashString) pbf
End
Theorem convert_pbf_satisfies:
pbf_vars (set pbf) ⊆ goodString ⇒
(satisfies w (set pbf) ⇔
satisfies (w o LINV hashString goodString) (set (convert_pbf pbf)))
Proof
rw[]>>
`INJ hashString goodString UNIV` by
metis_tac[hashString_INJ,SUBSET_REFL]>>
simp[convert_pbf_def,LIST_TO_SET_MAP]>>
rw[EQ_IMP_THM]
>- (
match_mp_tac satisfies_INJ>>
simp[])>>
drule satisfies_map_pbf>>
match_mp_tac satisfies_pbf_vars>>
rw[]>>fs[]>>
drule LINV_DEF>>
fs[pbf_vars_IMAGE,SUBSET_DEF]
QED
Theorem convert_pbf_satisfies_2:
pbf_vars (set pbf) ⊆ goodString ⇒
(satisfies w (set (convert_pbf pbf)) ⇔
satisfies (w o hashString) (set pbf))
Proof
rw[]>>
simp[convert_pbf_def,LIST_TO_SET_MAP]>>
rw[EQ_IMP_THM]
>-
metis_tac[satisfies_map_pbf]>>
match_mp_tac satisfies_INJ_2>>
simp[]>>
match_mp_tac INJ_SUBSET>>
first_x_assum (irule_at Any)>>
metis_tac[hashString_INJ,SUBSET_REFL]
QED
Definition flip_coeffs_def:
flip_coeffs xs = MAP (λ(c,l). (-c:int,l)) xs
End
(* Convert a list of pbc to one with ≥ constraints only *)
Definition pbc_ge_def:
(pbc_ge ((GreaterEqual,xs,n):'a pbc) = [(GreaterEqual,xs,n)]) ∧
(pbc_ge (Greater,xs,n) = [(GreaterEqual,xs,(n+1))]) ∧
(pbc_ge (LessEqual,xs,n) = [(GreaterEqual,flip_coeffs xs,-n)]) ∧
(pbc_ge (Less,xs,n) = [(GreaterEqual,flip_coeffs xs,-(n-1))]) ∧
(pbc_ge (Equal,xs,n) =
[(GreaterEqual,xs,n); (GreaterEqual,flip_coeffs xs,(-n))])
End
Theorem eq_disj:
(∀x. x = a ∨ x = b ⇒ P x) ⇔ P a ∧ P b
Proof
metis_tac[]
QED
Theorem eval_lin_term_flip_coeffs:
∀f.
eval_lin_term w (flip_coeffs f) =
-eval_lin_term w f
Proof
Induct>>fs[eval_lin_term_def,iSUM_def,flip_coeffs_def]>>
Cases_on`h`>>rw[]>>
Cases_on`r`>>rw[]>>Cases_on`w a`>>rw[]>>
intLib.ARITH_TAC
QED
Theorem pbc_ge_thm:
satisfies w (set (pbc_ge c)) ⇔
satisfies_pbc w c
Proof
PairCases_on`c`>>
rename1`(pbop,xs,n)`>>
Cases_on`pbop`>>
simp[pbc_ge_def,satisfies_def]
>- ( (* Equal *)
fs[satisfies_pbc_def,eq_disj,eval_lin_term_flip_coeffs]>>
intLib.ARITH_TAC)
>- ( (* Greater *)
simp[satisfies_pbc_def]>>
intLib.ARITH_TAC)
>- ( (* LessEqual *)
simp[satisfies_pbc_def,eval_lin_term_flip_coeffs]>>
intLib.ARITH_TAC)
>- ( (* Less*)
simp[satisfies_pbc_def,eval_lin_term_flip_coeffs]>>
intLib.ARITH_TAC)
QED
Definition term_lt_def[simp]:
term_lt (_,l1) (_,l2) = (lit_var l1 < lit_var l2)
End
Definition term_le_def[simp]:
term_le (_,l1) (_,l2) = (lit_var l1 <= lit_var l2)
End
(* Ensure compact LHS in preconstraint form:
sort by variables *)
Definition compact_lhs_def:
(compact_lhs ((c1:int,l1)::(c2,l2)::cs) n =
if lit_var l1 = lit_var l2 then
if l1 = l2 then
compact_lhs ((c1+c2,l1)::cs) n
else
compact_lhs ((c1-c2,l1)::cs) (n+c2)
else
let (cs',n') = compact_lhs ((c2,l2)::cs) n in
((c1,l1)::cs',n')) ∧
(compact_lhs c n = (c,n))
End
Theorem compact_lhs_MEM:
∀xs n xs' n' y l.
compact_lhs xs n = (xs',n') ∧
MEM (y,l) xs' ⇒
∃y'. MEM (y',l) xs
Proof
ho_match_mp_tac (theorem "compact_lhs_ind")>>
rw[compact_lhs_def]>> fs[]
>- metis_tac[]
>- metis_tac[]
>- (
pairarg_tac>>gs[]>>rw[]>>fs[]>>rw[]>>
metis_tac[])
>> metis_tac[]
QED
Theorem transitive_term_le:
transitive term_le
Proof
simp[transitive_def]>>
rpt Cases >>
simp[term_le_def]
QED
Theorem transitive_term_lt:
transitive term_lt
Proof
simp[transitive_def]>>
rpt Cases >>
simp[term_lt_def]
QED
Theorem lit_var_eq_term_le:
lit_var l1 = lit_var l2 ⇒
(term_le (a,l1) x ⇔ term_le (b,l2) x)
Proof
Cases_on`x`>>rw[term_le_def]
QED
Theorem compact_lhs_no_dup:
∀xs n xs' n'.
SORTED term_le xs ∧
compact_lhs xs n = (xs',n') ⇒
SORTED term_lt xs'
Proof
ho_match_mp_tac (theorem "compact_lhs_ind")>>
rw[compact_lhs_def]>> fs[]
>- (
first_x_assum match_mp_tac>>
qpat_x_assum `SORTED _ _` mp_tac>>
DEP_REWRITE_TAC[SORTED_EQ]>>simp[transitive_term_le]>>
metis_tac[lit_var_eq_term_le])
>- (
first_x_assum match_mp_tac>>
qpat_x_assum `SORTED _ _` mp_tac>>
DEP_REWRITE_TAC[SORTED_EQ]>>simp[transitive_term_le]>>
metis_tac[lit_var_eq_term_le])>>
pairarg_tac>>fs[]>>rw[]>>
qpat_x_assum `SORTED _ _` mp_tac>>
DEP_REWRITE_TAC[SORTED_EQ]>>
simp[transitive_term_le,transitive_term_lt]>>
simp[FORALL_PROD]>>rw[]>>
drule compact_lhs_MEM>>
disch_then drule>>strip_tac>>
fs[]>>first_x_assum drule>>
fs[]
QED
Theorem iSUM_PERM:
∀l1 l2. PERM l1 l2 ⇒
iSUM l1 = iSUM l2
Proof
ho_match_mp_tac PERM_IND>>rw[iSUM_def]>>
intLib.ARITH_TAC
QED
Theorem iSUM_mergesort_term_le[simp]:
iSUM (MAP (eval_term w) (mergesort $≤ l)) =
iSUM (MAP (eval_term w) l)
Proof
match_mp_tac iSUM_PERM>>
match_mp_tac PERM_MAP>>
metis_tac[mergesort_perm,PERM_SYM]
QED
Theorem eval_lit_eq_flip:
q * eval_lit w r = q + (-q * eval_lit w (negate r))
Proof
Cases_on`r` \\ EVAL_TAC
\\ Cases_on`w a` \\ EVAL_TAC
\\ fs[]
QED
Theorem compact_lhs_sound:
∀xs n xs' n'.
compact_lhs xs n = (xs',n') ⇒
iSUM (MAP (pbc$eval_term w) xs) + n = iSUM (MAP (pbc$eval_term w) xs') + n'
Proof
ho_match_mp_tac (theorem "compact_lhs_ind")>>
rw[compact_lhs_def]>> fs[]
>- (
(* l1 = l2 *)
fs[iSUM_def]>>
intLib.ARITH_TAC)
>- (
(* l1 = negate l2 *)
fs[iSUM_def]>>
qmatch_goalsub_abbrev_tac` A + _ + _`>>
REWRITE_TAC[Once eval_lit_eq_flip]>>
`negate l2 = l1` by
(Cases_on`l1`>>Cases_on`l2`>>fs[])>>
fs[Abbr`A`]>>
qpat_x_assum`_ = _ + _` sym_sub_tac>>
simp[integerTheory.INT_SUB_RDISTRIB]>>
qmatch_goalsub_abbrev_tac`_ * wl2 + _ +_ = _ - _ + is + _`>>
rpt (pop_assum kall_tac)>>
intLib.ARITH_TAC)>>
pairarg_tac>>fs[]>>
rw[]>>
fs[iSUM_def]>>
intLib.ARITH_TAC
QED
Definition mk_coeff_def:
(mk_coeff c (Pos v) = c) ∧
(mk_coeff c (Neg v) = -c:int)
End
Definition normalise_lhs_def:
(normalise_lhs [] acc n = (REVERSE acc,n)) ∧
(normalise_lhs ((x,l)::xs) acc n =
let v = lit_var l in
if x < 0 then
normalise_lhs xs ((mk_coeff x l,v)::acc) (n+x)
else if x > 0 then normalise_lhs xs ((mk_coeff x l,v)::acc) n
else normalise_lhs xs acc n)
End
Theorem normalise_lhs_normalises:
∀ls acc n ls' n'.
normalise_lhs ls acc n = (ls',n') ⇒
iSUM (MAP (pbc$eval_term w) ls) + &(SUM (MAP (eval_term w) acc)) + n =
&(SUM (MAP (eval_term w) ls')) + n'
Proof
Induct>>rw[normalise_lhs_def,iSUM_def]
>-
metis_tac[SUM_REVERSE,MAP_REVERSE] >>
Cases_on`h`>>fs[normalise_lhs_def]>>
every_case_tac>>fs[]
>- intLib.ARITH_TAC>>
first_x_assum drule>>
simp[GSYM integerTheory.INT_ADD]
>- (
qmatch_goalsub_abbrev_tac`&SUM _ + qq`>>
`qq = q * eval_lit w r` by
(fs[Abbr`qq`]>>Cases_on`r`>>simp[mk_coeff_def]>>
rename1`b2n( w a)`>>
Cases_on`w a`>>simp[]>>
intLib.COOPER_TAC)>>
rw[] >>
rename1`A +B + C + D = E`>>
pop_assum mp_tac>> rpt (pop_assum kall_tac)>> intLib.ARITH_TAC)
>- (
qmatch_goalsub_abbrev_tac`&SUM _ + qq`>>
`qq + q = q * eval_lit w r` by (
fs[Abbr`qq`]>>Cases_on`r`>>simp[mk_coeff_def]>>
rename1`b2n (w a)`>>
Cases_on`w a`>>simp[]>>
intLib.ARITH_TAC)>>
rw[] >>
rename1`A +B + C + D = E`>>
ntac 2 (pop_assum mp_tac)>> rpt (pop_assum kall_tac)>> intLib.ARITH_TAC)
>- (
`q=0` by intLib.ARITH_TAC>>
simp[])
QED
Definition pbc_to_npbc_def:
(pbc_to_npbc (GreaterEqual,lhs,n) =
let (lhs',m') = compact_lhs (mergesort term_le lhs) 0 in
let (lhs'',m'') = normalise_lhs lhs' [] 0 in
let rhs = if n-(m'+m'') ≥ 0 then Num(n-(m'+m'')) else 0 in
(lhs'',rhs):npbc) ∧
(pbc_to_npbc _ = ([],0))
End
Definition normalise_def:
normalise pbf =
let pbf' = FLAT (MAP pbc_ge pbf) in
MAP pbc_to_npbc pbf'
End
Theorem pbc_to_npbc_thm:
FST pbc = GreaterEqual ⇒
(satisfies_pbc w pbc ⇔ satisfies_npbc w (pbc_to_npbc pbc))
Proof
PairCases_on`pbc`>>fs[]>>
rw[satisfies_pbc_def,satisfies_npbc_def,pbc_to_npbc_def]>>
pairarg_tac>>fs[]>>
pairarg_tac>>fs[]>>
drule compact_lhs_sound>>
disch_then(qspec_then`w` assume_tac)>>fs[eval_lin_term_def]>>
drule normalise_lhs_normalises>>
disch_then(qspec_then`w` assume_tac)>>fs[]>>
simp[satisfies_npbc_def]>>
intLib.ARITH_TAC
QED
Theorem normalise_thm:
satisfies w (set (normalise pbf)) ⇔
satisfies w (set pbf)
Proof
simp[normalise_def]>>
qmatch_goalsub_abbrev_tac`MAP _ pbf'`>>
`satisfies w (set pbf) ⇔ satisfies w (set pbf')` by
(simp[Abbr`pbf'`]>>
Induct_on`pbf`>>
simp[]>>
metis_tac[pbc_ge_thm])>>
simp[]>>
`!x. MEM x pbf' ⇒ FST x = GreaterEqual` by
(simp[Abbr`pbf'`,MEM_FLAT,MEM_MAP,PULL_EXISTS]>>
rw[]>>
PairCases_on`y`>>Cases_on`y0`>>fs[pbc_ge_def])>>
pop_assum mp_tac>>
rpt(pop_assum kall_tac)>>
Induct_on`pbf'`>>simp[]>>
rw[]>>
metis_tac[pbc_to_npbc_thm]
QED
Definition full_normalise_def:
full_normalise pbf = normalise (convert_pbf pbf)
End
Theorem full_normalise_satisfies:
pbf_vars (set pbf) ⊆ goodString ⇒
(satisfies w (set pbf) ⇔
satisfies (w ∘ LINV hashString goodString) (set (full_normalise pbf)))
Proof
rw[full_normalise_def,normalise_thm]>>
metis_tac[convert_pbf_satisfies]
QED
Theorem full_normalise_satisfies_2:
pbf_vars (set pbf) ⊆ goodString ⇒
(satisfies w (set (full_normalise pbf)) ⇔
satisfies (w o hashString) (set pbf))
Proof
rw[full_normalise_def,normalise_thm]>>
metis_tac[convert_pbf_satisfies_2]
QED
Theorem full_normalise_unsatisfiable:
pbf_vars (set pbf) ⊆ goodString ⇒
(unsatisfiable (set (full_normalise pbf)) ⇔
unsatisfiable (set pbf))
Proof
rw[pbcTheory.unsatisfiable_def,npbcTheory.unsatisfiable_def]>>
fs[pbcTheory.satisfiable_def,npbcTheory.satisfiable_def]>>
metis_tac[full_normalise_satisfies,full_normalise_satisfies_2]
QED
Theorem lit_var_negate[simp]:
lit_var (negate r) = lit_var r
Proof
Cases_on`r`>>simp[]
QED
Theorem normalise_lhs_compact1:
∀lhs acc n lhs' n'.
normalise_lhs lhs acc n = (lhs',n') ∧
SORTED $< (MAP SND (REVERSE acc) ++ MAP (lit_var o SND) lhs) ⇒
SORTED $< (MAP SND lhs')
Proof
Induct>>simp[normalise_lhs_def]>>
Cases>>simp[normalise_lhs_def]>>rw[]>>
first_x_assum match_mp_tac>>
asm_exists_tac>>fs[]
>- (
PURE_REWRITE_TAC[Once (GSYM APPEND_ASSOC),APPEND]>>
fs[])
>- (
PURE_REWRITE_TAC[Once (GSYM APPEND_ASSOC),APPEND]>>
fs[]) >>
pop_assum mp_tac>>
DEP_REWRITE_TAC[SORTED_APPEND,SORTED_EQ] >>
CONJ_TAC>- simp[transitive_def]>>
simp[]
QED
Theorem normalise_lhs_compact2:
∀lhs acc n lhs' n'.
normalise_lhs lhs acc n = (lhs',n') ∧
EVERY (λc. c ≠ 0) (MAP FST acc) ⇒
EVERY (λc. c ≠ 0) (MAP FST lhs')
Proof
Induct>>simp[normalise_lhs_def]
>-
simp[EVERY_MAP]>>
Cases>>fs[normalise_lhs_def]>>rw[]>>
first_x_assum match_mp_tac>>
asm_exists_tac>>fs[]>>
Cases_on`r`>>fs[mk_coeff_def]>>
intLib.ARITH_TAC
QED
Theorem compact_pbc_to_npbc:
compact (pbc_to_npbc c)
Proof
PairCases_on`c`>>Cases_on`c0`>>
rw[pbc_to_npbc_def]>>
pairarg_tac>>fs[]>>
pairarg_tac>>fs[]>>
imp_res_tac compact_lhs_no_dup>>
pop_assum mp_tac>>
impl_tac>- (
match_mp_tac mergesort_sorted>>
fs[transitive_term_le]>>
simp[total_def]>>
Cases>>Cases>>simp[])>>
strip_tac>>
CONJ_TAC>- (
match_mp_tac normalise_lhs_compact1>>
asm_exists_tac>>
simp[sorted_map]>>
qmatch_goalsub_abbrev_tac`_ tlt _`>>
`tlt = term_lt` by
simp[Abbr`tlt`,FUN_EQ_THM,FORALL_PROD]>>
fs[])>>
match_mp_tac normalise_lhs_compact2>>
asm_exists_tac>>
simp[]
QED
Theorem normalise_compact:
EVERY compact (normalise pbf)
Proof
simp[normalise_def,EVERY_MAP,compact_pbc_to_npbc]
QED
Theorem full_normalise_optimal_val:
pbf_vars (set pbf) ⊆ goodString ∧
set (MAP (lit_var ∘ SND) f) ⊆ goodString ∧
normalise_lhs
(MAP (λ(a,b). (a, map_lit hashString b)) f) [] 0 = (f',m') ⇒
optimal_val (set (full_normalise pbf)) (SOME (f',m'+c)) =
optimal_val (set pbf) (f,c)
Proof
reverse (rw[optimal_val_def])
>- (
fs[pbcTheory.optimal_val_def,satisfiable_def,pbcTheory.satisfiable_def]>>
metis_tac[full_normalise_satisfies])>>
qmatch_goalsub_abbrev_tac`eval_obj _ w`>>
qsuff_tac `optimal (w o hashString) (set pbf) f`
>- (
drule normalise_lhs_normalises>>
simp[GSYM eval_lin_term_def,eval_lin_term_MAP]>>
rw[]>>drule optimal_optimal_val>>
simp[eval_obj_def,integerTheory.INT_ADD_ASSOC])>>
`optimal w (set (full_normalise pbf)) (SOME (f',m'+c))` by
(fs[satisfiable_def]>>
imp_res_tac optimal_exists>>
simp[Abbr`w`]>>
metis_tac[SELECT_AX])>>
qpat_x_assum`Abbrev _` kall_tac>>
fs[optimal_def,pbcTheory.optimal_def]>>
CONJ_TAC
>- (
drule full_normalise_satisfies_2>>
metis_tac[])>>
rw[]>>
drule normalise_lhs_normalises>>
simp[GSYM eval_lin_term_def,eval_lin_term_MAP]>>
rw[]>>
drule full_normalise_satisfies>>
disch_then(qspec_then `w'` assume_tac)>>fs[]>>
first_x_assum drule>>
qmatch_goalsub_abbrev_tac`_ <= eval_obj _ ww`>>
first_x_assum(qspec_then`ww` mp_tac)>>
qsuff_tac` eval_lin_term (ww ∘ hashString) f = eval_lin_term w' f`
>-
simp[eval_obj_def]>>
simp[Abbr`ww`]>>
simp[eval_lin_term_def]>>
AP_TERM_TAC>>
qpat_x_assum`_ ⊆ goodString` mp_tac>>
simp[MAP_EQ_f,SUBSET_DEF,MEM_MAP,PULL_EXISTS]>>
rw[]>>
first_x_assum drule>>
Cases_on`e`>>simp[]>>
rw[]>>
`INJ hashString goodString UNIV` by
metis_tac[hashString_INJ,SUBSET_REFL]>>
drule LINV_DEF>>
Cases_on`r`>>fs[]
QED
(*--------------------------------------------------------------*
converting α pbc into num pbc
*--------------------------------------------------------------*)
Datatype:
name_to_num_state =
<| to_num : (('a, num) mlmap$map) num_map
; to_str : 'a num_map
; hash_fun : 'a -> num
; cmp_name : 'a -> 'a -> ordering
; next_num : num |>
End
Definition init_state_def:
init_state hf cmp =
<| to_num := LN
; to_str := LN
; hash_fun := hf
; cmp_name := cmp
; next_num := 0 |>
End
Definition mk_map_def:
mk_map s v n = insert (mlmap$empty s.cmp_name) v (n:num)
End
Definition name_to_num_var_def:
name_to_num_var (v:'a) (s:'a name_to_num_state) =
let h = s.hash_fun v in
case sptree$lookup h s.to_num of
| NONE =>
(s.next_num,
s with <| to_num := insert h (mk_map s v s.next_num) s.to_num ;
to_str := insert s.next_num v s.to_str ;
next_num := s.next_num + 1 |>)
| SOME m =>
case mlmap$lookup m v of
| SOME index => (index,s)
| NONE =>
(s.next_num,
s with <| to_num := insert h (mlmap$insert m v s.next_num) s.to_num ;
to_str := insert s.next_num v s.to_str ;
next_num := s.next_num + 1 |>)
End
Definition name_to_num_lit_def:
name_to_num_lit (Pos v) s = (let (v1,s1) = name_to_num_var v s in (Pos v1,s1)) ∧
name_to_num_lit (Neg v) s = (let (v1,s1) = name_to_num_var v s in (Neg v1,s1))
End
Definition name_to_num_lin_term_def:
name_to_num_lin_term [] s acc = (REVERSE acc,s) ∧
name_to_num_lin_term ((i,l)::xs) s acc =
let (l1,s1) = name_to_num_lit l s in
name_to_num_lin_term xs s1 ((i,l1)::acc)
End
Definition name_to_num_pbf_def:
name_to_num_pbf [] s acc = (REVERSE acc,s) ∧
name_to_num_pbf ((p,l,i)::xs) s acc =
let (l1,s1) = name_to_num_lin_term l s [] in
name_to_num_pbf xs s1 ((p,l1,i)::acc)
End
(* ---- verification ---- *)
Definition lookup_index_def:
lookup_index s v =
let h = s.hash_fun v in
case sptree$lookup h s.to_num of
| NONE => NONE
| SOME m => mlmap$lookup m v
End
Definition lookup_name_def:
lookup_name s n = sptree$lookup n s.to_str
End
Definition name_to_num_state_ok_def:
name_to_num_state_ok s ⇔
TotOrd s.cmp_name ∧
(∀h m.
lookup h s.to_num = SOME m ⇒
map_ok m ∧
∀name index.
lookup m name = SOME index ⇒
s.hash_fun name = h ∧ index < s.next_num) ∧
(∀name index.
lookup_name s index = SOME name ⇔ lookup_index s name = SOME index)
End
Theorem lookup_name_inj:
name_to_num_state_ok s ∧
lookup_name s index1 = SOME name ∧
lookup_name s index2 = SOME name ⇒
index1 = index2
Proof
fs [name_to_num_state_ok_def] \\ rw [] \\ gvs []
QED
Theorem lookup_index_inj:
name_to_num_state_ok s ∧
lookup_index s name1 = SOME index ∧
lookup_index s name2 = SOME index ⇒
name1 = name2
Proof
fs [name_to_num_state_ok_def] \\ metis_tac [SOME_11]
QED
Theorem init_state_ok:
TotOrd cmp ⇒
name_to_num_state_ok (init_state hf cmp)
Proof
fs [name_to_num_state_ok_def,init_state_def,lookup_name_def,lookup_index_def]
QED
Theorem name_to_num_var_thm:
name_to_num_state_ok s ∧
name_to_num_var v s = (index,s1)
⇒
name_to_num_state_ok s1 ∧
lookup_name s1 index = SOME v ∧
lookup_index s1 v = SOME index ∧
(∀w i. lookup_index s w = SOME i ⇒ lookup_index s1 w = SOME i)
Proof
fs [name_to_num_var_def,AllCaseEqs()]
\\ reverse strip_tac \\ gvs []
>-
(conj_asm2_tac >- fs [name_to_num_state_ok_def]
\\ fs [lookup_name_def,lookup_index_def])
>-
(fs [lookup_index_def,lookup_name_def,AllCaseEqs(),
sptreeTheory.lookup_insert,PULL_EXISTS]
\\ gvs [name_to_num_state_ok_def] \\ res_tac
\\ gvs [mlmapTheory.lookup_insert,lookup_index_def,lookup_name_def,
sptreeTheory.lookup_insert,AllCaseEqs()]
\\ rw [] \\ gvs [insert_thm,lookup_insert]
\\ gvs [AllCaseEqs()]
\\ res_tac \\ fs []
>-
(Cases_on ‘v = name’ \\ gvs []
\\ gvs [insert_thm,lookup_insert]
\\ Cases_on ‘s.hash_fun name = s.hash_fun v’ \\ gvs []
\\ gvs [insert_thm,lookup_insert]
\\ Cases_on ‘lookup m name’ \\ gvs [] \\ res_tac \\ fs []
\\ Cases_on ‘lookup (s.hash_fun name) s.to_num’ \\ gvs []
\\ Cases_on ‘lookup x name’ \\ gvs [] \\ res_tac \\ fs [])
\\ Cases_on ‘s.hash_fun w = s.hash_fun v’ \\ gvs []
\\ rw [] \\ gvs [insert_thm,lookup_insert]
\\ rw [] \\ gvs [])
\\ gvs [mk_map_def]
\\ qpat_abbrev_tac ‘x = empty s.cmp_name’
\\ ‘map_ok x ∧ ∀n. lookup x n = NONE’ by
(unabbrev_all_tac
\\ gvs [empty_thm,name_to_num_state_ok_def]
\\ EVAL_TAC \\ fs [])
\\ unabbrev_all_tac
\\ fs [lookup_index_def,lookup_name_def,AllCaseEqs(),
sptreeTheory.lookup_insert,PULL_EXISTS,mk_map_def]
\\ gvs [name_to_num_state_ok_def] \\ res_tac
\\ gvs [mlmapTheory.lookup_insert,lookup_index_def,lookup_name_def,
sptreeTheory.lookup_insert,AllCaseEqs()]
\\ rw [] \\ gvs [insert_thm,lookup_insert]
\\ res_tac \\ fs []
>- (Cases_on ‘v = name’ \\ gvs []
\\ gvs [insert_thm,lookup_insert]
\\ Cases_on ‘s.hash_fun name = s.hash_fun v’ \\ gvs []
\\ gvs [insert_thm,lookup_insert]
\\ Cases_on ‘lookup (s.hash_fun name) s.to_num’ \\ gvs []
\\ Cases_on ‘lookup x name’ \\ gvs [] \\ res_tac \\ fs [])
\\ Cases_on ‘s.hash_fun w = s.hash_fun v’ \\ gvs []
\\ rw [] \\ gvs [insert_thm,lookup_insert]
QED
Definition map_lin_term_def:
map_lin_term f xs = MAP (λ(a,b). (a,map_lit f b)) xs
End
Theorem name_to_num_lit:
∀x (s:'a name_to_num_state) y t.
name_to_num_lit x s = (y,t) ∧ name_to_num_state_ok s
⇒
name_to_num_state_ok t ∧
y = map_lit (THE o lookup_index t) x ∧
(∀i n. lookup_index s i = SOME n ⇒ lookup_index t i = SOME n) ∧
lit_var x ∈ { i | lookup_index t i ≠ NONE }
Proof
Cases \\ fs [name_to_num_lit_def]
\\ rpt gen_tac \\ strip_tac \\ gvs []
\\ rpt (pairarg_tac \\ gvs [])
\\ drule_all name_to_num_var_thm
\\ strip_tac \\ gvs [map_lit_def]
QED
Theorem name_to_num_lin_term:
∀xs (s:'a name_to_num_state) zs acc ys t.
name_to_num_lin_term xs s acc = (ys,t) ∧ name_to_num_state_ok s ∧
acc = map_lin_term (THE o lookup_index s) zs ∧
set (MAP (lit_var ∘ SND) zs) ⊆ { i | lookup_index s i ≠ NONE }
⇒
name_to_num_state_ok t ∧
ys = REVERSE acc ++ map_lin_term (THE o lookup_index t) xs ∧
(∀i n. lookup_index s i = SOME n ⇒ lookup_index t i = SOME n) ∧
set (MAP (lit_var ∘ SND) xs) ⊆ { i | lookup_index t i ≠ NONE }
Proof
Induct
\\ gvs [name_to_num_lin_term_def,map_lin_term_def,FORALL_PROD]
\\ rpt gen_tac \\ strip_tac
\\ rpt (pairarg_tac \\ gvs [])
\\ drule name_to_num_lit
\\ strip_tac \\ gvs []
\\ last_x_assum $ qspecl_then [‘s1’,‘(p_1,p_2)::zs’] mp_tac \\ fs []
\\ ‘MAP (λ(a,b). (a,map_lit (THE ∘ lookup_index s) b)) zs =
MAP (λ(a,b). (a,map_lit (THE ∘ lookup_index s1) b)) zs’ by
(gvs [MAP_EQ_f,FORALL_PROD,map_lit_def] \\ rw []
\\ gvs [SUBSET_DEF,MEM_MAP,PULL_EXISTS]
\\ res_tac \\ fs []
\\ rename [‘lit_var v’]
\\ Cases_on ‘lookup_index s1 (lit_var v)’ \\ gvs []
\\ res_tac \\ gvs []
\\ Cases_on ‘v’ \\ gvs [map_lit_def]
\\ res_tac \\ fs []
\\ Cases_on ‘lookup_index s a’ \\ gvs []
\\ res_tac \\ gvs [])
\\ fs []
\\ impl_tac >-
(irule SUBSET_TRANS
\\ first_x_assum $ irule_at Any
\\ fs [SUBSET_DEF]
\\ strip_tac \\ Cases_on ‘lookup_index s x’ \\ gvs [] \\ res_tac \\ fs [])
\\ strip_tac \\ gvs []
\\ Cases_on ‘lookup_index s1 (lit_var p_2)’ \\ gvs []
\\ res_tac \\ fs []
\\ Cases_on ‘p_2’ \\ gvs [map_lit_def]
\\ res_tac \\ fs []
QED
Theorem name_to_num_pbf_rec:
∀xs (s:'a name_to_num_state) zs acc ys t.
name_to_num_pbf xs s acc = (ys,t) ∧ name_to_num_state_ok s ∧
acc = MAP (map_pbc (THE o lookup_index s)) zs ∧
pbf_vars (set zs) ⊆ { i | lookup_index s i ≠ NONE }
⇒
name_to_num_state_ok t ∧
ys = REVERSE acc ++ MAP (map_pbc (THE o lookup_index t)) xs ∧
(∀i n. lookup_index s i = SOME n ⇒ lookup_index t i = SOME n) ∧
pbf_vars (set xs) ⊆ { i | lookup_index t i ≠ NONE }
Proof
Induct
\\ fs [name_to_num_pbf_def,FORALL_PROD]
>- fs [pbf_vars_def,GSYM MAP_REVERSE]
\\ fs [map_pbc_def]
\\ rpt gen_tac
\\ strip_tac
\\ rpt (pairarg_tac \\ gvs [])
\\ drule name_to_num_lin_term
\\ gvs [map_lin_term_def]
\\ strip_tac \\ gvs []
\\ rename [‘pbf_vars ((p1,p2,p3) INSERT set xs) ⊆ _’]
\\ last_x_assum $ qspecl_then [‘s1’,‘(p1,p2,p3)::zs’] mp_tac
\\ fs [map_pbc_def]
\\ ‘MAP (map_pbc (THE ∘ lookup_index s)) zs =
MAP (map_pbc (THE ∘ lookup_index s1)) zs’ by
(gvs [MAP_EQ_f,FORALL_PROD] \\ rw []
\\ fs [map_pbc_def,MAP_EQ_f,FORALL_PROD]
\\ gvs [pbf_vars_def,pbc_vars_def,SUBSET_DEF,PULL_EXISTS]
\\ gvs [pbc_vars_def,FORALL_PROD,MEM_MAP,PULL_EXISTS]
\\ rw [] \\ res_tac
\\ rename [‘lit_var v’]