This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Proofs.v
843 lines (795 loc) · 31 KB
/
Proofs.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
(**
This file verifies some of the logic of Interval.hs from
bisect-binary. <https://github.com/nomeata/bisect-binary/>
*)
Require Import Intervals.
Require Import GHC.Base.
Require Import GHC.DeferredFix.
Require Import Coq.Sets.Ensembles.
Require Import Coq.Sets.Powerset_facts.
Require Import Ensemble_facts.
Import ListNotations.
Require Import Omega.
Definition goodI (i : Interval) : Prop :=
match i with I f t => (f < t)%Z end.
Fixpoint goodLIs (is : list Interval) (lb : Z) : Prop :=
match is with
| [] => True
| (I f t :: is) => (lb <= f)%Z /\ (f < t)%Z /\ goodLIs is t
end.
Definition good is := match is with
ival is => exists n, goodLIs is n end.
Definition range (f t : Z) : Ensemble Z :=
(fun z => (f <= z)%Z /\ (z < t)%Z).
Definition semI (i : Interval) : Ensemble Z :=
match i with I f t => range f t end.
Fixpoint semLIs (is : list Interval) : Ensemble Z :=
match is with
| [] => Empty_set Z
| (i :: is) => Union Z (semI i) (semLIs is)
end.
Definition sem is := match is with
ival is => semLIs is end.
(* utils *)
Lemma range_empty (z : Z) :
(z <= 0)%Z -> range 0 z = Empty_set Z.
Proof.
intro H. apply Extensionality_Ensembles. split.
* intros z' H2.
unfold range, In in *.
contradict H2.
intuition.
* apply Included_Empty.
Qed.
Lemma goodLIs_mono : forall is lb lb', (lb' <= lb)%Z -> goodLIs is lb -> goodLIs is lb'.
Proof.
intros.
induction is.
* auto.
* destruct a. simpl in *. intuition.
Qed.
Lemma good_sem_lb:
forall is lb x,
goodLIs is lb -> In Z (semLIs is) x -> (lb <= x)%Z.
Proof.
intros.
unfold In in *.
induction is.
* simpl in *. exfalso. intuition.
* destruct a as [f t]; simpl in *; intuition.
destruct H0; unfold In, range in *; intuition.
apply IHis.
refine (goodLIs_mono _ _ _ _ H3). intuition.
auto.
Qed.
Lemma Intersection_range_range:
forall f1 t1 f2 t2,
Intersection Z (range f1 t1) (range f2 t2)
= range (Z.max f1 f2) (Z.min t1 t2).
Proof.
intros. apply Extensionality_Ensembles. split.
* intros x H1. destruct H1. unfold In, range in *.
rewrite Z.max_lub_iff.
rewrite Z.min_glb_lt_iff.
intuition.
* intros x H. constructor;
unfold In, range in *;
rewrite Z.max_lub_iff in *;
rewrite Z.min_glb_lt_iff in *;
intuition.
Qed.
Lemma Intersection_range_range_empty:
forall f1 t1 f2 t2,
(t1 <= f2)%Z \/ (t2 <= f1)%Z ->
Intersection Z (range f1 t1) (range f2 t2) = Empty_set Z.
Proof.
intros. apply Extensionality_Ensembles. split.
* intros x H1. destruct H1. unfold In, range in *.
exfalso. intuition.
* intuition.
Qed.
Lemma Included_range_range:
forall f1 t1 f2 t2,
(f2 <= f1)%Z /\ (t1 <= t2)%Z ->
Included Z (range f1 t1) (range f2 t2).
Proof.
intros.
intros x H1.
unfold In, range in *. intuition.
Qed.
Lemma Intersection_range_semLIs_empty:
forall f t is lb,
goodLIs is lb -> (t <= lb)%Z ->
Intersection Z (range f t) (semLIs is) = Empty_set Z.
Proof.
induction is; intros.
* apply Disjoint_Empty_set_r.
* destruct a as [f' t']. simpl in *.
rewrite Distributivity.
rewrite Intersection_range_range_empty.
rewrite Empty_set_zero.
apply IHis with (lb := t').
intuition.
intuition.
intuition.
Qed.
(** proofs *)
(** [nullIntervals] *)
Theorem nullIntervals_good : good nullInterval.
Proof.
exists 0%Z. constructor.
Qed.
Theorem nullIntervals_spec : sem nullInterval = Empty_set Z.
Proof. reflexivity. Qed.
(** [fullIntervals] *)
Theorem fullIntervals_good : forall z, good (fullIntervals z).
Proof.
intros.
unfold fullIntervals, mkInterval.
unfold GHC.Base.op_zl__, Ord_Integer___, op_zl____ in *.
simpl in *.
destruct (Z.ltb_spec 0 z).
* exists 0%Z. unfold goodLIs. intuition.
* exists 0%Z. unfold goodLIs. intuition.
Qed.
Theorem fullIntervals_spec (z : Z) : sem (fullIntervals z) = range 0 z.
Proof.
intros.
unfold fullIntervals, mkInterval.
unfold GHC.Base.op_zl__, Ord_Integer___, op_zl____ in *.
simpl in *.
destruct (Z.ltb_spec 0 z).
* simpl. rewrite Union_commutative. rewrite Empty_set_zero. reflexivity.
* simpl. rewrite range_empty by assumption. reflexivity.
Qed.
(** [isEmpty] *)
Lemma isEmpty_specL (is : list Interval) (lb : Z) (Hgood : goodLIs is lb) :
is = [] <-> (semLIs is = Empty_set Z).
Proof.
split; intros.
* subst. reflexivity.
* destruct is; try congruence.
destruct i.
simpl in *.
assert (In Z (range from to) from).
- unfold range. intuition.
- eapply Union_introl in H0.
rewrite H in H0.
apply Noone_in_empty in H0.
contradict H0.
Qed.
Theorem isEmpty_spec (i : Intervals) (Hgood : good i) :
isEmpty i = true <-> (sem i = Empty_set Z).
Proof.
destruct i.
simpl.
simpl in Hgood; destruct Hgood.
unfold Foldable.null, Foldable.Foldable__list, Foldable.null__, Foldable.Foldable__list_null.
rewrite <- isEmpty_specL by eassumption.
destruct l; simpl; intuition; try congruence.
Qed.
(** deferred fix *)
Axiom deferredFix2_eq: forall {a b r} `{Default r} (f : (a -> b -> r) -> (a -> b -> r)), deferredFix2 f = f (deferredFix2 f).
(** induction principle *)
Definition needs_reorder (is1 is2 : list Interval) : bool :=
match is1, is2 with
| (I f1 t1 :: _), (I f2 t2 :: _) => (t1 <? t2)%Z
| _, _ => false
end.
Definition size2 (is1 is2 : list Interval) : nat :=
(if needs_reorder is1 is2 then 1 else 0) + 2 * length is1 + 2 * length is2.
Definition my_ind {A}
(sz : A -> A -> nat)
(P : A -> A -> Prop) :
(forall a b, (forall x y, (sz x y < sz a b)%nat -> P x y) -> P a b ) ->
forall x y, P x y.
Proof.
intros.
remember (sz x y) as n.
revert x y Heqn.
apply (lt_wf_ind n).
intros. apply H. intros. apply H0 with (m := sz x0 y0); intuition.
Qed.
(** [union] *)
Lemma union_good : forall (is1 is2 : Intervals),
good is1 -> good is2 -> good (union is1 is2).
Proof.
intros.
destruct is1 as [is1], is2 as [is2].
destruct H as [lb1 H1] , H0 as [lb2 H2].
exists (Z.min lb1 lb2).
match goal with [ |- goodLIs (deferredFix2 ?f _ _) _ ] => set (u := f) end.
apply (goodLIs_mono _ _ _ (Z.le_min_l lb1 lb2)) in H1.
apply (goodLIs_mono _ _ _ (Z.le_min_r lb1 lb2)) in H2.
revert H1 H2.
generalize (Z.min lb1 lb2).
revert is1 is2.
refine (my_ind size2 _ _).
intros is1 is2 IH lb H1 H2.
rewrite deferredFix2_eq.
destruct is1 as [|i1 is1], is2 as [|i2 is2].
* simpl. trivial.
* destruct i2. simpl in *. intuition.
* destruct i1. simpl in *. intuition.
* simpl.
unfold GHC.Base.op_zl__, Ord_Integer___, op_zl____ in *.
unfold GHC.Base.op_zg__, Ord_Integer___, op_zg____ in *.
destruct (Z.ltb_spec (to i1) (to i2)); [|destruct (Z.ltb_spec (to i2) (from i1))];
destruct i1 as [f1 t1], i2 as [f2 t2].
- apply IH; try assumption.
+ unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t2 t1), (Z.ltb_spec t1 t2); simpl; omega.
- simpl in *. repeat rewrite Z.min_le_iff.
intuition.
apply IH.
+ destruct is2 as [|[f2' t2'] is].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2'), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl. intuition.
+ simpl. intuition.
- simpl in *.
apply IH.
+ destruct is2 as [|[f2' t2'] is].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2'), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl. intuition.
rewrite Z.min_glb_iff in *. intuition.
rewrite Z.min_lt_iff in *. intuition.
+ simpl. intuition.
refine (goodLIs_mono _ _ _ _ H7). intuition.
Qed.
Lemma union_spec : forall (is1 is2 : Intervals),
good is1 -> good is2 -> sem (union is1 is2) = Union Z (sem is1) (sem is2).
Proof.
intros.
destruct is1 as [is1], is2 as [is2].
destruct H as [lb1 H1] , H0 as [lb2 H2].
unfold union.
match goal with [ |- context [deferredFix2 ?f _ _] ] => set (u := f) end.
apply (goodLIs_mono _ _ _ (Z.le_min_l lb1 lb2)) in H1.
apply (goodLIs_mono _ _ _ (Z.le_min_r lb1 lb2)) in H2.
simpl.
revert H1 H2.
generalize (Z.min lb1 lb2).
revert is1 is2.
refine (my_ind size2 _ _).
intros is1 is2 IH lb H1 H2.
rewrite deferredFix2_eq.
destruct is1 as [|i1 is1], is2 as [|i2 is2].
* simpl. rewrite Empty_set_zero. reflexivity.
* destruct i2. simpl in *. intuition.
* destruct i1. simpl in *.
generalize (Union Z (range from to) (semLIs is1)). intro. (* ugh *)
rewrite Union_commutative at 1. rewrite Empty_set_zero. intuition.
* simpl.
unfold GHC.Base.op_zl__, Ord_Integer___, op_zl____ in *.
unfold GHC.Base.op_zg__, Ord_Integer___, op_zg____ in *.
destruct (Z.ltb_spec (to i1) (to i2)); [|destruct (Z.ltb_spec (to i2) (from i1))];
destruct i1 as [f1 t1], i2 as [f2 t2].
- rewrite IH with (z:=lb).
+ intuition.
+ unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t2 t1), (Z.ltb_spec t1 t2); simpl; omega.
+ assumption.
+ assumption.
- simpl in *. repeat rewrite Z.min_le_iff.
intuition.
rewrite IH with (z:=t2).
+ simpl. intuition. clear dependent u.
(* reorder Union *)
repeat rewrite Union_associative.
rewrite Union_commutative.
repeat rewrite Union_associative.
do 2 f_equal.
rewrite Union_commutative.
reflexivity.
+ destruct is2 as [|[f2' t2'] is].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2'), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl in *. intuition.
+ simpl. intuition.
- simpl in *.
rewrite IH with (z:=lb).
+ simpl. clear dependent u.
rewrite union_reorder.
rewrite Union_associative.
f_equal.
(* range and min *)
apply Extensionality_Ensembles. split.
** intros z' H3.
unfold range, In in *.
rewrite Z.min_le_iff in *.
intuition.
left. unfold In. intuition.
destruct (Z.ltb_spec z' t2).
right. unfold In. intuition.
left. unfold In. intuition.
** intros z' H3.
apply Union_inv in H3.
unfold range, In in *.
rewrite Z.min_le_iff in *.
intuition.
+ destruct is2 as [|[f2' t2'] is].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2'), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl. intuition.
rewrite Z.min_glb_iff in *. intuition.
rewrite Z.min_lt_iff in *. intuition.
+ simpl. intuition.
refine (goodLIs_mono _ _ _ _ H7). intuition.
Qed.
(** [intersect] *)
Lemma intersect_good : forall (is1 is2 : Intervals),
good is1 -> good is2 -> good (intersect is1 is2).
Proof.
intros.
destruct is1 as [is1], is2 as [is2].
destruct H as [lb1 H1] , H0 as [lb2 H2].
exists (Z.min lb1 lb2).
match goal with [ |- goodLIs (deferredFix2 ?f _ _) _ ] => set (u := f) end.
apply (goodLIs_mono _ _ _ (Z.le_min_l lb1 lb2)) in H1.
apply (goodLIs_mono _ _ _ (Z.le_min_r lb1 lb2)) in H2.
revert H1 H2.
generalize (Z.min lb1 lb2).
revert is1 is2.
refine (my_ind size2 _ _).
intros is1 is2 IH lb H1 H2.
rewrite deferredFix2_eq.
destruct is1 as [|i1 is1], is2 as [|i2 is2].
* simpl. trivial.
* destruct i2. simpl in *. intuition.
* destruct i1. simpl in *. intuition.
* simpl.
unfold GHC.Base.op_zl__, Ord_Integer___, op_zl____ in *.
unfold GHC.Base.op_zgze__, Ord_Integer___, op_zgze____ in *.
unfold GHC.Base.op_zeze__, Eq_Integer___, op_zeze____ in *.
destruct i1 as [f1 t1], i2 as [f2 t2]; simpl in *.
destruct (Z.ltb_spec t1 t2);
[|destruct (Z.leb_spec t2 f1)];
[| |destruct (Z.eqb_spec t1 t2)].
- apply IH; clear dependent u.
+ unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t2 t1), (Z.ltb_spec t1 t2); simpl; omega.
+ assumption.
+ assumption.
- simpl in *. repeat rewrite Z.min_le_iff.
intuition.
apply IH; clear dependent u.
+ destruct is2 as [|[f2' t2'] is].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2'), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl. intuition.
+ simpl. refine (goodLIs_mono _ _ _ _ H7). intuition.
- simpl in *. repeat rewrite Z.max_le_iff. repeat rewrite Z.max_lub_lt_iff.
intuition.
apply IH; clear dependent u.
+ unfold size2. simpl in *.
destruct is1 as [|[f1' t1'] is1];
destruct is2 as [|[f2' t2'] is2].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2), (Z.ltb_spec t1' t2'); simpl; omega.
+ simpl. refine (goodLIs_mono _ _ _ _ H6). intuition.
+ simpl. refine (goodLIs_mono _ _ _ _ H7). intuition.
- simpl in *. repeat rewrite Z.max_le_iff. repeat rewrite Z.max_lub_lt_iff.
intuition.
apply IH; clear dependent u.
+ destruct is2 as [|[f2' t2'] is].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2'), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl. intuition.
+ simpl. intuition.
Qed.
Lemma intersection_spec : forall (is1 is2 : Intervals),
good is1 -> good is2 -> sem (intersect is1 is2) = Intersection Z (sem is1) (sem is2).
Proof.
intros.
destruct is1 as [is1], is2 as [is2].
destruct H as [lb1 H1] , H0 as [lb2 H2].
unfold intersect.
match goal with [ |- context [deferredFix2 ?f _ _] ] => set (u := f) end.
apply (goodLIs_mono _ _ _ (Z.le_min_l lb1 lb2)) in H1.
apply (goodLIs_mono _ _ _ (Z.le_min_r lb1 lb2)) in H2.
simpl.
revert H1 H2.
generalize (Z.min lb1 lb2).
revert is1 is2.
refine (my_ind size2 _ _).
intros is1 is2 IH lb H1 H2.
rewrite deferredFix2_eq.
destruct is1 as [|i1 is1], is2 as [|i2 is2].
* simpl. clear dependent u.
apply Extensionality_Ensembles. split.
- intuition.
- intros x H. destruct H. intuition.
* destruct i2. simpl in *. intuition.
* destruct i1. simpl in *. intuition.
* simpl.
unfold GHC.Base.op_zl__, Ord_Integer___, op_zl____ in *.
unfold GHC.Base.op_zgze__, Ord_Integer___, op_zgze____ in *.
unfold GHC.Base.op_zeze__, Eq_Integer___, op_zeze____ in *.
destruct i1 as [f1 t1], i2 as [f2 t2]; simpl in *.
destruct (Z.ltb_spec t1 t2);
[|destruct (Z.leb_spec t2 f1)];
[| |destruct (Z.eqb_spec t1 t2)].
- rewrite IH with (z := lb). clear dependent u.
+ simpl.
apply Intersection_commutative.
+ unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t2 t1), (Z.ltb_spec t1 t2); simpl; omega.
+ assumption.
+ assumption.
- simpl in *. repeat rewrite Z.min_le_iff.
intuition.
rewrite IH with (z := lb). clear dependent u.
+ simpl.
rewrite Distributivity.
repeat rewrite Distributivity_l.
rewrite Intersection_range_range_empty.
rewrite Empty_set_zero.
rewrite (Intersection_commutative _ (semLIs is1) (range f2 t2)).
rewrite (Intersection_range_semLIs_empty _ _ _ _ H6).
rewrite Empty_set_zero.
reflexivity.
intuition.
intuition.
+ destruct is2 as [|[f2' t2'] is].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2'), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl. intuition.
+ simpl. refine (goodLIs_mono _ _ _ _ H7). intuition.
- simpl in *. repeat rewrite Z.max_le_iff. repeat rewrite Z.max_lub_lt_iff.
intuition.
rewrite IH with (z := t2). clear dependent u.
+ simpl. subst.
rewrite Distributivity.
repeat rewrite Distributivity_l.
rewrite Intersection_range_range.
rewrite (Intersection_commutative _ (semLIs is1) (range f2 t2)).
rewrite (Intersection_range_semLIs_empty _ _ _ _ H6).
rewrite Empty_set_zero_l.
rewrite (Intersection_range_semLIs_empty _ _ _ _ H7).
rewrite Empty_set_zero.
f_equal. f_equal.
rewrite Z.min_r.
intuition. intuition. intuition. intuition.
+ unfold size2. simpl in *.
destruct is1 as [|[f1' t1'] is1];
destruct is2 as [|[f2' t2'] is2].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2), (Z.ltb_spec t1' t2'); simpl; omega.
+ simpl. refine (goodLIs_mono _ _ _ _ H6). intuition.
+ simpl. refine (goodLIs_mono _ _ _ _ H7). intuition.
- simpl in *. repeat rewrite Z.max_le_iff. repeat rewrite Z.max_lub_lt_iff.
intuition.
rewrite IH with (z := t2). clear dependent u.
+ simpl in *.
rewrite Distributivity.
repeat rewrite Distributivity_l.
rewrite Intersection_range_range.
rewrite (Intersection_commutative _ (semLIs is1) (range f2 t2)).
rewrite (Intersection_range_semLIs_empty _ _ is1 _ H6) by intuition.
rewrite Empty_set_zero_l.
do 2 f_equal.
rewrite Z.min_r. reflexivity.
intuition.
(* lets do it by hand *)
apply Extensionality_Ensembles. split.
++ intros x H8.
destruct H8. constructor.
-- apply (good_sem_lb _ _ _ H7) in H8.
unfold In, range in *. intuition.
-- assumption.
++ intros x H8.
destruct H8. constructor.
-- apply (good_sem_lb _ _ _ H7) in H8.
unfold In, range in *. intuition.
-- assumption.
+ destruct is2 as [|[f2' t2'] is].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2'), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl. intuition.
+ simpl. intuition.
Qed.
(** [subtract] *)
Lemma subtract_good : forall (is1 is2 : Intervals),
good is1 -> good is2 -> good (subtract is1 is2).
Proof.
intros.
destruct is1 as [is1], is2 as [is2].
destruct H as [lb1 H1] , H0 as [lb2 H2].
exists (Z.min lb1 lb2).
match goal with [ |- goodLIs (deferredFix2 ?f _ _) _ ] => set (u := f) end.
apply (goodLIs_mono _ _ _ (Z.le_min_l lb1 lb2)) in H1.
apply (goodLIs_mono _ _ _ (Z.le_min_r lb1 lb2)) in H2.
revert H1 H2.
generalize (Z.min lb1 lb2).
revert is1 is2.
refine (my_ind size2 _ _).
intros is1 is2 IH lb H1 H2.
rewrite deferredFix2_eq.
destruct is1 as [|i1 is1], is2 as [|i2 is2].
* simpl. auto.
* destruct i2. simpl in *. intuition.
* destruct i1. simpl in *. intuition.
* simpl.
unfold GHC.Base.op_zl__, Ord_Integer___, op_zl____ in *.
unfold GHC.Base.op_zgze__, Ord_Integer___, op_zgze____ in *.
unfold GHC.Base.op_zlze__, Ord_Integer___, op_zlze____ in *.
destruct i1 as [f1 t1], i2 as [f2 t2]; simpl in *.
destruct (Z.leb_spec t1 f2);
[| destruct (Z.leb_spec t2 f1)];
[| | destruct (Z.leb_spec f2 f1)];
[| | destruct (Z.leb_spec t1 t2) | destruct (Z.leb_spec t1 t2)].
- simpl. intuition. apply IH with (z := t1); clear dependent u.
+ unfold size2. simpl.
destruct is1 as [|[f1' t1'] is1].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2), (Z.ltb_spec t1' t2); simpl; omega.
+ assumption.
+ simpl. intuition.
- intuition. apply IH with (z := lb); clear dependent u.
+ unfold size2. simpl.
destruct is2 as [|[f2' t2'] is2].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2), (Z.ltb_spec t1 t2'); simpl; omega.
+ simpl. intuition.
+ simpl. refine (goodLIs_mono _ _ _ _ H7). intuition.
- intuition. apply IH with (z := lb); clear dependent u.
+ unfold size2. simpl.
destruct is1 as [|[f1' t1'] is1].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1' t2), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl. refine (goodLIs_mono _ _ _ _ H8). intuition.
+ simpl. intuition.
- simpl. intuition. apply IH with (z := lb); clear dependent u.
+ unfold size2. simpl.
destruct is2 as [|[f2' t2'] is2].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2), (Z.ltb_spec t1 t2'); simpl; omega.
+ simpl. intuition.
+ simpl. refine (goodLIs_mono _ _ _ _ H9). intuition.
- simpl. intuition. apply IH with (z := f2); clear dependent u.
+ unfold size2. simpl.
destruct is1 as [|[f1' t1'] is1].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1' t2), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl. refine (goodLIs_mono _ _ _ _ H8). intuition.
+ simpl. intuition.
- simpl. intuition. apply IH with (z := f2); clear dependent u.
+ unfold size2. simpl.
destruct is2 as [|[f2' t2'] is2].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2'), (Z.ltb_spec t1 t2) ; simpl; omega.
+ simpl. intuition.
+ simpl. refine (goodLIs_mono _ _ _ _ H9). intuition.
Qed.
Lemma subtract_spec : forall (is1 is2 : Intervals),
good is1 -> good is2 -> sem (subtract is1 is2) = Setminus Z (sem is1) (sem is2).
Proof.
intros.
destruct is1 as [is1], is2 as [is2].
destruct H as [lb1 H1] , H0 as [lb2 H2].
unfold subtract.
match goal with [ |- context [deferredFix2 ?f _ _] ] => set (u := f) end.
apply (goodLIs_mono _ _ _ (Z.le_min_l lb1 lb2)) in H1.
apply (goodLIs_mono _ _ _ (Z.le_min_r lb1 lb2)) in H2.
revert H1 H2.
generalize (Z.min lb1 lb2).
clear lb1 lb2.
revert is1 is2.
refine (my_ind size2 _ _).
intros is1 is2 IH lb H1 H2.
rewrite deferredFix2_eq.
destruct is1 as [|i1 is1], is2 as [|i2 is2].
* simpl. clear dependent u.
rewrite Seminus_Empty_r.
reflexivity.
* destruct i2. simpl in *. clear dependent u.
rewrite Seminus_Empty_l.
reflexivity.
* destruct i1. simpl in *. clear dependent u.
rewrite Seminus_Empty_r.
reflexivity.
* simpl.
unfold GHC.Base.op_zl__, Ord_Integer___, op_zl____ in *.
unfold GHC.Base.op_zgze__, Ord_Integer___, op_zgze____ in *.
unfold GHC.Base.op_zlze__, Ord_Integer___, op_zlze____ in *.
destruct i1 as [f1 t1], i2 as [f2 t2]; simpl in *.
destruct (Z.leb_spec t1 f2);
[| destruct (Z.leb_spec t2 f1)];
[| | destruct (Z.leb_spec f2 f1)];
[| | destruct (Z.leb_spec t1 t2) | destruct (Z.leb_spec t1 t2)].
- simpl. intuition. rewrite IH with (z := t1); clear dependent u.
+ simpl.
rewrite Setminus_Union.
f_equal.
symmetry.
apply Setminus_noop.
rewrite Distributivity.
rewrite Intersection_range_range_empty by intuition.
rewrite (Intersection_range_semLIs_empty _ _ _ _ H6) by intuition.
intuition.
+ unfold size2. simpl.
destruct is1 as [|[f1' t1'] is1].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2), (Z.ltb_spec t1' t2); simpl; omega.
+ assumption.
+ simpl. intuition.
- intuition. rewrite IH with (z := Z.min f1 f2); clear dependent u.
+ simpl.
rewrite Setminus_Union_r.
f_equal.
symmetry.
apply Setminus_noop.
rewrite Distributivity_l.
rewrite Intersection_range_range_empty by intuition.
rewrite Intersection_commutative.
rewrite (Intersection_range_semLIs_empty _ _ _ _ H6) by intuition.
intuition.
+ unfold size2. simpl.
destruct is2 as [|[f2' t2'] is2].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2), (Z.ltb_spec t1 t2'); simpl; omega.
+ simpl. intuition.
+ simpl. refine (goodLIs_mono _ _ _ _ H7). rewrite Z.min_le_iff. intuition.
- intuition. simpl. rewrite IH with (z := Z.min f1 f2); clear dependent u.
+ simpl.
rewrite Setminus_Union.
rewrite (Setminus_empty _ (range f1 t1) (Union Z (range f2 t2) (semLIs is2))).
rewrite Empty_set_zero. reflexivity.
apply Included_Union_l.
apply Included_range_range.
intuition.
+ unfold size2. simpl.
destruct is1 as [|[f1' t1'] is1].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1' t2), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl. refine (goodLIs_mono _ _ _ _ H8). rewrite Z.min_le_iff. intuition.
+ simpl. intuition.
- simpl. intuition. rewrite IH with (z := Z.min f1 f2); clear dependent u.
+ simpl.
repeat rewrite Setminus_Union.
f_equal.
-- rewrite Setminus_Union_r.
f_equal.
(* lets do it by hand *)
apply Extensionality_Ensembles. split.
++ unfold Included, Setminus, In, range.
intuition.
++ unfold Included, Setminus, In, range.
intuition.
-- rewrite Setminus_Union_r.
f_equal.
symmetry.
apply Setminus_noop.
rewrite Intersection_commutative.
apply (Intersection_range_semLIs_empty _ _ _ _ H8).
intuition.
+ unfold size2. simpl.
destruct is2 as [|[f2' t2'] is2].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2), (Z.ltb_spec t1 t2'); simpl; omega.
+ simpl. rewrite Z.min_le_iff. intuition.
+ simpl. refine (goodLIs_mono _ _ _ _ H9). rewrite Z.min_le_iff. intuition.
- simpl. intuition. rewrite IH with (z := f2); clear dependent u.
+ simpl.
rewrite Setminus_Union.
f_equal.
symmetry.
rewrite Setminus_Union_r.
rewrite (Setminus_noop _ (Setminus Z (range f1 t1) (range f2 t2)) (semLIs is2)).
-- (* lets do it by hand *)
apply Extensionality_Ensembles. split.
++ unfold Included, Setminus, In, range.
intuition.
++ unfold Included, Setminus, In, range.
intuition.
-- assert (Included Z (Setminus Z (range f1 t1) (range f2 t2)) (range f1 t2))
by (unfold Included, Setminus, In, range; intuition).
apply Extensionality_Ensembles; split; try intuition.
apply (Intersection_mono_trans_l _ _ _ _ _ H7).
rewrite (Intersection_range_semLIs_empty _ _ _ _ H9) by intuition.
intuition.
+ unfold size2. simpl.
destruct is1 as [|[f1' t1'] is1].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1' t2), (Z.ltb_spec t1 t2); simpl; omega.
+ simpl. refine (goodLIs_mono _ _ _ _ H8). intuition.
+ simpl. intuition.
- simpl. intuition. rewrite IH with (z := f2); clear dependent u.
+ simpl.
symmetry.
repeat rewrite Setminus_Union.
rewrite <- Union_associative.
f_equal.
-- rewrite Setminus_Union_r.
replace (range f1 f2) with (Setminus Z (range f1 f2) (semLIs is2)).
rewrite <- Setminus_Union.
f_equal.
++ (* lets do it by hand *)
apply Extensionality_Ensembles. split.
** intros x H10. inversion H10.
destruct (Z.ltb_spec x f2).
--- left. unfold In, range in *; intuition.
--- right. unfold In, range in *; intuition.
** intros X H10. inversion H10; subst.
--- unfold Setminus, In, range in *; intuition.
--- unfold Setminus, In, range in *; intuition.
++ apply Setminus_noop.
apply (Intersection_range_semLIs_empty _ _ _ _ H9).
intuition.
-- rewrite Setminus_Union_r.
f_equal.
apply Setminus_noop.
rewrite Intersection_commutative.
apply (Intersection_range_semLIs_empty _ _ _ _ H8).
intuition.
+ unfold size2. simpl.
destruct is2 as [|[f2' t2'] is2].
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2); simpl; omega.
-- unfold size2. simpl in *. repeat rewrite Z.ltb_irrefl.
destruct (Z.ltb_spec t1 t2'), (Z.ltb_spec t1 t2) ; simpl; omega.
+ simpl. intuition.
+ simpl. refine (goodLIs_mono _ _ _ _ H9). intuition.
Qed.
(** subsetof *)
Lemma subSetOf_spec:
forall s1 s2, good s1 -> good s2 -> subSetOf s1 s2 = true <-> Included Z (sem s1) (sem s2).
Proof.
intros.
unfold subSetOf.
rewrite isEmpty_spec by (apply subtract_good; assumption).
rewrite subtract_spec by assumption.
apply Setminus_empty_classical.
Qed.