-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathnpbcScript.sml
More file actions
3713 lines (3471 loc) · 96.5 KB
/
npbcScript.sml
File metadata and controls
3713 lines (3471 loc) · 96.5 KB
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
(*
Formalisation of normalised pseudo-boolean constraints
*)
Theory npbc
Ancestors
pbc integer
Libs
preamble
val _ = numLib.temp_prefer_num();
Type var = “:num”
(* Normalized pseudoboolean constraints (xs,n) represents constraint xs ≥ n
An additional compactness assumption guarantees uniqueness *)
Type npbc = ``: ((int # var) list) # int``
(* semantics *)
Definition b2n_def[simp]:
b2n T = 1:num ∧
b2n F = 0:num
End
Definition eval_lit_def[simp]:
eval_lit w b (v:var) =
if b
then 1 - b2n (w v)
else b2n (w v)
End
Definition eval_term_def[simp]:
eval_term w (c,v) = Num (ABS c) * eval_lit w (c < 0) v
End
(* npbc are trivially satisfied when n is <= 0 *)
Definition satisfies_npbc_def:
satisfies_npbc w ((xs,n):npbc) ⇔ n ≤ &(SUM (MAP (eval_term w) xs))
End
(* Tentative representation of PBF as a set of constraints *)
Definition satisfies_def:
satisfies w npbf ⇔
∀c. c ∈ npbf ⇒ satisfies_npbc w c
End
Definition satisfiable_def:
satisfiable npbf ⇔
∃w. satisfies w npbf
End
Definition unsatisfiable_def:
unsatisfiable npbf ⇔ ¬satisfiable npbf
End
Definition eval_obj_def:
eval_obj fopt w =
case fopt of NONE => 0
| SOME (f,c:int) => &(SUM (MAP (eval_term w) f)) + c
End
(* Optimality of an assignment
Here, the special case with no objective is treated as 0
*)
Definition optimal_def:
optimal w npbf fopt ⇔
satisfies w npbf ∧
∀w'.
satisfies w' npbf ⇒
eval_obj fopt w ≤ eval_obj fopt w'
End
Definition optimal_val_def:
optimal_val npbf fopt =
if satisfiable npbf then
SOME (eval_obj fopt (@w. optimal w npbf fopt))
else
NONE
End
(* compactness *)
Definition compact_def[simp]:
compact ((xs,n):npbc) ⇔
SORTED $< (MAP SND xs) ∧ (* implies that no var is mentioned twice *)
EVERY (λc. c ≠ 0) (MAP FST xs)
End
(* addition -- implementation *)
Definition offset_def:
offset c1 c2 =
if (c1 < 0) = (c2 < 0) then 0 else
let a1 = Num (ABS c1) in
let a2 = Num (ABS c2) in
if a1 <= a2 then a1 else a2
End
Definition add_terms_def:
add_terms c1 c2 v zs (k:num) =
let c = c1 + c2 in
if c = 0 then (zs, k + Num(ABS c1))
else
((c,v)::zs, k+ offset c1 c2)
End
Definition add_lists_def:
add_lists [] [] = ([],0) ∧
add_lists xs [] = (xs,0) ∧
add_lists [] ys = (ys,0) ∧
add_lists ((c,x)::xs) ((d,y)::ys) =
if x < y then
let (zs,n) = add_lists xs ((d,y)::ys) in
((c,x)::zs,n)
else if y < x then
let (zs,n) = add_lists ((c,x)::xs) ys in
((d,y)::zs,n)
else (* x = y *)
let (zs,n2) = add_lists xs ys in
add_terms c d x zs n2
End
Definition add_def:
add (xs,m) (ys,n) =
let (xs,d) = add_lists xs ys in
(xs,((m + n) - &d)):npbc
End
(* addition -- proof *)
Theorem add_terms_thm:
add_terms x y v zs k = (zs1,d) ⇒
eval_term w (x,v) + eval_term w (y,v) + SUM (MAP (eval_term w) zs) + k =
SUM (MAP (eval_term w) zs1) + d
Proof
rw[add_terms_def,AllCaseEqs(),offset_def]>>
Cases_on`x`>>Cases_on`y`>>gvs[]>>
TRY (
fs[INT_ADD_CALCULATE]>>
Cases_on`w v`>>gs[]>> NO_TAC)
>- (
Cases_on`w v`>>gs[]>>
intLib.ARITH_TAC)>>
`n < n'` by intLib.ARITH_TAC>>
simp[INT_ADD_CALCULATE]>>
Cases_on`w v`>>gs[]
QED
Theorem add_lists_thm:
∀x y zs d.
add_lists x y = (zs,d) ⇒
SUM (MAP (eval_term w) x) + SUM (MAP (eval_term w) y) =
SUM (MAP (eval_term w) zs) + d
Proof
ho_match_mp_tac add_lists_ind \\ rw [] \\ gvs [add_lists_def]
\\ Cases_on ‘x < y’ \\ fs []
\\ Cases_on ‘y < x’ \\ fs []
\\ rpt (pairarg_tac \\ gvs [])
\\ `x = y` by gs[]
\\ drule_all add_terms_thm
\\ disch_then (qspec_then ‘w’ assume_tac)
\\ gs [SUM_APPEND]
QED
Theorem add_thm:
satisfies_npbc w c1 ∧ satisfies_npbc w c2 ⇒ satisfies_npbc w (add c1 c2)
Proof
Cases_on ‘c1’ \\ Cases_on ‘c2’ \\ fs [add_def]
\\ pairarg_tac \\ fs [] \\ rw []
\\ fs [satisfies_npbc_def]
\\ drule_all add_lists_thm
\\ disch_then (qspec_then ‘w’ assume_tac)
\\ fs []
\\ intLib.ARITH_TAC
QED
(* addition -- compactness *)
Theorem add_lists_sorted_lemma[local]:
∀l1 l2 h t d x.
add_lists l1 l2 = (h::t,d) ∧
SORTED $< (x::MAP SND l1) ∧
SORTED $< (x::MAP SND l2) ⇒
x < SND h
Proof
ho_match_mp_tac add_lists_ind \\ rpt strip_tac
\\ fs [add_lists_def]
THEN1 gvs []
THEN1 gvs []
\\ Cases_on ‘x < y’ \\ fs []
\\ Cases_on ‘y < x’ \\ fs []
\\ rpt (pairarg_tac \\ gvs [])
\\ gvs [AllCaseEqs(),add_terms_def |> DefnBase.one_line_ify NONE]
\\ ‘x = y’ by fs [] \\ gvs []
\\ last_x_assum drule_all \\ fs []
QED
Theorem add_lists_sorted:
∀l l' xs d.
EVERY (λc. c ≠ 0) (MAP FST l) ∧ EVERY (λc. c ≠ 0) (MAP FST l') ∧
SORTED $< (MAP SND l) ∧ SORTED $< (MAP SND l') ∧
add_lists l l' = (xs,d) ⇒
SORTED $< (MAP SND xs) ∧ EVERY (λc. c ≠ 0) (MAP FST xs)
Proof
ho_match_mp_tac add_lists_ind
\\ REVERSE (rpt strip_tac)
\\ fs [add_lists_def] \\ gvs []
\\ imp_res_tac SORTED_TL
THEN1
(Cases_on ‘x < y’ \\ fs [] THEN1 (pairarg_tac \\ gvs [])
\\ Cases_on ‘y < x’ \\ fs [] THEN1 (pairarg_tac \\ gvs [])
\\ rpt (pairarg_tac \\ gvs [])
\\ gvs [AllCaseEqs(),add_terms_def |> DefnBase.one_line_ify NONE])
\\ Cases_on ‘x < y’ \\ fs []
THEN1
(pairarg_tac \\ gvs [] \\ Cases_on ‘zs’ \\ fs []
\\ drule add_lists_sorted_lemma \\ fs [])
\\ Cases_on ‘y < x’ \\ fs []
THEN1
(pairarg_tac \\ gvs [] \\ Cases_on ‘zs’ \\ fs []
\\ drule add_lists_sorted_lemma \\ fs [])
\\ rpt (pairarg_tac \\ gvs [])
\\ gvs [AllCaseEqs(),add_terms_def |> DefnBase.one_line_ify NONE]
\\ Cases_on ‘zs’ \\ fs []
\\ ‘x = y’ by fs [] \\ gvs []
\\ drule_all add_lists_sorted_lemma
\\ Cases_on ‘h’ \\ fs []
QED
Theorem compact_add:
compact c1 ∧ compact c2 ⇒ compact (add c1 c2)
Proof
Cases_on ‘c1’ \\ Cases_on ‘c2’ \\ fs [add_def]
\\ pairarg_tac \\ fs [] \\ metis_tac [add_lists_sorted]
QED
(* faster version of add_lists *)
Definition add_lists'_def:
add_lists' xs ys zs n =
case xs of
| [] => (REV zs ys,n)
| (x::xs1) =>
case ys of
| [] => (REV zs xs,n)
| (y::ys1) =>
let (cx,xn) = x in
let (cy,yn) = y in
if xn < yn then add_lists' xs1 ys (x::zs) n else
if yn < xn then add_lists' xs ys1 (y::zs) n else
let (zs1,n1) = add_terms cx cy xn zs n in
add_lists' xs1 ys1 zs1 n1
End
Theorem add_lists'_thm:
add_lists xs ys = add_lists' xs ys [] 0
Proof
qsuff_tac ‘∀xs ys zs n.
add_lists' xs ys zs n =
let (zs0,n0) = add_lists xs ys in
(REVERSE zs ++ zs0, n0+n)’
>- (fs [] \\ pairarg_tac \\ fs [])
\\ ho_match_mp_tac add_lists'_ind
\\ rpt gen_tac \\ strip_tac
\\ once_rewrite_tac [add_lists'_def]
\\ Cases_on ‘xs’ \\ fs [add_lists_def,REV_REVERSE_LEM]
\\ Cases_on ‘ys’ \\ fs [add_lists_def,REV_REVERSE_LEM]
\\ rename [‘add_lists (h1::_) (h2::_)’]
\\ PairCases_on ‘h1’ \\ PairCases_on ‘h2’ \\ fs []
\\ fs [add_lists_def,REV_REVERSE_LEM]
\\ rpt (IF_CASES_TAC \\ fs [])
\\ rpt (pairarg_tac \\ fs [])
\\ gvs [DefnBase.one_line_ify NONE add_terms_def,AllCaseEqs()]
QED
(* division *)
Definition IQ_def:
IQ (i:int) (j:int) =
if 0 < j then
if 0 ≤ i then &(Num i DIV Num j):int else -&(Num (-i) DIV Num j)
else if 0 ≤ i then -&(Num i DIV Num (-j))
else &(Num (-i) DIV Num (-j))
End
Definition div_ceiling_def:
div_ceiling (m:int) (n:num) =
IQ
(if m < 0
then m-(&n-1)
else m+ (&n - 1)) &n
End
Theorem IQ_quot:
j ≠ 0 ⇒
IQ i j = i quot j
Proof
simp[int_quot,IQ_def]
QED
Theorem div_ceiling_compute:
k ≠ 0 ⇒
div_ceiling (&n) k = & (n \\ k) ∧
div_ceiling (-&n) k = - & (n \\ k)
Proof
fs [div_ceiling_def,CEILING_DIV_def,IQ_quot] \\ rw []
\\ Cases_on ‘k’ \\ fs []
\\ fs [ADD1,integerTheory.INT_ADD_CALCULATE,
integerTheory.INT_SUB_CALCULATE,DIV_EQ_X]
\\ rw []
\\ fs [ADD1,integerTheory.INT_ADD_CALCULATE,
integerTheory.INT_SUB_CALCULATE,DIV_EQ_X]
\\ qmatch_goalsub_abbrev_tac ‘_ DIV k’
\\ ‘0 < k’ by fs [Abbr‘k’]
\\ drule DIVISION
\\ disch_then $ qspec_then ‘n+n'’ mp_tac
\\ drule DIVISION
\\ disch_then $ qspec_then ‘n’ mp_tac
\\ strip_tac
\\ rewrite_tac [LEFT_ADD_DISTRIB,RIGHT_ADD_DISTRIB]
\\ decide_tac
QED
Theorem div_ceiling_sign:
n ≠ 0 ⇒
(div_ceiling m n < 0 ⇔ m < 0)
Proof
Cases_on`m` \\ fs[div_ceiling_compute,IQ_quot]
\\ fs [CEILING_DIV]
\\ rw [] \\ Cases_on ‘1 < n’
\\ gvs [DIV_EQ_0]
\\ ‘n = 1’ by fs [] \\ fs []
QED
Theorem DIV_CEILING_EQ_0:
n ≠ 0 ⇒ (m \\ n = 0 ⇔ m = 0)
Proof
fs [CEILING_DIV,IQ_quot]
\\ Cases_on ‘m = 0’ \\ fs [ZERO_DIV]
\\ rw [] \\ Cases_on ‘1 < n’
\\ gvs [DIV_EQ_0]
\\ ‘n = 1’ by fs [] \\ fs []
QED
Definition div_ceiling_up_def:
div_ceiling_up (m:int) (n:num) =
IQ
(if m < 0
then m
else m+ (&n - 1)) &n
End
Definition divide_def:
divide ((l,n):npbc) k =
(MAP (λ(c,v). (div_ceiling c k, v)) l,
div_ceiling_up n k)
End
Theorem div_ceiling_le_x:
k ≠ 0 ∧ 0 ≤ n ⇒ (div_ceiling n k ≤ m ⇔ n ≤ m * &k)
Proof
rw[]>>
Cases_on ‘0 ≤ m’
>- (
Cases_on ‘m’>>
fs[]>>
Cases_on ‘n’>>
fs[div_ceiling_compute,CEILING_DIV_LE_X])>>
‘m < 0’ by intLib.ARITH_TAC>>
iff_tac>>
strip_tac
>-(
‘div_ceiling n k < 0’ by intLib.ARITH_TAC>>
rfs[div_ceiling_sign]>>
intLib.ARITH_TAC)>>
‘m * int_of_num k < 0’ by simp[integerTheory.INT_MUL_SIGN_CASES]>>
intLib.ARITH_TAC
QED
Theorem Num_div_ceiling:
0 < k ⇒ Num (ABS q) ≤ k * Num (ABS (div_ceiling q k))
Proof
Cases_on ‘q’>>
rw[div_ceiling_compute,LE_MULT_CEILING_DIV]
QED
Theorem LT_LE_ADD:
x < a ∧
y ≤ (b:num) ⇒
x + y < a + b
Proof
intLib.ARITH_TAC
QED
Theorem div_ceiling_up_eq:
(k < 0 ⇒
div_ceiling_up k n = IQ k (&n)) ∧
(¬ (k < 0) ⇒
div_ceiling_up k n = div_ceiling k n)
Proof
rw[div_ceiling_up_def,div_ceiling_def]>>
intLib.ARITH_TAC
QED
Theorem divide_thm:
satisfies_npbc w c ∧ k ≠ 0 ⇒
satisfies_npbc w (divide c k)
Proof
Cases_on ‘c’>>
rename1 ‘satisfies_npbc w (q,r)’>>
rw[divide_def,satisfies_npbc_def,MAP_MAP_o]>>
Cases_on`r < 0` >> fs[div_ceiling_up_eq]
>- (
Cases_on`r`>>
DEP_REWRITE_TAC[IQ_quot]>>
fs[]>>
intLib.ARITH_TAC)>>
DEP_REWRITE_TAC[div_ceiling_le_x]>>
CONJ_TAC >- intLib.ARITH_TAC>>
irule INT_LE_TRANS>>
goal_assum $ drule_at Any>>
last_x_assum $ kall_tac>>
Induct_on ‘q’>>
simp[]>> Cases>>
qmatch_goalsub_abbrev_tac` _ + A ≤ k * (_ + B)`>>
fs[]>>
qsuff_tac`A <= k * B`
>- intLib.ARITH_TAC>>
unabbrev_all_tac>>
rw[div_ceiling_sign,oneline b2n_def]>>
simp[Num_div_ceiling]
QED
Theorem div_ceiling_eq_0:
k ≠ 0 ⇒ (div_ceiling c k = 0 ⇔ c = 0)
Proof
fs [div_ceiling_def,IQ_quot]
\\ Cases_on ‘c = 0’ \\ fs []
\\ Cases_on ‘k’
\\ fs [ADD1,integerTheory.INT_ADD_CALCULATE,
integerTheory.INT_SUB_CALCULATE,DIV_EQ_X]
\\ Cases_on ‘c’
\\ fs [ADD1,integerTheory.INT_ADD_CALCULATE,
integerTheory.INT_SUB_CALCULATE,DIV_EQ_X]
\\ rw []
\\ fs [ADD1,integerTheory.INT_ADD_CALCULATE,
integerTheory.INT_SUB_CALCULATE,DIV_EQ_X]
QED
Theorem compact_divide:
compact c ∧ k ≠ 0 ⇒ compact (divide c k)
Proof
Cases_on`c` \\
rename1`(l,r)` \\
rw[compact_def,divide_def]
THEN1 (Induct_on `l` \\ fs [FORALL_PROD]
\\ Cases_on ‘l’ \\ fs []
\\ Cases_on ‘t’ \\ fs []
\\ PairCases_on ‘h’ \\ fs [])
\\ fs[EVERY_MAP,EVERY_MEM]
\\ rw[] \\ first_x_assum drule
\\ pairarg_tac \\ fs[]
\\ fs[div_ceiling_eq_0]
QED
(* variable-form division (Chvatal-Gomory) *)
(* Strip out 0 coefficients *)
Definition strip_zero_def:
(strip_zero [] = []) ∧
(strip_zero ((c,v)::xs) =
if c = 0i then strip_zero xs
else
(c,v)::strip_zero xs)
End
Theorem satisfies_npbc_strip_zero[simp]:
satisfies_npbc w (strip_zero l,k) ⇔
satisfies_npbc w (l,k)
Proof
rw[satisfies_npbc_def]>>
AP_TERM_TAC>>
qid_spec_tac`l`>>
ho_match_mp_tac strip_zero_ind>>
rw[strip_zero_def]
QED
Theorem EVERY_strip_zero[simp]:
∀l.
EVERY (λc. c ≠ 0)
(MAP FST (strip_zero l))
Proof
ho_match_mp_tac strip_zero_ind>>
rw[strip_zero_def]
QED
Definition cg_offset_def:
(cg_offset [] k = 0) ∧
(cg_offset ((c,v)::xs) k =
let r = (if c < 0 then Num (-c) MOD k else 0) in
r + cg_offset xs k)
End
Definition var_divide_def:
var_divide ((l,n):npbc) k =
(
strip_zero
(MAP (λ(c,v). (div_ceiling_up c k,v)) l),
div_ceiling_up (n - &cg_offset l k) k)
End
Theorem int_neg_add:
-(&(x+y)):int = -&x - &y
Proof
rw[]>>
intLib.ARITH_TAC
QED
Theorem var_divide_thm:
satisfies_npbc w c ∧ k ≠ 0 ⇒
satisfies_npbc w (var_divide c k)
Proof
Cases_on ‘c’>>
rename1 ‘satisfies_npbc w (q,r)’>>
rw[var_divide_def]>>
fs[satisfies_npbc_def,MAP_MAP_o]>>
qmatch_goalsub_abbrev_tac`r - &rr`>>
Cases_on`r − &rr < 0`>>
fs[div_ceiling_up_eq]
>- (
DEP_REWRITE_TAC[IQ_quot]>>
Cases_on`r - &rr`>>fs[]>>
intLib.ARITH_TAC)>>
DEP_REWRITE_TAC[div_ceiling_le_x]>>
CONJ_TAC >- intLib.ARITH_TAC>>
simp[INT_LE_SUB_RADD]>>
irule INT_LE_TRANS>>
goal_assum $ drule_at Any>>
last_x_assum $ kall_tac>>
pop_assum $ kall_tac>>
simp[Abbr`rr`]>>
Induct_on ‘q’>>
simp[]>> Cases>>
fs[cg_offset_def]>>
qmatch_goalsub_abbrev_tac` &(xx + A) ≤ &(k * (yy + B)) + &(zz + C)`>>
qsuff_tac`A <= k * B + C`
>- intLib.ARITH_TAC>>
unabbrev_all_tac>>
`0 < k` by fs[]>>
rename1`Num(-qq)`>>
Cases_on`qq`>>
fs[div_ceiling_up_eq,IQ_quot]>>
rw[oneline b2n_def]>>
gvs[div_ceiling_sign]
>- (
simp[div_ceiling_compute]>>
irule LE_MULT_CEILING_DIV >> fs[])
>- (
drule DIVISION>>
simp[]>>
disch_then (fn th => simp[GSYM th]))
>- (
drule DIVISION>>
disch_then (qspec_then`n` mp_tac)>>
simp[])
QED
Theorem MEM_strip_zero:
∀l c v.
MEM (c,v) (strip_zero l) ⇒
MEM v (MAP SND l) ∧ c ≠ 0
Proof
ho_match_mp_tac strip_zero_ind>>
rw[strip_zero_def]>>
metis_tac[]
QED
Theorem SORTED_strip_zero:
∀l.
SORTED $< (MAP SND l) ⇒
SORTED $< (MAP SND (strip_zero l))
Proof
ho_match_mp_tac strip_zero_ind>>
rw[strip_zero_def]>>
gvs[less_sorted_eq]>>rw[]>>
first_x_assum irule>>
fs[MEM_MAP,EXISTS_PROD]>>
metis_tac[MEM_strip_zero,MEM_MAP,PAIR]
QED
Theorem compact_var_divide:
compact c ∧ k ≠ 0 ⇒ compact (var_divide c k)
Proof
Cases_on`c` \\
rename1`(l,r)` \\
rw[var_divide_def]>>
irule SORTED_strip_zero >>
simp[MAP_MAP_o,o_DEF,LAMBDA_PROD]>>
metis_tac[SND_pair]
QED
(* MIR cut (normalized) *)
Definition mir_coeff_def:
mir_coeff c k Ak =
let a = Num (ABS c) in
let cc = &(MIN (a MOD k) (Ak) + a DIV k * Ak) in
if c < 0
then -cc
else cc
End
Definition mir_def:
mir ((l,n):npbc) k =
let Ak = n % &k in
(strip_zero (MAP (λ(c,v). (mir_coeff c k (Num Ak), v)) l),
div_ceiling_up n k * Ak)
End
(* TODO: some SUM lemmas not needed, but should be in the libs *)
Theorem INT_MOD_nat_nn:
k ≠ 0 ⇒ 0 ≤ p % &k
Proof
rw[]>>
`&k ≠ 0` by fs[]>>
drule INT_MOD_BOUNDS>>
rw[]
QED
Theorem INT_MUL_LE_ZERO:
x ≤ 0 ∧ 0 ≤ y ⇒ x * y ≤ 0i
Proof
Cases_on`y=0`>>rw[]>>
`0 < y` by intLib.ARITH_TAC>>
drule INT_LE_MONO>>
disch_then(qspecl_then[`x`,`0`] assume_tac)>>
gvs[]>>
intLib.ARITH_TAC
QED
Theorem SUM_MAP_MUL_CONST:
∀ls.
SUM (MAP (\x. C * f x) ls) =
C * (SUM (MAP f ls))
Proof
Induct>>rw[]
QED
Theorem SUM_MAP_FILTER:
∀ls.
SUM (MAP f (FILTER P ls)) ≤
SUM (MAP f ls)
Proof
Induct>>rw[]
QED
Theorem SUB_RIGHT_ADD':
n:num ≤ m ⇒
m - n + p = m + p - n
Proof
rw[]
QED
Theorem SUM_MIN_split:
∀ls.
SUM (MAP (\x. MIN (f x) (g x)) ls) =
SUM (MAP f ls) -
SUM (MAP f (FILTER (λx. g x ≤ f x) ls)) +
SUM (MAP g (FILTER (λx. g x ≤ f x) ls))
Proof
Induct>>rw[]>>
gvs[MIN_DEF]>>
DEP_ONCE_REWRITE_TAC[SUB_RIGHT_ADD']>>
CONJ_ASM1_TAC
>- metis_tac[SUM_MAP_FILTER]
>- intLib.ARITH_TAC
QED
Theorem SUM_CONST:
∀ls.
SUM (MAP (λx. c) ls) = c * LENGTH ls
Proof
Induct>>rw[ADD1]
QED
Theorem iSUM_MAP_PLUS:
∀ls.
iSUM (MAP (λx. f x + g x) ls) =
iSUM (MAP f ls) + iSUM (MAP g ls)
Proof
Induct>>rw[iSUM_def]>>
intLib.ARITH_TAC
QED
Theorem iSUM_MAP_MUL_CONST:
∀ls.
iSUM (MAP (\x. f x * C) ls) =
C * (iSUM (MAP f ls))
Proof
Induct>>rw[iSUM_def]>>
intLib.ARITH_TAC
QED
Theorem iSUM_int_min_split:
∀ls.
iSUM (MAP (\x. int_min (f x) (g x)) ls) =
iSUM (MAP f ls) -
iSUM (MAP f (FILTER (λx. g x ≤ f x) ls)) +
iSUM (MAP g (FILTER (λx. g x ≤ f x) ls))
Proof
Induct>>rw[iSUM_def]>>
gvs[INT_MIN]>>rw[]>>
intLib.ARITH_TAC
QED
Theorem iSUM_CONST:
∀ls.
iSUM (MAP (λx. c) ls) = c * &LENGTH ls
Proof
Induct>>rw[ADD1,iSUM_def]>>
intLib.ARITH_TAC
QED
Theorem iSUM_MAP_FILTER:
∀ls.
(∀x. MEM x ls ⇒ 0 ≤ f x) ⇒
iSUM (MAP f (FILTER P ls)) ≤
iSUM (MAP f ls)
Proof
Induct>>rw[iSUM_def]>>
gvs[SF DNF_ss]>>
intLib.ARITH_TAC
QED
Theorem INT_MOD_BOUNDS':
0 < k ⇒ 0 ≤ p % k ∧ p % k < k
Proof
rw[]>>
`k ≠ 0` by intLib.ARITH_TAC>>
drule INT_MOD_BOUNDS>>
disch_then(qspec_then`p` mp_tac)>>rw[]>>
intLib.ARITH_TAC
QED
Theorem iSUM_EQ_DIV_MOD:
0 < d ⇒
iSUM ls =
d * iSUM ( MAP (λx. x / d) ls ) +
iSUM (MAP ( λx. x % d) ls)
Proof
Induct_on`ls`>>rw[iSUM_def]>>gvs[]>>
`d ≠ 0` by intLib.ARITH_TAC>>
drule INT_DIVISION>>
disch_then(qspec_then`h` (assume_tac o cj 1))>>
intLib.ARITH_TAC
QED
Theorem mix_inequality:
B ≤ A ∧ C ≤ D ⇒
A * C + D * B ≤
A * D + C * (B:int)
Proof
rw[]>>
`(D - C) * B ≤ (D - C) * A` by (
Cases_on`0 < D-C`>>fs[]
>- (
DEP_REWRITE_TAC[INT_LE_MONO]>>
fs[])>>
`D - C = 0` by intLib.ARITH_TAC>>
simp[])>>
fs[INT_SUB_RDISTRIB]>>
intLib.ARITH_TAC
QED
Theorem int_mir_inequality:
0 < d ∧ 0 ≤ C ∧ C < d ∧
B * d + C ≤ iSUM ls ⇒
(B + 1) * C ≤
iSUM
(MAP (λx.
int_min (x % d) C + x / d * C
) ls)
Proof
rw[]>>
qmatch_goalsub_abbrev_tac`_ ≤ rhs`>>
`rhs =
iSUM (MAP (λx. int_min (x % d) C) ls) +
C * iSUM (MAP (λx. x / d) ls)` by
rw[Abbr`rhs`,iSUM_MAP_PLUS,iSUM_MAP_MUL_CONST]>>
gvs[]>>pop_assum kall_tac>>
qabbrev_tac`lsS = FILTER (λx. C ≤ x % d) ls`>>
qmatch_goalsub_abbrev_tac`_ ≤ rhs1 + _`>>
`rhs1 =
iSUM (MAP (λx. x % d) ls) -
iSUM (MAP (λx. x % d) lsS) +
C * &(LENGTH lsS)` by
simp[Abbr`rhs1`,iSUM_int_min_split,iSUM_CONST]>>
gvs[]>>pop_assum kall_tac>>
qmatch_goalsub_abbrev_tac`_ ≤ xx - yy + C * &LENGTH lsS + C * E`>>
drule INT_MOD_BOUNDS'>> strip_tac>>
`yy ≤ xx` by (
unabbrev_all_tac>>
irule iSUM_MAP_FILTER>>
rw[])>>
reverse(Cases_on`E + &LENGTH lsS ≤ B`)
>- (
`B + 1 ≤ E + &LENGTH lsS` by intLib.ARITH_TAC>>
`C * (B+1) ≤ C * (E + &LENGTH lsS)` by
(Cases_on`C`>>fs[]>>
DEP_REWRITE_TAC[INT_LE_MONO]>>
fs[])>>
fs[]>>
intLib.ARITH_TAC)>>
`yy ≤ d * &LENGTH lsS` by (
unabbrev_all_tac>>
ntac 2 $ pop_assum kall_tac>>
qpat_x_assum` _ ≤ iSUM _` kall_tac>>
Induct_on`ls`>>rw[ADD1,iSUM_def]>>
`h % d < d` by fs[]>>
intLib.ARITH_TAC)>>
`iSUM ls = d * E + xx` by (
unabbrev_all_tac>>
irule iSUM_EQ_DIV_MOD>>
fs[])>>
`(B - E) * d + C ≤ xx` by (
fs[INT_SUB_RDISTRIB]>>
intLib.ARITH_TAC)>>
qsuff_tac`
(B − E) * C + d * &LENGTH lsS ≤
(B − E) * d + C * &LENGTH lsS`
>- (
fs[INT_SUB_RDISTRIB]>>
intLib.ARITH_TAC)>>
irule mix_inequality>>
gvs[]>>
intLib.ARITH_TAC
QED
Theorem SUM_iSUM:
∀ls.
&SUM ls = iSUM (MAP (λn:num. (&n):int) ls)
Proof
Induct>>rw[iSUM_def]>>
intLib.ARITH_TAC
QED
Theorem mir_inequality:
0 < d ∧ C < d ∧
B * d + C ≤ SUM ls ⇒
(B + 1) * C ≤
SUM (MAP (λx.
MIN (x MOD d) C + x DIV d * C
) ls)
Proof
PURE_REWRITE_TAC[Once (GSYM INT_OF_NUM_LE),SUM_iSUM]>>
strip_tac>>
`&(B * d + C) = &B * &d + &C` by
fs[INT_OF_NUM_ADD]>>
pop_assum SUBST_ALL_TAC>>
drule_at Any int_mir_inequality>>
simp[]>>
strip_tac>>
PURE_REWRITE_TAC[Once (GSYM INT_OF_NUM_LE),SUM_iSUM]>>
fs[INT_OF_NUM_ADD,MAP_MAP_o,o_DEF]>>
qmatch_asmsub_abbrev_tac`_ ≤ iSUM ls1`>>
qmatch_goalsub_abbrev_tac`_ ≤ iSUM ls2`>>
`ls1 = ls2` by (
unabbrev_all_tac>>
rw[MAP_EQ_f]>>
intLib.ARITH_TAC)>>
fs[]
QED
Theorem mir_thm:
satisfies_npbc w c ∧ k ≠ 0 ⇒
satisfies_npbc w (mir c k)
Proof
Cases_on ‘c’>>
rename1 ‘satisfies_npbc w (q,r)’>>
rw[mir_def,satisfies_npbc_def,MAP_MAP_o]>>
drule INT_MOD_nat_nn>>
disch_then(qspec_then`r` assume_tac)>>
Cases_on`r < 0` >> fs[div_ceiling_up_eq]
>- (
Cases_on`r`>>
DEP_REWRITE_TAC[IQ_quot]>>
fs[]>>
qmatch_goalsub_abbrev_tac`lhs ≤ _`>>
qsuff_tac`lhs ≤ 0` >- intLib.ARITH_TAC>>
fs[Abbr`lhs`]>>
irule INT_MUL_LE_ZERO>>
fs[])>>
Cases_on`r`>>fs[]>>
`n DIV k * k + n MOD k ≤ SUM (MAP (eval_term w) q)` by
(`0 < k` by fs[]>>
metis_tac[DIVISION])>>
drule_at Any mir_inequality>>
simp[]>>
qmatch_goalsub_abbrev_tac`ll ≤ rr ⇒ l ≤ &r`>>
`l = &ll` by (
unabbrev_all_tac>>
simp[div_ceiling_compute,CEILING_DIV]>>
rw[MIN_DEF])>>
`rr = r` by (
unabbrev_all_tac>>
AP_TERM_TAC>>
simp[MAP_EQ_f,MAP_MAP_o]>>rw[]>>
pairarg_tac>>simp[oneline b2n_def,mir_coeff_def]>>rw[]>>gvs[]
)>>
simp[]
QED
Theorem compact_mir:
compact c ∧ k ≠ 0 ⇒ compact (mir c k)
Proof
Cases_on`c` \\
rename1`(l,r)` \\
rw[mir_def]>>
irule SORTED_strip_zero >>
simp[MAP_MAP_o,o_DEF,LAMBDA_PROD]>>
metis_tac[SND_pair]
QED
(* MIR cut variable-form *)
Definition var_form_offset_def:
(var_form_offset [] = 0) ∧
(var_form_offset ((c,v)::xs) =
let r = (if c < 0 then c else 0i) in
r + var_form_offset xs)
End
(* the int_min expression can be simplified...*)
Definition var_mir_coeff_def:
var_mir_coeff c k Ak =
if c < 0
then
int_min (c % &k) (&Ak) + c / &k * &Ak
else
let a = Num c in
&(MIN (a MOD k) (Ak) + a DIV k * Ak)
End
(* n'/&k should be the same as div_ceiling_up, but this is easier to prove *)
Definition var_mir_def:
var_mir ((l,n):npbc) k =
let n' = n + var_form_offset l in
let Ak = n' % &k in
let ll = MAP (λ(c,v). (var_mir_coeff c k (Num Ak), v)) l in
(strip_zero ll,
(n' / &k + 1) * Ak - var_form_offset ll)
End
(* re-stating in variable normal form *)
Theorem SUM_eval_term_alt:
∀xs.
&SUM (MAP (eval_term w) xs) =
iSUM (MAP (λ(c,v). c * b2i (w v)) xs) - var_form_offset xs
Proof
ho_match_mp_tac var_form_offset_ind>>
rw[var_form_offset_def,iSUM_def]>>
Cases_on`w xs`>>fs[]>>
intLib.ARITH_TAC
QED
Theorem satisfies_npbc_alt:
satisfies_npbc w (xs,n) ⇔
n + var_form_offset xs ≤
iSUM ( MAP (λ(c,v). c * b2i (w v)) xs)
Proof
rw[satisfies_npbc_def]>>
simp[SUM_eval_term_alt]>>
intLib.ARITH_TAC
QED
Theorem var_mir_thm:
satisfies_npbc w c ∧ k ≠ 0 ⇒
satisfies_npbc w (var_mir c k)
Proof
Cases_on ‘c’>>
rename1 ‘satisfies_npbc w (q,r)’>>
simp[Once satisfies_npbc_alt]>>
qmatch_goalsub_abbrev_tac`n ≤ _`>>
strip_tac>>
`&k ≠ 0i` by fs[]>>
drule INT_DIVISION>>fs[SF DNF_ss]>>
strip_tac>>
`n / &k * &k + n % &k ≤ iSUM (MAP (λ(c,v). c * b2i (w v)) q)` by
metis_tac[]>>
drule_at Any int_mir_inequality>>
simp[var_mir_def,Once satisfies_npbc_alt]>>
`0 ≤ n % &k` by metis_tac[INT_MOD_nat_nn]>>
`?nn. n % &k = &nn` by
(Cases_on`n % &k`>>fs[])>>
pop_assum SUBST_ALL_TAC>>
qmatch_goalsub_abbrev_tac`_ ≤ rr ⇒ _ ≤ rrr`>>
`rr = rrr` by (
unabbrev_all_tac>>
AP_TERM_TAC>>
simp[MAP_EQ_f,MAP_MAP_o]>>rw[]>>
pairarg_tac>>simp[oneline b2i_def,var_mir_coeff_def]>>
rw[]>>gvs[]>>
Cases_on`c`>>fs[]>>