1 (********************************************************************)
3 (* The Why3 Verification Platform
/ The Why3 Development Team
*)
4 (* Copyright
2010-2023 -- Inria
- CNRS
- Paris
-Saclay University
*)
6 (* This software is distributed under the terms of the GNU Lesser
*)
7 (* General Public License version
2.1, with the special exception
*)
8 (* on linking described in file LICENSE.
*)
10 (********************************************************************)
12 (* This file is generated by Why3
's Coq-realize driver *)
13 (* Beware! Only edit allowed sections below *)
14 Require Import BuiltIn.
23 Definition min_elt : set.Fset.fset Numbers.BinNums.Z -> Numbers.BinNums.Z.
26 destruct X as (fs, H).
27 (* We use the list to define the algorithm *)
28 eapply ClassicalEpsilon.constructive_indefinite_description in H.
29 destruct H as (l, Hl).
31 - apply Z.zero. (* If the list is empty the result is unspecified *)
32 - apply (List.fold_left (fun acc x => if Z_le_dec acc x then acc else x) l z).
37 forall (s:set.Fset.fset Numbers.BinNums.Z), ~ set.Fset.is_empty s ->
38 set.Fset.mem (min_elt s) s /\
39 (forall (x:Numbers.BinNums.Z), set.Fset.mem x s -> ((min_elt s) <= x)%Z).
42 unfold min_elt, Fset.is_empty, Fset.mem, set.Set.mem in *.
44 destruct ClassicalEpsilon.constructive_indefinite_description as [l [Hdupl Heql]].
46 { destruct h1. intros. intro. apply Heql in H. destruct H. }
48 forall x, List.In x l -> List.fold_left (fun x1 acc : int => if Z_le_dec x1 acc then x1 else acc) l z <= x)%Z.
52 assert (forall l z, List.fold_left (fun x1 acc : int => if Z_le_dec x1 acc then x1 else acc) l z <= z)%Z.
54 induction l1; intros; simpl; eauto. lia.
55 simpl. destruct Z_le_dec. eauto. eapply Z.le_trans with a0; eauto. lia.
57 simpl. destruct Z_le_dec.
58 destruct (Z_le_dec z0 x0).
59 eapply Z.le_trans with z0. eapply H0. assumption.
60 simpl in H. destruct H. subst. lia.
63 destruct (Z_le_dec a x0).
64 eapply Z.le_trans with a. eapply H0. assumption.
65 simpl in H. destruct H. subst. lia.
69 assert (forall l z, List.In (List.fold_left (fun x acc => if Z_le_dec x acc then x else acc) l z) l \/
70 List.fold_left (fun x acc => if Z_le_dec x acc then x else acc) l z = z).
73 + simpl. right. reflexivity.
74 + simpl. destruct Z_le_dec.
75 - specialize (IHl0 z0). intuition.
76 - specialize (IHl0 a). intuition.
80 rewrite <- Heql. simpl. specialize (H0 l z). intuition.
82 intros. eapply Heql in H1. eapply (H (List.cons z l) z) in H1; eauto. simpl in H1.
83 destruct Z_le_dec. assumption.
88 Definition max_elt : set.Fset.fset Numbers.BinNums.Z -> Numbers.BinNums.Z.
91 apply (- min_elt (Fset.map (fun x => - x) X))%Z.
96 forall (s:set.Fset.fset Numbers.BinNums.Z), ~ set.Fset.is_empty s ->
97 set.Fset.mem (max_elt s) s /\
98 (forall (x:Numbers.BinNums.Z), set.Fset.mem x s -> (x <= (max_elt s))%Z).
102 assert (~ Fset.is_empty (Fset.map (fun x : int => (- x)%Z) s))%Z.
103 { unfold Fset.is_empty in *.
104 intro. destruct h1. intros. intro. specialize (H (-x)%Z).
105 apply H. eapply Fset.mem_map. assumption.
107 specialize (min_elt_def (Fset.map (fun x => -x)%Z s) H).
111 rewrite Fset.map_def in H0. destruct H0.
112 destruct H. rewrite H0. replace ( - - x)%Z with x. assumption.
115 intros. clear H0. clear h1 H.
116 assert (min_elt (Fset.map (fun x => - x) s) <= -x)%Z.
118 eapply H1; eauto. apply Fset.mem_map. assumption.
123 Fixpoint seqZ l len : list Numbers.BinNums.Z :=
125 | S n => List.cons l (seqZ (l + 1)%Z n)
129 Lemma seqZ_le: forall len x l, List.In x (seqZ l len) -> (l <= x)%Z.
131 induction len; simpl; intuition.
132 eapply IHlen in H0; intuition.
135 Lemma seqZ_le2: forall len x l, List.In x (seqZ l len) -> (x < l + Z.of_nat len)%Z.
137 induction len; simpl; intuition idtac.
139 - eapply IHlen in H0. lia.
142 Lemma seqZ_rev: forall len x l, (l <= x < l + Z.of_nat len)%Z -> List.In x (seqZ l len).
144 induction len; intros; simpl in *.
146 + destruct (Z.eq_dec l x); eauto.
147 right. eapply IHlen; eauto. lia.
150 Lemma seqZ_In_iff: forall l len x, List.In x (seqZ l len) <-> (l <= x < l + Z.of_nat len)%Z.
153 intros. eauto using seqZ_le, seqZ_le2.
157 Lemma seqZ_NoDup: forall len l, List.NoDup (seqZ l len).
159 induction len; intros.
161 + simpl. constructor; eauto.
162 intro Habs. eapply seqZ_le in Habs. lia.
165 Lemma seqZ_length: forall len l, List.length (seqZ l len) = len.
167 induction len; eauto.
168 intros. simpl. rewrite IHlen. reflexivity.
171 Lemma interval_proof :
172 forall l r : int, exists s : list int,
174 forall e : int, List.In e s <->
175 (if Z_le_dec l e then if Z_lt_dec e r then true else false else false) = true.
178 destruct (Z_le_dec l r).
179 + exists (seqZ l (Z.to_nat (r - l))%Z).
184 rewrite Z2Nat.id; [|lia].
186 * destruct Z_lt_dec. split; intros; [reflexivity|].
188 intuition ; try inversion H.
189 * intuition ; try inversion H.
194 destruct Z_le_dec; try destruct Z_lt_dec; intuition.
201 Definition interval :
202 Numbers.BinNums.Z -> Numbers.BinNums.Z -> set.Fset.fset Numbers.BinNums.Z.
207 if Z_lt_dec x r then true else false
209 apply interval_proof.
214 forall (l:Numbers.BinNums.Z) (r:Numbers.BinNums.Z) (x:Numbers.BinNums.Z),
215 set.Fset.mem x (interval l r) <-> (l <= x)%Z /\ (x < r)%Z.
218 unfold interval, Fset.mem, set.Set.mem.
219 destruct Z_le_dec; try destruct Z_lt_dec; intuition; try inversion H.
222 Lemma interval_is_finite: forall l r,
223 Cardinal.is_finite (fun x : int =>
224 if Z_le_dec l x then if Z_lt_dec x r then true
225 else false else false).
228 unfold Cardinal.is_finite.
229 destruct (Z_le_dec l r).
230 + exists (seqZ l (Z.to_nat (r - l)%Z)).
231 split. apply seqZ_NoDup.
232 intros. rewrite seqZ_In_iff.
233 rewrite Z2Nat.id; [|lia].
234 destruct Z_le_dec; try destruct Z_lt_dec; intuition; try inversion H.
235 + exists nil. split. constructor.
236 simpl. intros. destruct Z_le_dec; try destruct Z_lt_dec; intuition.
241 Lemma cardinal_interval :
242 forall (l:Numbers.BinNums.Z) (r:Numbers.BinNums.Z),
243 ((l <= r)%Z -> ((set.Fset.cardinal (interval l r)) = (r - l)%Z)) /\
244 (~ (l <= r)%Z -> ((set.Fset.cardinal (interval l r)) = 0%Z)).
247 unfold interval, Fset.mem, set.Set.mem, Fset.cardinal, Cardinal.cardinal.
248 assert (H := interval_is_finite l r).
249 destruct ClassicalEpsilon.excluded_middle_informative; [| intuition].
250 destruct ClassicalEpsilon.classical_indefinite_description.
255 assert (length (seqZ l (Z.to_nat (r - l)%Z)) = length x).
257 eapply Nat.le_antisymm.
258 + eapply List.NoDup_incl_length. eapply seqZ_NoDup. intro. rewrite H2.
259 rewrite seqZ_In_iff. destruct Z_le_dec; try destruct Z_lt_dec; intuition idtac.
260 rewrite Z2Nat.id in H5; lia.
261 + eapply List.NoDup_incl_length. assumption. intro. rewrite H2.
262 rewrite seqZ_In_iff. destruct Z_le_dec; try destruct Z_lt_dec; intuition (try discriminate).
263 rewrite Z2Nat.id; lia.
265 rewrite <- H3. rewrite seqZ_length. rewrite Z2Nat.id; lia.
266 + intros. destruct a.
267 destruct x. reflexivity.
268 specialize (H2 z). contradict H2. destruct Z_le_dec.
269 destruct Z_lt_dec. lia. intuition. intuition.