This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
MSetIntervals.v
2609 lines (2303 loc) · 72.4 KB
/
MSetIntervals.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
(*
* Efficiently Executable Sets Library
* Author: FireEye, Inc. - Formal Methods Team <[email protected]>
*
* Copyright (C) 2016 FireEye Technologie Deutschland GmbH
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* If not, see <http://www.gnu.org/licenses/>.
*)
(** * Weak sets implemented by interval lists
This file contains an implementation of the weak set interface
[WSetsOn] which uses internally intervals of Z. This allows some
large sets, which naturally map to intervals of integers to be
represented very efficiently.
Internally intervals of Z are used. However, via an encoding
and decoding layer, other types of elements can be handled as
well. There are instantiations for Z, N and nat currently.
More can be easily added.
*)
Require Import MSetInterface OrdersFacts OrdersLists.
Require Import BinNat.
Require Import ssreflect.
Require Import NArith.
Require Import ZArith.
Require Import NOrder.
Require Import DecidableTypeEx.
Module Import NOP := NOrderProp N.
Open Scope Z_scope.
(** ** Auxiliary stuff *)
(** Simple auxiliary lemmata *)
Lemma Z_le_add_r : forall (z : Z) (n : N),
z <= z + Z.of_N n.
Proof.
intros z n.
suff : (z + 0 <= z + Z.of_N n). {
rewrite Z.add_0_r //.
}
apply Zplus_le_compat_l.
apply N2Z.is_nonneg.
Qed.
Lemma add_add_sub_eq : forall (x y : Z), (x + (y - x) = y).
Proof.
intros x y.
rewrite Z.add_sub_assoc => //.
rewrite Z.add_sub_swap Z.sub_diag Z.add_0_l //.
Qed.
Lemma NoDupA_map {A B} : forall (eqA : A -> A -> Prop) (eqB : B -> B -> Prop) (f : A -> B) l,
NoDupA eqA l ->
(forall x1 x2, List.In x1 l -> List.In x2 l ->
eqB (f x1) (f x2) -> eqA x1 x2) ->
NoDupA eqB (map f l).
Proof.
intros eqA eqB f.
induction l as [| x xs IH]. {
move => _ _; rewrite /=.
apply NoDupA_nil.
} {
move => H_pre H_eqA_impl.
have [H_nin_x H_no_dup_xs] : ~ InA eqA x xs /\ NoDupA eqA xs. {
by inversion_clear H_pre.
}
simpl.
apply NoDupA_cons; last first. {
apply IH => //.
intros x1 x2 H_in_x1 H_in_x2 H_eqB.
apply H_eqA_impl => //=; by right.
}
move => H_in_map; apply H_nin_x.
move : H_in_map.
rewrite !InA_alt => [[y] [H_eqB_y]].
rewrite in_map_iff => [[y'] [H_y_eq] H_y'_in].
subst.
exists y'.
split => //.
apply H_eqA_impl => //. {
by simpl; left.
} {
by simpl; right.
}
}
Qed.
(** *** rev_map
rev_map is used for efficiency. *)
Fixpoint rev_map_aux {A B} (f : A -> B) (acc : list B) (l : list A) :=
match l with
| nil => acc
| x :: xs => rev_map_aux f ((f x)::acc) xs
end.
Definition rev_map {A B} (f : A -> B) (l : list A) : list B := rev_map_aux f nil l.
(** Lemmata about rev_map *)
Lemma rev_map_aux_alt_def {A B} : forall (f : A -> B) l acc,
rev_map_aux f acc l = List.rev_append (List.map f l) acc.
Proof.
intro f.
induction l as [| x xs IH]. {
intros acc.
by simpl.
} {
intros acc.
rewrite /= IH //.
}
Qed.
Lemma rev_map_alt_def {A B} : forall (f : A -> B) l,
rev_map f l = List.rev (List.map f l).
Proof.
intros f l.
rewrite /rev_map rev_map_aux_alt_def -rev_alt //.
Qed.
(** ** Encoding Elements *)
(** We want to encode not only elements of type Z, but other types
as well. In order to do so, an encoding / decoding layer is used.
This layer is represented by module type [ElementEncode]. It
provides encode and decode function. *)
Module Type ElementEncode.
Declare Module E : DecidableType.
Parameter encode : E.t -> Z.
Parameter decode : Z -> E.t.
(** Decoding is the inverse of encoding. Notice that
the reverse is not demanded. This means that
we do need to provide for all integers [z] an
element [e] with [encode v = z]. *)
Axiom decode_encode_ok: forall (e : E.t),
decode (encode e) = e.
(** Encoding is compatible with the equality of elements. *)
Axiom encode_eq : forall (e1 e2 : E.t),
(Z.eq (encode e1) (encode e2)) <-> E.eq e1 e2.
End ElementEncode.
(** ** Set Operations
We represent sets of Z via lists of intervals. The intervals are all
in increasing order and non-overlapping. Moreover, we require the most compact
representation, i.e. no two intervals can be merged. For example
[0-2, 4-4, 6-8] is a valid interval list for the set {0;1;2;4;6;7;8}
In contrast
[4-4, 0-2, 6-8] is a invalid because the intervals are not ordered andb
[0-2, 4-5, 6-8] is a invalid because it is not compact ([0-2, 4--8] is valid).
Intervals we represent by tuples [(Z, N)]. The tuple [(z, c)] represents the
interval [z-(z+c)].
We apply the encode function before adding an element to such interval sets and
the decode function when checking it. This allows for sets with other element types
than Z.
*)
Module Ops (Enc : ElementEncode) <: WOps Enc.E.
Definition elt := Enc.E.t.
Definition t := list (Z * N).
(** The empty list is trivial to define and check for. *)
Definition empty : t := nil.
Definition is_empty (l : t) := match l with nil => true | _ => false end.
(** Defining the list of elements, is much more tricky, especially,
if it needs to be executable. *)
Lemma acc_pred : forall n p, n = Npos p -> Acc N.lt n -> Acc N.lt (N.pred n).
Proof.
intros n p H0 H1.
apply H1.
rewrite H0.
apply N.lt_pred_l.
discriminate.
Defined.
Fixpoint elementsZ_aux'' (acc : list Z) (x : Z) (c : N) (H : Acc N.lt c) { struct H } : list Z :=
match c as c0 return c = c0 -> list Z with
| N0 => fun _ => acc
| c => fun Heq => elementsZ_aux'' (x::acc) (Z.succ x) (N.pred c) (acc_pred _ _ Heq H)
end (refl_equal _).
Extraction Inline elementsZ_aux''.
Definition elementsZ_aux' acc x c := elementsZ_aux'' acc x c (lt_wf_0 _).
Fixpoint elementsZ_aux acc (s : t) : list Z :=
match s with
| nil => acc
| (x, c) :: s' =>
elementsZ_aux (elementsZ_aux' acc x c) s'
end.
Definition elementsZ (s : t) : list Z :=
elementsZ_aux nil s.
Definition elements (s : t) : list elt :=
rev_map Enc.decode (elementsZ s).
(** membership is easily defined *)
Fixpoint memZ (x : Z) (s : t) :=
match s with
| nil => false
| (y, c) :: l =>
if (Z.ltb x y) then false else
if (Z.ltb x (y+Z.of_N c)) then true else
memZ x l
end.
Definition mem (x : elt) (s : t) := memZ (Enc.encode x) s.
(** adding an element needs to be defined carefully again in order to
generate efficient code *)
Fixpoint addZ_aux (acc : list (Z * N)) (x : Z) (s : t) :=
match s with
| nil => List.rev' ((x, (1%N))::acc)
| (y, c) :: l =>
match (Z.compare (Z.succ x) y) with
| Lt => List.rev_append ((x, (1%N))::acc) s
| Eq => List.rev_append ((x, N.succ c)::acc) l
| Gt => match (Z.compare x (y+Z.of_N c)) with
| Lt => List.rev_append acc s
| Gt => addZ_aux ((y,c) :: acc) x l
| Eq => match l with
| nil => List.rev' ((y, N.succ c)::acc)
| (z, c') :: l' => if (Z.eqb z (Z.succ x)) then
List.rev_append ((y,N.succ (c+c')) :: acc) l'
else
List.rev_append ((y,N.succ c) :: acc) l
end
end
end
end.
Definition addZ x s := addZ_aux nil x s.
Definition add x s := addZ (Enc.encode x) s.
(** [add_list] simple extension to add many elements, which then allows to
define from_elements. *)
Definition add_list (l : list elt) (s : t) : t :=
List.fold_left (fun s x => add x s) l s.
Definition from_elements (l : list elt) : t := add_list l empty.
(** [singleton] is trivial to define *)
Definition singleton (x : elt) : t := (Enc.encode x, 1%N) :: nil.
Lemma singleton_alt_def : forall x, singleton x = add x empty.
Proof. by []. Qed.
(** removing needs to be done with code extraction in mind again. *)
Definition removeZ_aux_insert_guarded (x : Z) (c : N) s :=
if (N.eqb c 0) then s else (x, c) :: s.
Fixpoint removeZ_aux (acc : list (Z * N)) (x : Z) (s : t) : t :=
match s with
| nil => List.rev' acc
| (y, c) :: l =>
if (Z.ltb x y) then List.rev_append acc s else
if (Z.ltb x (y+Z.of_N c)) then (
List.rev_append (removeZ_aux_insert_guarded (Z.succ x)
(Z.to_N ((y+Z.of_N c)- (Z.succ x)))
(removeZ_aux_insert_guarded y (Z.to_N (x-y)) acc)) l
) else removeZ_aux ((y,c)::acc) x l
end.
Definition removeZ (x : Z) (s : t) : t := removeZ_aux nil x s.
Definition remove (x : elt) (s : t) : t := removeZ (Enc.encode x) s.
Definition remove_list (l : list elt) (s : t) : t :=
List.fold_left (fun s x => remove x s) l s.
(** all other operations are defined trivially (if not always efficiently)
in terms of already defined ones. In the future it might be worth implementing
some of them more efficiently. *)
Definition union (s1 s2 : t) :=
add_list (elements s1) s2.
Definition filter (f : elt -> bool) (s : t) : t :=
from_elements (List.filter f (elements s)).
Definition inter (s1 s2 : t) : t :=
filter (fun x => mem x s2) s1.
Definition diff (s1 s2 : t) : t :=
remove_list (elements s2) s1.
Definition subset s s' :=
List.forallb (fun x => mem x s') (elements s).
Fixpoint equal (s s' : t) : bool := match s, s' with
| nil, nil => true
| ((x,cx)::xs), ((y,cy)::ys) => andb (Z.eqb x y) (andb (N.eqb cx cy) (equal xs ys))
| _, _ => false
end.
Definition fold {B : Type} (f : elt -> B -> B) (s : t) (i : B) : B :=
List.fold_left (flip f) (elements s) i.
Definition for_all (f : elt -> bool) (s : t) : bool :=
List.forallb f (elements s).
Definition exists_ (f : elt -> bool) (s : t) : bool :=
List.existsb f (elements s).
Definition partition (f : elt -> bool) (s : t) : t * t :=
(filter f s, filter (fun x => negb (f x)) s).
Fixpoint cardinalN c (s : t) : N := match s with
| nil => c
| (_,cx)::xs => cardinalN (c + cx)%N xs
end.
Definition cardinal (s : t) : nat := N.to_nat (cardinalN (0%N) s).
Definition chooseZ (s : t) : option Z :=
match List.rev' (elementsZ s) with
| nil => None
| x :: _ => Some x
end.
Definition choose (s : t) : option elt :=
match elements s with
| nil => None
| e :: _ => Some e
end.
End Ops.
(** ** Raw Module
Following the idea of [MSetInterface.RawSets], we first
define a module [Raw] proves all the required properties with
respect to an explicitly provided invariant. In a next step, this
invariant is then moved into the set type. This allows to instantiate
the [WSetsOn] interface. *)
Module Raw (Enc : ElementEncode).
Include (Ops Enc).
(** *** Defining invariant [IsOk] *)
Definition inf (x:Z) (l: t) :=
match l with
| nil => true
| (y,_)::_ => Z.ltb x y
end.
Fixpoint isok (l : t) :=
match l with
| nil => true
| (x, c) ::l => inf (x+(Z.of_N c)) l && negb (N.eqb c 0) && isok l
end.
Definition is_encoded_elems_list (l : list Z) : Prop :=
(forall x, List.In x l -> exists e, Enc.encode e = x).
Definition IsOk s := (isok s = true /\ is_encoded_elems_list (elementsZ s)).
(** *** Defining notations *)
Section ForNotations.
Class Ok (s:t) : Prop := ok : IsOk s.
Hint Resolve @ok.
Hint Unfold Ok.
Instance IsOk_Ok s `(Hs : IsOk s) : Ok s := { ok := Hs }.
Definition In x s := (SetoidList.InA Enc.E.eq x (elements s)).
Definition InZ x s := (List.In x (elementsZ s)).
Definition Equal s s' := forall a : elt, In a s <-> In a s'.
Definition Subset s s' := forall a : elt, In a s -> In a s'.
Definition Empty s := forall a : elt, ~ In a s.
Definition For_all (P : elt -> Prop) s := forall x, In x s -> P x.
Definition Exists (P : elt -> Prop) (s : t) := exists x, In x s /\ P x.
End ForNotations.
(** *** elements list properties
The functions [elementsZ], [elementsZ_single],
[elements] and [elements_single] are crucial and used
everywhere. Therefore, we first establish a few properties of
these important functions. *)
Lemma elementsZ_nil : (elementsZ (nil : t) = nil).
Proof. done. Qed.
Lemma elements_nil : (elements (nil : t) = nil).
Proof. done. Qed.
Definition elementsZ_single (x:Z) (c:N) :=
List.rev' (N.peano_rec (fun _ => list Z)
nil (fun n ls => (x+Z.of_N n)%Z :: ls) c).
Definition elements_single x c :=
List.map Enc.decode (elementsZ_single x c).
Lemma elementsZ_single_base : forall x,
elementsZ_single x (0%N) = nil.
Proof. done. Qed.
Lemma elementsZ_single_succ : forall x c,
elementsZ_single x (N.succ c) =
elementsZ_single x c ++ (x+Z.of_N c)::nil.
Proof.
intros x c.
rewrite /elementsZ_single N.peano_rec_succ /rev' -!rev_alt //.
Qed.
Lemma elementsZ_single_add : forall x c2 c1,
elementsZ_single x (c1 + c2)%N =
elementsZ_single x c1 ++ elementsZ_single (x+Z.of_N c1) c2.
Proof.
intros x.
induction c2 as [| c2' IH] using N.peano_ind. {
move => c1.
rewrite elementsZ_single_base /= app_nil_r N.add_0_r //.
} {
move => c1.
rewrite N.add_succ_r !elementsZ_single_succ IH app_assoc N2Z.inj_add Z.add_assoc //.
}
Qed.
Lemma elementsZ_single_succ_front : forall x c,
elementsZ_single x (N.succ c) =
x :: elementsZ_single (Z.succ x) c.
Proof.
intros x c.
rewrite -N.add_1_l elementsZ_single_add.
rewrite N.one_succ elementsZ_single_succ elementsZ_single_base /= Z.add_0_r.
by rewrite Z.add_1_r.
Qed.
Lemma In_elementsZ_single : forall c y x,
List.In y (elementsZ_single x c) <->
(x <= y) /\ (y < (x+Z.of_N c)).
Proof.
induction c as [| c' IH] using N.peano_ind. {
intros y x.
rewrite elementsZ_single_base Z.add_0_r /=.
split; first done.
move => [H_x_le H_y_lt].
have := Z.le_lt_trans _ _ _ H_x_le H_y_lt.
apply Z.lt_irrefl.
} {
intros y x.
rewrite elementsZ_single_succ in_app_iff IH /= N2Z.inj_succ Z.add_succ_r Z.lt_succ_r.
split. {
move => []. {
move => [H_x_le H_y_le].
split; first done.
by apply Z.lt_le_incl.
}
move => [] // <-.
split. {
apply Z_le_add_r.
} {
by apply Z.le_refl.
}
} {
move => [H_x_le] H_y_lt.
apply Z.lt_eq_cases in H_y_lt as [H_y_lt | <-]. {
by left.
} {
by right; left.
}
}
}
Qed.
Lemma In_elementsZ_single1 : forall y x,
List.In y (elementsZ_single x (1%N)) <->
(x = y).
Proof.
intros y x.
rewrite In_elementsZ_single /= Z.add_1_r Z.lt_succ_r.
split. {
move => [? ?]. by apply Z.le_antisymm.
} {
move => ->.
split; apply Z.le_refl.
}
Qed.
Lemma length_elementsZ_single : forall cx x,
length (elementsZ_single x cx) = N.to_nat cx.
Proof.
induction cx as [| cx' IH] using N.peano_ind. {
by simpl.
} {
intros x.
rewrite elementsZ_single_succ_front /=.
rewrite IH N2Nat.inj_succ //.
}
Qed.
Lemma elementsZ_aux''_irrel : forall c acc x H1 H2,
elementsZ_aux'' acc x c H1 = elementsZ_aux'' acc x c H2.
Proof.
intro c.
induction c using (well_founded_ind lt_wf_0).
case_eq c. {
intros H_c acc x; case; intro; case; intro.
reflexivity.
} {
intros p H_c acc x; case; intro; case; intro.
simpl.
apply H.
rewrite H_c.
rewrite N.pos_pred_spec.
apply N.lt_pred_l.
discriminate.
}
Qed.
Lemma elementsZ_aux'_pos : forall s x p,
elementsZ_aux' s x (N.pos p) = elementsZ_aux' (x::s) (Z.succ x) (Pos.pred_N p).
Proof.
intros s x p.
unfold elementsZ_aux'.
unfold elementsZ_aux''.
case: (lt_wf_0 _).
intro.
apply elementsZ_aux''_irrel.
Qed.
Lemma elementsZ_aux'_zero : forall s x,
elementsZ_aux' s x (0%N) = s.
Proof.
intros s x.
unfold elementsZ_aux'.
by case (lt_wf_0 (0%N)); intro.
Qed.
Lemma elementsZ_aux'_succ : forall s x c,
elementsZ_aux' s x (N.succ c) = elementsZ_aux' (x::s) (Z.succ x) c.
Proof.
intros s x c.
case c.
- by rewrite elementsZ_aux'_pos.
- intro p; simpl.
rewrite elementsZ_aux'_pos.
by rewrite Pos.pred_N_succ.
Qed.
Lemma elementsZ_single_intro : forall c s x,
elementsZ_aux' s x c =
(List.rev (elementsZ_single x c)) ++ s.
Proof.
induction c as [| c' IH] using N.peano_ind. {
intros s x.
rewrite elementsZ_aux'_zero.
rewrite elementsZ_single_base //.
} {
intros s x.
rewrite elementsZ_aux'_succ elementsZ_single_succ_front IH.
rewrite -app_assoc /= //.
}
Qed.
Lemma elementsZ_aux_alt_def : forall s acc,
elementsZ_aux acc s = elementsZ s ++ acc.
Proof.
induction s as [| [x c] s' IH]. {
move => acc.
rewrite /elementsZ_aux elementsZ_nil app_nil_l //.
} {
move => acc.
rewrite /elementsZ /elementsZ_aux -/elementsZ_aux !IH !elementsZ_single_intro.
rewrite -!app_assoc app_nil_l //.
}
Qed.
Lemma elementsZ_cons : forall x c s, elementsZ (((x, c) :: s) : t) =
((elementsZ s) ++ (List.rev (elementsZ_single x c))).
Proof.
intros x c s.
rewrite /elementsZ /elementsZ_aux -/elementsZ_aux.
rewrite !elementsZ_aux_alt_def elementsZ_single_intro !app_nil_r //.
Qed.
Lemma elements_cons : forall x c s, elements (((x, c) :: s) : t) =
((elements_single x c) ++ elements s).
Proof.
intros x c s.
rewrite /elements /elements_single elementsZ_cons.
rewrite !rev_map_alt_def map_app rev_app_distr map_rev rev_involutive //.
Qed.
Lemma In_elementsZ_single_hd : forall (c : N) x, (c <> 0)%N -> List.In x (elementsZ_single x c).
Proof.
intros c x H_c_neq.
rewrite In_elementsZ_single.
split. {
apply Z.le_refl.
} {
apply Z.lt_add_pos_r.
have -> : 0 = Z.of_N (0%N) by [].
apply N2Z.inj_lt.
by apply N.neq_0_lt_0.
}
Qed.
(** *** Alternative definition of addZ *)
(** [addZ] is defined with efficient execution in mind.
We derive first an alternative definition that demonstrates
the intention better and is better suited for proofs. *)
Lemma addZ_ind :
forall (P : Z -> list (Z * N) -> Prop),
(* case s = nil *)
(forall (x : Z), P x nil) ->
(* case s = [y, c] :: l, compare (x+1) y = Eq *)
(forall (x : Z) (l : list (Z * N)) (c : N),
P x ((x + 1, c) :: l)) ->
(* case s = [y, c] :: l, compare (x+1) y = Lt *)
(forall (x : Z) (l : list (Z * N)) (y : Z) (c : N),
(x + 1 ?= y) = Lt ->
P x ((y, c) :: l)) ->
(* case s = [y, c] :: l, compare (x+1) y = Gt, compare x (y+c) = Eq,
l = nil *)
(forall (y : Z) (c : N),
((y + Z.of_N c) + 1 ?= y) = Gt ->
P (y + Z.of_N c) ((y, c) :: nil)) ->
(* case s = (y, c) :: (z, c') :: l, compare (x+1) y = Gt, compare x (y+c) = Eq,
(z = x + 1) *)
(forall (l : list (Z * N)) (y : Z) (c c' : N),
((y + Z.of_N c) + 1 ?= y) = Gt ->
(P (y+Z.of_N c) l) ->
P (y+Z.of_N c) ((y, c) :: (((y+Z.of_N c) + 1, c') :: l))) ->
(* case s = (y, c) :: (z, c') :: l, compare (x+1) y = Gt, compare x (y+c) = Eq,
(z <> x + 1) *)
(forall (l : list (Z * N)) (y : Z) (c : N) (z : Z) (c' : N),
((y + Z.of_N c) + 1 ?= y) = Gt ->
(z =? (y+Z.of_N c) + 1) = false ->
(P (y+Z.of_N c) ((y, c) :: (z, c') :: l))) ->
(* case s = (y, c) :: l, compare (x+1) y = Gt, compare x (y+c) = Lt *)
(forall (x : Z) (l : list (Z * N)) (y : Z) (c : N),
(x + 1 ?= y) = Gt ->
(x ?= y + Z.of_N c) = Lt ->
P x ((y, c) :: l)) ->
(* case s = (y, c) :: l, compare (x+1) y = Gt, compare x (y+c) = Lt *)
(forall (x : Z)(l : list (Z * N)) (y : Z) (c : N),
(x + 1 ?= y) = Gt ->
(x ?= y + (Z.of_N c)) = Gt ->
(P x l) ->
P x ((y, c) :: l)) ->
forall (x : Z) (s : list (Z * N)),
P x s.
Proof.
intros P H1 H2 H3 H4 H5 H6 H7 H8.
move => x s.
remember (length s) as n.
revert s Heqn.
induction n as [n IH] using Wf_nat.lt_wf_ind.
case. {
move => _. apply H1.
} {
move => [y c] s' H_n.
simpl in H_n.
have IH_s': (P x s'). {
apply (IH (length s')) => //.
rewrite H_n //.
}
case_eq (x + 1 ?= y). {
move => /Z.compare_eq_iff <-.
apply H2.
} {
move => H_x1y_comp.
by apply H3.
} {
move => H_x1y_comp.
case_eq (x ?= y + Z.of_N c). {
move => H_x_eqb.
move : (H_x_eqb) => /Z.compare_eq_iff H_x_eq.
case_eq s'. {
move => _.
rewrite H_x_eq.
apply H4.
rewrite -H_x1y_comp H_x_eq //.
} {
move => [z c'] l H_s'.
have IH_l: (P x l). {
apply (IH (length l)) => //.
rewrite H_n H_s' /=.
apply Lt.lt_trans with (m := S (length l)) => //.
}
case_eq (z =? x + 1). {
move => /Z.eqb_eq ->.
rewrite H_x_eq.
apply H5. {
rewrite -H_x1y_comp H_x_eq //.
} {
rewrite -H_x_eq.
apply IH_l.
}
} {
move => H_z_neq.
rewrite H_x_eq.
eapply H6 => //; rewrite -H_x_eq //.
}
}
} {
by apply H7.
} {
move => H_x_gt.
apply H8 => //.
}
}
}
Qed.
Lemma addZ_aux_alt_def : forall x s acc,
addZ_aux acc x s = (List.rev acc) ++ addZ x s.
Proof.
move => ? ?.
eapply addZ_ind with (P := (fun x s => forall acc, addZ_aux acc x s = rev acc ++ addZ x s)); clear. {
intros x acc.
rewrite /addZ_aux /rev' -rev_alt //.
} {
intros x l c acc.
rewrite /addZ /= Z.compare_refl rev_append_rev //.
} {
intros x l y c H_y_lt acc.
rewrite /addZ /= H_y_lt rev_append_rev //.
} {
intros y c H_y_comp acc.
rewrite /addZ /= H_y_comp Z.compare_refl /rev' -rev_alt //.
} {
intros l y c c' H_y_comp IH acc.
rewrite /addZ /= H_y_comp Z.compare_refl Z.eqb_refl.
rewrite rev_append_rev //.
} {
intros l y c z c' H_y_comp H_z_comp acc.
rewrite /addZ /= H_y_comp Z.compare_refl H_z_comp.
rewrite rev_append_rev /= //.
} {
intros x l y c H_y_comp H_x_comp acc.
rewrite /addZ /= H_x_comp H_y_comp rev_append_rev //.
} {
intros x l y c H_y_comp H_x_comp IH acc.
rewrite /addZ /= H_x_comp H_y_comp.
rewrite !IH /= -app_assoc //.
}
Qed.
Lemma addZ_alt_def : forall x s,
addZ x s =
match s with
| nil => (x, 1%N)::nil
| (y, c) :: l =>
match (Z.compare (x+1) y) with
| Lt => (x, 1%N)::s
| Eq => (x, (c+1)%N)::l
| Gt => match (Z.compare x (y+Z.of_N c)) with
| Lt => s
| Gt => (y,c) :: addZ x l
| Eq => match l with
| nil => (y, (c+1)%N)::nil
| (z, c') :: l' => if (Z.eqb z (x + 1)) then
(y, (c + c' + 1)%N) :: l'
else
(y,(c+1)%N) :: (z, c') :: l'
end
end
end
end.
Proof.
intros x s.
rewrite /addZ Z.add_1_r.
case s => //.
move => [y c] s' /=.
case (Z.succ x ?= y). {
rewrite N.add_1_r //.
} {
reflexivity.
} {
case (x ?= y+ Z.of_N c) => //. {
rewrite !N.add_1_r.
case s' => //.
move => [z c'] s'' //.
rewrite !N.add_1_r //.
} {
rewrite addZ_aux_alt_def //.
}
}
Qed.
(** *** Alternative definition of removeZ *)
(** [removeZ] is defined with efficient execution in mind.
We derive first an alternative definition that demonstrates
the intention better and is better suited for proofs. *)
Lemma removeZ_aux_alt_def : forall s x acc,
removeZ_aux acc x s = (List.rev acc) ++ removeZ x s.
Proof.
induction s as [| [y c] s' IH]. {
intros x acc.
rewrite /removeZ /removeZ_aux /= app_nil_r /rev' -rev_alt //.
} {
intros x acc.
rewrite /removeZ /removeZ_aux -/removeZ_aux.
case (x <? y). {
rewrite !rev_append_rev //=.
} {
case (x <? y + Z.of_N c). {
rewrite /removeZ_aux_insert_guarded.
case (Z.to_N (y + Z.of_N c - (Z.succ x)) =? 0)%N, (Z.to_N (x - y) =? 0)%N;
rewrite !rev_append_rev /= // -!app_assoc /= //.
} {
rewrite !IH /= -app_assoc /= //.
}
}
}
Qed.
Lemma removeZ_alt_def : forall x s,
removeZ x s =
match s with
| nil => nil
| (y, c) :: l =>
if (Z.ltb x y) then s else
if (Z.ltb x (y+Z.of_N c)) then (
(removeZ_aux_insert_guarded y (Z.to_N (x-y))
(removeZ_aux_insert_guarded (Z.succ x) (Z.to_N ((y+Z.of_N c)- (Z.succ x))) l))
) else (y, c) :: removeZ x l
end.
Proof.
intros x s.
rewrite /removeZ /=.
case s => //.
move => [y c] s' /=.
case(x <? y) => //.
case(x <? y + Z.of_N c). {
rewrite /removeZ_aux_insert_guarded.
case (Z.to_N (y + Z.of_N c - (Z.succ x)) =? 0)%N, (Z.to_N (x - y) =? 0)%N => //.
} {
rewrite removeZ_aux_alt_def //.
}
Qed.
(** *** Auxiliary Lemmata about Invariant *)
Lemma inf_impl : forall x y s,
(y <= x) -> inf x s = true -> inf y s = true.
Proof.
intros x y s.
case s => //.
move => [z c] s'.
rewrite /inf.
move => H_y_leq /Z.ltb_lt H_x_lt.
apply Z.ltb_lt.
eapply Z.le_lt_trans; eauto.
Qed.
Lemma Ok_nil : Ok nil <-> True.
Proof.
rewrite /Ok /IsOk /isok /is_encoded_elems_list //.
Qed.
Lemma is_encoded_elems_list_app : forall l1 l2,
is_encoded_elems_list (l1 ++ l2) <->
(is_encoded_elems_list l1 /\ is_encoded_elems_list l2).
Proof.
intros l1 l2.
rewrite /is_encoded_elems_list.
setoid_rewrite in_app_iff.
split; firstorder.
Qed.
Lemma is_encoded_elems_list_rev : forall l,
is_encoded_elems_list (List.rev l) <->
is_encoded_elems_list l.
Proof.
intros l.
rewrite /is_encoded_elems_list.
split; (
move => H x H_in;
apply H;
move : H_in;
rewrite -in_rev => //
).
Qed.
Lemma isok_cons : forall y c s', isok ((y, c) :: s') = true <->
(inf (y+Z.of_N c) s' = true /\ ((c <> 0)%N) /\ isok s' = true).
Proof.
rewrite /isok -/isok.
intros y c s'.
rewrite !Bool.andb_true_iff negb_true_iff.
split. {
move => [] [H_inf] /N.eqb_neq H_c H_s'. tauto.
} {
move => [H_inf] [/N.eqb_neq H_c] H_s'. tauto.
}
Qed.
Lemma Ok_cons : forall y c s', Ok ((y, c) :: s') <->
(inf (y+Z.of_N c) s' = true /\ ((c <> 0)%N) /\
is_encoded_elems_list (elementsZ_single y c) /\ Ok s').
Proof.
intros y c s'.
rewrite /Ok /IsOk isok_cons elementsZ_cons is_encoded_elems_list_app
is_encoded_elems_list_rev.
tauto.
Qed.
Lemma Nin_elements_greater : forall s y,
inf y s = true ->
isok s = true ->
forall x, x <= y ->
~(InZ x s).
Proof.
induction s as [| [z c] s' IH]. {
intros y _ _ x _.
done.
} {
rewrite /inf.
move => y /Z.ltb_lt H_y_lt /isok_cons [H_inf'] [H_c] H_s' x H_x_le.
rewrite /InZ elementsZ_cons in_app_iff -!in_rev In_elementsZ_single.
move => []. {
eapply IH; eauto.
apply Z.le_trans with (m := z). {
eapply Z.lt_le_incl.
eapply Z.le_lt_trans; eauto.
} {
apply Z_le_add_r.
}
} {
move => [H_z_leq] _; contradict H_z_leq.
apply Z.nle_gt.
eapply Z.le_lt_trans; eauto.
}
}
Qed.
Lemma isok_inf_nin :
forall x s,
isok s = true ->
inf x s = true ->
~ (InZ x s).
Proof.
move => x.
induction s as [| [y c] s' IH]. {
move => _ _.
rewrite /InZ elementsZ_nil //.
} {
rewrite isok_cons.