-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctagon.v
1310 lines (1156 loc) · 38.1 KB
/
octagon.v
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
Require Import Nat.
Require Import Coq.NArith.NArith.
Open Scope bool_scope.
Require Import ZArith.
Open Scope Z_scope.
Require Import List.
Import ListNotations.
Require Import Lia.
Module Octagon.
Definition environment : Type := nat -> Z.
Definition pmUnit := { z : Z | z = 1 \/ z = - 1 }.
Lemma pmUnit_hprop : forall (z : Z) (p q : z = 1 \/ z = -1 ), p = q.
Proof.
intros z p q.
destruct p, q; try lia.
all: f_equal. all: eapply Logic.Eqdep_dec.UIP_dec.
all: repeat decide equality.
(* https://coq.inria.fr/doc/V8.17.1/stdlib/Coq.Logic.Eqdep_dec.html#UIP_dec *)
Qed.
Definition opt_to_list{A: Type}(a: option A): list A :=
match a with
| Some a' => [a']
| None => []
end.
Module Term.
Declare Scope term_scope.
Delimit Scope term_scope with term.
Open Scope term_scope.
Record term := {a: pmUnit; x: nat}.
Program Definition eqb(t t': term) :=
(t.(a) =? t'.(a))%Z && (t.(x) =? t'.(x))%nat.
Infix "=?" := eqb (at level 70) : term_scope.
Lemma eqb_refl(t: term) : (t =? t = true).
Proof.
unfold eqb.
apply andb_true_intro.
split.
- apply Z.eqb_refl.
- apply Nat.eqb_refl.
Qed.
Lemma eqb_eq (t t': term): t = t' <-> (t =? t') = true.
Proof.
split.
- intros t_t'. subst. apply eqb_refl.
- destruct t as [a x]; destruct t' as [a' x'].
intros h.
apply andb_prop in h as [a_eqb_a' x_eqb_x'].
simpl in a_eqb_a'.
apply Z.eqb_eq in a_eqb_a'.
apply (eq_sig_hprop pmUnit_hprop) in a_eqb_a'.
simpl in x_eqb_x'.
apply Nat.eqb_eq in x_eqb_x'.
subst a' x'.
reflexivity.
Qed.
Lemma eqb_spec(t t': term) : Bool.reflect (t = t') (t =? t').
Proof.
apply Bool.iff_reflect.
exact (eqb_eq t t').
Qed.
Program Definition op(t: term) :=
{| a := - t.(a)
; x := t.(x)
|}.
Next Obligation.
destruct (a t) as [av [h | h']]; subst.
- right. reflexivity.
- left. reflexivity.
Qed.
Notation "'-' t" := (op t) (at level 35, right associativity): term_scope.
End Term.
Import Term.
Inductive Constraint :=
| AddConstr (l r: term)(d: Z)
| BndConstr (t: term)(d: Z).
Module ConstraintEq.
Declare Scope constr_scope.
Delimit Scope constr_scope with constr.
Open Scope constr_scope.
Definition eqb(c c': Constraint): bool :=
match c, c' with
| AddConstr l r d, AddConstr l' r' d' => ((l =? l') && (r =? r') && (d =? d')%Z)%term
| BndConstr t d, BndConstr t' d' => ((t =? t') && (d =? d')%Z)%term
| _, _ => false
end.
Infix "=?" := eqb (at level 70) : constr_scope.
Lemma eqb_refl(c: Constraint): c =? c = true.
Proof.
destruct c; simpl.
- rewrite (Term.eqb_refl l).
rewrite (Term.eqb_refl r).
rewrite (Z.eqb_refl d).
reflexivity.
- rewrite (Term.eqb_refl t).
rewrite (Z.eqb_refl d).
reflexivity.
Qed.
Lemma eqb_eq(c c': Constraint) : (c = c') <-> (c =? c' = true).
Proof.
split.
- intros h. subst. apply eqb_refl.
- intros c_c'.
destruct c; destruct c' as [l' r' d' | t' d']; try discriminate; simpl in c_c'.
-- apply Bool.andb_true_iff in c_c' as [ht d_d'].
apply Bool.andb_true_iff in ht as [l_l' r_r'].
apply Term.eqb_eq in l_l'.
apply Term.eqb_eq in r_r'.
apply Z.eqb_eq in d_d'.
subst; reflexivity.
-- apply Bool.andb_true_iff in c_c' as [t_t' d_d'].
apply Term.eqb_eq in t_t'.
apply Z.eqb_eq in d_d'.
subst; reflexivity.
Qed.
Definition eqb_spec(c c': Constraint) : Bool.reflect (c = c') (c =? c') :=
Bool.iff_reflect _ _ (eqb_eq _ _).
End ConstraintEq.
Definition combine(c c': Constraint): option Constraint :=
match c, c' with
| AddConstr l r d, AddConstr l' r' d' =>
if l =? (op l') then Some (AddConstr r r' (d + d'))
else if l =? (op r') then Some (AddConstr r l' (d + d'))
else if r =? (op l') then Some (AddConstr l r' (d + d'))
else if r =? (op r') then Some (AddConstr l l' (d + d'))
else None
| AddConstr l r d, BndConstr t' d' =>
if l =? (op t') then Some (BndConstr r (d + d'))
else if r =? (op t') then Some (BndConstr l (d + d'))
else None
| BndConstr t d, AddConstr l' r' d' =>
if l' =? (op t) then Some (BndConstr r' (d + d'))
else if r' =? (op t) then Some (BndConstr l' (d + d'))
else None
| BndConstr t d, BndConstr t' d' => Some (AddConstr t t' (d + d'))
end.
Definition trivial_impl(c c': Constraint): bool :=
match c, c' with
| AddConstr l r d, AddConstr l' r' d' =>
((l =? l') && (r =? r') || (l =? r') && (r =? l')) && (d <=? d')%Z
| BndConstr t d, BndConstr t' d' => (t =? t') && (d <=? d')%Z
| _, _ => false
end.
Lemma destruct_aoaa(a b c d e: bool) :
(a && b || c && d) && e = true -> (a = true /\ b = true \/ c = true /\ d = true) /\ e = true.
Proof.
intros h.
apply Bool.andb_true_iff in h.
destruct h as [h Pe].
split; try assumption.
apply Bool.orb_true_iff in h.
destruct h as [h | h].
- left. apply Bool.andb_true_iff in h. assumption.
- right. apply Bool.andb_true_iff in h. assumption.
Qed.
Definition joined(c': Constraint)(cs: list Constraint) :=
c' :: filter (fun c => negb (trivial_impl c' c)) cs.
Definition join(C C': list Constraint): list Constraint :=
fold_left (fun cs c' =>
if forallb (fun c => negb (trivial_impl c c')) cs
then joined c' cs
else cs
) C' C.
Definition normalize_constraint(c: Constraint): Constraint :=
match c with
| AddConstr l r d =>
if l =? r then BndConstr l (d / 2)
else c
| c => c
end.
Definition flatten(C: list Constraint): list Constraint :=
map normalize_constraint C.
Definition flatten_is_nil(C: list Constraint): flatten C = [] <-> C = [].
Proof.
split; intros h; try (subst; reflexivity).
unfold flatten in h.
destruct C as [|c' C']; try reflexivity.
simpl in h.
unfold normalize_constraint in h.
destruct c'; [ destruct (l =? r) | idtac ]; discriminate.
Qed.
Definition new_constraints(C: list Constraint): list Constraint :=
flat_map (fun c => flat_map (fun c' => opt_to_list(combine c c')) C) C.
Definition iterate(C: list Constraint) : list Constraint :=
let C' := new_constraints C in
let C'' := flatten C' in
join C C''.
Definition model : Type := nat -> Z.
Definition term_value(m: model)(t: term): Z := proj1_sig t.(a) * (m t.(x)).
Lemma term_value_op(t: term)(m: model): term_value m (-t) = (- (term_value m t))%Z.
Proof.
unfold term_value.
destruct t as [a x].
unfold Term.a.
simpl.
apply Z.mul_opp_l.
Qed.
Definition satisfies_single_constraint (m: model) (c: Constraint): bool :=
match c with
| AddConstr l r d => term_value m l + term_value m r <=? d
| BndConstr t d => term_value m t <=? d
end.
Definition satisfies_constraints (m: model) (C: list Constraint): bool :=
forallb (satisfies_single_constraint m) C.
Definition implies(c c': Constraint) := forall (m : model),
let is_sat := satisfies_single_constraint m in
is_sat c = true -> is_sat c' = true.
Infix "-->" := implies (at level 90, right associativity).
Definition imply(C: list Constraint)(c': Constraint) :=
forall (m : model),
satisfies_constraints m C = true -> satisfies_single_constraint m c' = true.
Infix "==>" := imply (at level 95, right associativity).
Definition implication(C: list Constraint)(C': list Constraint) :=
forall m, satisfies_constraints m C = true -> satisfies_constraints m C' = true.
Infix "==>>" := implication (at level 96, right associativity).
Theorem implication_caract(C C': list Constraint) :
(C ==>> C') <-> forall c', In c' C' -> (C ==> c').
Proof.
split.
- intros C_sat_imp_C'_sat x x_in_C' m C_sat.
pose proof (C_sat_imp_C'_sat m C_sat) as C'_sat.
unfold satisfies_constraints in C'_sat.
rewrite forallb_forall in C'_sat.
exact (C'_sat x x_in_C').
- intros C_imp_C' m C_sat.
unfold implication in C_imp_C'.
unfold satisfies_constraints.
apply forallb_forall.
intros x x_in_C'.
apply (C_imp_C' x x_in_C' m).
assumption.
Qed.
Lemma nil_caract{A: Type}(ls: list A): (forall x, ~ In x ls) -> ls = [].
Proof.
intros no_mem_ls.
destruct ls.
- reflexivity.
- specialize (no_mem_ls a0).
exfalso.
apply no_mem_ls.
apply in_eq.
Qed.
Lemma app_and_cons{A : Type}(x:A)(xs: list A): [x] ++ xs = x :: xs.
Proof.
auto.
Qed.
Module ImplFacts.
Lemma impl_trans(c c' c'': Constraint) :
(c --> c') -> (c' --> c'') -> (c --> c'').
Proof.
intros c_imp_c' c'_imp_c'' m.
intros ? c_proof.
exact (c'_imp_c'' m (c_imp_c' m c_proof)).
Qed.
Theorem impls_more(C: list Constraint)(c c': Constraint) :
(C ==> c) -> (c' :: C ==> c').
Proof.
intros C_imp_c m.
unfold satisfies_constraints.
apply Bool.andb_true_iff.
Qed.
Lemma superset_preserves_implication(C C': list Constraint)(c: Constraint) :
(forall x, In x C -> In x C') ->
(C ==> c) -> (C' ==> c).
Proof.
intros C_subset_C' C_imp_c.
destruct C' as [|c' C's] eqn:h.
- simpl in C_subset_C'.
pose proof (nil_caract C C_subset_C') as h'.
subst. apply C_imp_c.
- intros m.
rewrite <- h; rewrite <- h in C_subset_C'.
unfold imply in C_imp_c.
specialize (C_imp_c m).
intros C'_sat. apply C_imp_c.
unfold satisfies_constraints.
apply forallb_forall.
intros x x_in_C.
pose proof (C_subset_C' x x_in_C) as x_in_C'.
unfold satisfies_constraints in C'_sat.
assert (in_C'_sat: forall x0 : Constraint, In x0 C' -> satisfies_single_constraint m x0 = true).
{
apply forallb_forall.
apply C'_sat.
}
apply in_C'_sat.
exact x_in_C'.
Qed.
Lemma order_does_not_matter(C C': list Constraint)(c: Constraint) :
(forall x, In x C <-> In x C') ->
(C ==> c) <-> (C' ==> c).
Proof.
intros same_elems.
split.
- apply superset_preserves_implication.
intros x. apply same_elems.
- apply superset_preserves_implication.
intros x. apply same_elems.
Qed.
Theorem impls_trans(C: list Constraint)(c c': Constraint) :
(C ==> c) -> (c :: C ==> c') -> (C ==> c').
Proof.
intros C_imp_c c_C_imp_c'.
intros m.
specialize (C_imp_c m).
specialize (c_C_imp_c' m).
unfold satisfies_constraints.
intros h.
apply c_C_imp_c'.
unfold satisfies_constraints.
simpl.
apply Bool.andb_true_iff.
split; auto.
Qed.
Lemma app_imp_l(C: list Constraint)(c c': Constraint):
(C ==> c') -> (c ::C ==> c').
Proof.
intros C_imp_c' m.
specialize (C_imp_c' m).
intros h.
apply Bool.andb_true_iff in h as [h h'].
apply C_imp_c'.
unfold satisfies_constraints.
assumption.
Qed.
Lemma app_implication_r(C C': list Constraint)(c': Constraint):
(C ==>> c' :: C') -> (C ==>> C').
Proof.
intros C_imp_c' m C_sat.
pose proof (C_imp_c' m C_sat) as c'_C'_sat.
simpl in c'_C'_sat;
apply Bool.andb_true_iff in c'_C'_sat as [c'_sat C'_sat].
assumption.
Qed.
Theorem implication_trans(C C' C'': list Constraint):
(C ==>> C') -> (C' ==>> C'') -> (C ==>> C'').
Proof.
unfold implication.
intros C_C' C'_C''.
assert (h: (C ++ C' ==>> C'')). {
intros m C_C'_sat; specialize (C_C' m); specialize (C'_C'' m).
apply C'_C''.
unfold satisfies_constraints in *.
rewrite forallb_app in C_C'_sat.
apply Bool.andb_true_iff in C_C'_sat as [C_sat C'_sat].
assumption.
}
intros m; specialize (C_C' m); specialize (C'_C'' m).
specialize (h m).
unfold satisfies_constraints in h; rewrite forallb_app in h.
intros C_sat.
apply h.
apply Bool.andb_true_iff.
split; try assumption.
exact (C_C' C_sat).
Qed.
Theorem implication_refl(C: list Constraint):
(C ==>> C).
Proof.
intros m C_sat; assumption.
Qed.
End ImplFacts.
Import ImplFacts.
Lemma add_constr_comm(l r: term)(d: Z) :
AddConstr l r d --> AddConstr r l d.
Proof.
intros m. simpl.
intros h.
rewrite (Z.add_comm (term_value m r) (term_value m l)).
assumption.
Qed.
Lemma trivial_impl_is_implication(c c': Constraint) :
trivial_impl c c' = true -> (c --> c').
Proof.
intros c_ti_c' m.
destruct c; destruct c' as [l' r' d'| t' d']; try (discriminate || unfold satisfies_single_constraint).
- intros h.
unfold trivial_impl in c_ti_c'.
apply destruct_aoaa in c_ti_c' as [[[l_l' r_r'] | [l_r' r_l']] d_le_d'].
-- apply Term.eqb_eq in l_l'.
apply Term.eqb_eq in r_r'.
subst l' r'.
apply Zle_is_le_bool in d_le_d'.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool.
apply (Z.le_trans _ _ _ h d_le_d').
-- apply Term.eqb_eq in l_r'.
apply Term.eqb_eq in r_l'.
subst l' r'.
apply Zle_is_le_bool in d_le_d'.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool.
rewrite (Z.add_comm (term_value m r) (term_value m l)).
apply (Z.le_trans _ _ _ h d_le_d').
- unfold trivial_impl in c_ti_c'.
apply Bool.andb_true_iff in c_ti_c' as [t_t' d_le_d'].
apply Term.eqb_eq in t_t'.
subst t'.
intros h.
apply Zle_is_le_bool in d_le_d'.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool.
apply (Z.le_trans _ _ _ h d_le_d').
Qed.
Lemma combine_addition_constraints(x y z t: Z)(d d': Z) :
x + y <= d
-> z + t <= d'
-> x + y + z + t <= d + d'.
Proof.
intros h h'.
transitivity (x + y + (z + t)).
- rewrite <- (Zplus_assoc_reverse _ (z) (t)).
apply Z.le_refl.
- apply (Zplus_le_compat _ _ _ _ h h').
Qed.
Lemma combine_bound_constraints(x y:Z)(d d': Z):
x <= d
-> y <= d'
-> x + y <= d + d'.
Proof.
intros h h'.
apply (Zplus_le_compat _ _ _ _ h h').
Qed.
Lemma combine_mixed_constraints(x y z: Z)(d d': Z) :
x + y <= d
-> z <= d'
-> x + y + z <= d + d'.
Proof.
intros h h'.
apply (Zplus_le_compat _ _ _ _ h h').
Qed.
Lemma add_constr_are_commutable(l r: term)(d: Z) :
(AddConstr l r d) --> (AddConstr r l d).
Proof.
intros m is_sat h.
subst is_sat.
simpl in *.
rewrite (Z.add_comm (term_value m r) (term_value m l)).
assumption.
Qed.
Theorem combine_impl(c c' c'': Constraint) :
combine c c' = Some c'' -> [c;c'] ==> c''.
Proof.
destruct c; destruct c' as [l' r' d' | t' d'].
- unfold combine.
destruct (l =? - l') eqn:E.
--
intros h. injection h as h.
apply Term.eqb_eq in E.
subst c'' l.
intros m.
simpl.
rewrite term_value_op.
intros h.
apply Bool.andb_true_iff in h as [h h'].
rewrite -> (Bool.andb_true_r _) in h'.
apply Zle_is_le_bool.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool in h'.
transitivity (- term_value m l' + term_value m r + term_value m l' + term_value m r').
--- lia.
--- apply (combine_addition_constraints _ _ _ _ _ _ h h').
-- destruct E. destruct (l =? - r') eqn:E.
--- intros h. injection h as h.
apply Term.eqb_eq in E.
subst c'' l.
intros m.
simpl.
rewrite term_value_op.
intros h.
apply Bool.andb_true_iff in h as [h h'].
rewrite -> (Bool.andb_true_r _) in h'.
apply Zle_is_le_bool.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool in h'.
transitivity (- term_value m r' + term_value m r + term_value m l' + term_value m r').
---- lia.
---- apply (combine_addition_constraints _ _ _ _ _ _ h h').
--- destruct E. destruct (r =? - l') eqn:E.
---- intros h. injection h as h.
apply Term.eqb_eq in E.
subst c'' r.
intros m.
simpl.
rewrite term_value_op.
intros h.
apply Bool.andb_true_iff in h as [h h'].
rewrite -> (Bool.andb_true_r _) in h'.
apply Zle_is_le_bool.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool in h'.
transitivity (term_value m l + - term_value m l' + term_value m l' + term_value m r').
+ lia.
+ apply (combine_addition_constraints _ _ _ _ _ _ h h').
---- destruct E. destruct (r =? - r') eqn:E.
----- intros h. injection h as h.
apply Term.eqb_eq in E.
subst c'' r.
intros m.
simpl.
rewrite term_value_op.
intros h.
apply Bool.andb_true_iff in h as [h h'].
rewrite -> (Bool.andb_true_r _) in h'.
apply Zle_is_le_bool.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool in h'.
transitivity (term_value m l + - term_value m r' + term_value m l' + term_value m r').
+ lia.
+ apply (combine_addition_constraints _ _ _ _ _ _ h h').
----- discriminate.
- unfold combine.
destruct (l =? - t') eqn:E.
-- intros h. injection h as h.
apply Term.eqb_eq in E.
subst c'' l.
intros m.
simpl.
rewrite term_value_op.
intros h.
apply Bool.andb_true_iff in h as [h h'].
rewrite -> (Bool.andb_true_r _) in h'.
apply Zle_is_le_bool.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool in h'.
transitivity (- term_value m t' + term_value m r + term_value m t').
+ lia.
+ apply (combine_mixed_constraints _ _ _ _ _ h h').
-- destruct E. destruct (r =? -t') eqn:E.
--- intros h. injection h as h.
apply Term.eqb_eq in E.
subst c'' r.
intros m.
simpl.
rewrite term_value_op.
intros h.
apply Bool.andb_true_iff in h as [h h'].
rewrite -> (Bool.andb_true_r _) in h'.
apply Zle_is_le_bool.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool in h'.
transitivity (term_value m l + - term_value m t' + term_value m t').
+ lia.
+ apply (combine_mixed_constraints _ _ _ _ _ h h').
--- discriminate.
- unfold combine.
destruct (l' =? -t) eqn:E.
-- intros h. injection h as h.
apply Term.eqb_eq in E.
subst c'' l'.
intros m.
simpl.
rewrite term_value_op.
intros h.
apply Bool.andb_true_iff in h as [h h'].
rewrite -> (Bool.andb_true_r _) in h'.
apply Zle_is_le_bool.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool in h'.
transitivity (- term_value m t + term_value m r' + term_value m t).
+ lia.
+ rewrite (Z.add_comm d).
apply (combine_mixed_constraints _ _ _ _ _ h' h).
-- destruct E. destruct (r' =? -t) eqn:E.
--- intros h. injection h as h.
apply Term.eqb_eq in E.
subst c'' r'.
intros m.
simpl.
rewrite term_value_op.
intros h.
apply Bool.andb_true_iff in h as [h h'].
rewrite -> (Bool.andb_true_r _) in h'.
apply Zle_is_le_bool.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool in h'.
transitivity (term_value m l' + - term_value m t + term_value m t).
+ lia.
+ rewrite (Z.add_comm d).
apply (combine_mixed_constraints _ _ _ _ _ h' h).
--- discriminate.
- unfold combine.
intros h. injection h as h. subst.
intros m h.
apply Bool.andb_true_iff in h as [h h'].
rewrite -> (Bool.andb_true_r _) in h'.
apply Zle_is_le_bool.
apply Zle_is_le_bool in h.
apply Zle_is_le_bool in h'.
apply (combine_bound_constraints _ _ _ _ h h').
Qed.
(* Print Z. *)
(* Print positive. *)
(* Search (nat -> Z). *)
Lemma scale_monotonous(x y: Z)(k: nat):
x <= y -> (Z.of_nat k * x <= Z.of_nat k*y).
Proof.
induction k as [| k' IHk']; intros x_le_y.
- simpl. reflexivity.
- rewrite Nat2Z.inj_succ.
repeat rewrite Z.mul_succ_l.
apply Z.add_le_mono; auto.
Qed.
Theorem normalize_constraint_impl(c: Constraint):
forall m, satisfies_single_constraint m c = true
<-> satisfies_single_constraint m (normalize_constraint c) = true.
Proof with auto.
intros m.
unfold satisfies_single_constraint.
destruct c...
simpl.
destruct (l =? r) eqn:E.
- apply eqb_eq in E; subst.
split.
-- intros h; apply Zle_is_le_bool in h.
apply Zle_is_le_bool.
rewrite Z.add_diag in h.
apply (Z.div_le_mono _ _ 2) in h; try lia.
rewrite (Z.mul_comm 2) in h.
rewrite (Z.div_mul) in h; try (assumption || discriminate).
-- intros h; apply Zle_is_le_bool in h.
apply Zle_is_le_bool.
rewrite Z.add_diag.
apply (Z.mul_le_mono_nonneg_l _ _ 2) in h; try lia.
apply Z.le_trans with (2*(d/2)); try lia.
apply Z.mul_div_le; try lia.
- simpl; apply iff_refl.
- simpl; apply iff_refl.
Qed.
Theorem flatten_impl(C: list Constraint)(c: Constraint) :
(C ==> c) -> (flatten C ==> c).
Proof.
intros C_imp_c m.
specialize (C_imp_c m).
intros h.
apply C_imp_c.
unfold flatten in h.
unfold satisfies_constraints in *.
apply forallb_forall.
rewrite forallb_forall in h.
intros x x_in_C.
apply (in_map normalize_constraint) in x_in_C.
pose proof (h _ x_in_C) as h'.
apply normalize_constraint_impl; assumption.
Qed.
Theorem flatten_implication(C T: list Constraint):
(C ==>> T) -> (C ==>> flatten T).
Proof.
intros C_imp_T m C_sat.
pose proof (C_imp_T m C_sat) as T_sat.
unfold satisfies_constraints in *.
rewrite forallb_forall in *.
unfold flatten.
intros y y_in_flatten_T.
apply in_map_iff in y_in_flatten_T as [x [x_y x_in_T]].
subst.
pose proof (T_sat x x_in_T) as x_sat.
apply normalize_constraint_impl in x_sat.
assumption.
Qed.
Lemma joined_implication_l(C T: list Constraint)(c: Constraint):
(C ==>> T) -> (joined c C ==>> T).
Proof.
intros C_imp_T m cC_sat.
apply (C_imp_T m).
unfold joined in cC_sat.
simpl in cC_sat; apply Bool.andb_true_iff in cC_sat as [c_sat filtered_sat].
unfold satisfies_constraints in *.
rewrite forallb_forall in *.
intros x x_in_C.
destruct (negb (trivial_impl c x)) eqn:E.
- apply filtered_sat.
apply filter_In.
split; try assumption.
- apply Bool.negb_false_iff in E.
apply trivial_impl_is_implication in E.
specialize (E m) as c_sat_imp_x_sat.
exact (c_sat_imp_x_sat c_sat).
Qed.
Theorem join_implication(C T: list Constraint):
(C ==>> T) -> (C ==>> join C T).
Proof.
intros C_T m C_sat.
pose proof (C_T m C_sat) as T_sat.
unfold join.
generalize dependent C.
induction T as [|t ts].
- intros __ _ C_sat. exact C_sat.
- intros C C_T C_sat.
simpl in *.
apply Bool.andb_true_iff in T_sat as [t_sat ts_sat].
specialize (IHts ts_sat).
destruct (forallb (fun c : Constraint => negb (trivial_impl c t)) C) eqn:E.
-- simpl.
apply IHts.
--- apply joined_implication_l. apply app_implication_r with t. assumption.
--- unfold satisfies_constraints.
unfold joined.
apply forallb_forall.
intros x [x_t | x_in_filtered].
---- subst; assumption.
---- apply filter_In in x_in_filtered as [x_in_C x_not_triv].
unfold satisfies_constraints in C_sat.
rewrite forallb_forall in C_sat.
apply C_sat.
assumption.
-- apply IHts; try assumption.
apply app_implication_r with t; assumption.
Qed.
Theorem new_constraints_implication(C T: list Constraint):
(C ==>> T) -> (C ==>> new_constraints T).
Proof.
intros C_T m C_sat.
pose proof (C_T m C_sat) as T_sat.
unfold new_constraints.
unfold satisfies_constraints.
apply forallb_forall.
intros x h.
apply in_flat_map in h as [x1 [x1_T h]].
apply in_flat_map in h as [x2 [x2_T x_is_combine]].
destruct (combine x1 x2) eqn:E; try (simpl; exfalso; assumption).
destruct x_is_combine as [h | []]; subst.
apply (combine_impl x1 x2 x E).
apply Bool.andb_true_iff.
unfold satisfies_constraints in T_sat.
rewrite forallb_forall in T_sat.
split; try rewrite Bool.andb_true_r; auto.
Qed.
Theorem iterate_implication(C: list Constraint):
(C ==>> iterate C).
Proof.
apply join_implication.
apply flatten_implication.
apply new_constraints_implication.
apply implication_refl.
Qed.
Fixpoint church_numeral{A: Type}(n: nat)(f: A -> A)(x: A):=
match n with
| O => x
| S m => f (church_numeral m f x)
end.
Program Definition mkadd_pp(x: nat)(y: nat)(d: Z) :=
AddConstr (Build_term 1 x) (Build_term 1 y) d.
Program Definition mkadd_pn(x: nat)(y: nat)(d: Z) :=
AddConstr (Build_term 1 x) (Build_term (-1) y) d.
Program Definition mkadd_nn(x: nat)(y: nat)(d: Z) :=
AddConstr (Build_term (-1) x) (Build_term (-1) y) d.
Program Definition mkbnd_p(x: nat)(d: Z) :=
BndConstr (Build_term 1 x) d.
Program Definition mkbnd_n(x: nat)(d: Z) :=
BndConstr (Build_term (-1) x) d.
Module ImplChecker.
Definition satisfies_constraint_disjunction (m: model) (C: list (list Constraint)): bool :=
existsb (satisfies_constraints m) C.
Record imp_checker: Type :=
{ imp_checker_fun: list (list Constraint) -> Constraint -> bool
; imp_checker_snd: forall (cs: list (list Constraint)) (c: Constraint),
imp_checker_fun cs c = true -> forall model,
satisfies_constraint_disjunction model cs = true -> satisfies_single_constraint model c = true
}.
Record conj_imp_checker: Type :=
{ conj_imp_checker_fun: list Constraint -> Constraint -> bool
; conj_imp_checker_snd: forall (cs: list Constraint) (c: Constraint),
conj_imp_checker_fun cs c = true -> (cs ==> c)
}.
Program Definition mk_imp_checker (checker: conj_imp_checker): imp_checker := {|
imp_checker_fun cs c :=
match cs with
| [] => false
| _ => forallb (fun conj => conj_imp_checker_fun checker conj c) cs
(* We have that hypothesis A or hypothesis B are true, and have to prove that
thesis C is true. We can only ensure that if A implies C and B implies C
*)
end
|}.
Next Obligation.
destruct checker as [checker checker_snd].
rename H into full_checker__cs_imp_c.
generalize dependent H0.
induction cs as [|c' cs' IHcs']; try discriminate.
simpl in full_checker__cs_imp_c.
apply Bool.andb_true_iff in full_checker__cs_imp_c as [checker__c'_imp_c checker__cs'_imp_c].
destruct cs' as [|c'' cs''] eqn:E; auto.
- simpl.
rewrite Bool.orb_false_r.
apply checker_snd.
assumption.
- intros h; apply Bool.orb_true_iff in h as [c'_sat | E_sat].
-- apply (checker_snd c' _ checker__c'_imp_c _).
assumption.
-- pose proof (IHcs' checker__cs'_imp_c) as IHcs'.
apply IHcs'.
assumption.
Qed.
Lemma implication_imp_implies(c c': Constraint)(cs: list Constraint):
In c' cs -> (c' --> c) -> (cs ==> c).
Proof.
intros c'_in_cs c'_c m cs_sat.
specialize (c'_c m).
apply c'_c.
unfold satisfies_constraints in cs_sat.
rewrite forallb_forall in cs_sat.
apply cs_sat.
assumption.
Qed.
Lemma implies_imply(c c': Constraint): (c --> c') <-> ([c] ==> c').
Proof.
split; intros c_c'.
- intros m c_sat.
specialize (c_c' m).
simpl in c_sat.
apply c_c'.
rewrite Bool.andb_true_r in c_sat.
assumption.
- intros m is_sat c_sat.
specialize (c_c' m).
unfold is_sat.
apply c_c'.
simpl.
rewrite Bool.andb_true_r.
assumption.
Qed.
Lemma imply_implication(cs: list Constraint)(c: Constraint): (cs ==> c) <-> (cs ==>> [c]).
Proof.
split; intros cs_c.
- intros m cs_sat.
specialize (cs_c m).
simpl.
rewrite Bool.andb_true_r.
apply cs_c.
assumption.
- intros m cs_sat.
specialize (cs_c m).
simpl in cs_c.
rewrite Bool.andb_true_r in cs_c.
apply cs_c.
assumption.
Qed.
(* Lemma h''{A: Type}{P : A -> Prop}(f: A -> A): *)
(* (forall x, P x -> P (f x)) -> *)
(* forall n x, P x -> P (church_numeral n f x). *)
(* Proof. *)
(* intros cs_imp_f_cs. *)
(* intros n x h_Px. *)
(* induction n as [|n' IHn']; try exact h_Px. *)
(* simpl. *)
(* apply cs_imp_f_cs. *)
(* assumption. *)
(* Qed. *)
Lemma church_numeral_implication
(n: nat)(f: list Constraint -> list Constraint)(cs: list Constraint):
(forall C, C ==>> f C)
-> cs ==>> church_numeral n f cs.
Proof.
intros cs_imp_f_cs.
induction n as [|n' IHn']; try apply implication_refl.
apply implication_trans with (church_numeral n' f cs); try assumption.
apply cs_imp_f_cs.
Qed.
Lemma imply_refl(cs: list Constraint)(c: Constraint)(c_in_cs: In c cs): cs ==> c.
Proof.
intros m cs_sat.
unfold satisfies_constraints in cs_sat.
rewrite forallb_forall in cs_sat.
apply cs_sat.
assumption.
Qed.
Open Scope constr_scope.
Program Definition conj_trans_closure_checker(n: nat) : conj_imp_checker := {|
conj_imp_checker_fun cs c :=
let trans_closure := church_numeral n iterate cs
in existsb (fun c' => trivial_impl c' c) trans_closure
|}.
Next Obligation.
(*
Sea c' en trans_closure tal que c' --> c.
Entonces tenemos: cs ==>> iterate cs
iterate cs ==> c'
c' --> c
Por transitividad, cs ==> c
*)
apply existsb_exists in H as [c' [c'_in_trans c'_imp_c]].
apply trivial_impl_is_implication in c'_imp_c.
apply implies_imply in c'_imp_c.
apply imply_implication in c'_imp_c.
apply imply_implication.
assert(tcs_imp_c': church_numeral n iterate cs ==>> [c']). {
apply imply_implication.