1
(************************************************************************)
2
(* v * The Coq Proof Assistant / The Coq Development Team *)
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
4
(* \VV/ **************************************************************)
5
(* // * This file is distributed under the terms of the *)
6
(* * GNU Lesser General Public License Version 2.1 *)
7
(************************************************************************)
9
(* Library about sorted (sub-)arrays / Nicolas Magaud, July 1998 *)
11
(* $Id: Sorted.v 5920 2004-07-16 20:01:26Z herbelin $ *)
13
Require Export Arrays.
14
Require Import ArrayPermut.
16
Require Import ZArithRing.
18
Open Local Scope Z_scope.
20
Set Implicit Arguments.
24
Definition sorted_array (N:Z) (A:array N Z) (deb fin:Z) :=
25
deb <= fin -> forall x:Z, x >= deb -> x < fin -> #A [x] <= #A [x + 1].
27
(* Elements of a sorted sub-array are in increasing order *)
29
(* one element and the next one *)
31
Lemma sorted_elements_1 :
32
forall (N:Z) (A:array N Z) (n m:Z),
35
k >= n -> forall i:Z, 0 <= i -> k + i <= m -> #A [k] <= #A [k + i].
37
intros N A n m H_sorted k H_k i H_i.
38
pattern i in |- *. apply natlike_ind.
40
replace (k + 0) with k; omega. (*** Ring `k+0` => BUG ***)
43
apply Zle_trans with (m := #A [k + x]).
47
replace (k + (x + 1)) with (k + x + 1).
48
unfold sorted_array in H_sorted.
49
apply H_sorted; omega.
56
(* one element and any of the following *)
58
Lemma sorted_elements :
59
forall (N:Z) (A:array N Z) (n m k l:Z),
61
k >= n -> l < N -> k <= l -> l <= m -> #A [k] <= #A [l].
64
replace l with (k + (l - k)).
65
apply sorted_elements_1 with (n := n) (m := m);
66
[ assumption | omega | omega | omega ].
70
Hint Resolve sorted_elements: datatypes v62.
72
(* A sub-array of a sorted array is sorted *)
74
Lemma sub_sorted_array :
75
forall (N:Z) (A:array N Z) (deb fin i j:Z),
76
sorted_array A deb fin ->
77
i >= deb -> j <= fin -> i <= j -> sorted_array A i j.
79
unfold sorted_array in |- *.
84
Hint Resolve sub_sorted_array: datatypes v62.
86
(* Extension on the left of the property of being sorted *)
88
Lemma left_extension :
89
forall (N:Z) (A:array N Z) (i j:Z),
92
sorted_array A i j -> #A [i - 1] <= #A [i] -> sorted_array A (i - 1) j.
94
intros; unfold sorted_array in |- *; intros.
95
elim (Z_ge_lt_dec x i). (* (`x >= i`) + (`x < i`) *)
100
replace x with (i - 1).
101
replace (i - 1 + 1) with i; [ assumption | omega ].
106
(* Extension on the right *)
108
Lemma right_extension :
109
forall (N:Z) (A:array N Z) (i j:Z),
112
sorted_array A i j -> #A [j] <= #A [j + 1] -> sorted_array A i (j + 1).
114
intros; unfold sorted_array in |- *; intros.
115
elim (Z_lt_ge_dec x j).
120
replace x with j; [ assumption | omega ].
123
(* Substitution of the leftmost value by a smaller value *)
125
Lemma left_substitution :
126
forall (N:Z) (A:array N Z) (i j v:Z),
129
sorted_array A i j -> v <= #A [i] -> sorted_array (store A i v) i j.
131
intros N A i j v H_i H_j H_sorted H_v.
132
unfold sorted_array in |- *; intros.
134
cut (x = i \/ x > i).
135
intro Hcut; elim Hcut; clear Hcut; intro.
137
rewrite store_def_1; try omega.
138
rewrite store_def_2; try omega.
139
apply Zle_trans with (m := #A [i]); [ assumption | apply H_sorted; omega ].
141
rewrite store_def_2; try omega.
142
rewrite store_def_2; try omega.
143
apply H_sorted; omega.
147
(* Substitution of the rightmost value by a larger value *)
149
Lemma right_substitution :
150
forall (N:Z) (A:array N Z) (i j v:Z),
153
sorted_array A i j -> #A [j] <= v -> sorted_array (store A j v) i j.
155
intros N A i j v H_i H_j H_sorted H_v.
156
unfold sorted_array in |- *; intros.
158
cut (x = j - 1 \/ x < j - 1).
159
intro Hcut; elim Hcut; clear Hcut; intro.
161
replace (j - 1 + 1) with j; [ idtac | omega ]. (*** Ring `j-1+1`. => BUG ***)
162
rewrite store_def_2; try omega.
163
rewrite store_def_1; try omega.
164
apply Zle_trans with (m := #A [j]).
165
apply sorted_elements with (n := i) (m := j); try omega; assumption.
168
rewrite store_def_2; try omega.
169
rewrite store_def_2; try omega.
170
apply H_sorted; omega.
175
(* Affectation outside of the sorted region *)
178
forall (N:Z) (A:array N Z) (i j k v:Z),
181
sorted_array A i j ->
182
0 <= k < i \/ j < k < N -> sorted_array (store A k v) i j.
185
unfold sorted_array in |- *; intros.
186
rewrite store_def_2; try omega.
187
rewrite store_def_2; try omega.
188
apply H1; assumption.
191
Lemma sorted_array_id :
192
forall (N:Z) (t1 t2:array N Z) (g d:Z),
193
sorted_array t1 g d -> array_id t1 t2 g d -> sorted_array t2 g d.
195
intros N t1 t2 g d Hsorted Hid.
196
unfold array_id in Hid.
197
unfold sorted_array in Hsorted. unfold sorted_array in |- *.
198
intros Hgd x H1x H2x.
199
rewrite <- (Hid x); [ idtac | omega ].
200
rewrite <- (Hid (x + 1)); [ idtac | omega ].
201
apply Hsorted; assumption.
b'\\ No newline at end of file'