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
|
(**************************************************************************)
(* *)
(* Proof of the Quicksort Algorithm. *)
(* *)
(* Jean-Christophe Fillitre (LRI, Universit Paris Sud) *)
(* August 1998 *)
(* *)
(**************************************************************************)
Require Import Why.
Require Import Omega.
Set Implicit Arguments.
Unset Strict Implicit.
(* Here we define the property which expresses that,
* on the segment [g,d] of the array t, all elements on the left of p
* are lesser or equal than t[p] and all elements on the right of p
* are greater or equal than t[p].
*
* So we introduce the properties array_le and array_ge which express
* that all elements of a segment [g,d] are <= (resp. >=) than a given value
*)
Inductive array_le (t:array Z) (g d v:Z) : Prop :=
array_le_cons :
(forall i:Z, (g <= i <= d)%Z -> (access t i <= v)%Z) ->
array_le t g d v.
Inductive array_ge (t:array Z) (g d v:Z) : Prop :=
array_ge_cons :
(forall i:Z, (g <= i <= d)%Z -> (v <= access t i)%Z) ->
array_ge t g d v.
Inductive partition_p (t:array Z) (g d p:Z) : Prop :=
piv :
(g <= p)%Z ->
(p <= d)%Z ->
array_le t g (p - 1) (access t p) ->
array_ge t (p + 1) d (access t p) -> partition_p t g d p.
(* Lemmas about array_le *)
Lemma array_le_empty :
forall (t:array Z) (g d v:Z), (d < g)%Z -> array_le t g d v.
Proof.
intros t g d v H.
constructor.
intros.
absurd (g <= d)%Z; omega.
Qed.
Lemma array_le_right_extension :
forall (t:array Z) (g d v:Z),
array_le t g d v ->
(access t (d + 1) <= v)%Z -> array_le t g (d + 1) v.
Proof.
intros t g d v H_le Hv.
elim H_le; intros.
constructor.
intros.
elim (Z_eq_dec i (d + 1)); intro.
rewrite a; assumption.
apply H; omega.
Qed.
Lemma array_le_exchange :
forall (t t':array Z) (g d v x y:Z),
(0 <= g)%Z ->
(d < array_length t)%Z ->
array_le t g d v ->
(d < x <= y)%Z -> exchange t t' x y -> array_le t' g d v.
Proof.
intros t t' g d v x y Hg Hd Hle Hxy Hex.
elim Hle; intros.
elim Hex; intros.
constructor.
intros.
rewrite <- H5; try omega.
apply H; assumption.
Qed.
(* Lemmas about array_ge *)
Lemma array_ge_empty :
forall (t:array Z) (g d v:Z), (d < g)%Z -> array_ge t g d v.
Proof.
intros t g d v H.
constructor.
intros.
absurd (g <= d)%Z; omega.
Qed.
Lemma array_ge_left_extension :
forall (t:array Z) (g d v:Z),
array_ge t g d v ->
(v <= access t (g - 1))%Z -> array_ge t (g - 1) d v.
Proof.
intros t g d v H_ge Hv.
elim H_ge; intros.
constructor.
intros.
elim (Z_eq_dec i (g - 1)); intro.
rewrite a; assumption.
apply H; omega.
Qed.
Lemma array_ge_exchange :
forall (t t':array Z) (g d v x y:Z),
(0 <= g)%Z ->
(d < array_length t)%Z ->
array_ge t g d v ->
(x <= y < g)%Z -> exchange t t' x y -> array_ge t' g d v.
Proof.
intros t t' g d v x y Hg Hd Hge Hxy Hex.
elim Hge; intros.
elim Hex; intros.
constructor.
intros.
rewrite <- H5; try omega.
apply H; assumption.
Qed.
(* Lemmas about partition_p and sub_permut *)
Lemma partition_p_permut_left :
forall (t t':array Z) (g d p:Z),
(0 <= g)%Z ->
(g < d)%Z ->
(d < array_length t)%Z ->
(g <= p <= d)%Z ->
partition_p t g d p ->
sub_permut g (Zpred p) t t' -> partition_p t' g d p.
Proof.
intros t t' g d p hyp1 hyp2 hyp3 hyp4 piv_t perm.
elim piv_t; intros.
cut (Zpred p < array_length t)%Z; [ intro | unfold Zpred; omega ].
generalize (sub_permut_function perm hyp1 H3).
intro.
constructor; try assumption.
(* array_le *)
constructor.
intros.
rewrite <- (sub_permut_eq perm (i:=p)).
elim H1; intros.
elim (H4 i); try (unfold Zpred; omega).
intros.
elim H8; intros.
elim H9; intros.
rewrite H11.
apply H6; unfold Zpred in H10; omega.
unfold Zpred; omega.
(* array_ge *)
constructor.
intros.
rewrite <- (sub_permut_eq perm (i:=p)).
rewrite <- (sub_permut_eq perm (i:=i)).
elim H2; intros.
apply H6; omega.
unfold Zpred; omega.
unfold Zpred; omega.
Qed.
Lemma partition_p_permut_right :
forall (t t':array Z) (g d p:Z),
(0 <= g)%Z ->
(g < d)%Z ->
(d < array_length t)%Z ->
(g <= p <= d)%Z ->
partition_p t g d p ->
sub_permut (Zsucc p) d t t' -> partition_p t' g d p.
Proof.
intros t t' g d p hyp1 hyp2 hyp3 hyp4 piv_t perm.
elim piv_t; intros.
cut (0 <= Zsucc p)%Z; [ intro | unfold Zpred; omega ].
generalize (sub_permut_function perm H3 hyp3).
intro.
constructor; try assumption.
(* array_le *)
constructor.
intros.
rewrite <- (sub_permut_eq perm (i:=p)).
rewrite <- (sub_permut_eq perm (i:=i)).
elim H1; intros.
apply H6; omega.
unfold Zsucc; omega.
unfold Zsucc; omega.
(* array_ge *)
constructor.
intros.
rewrite <- (sub_permut_eq perm (i:=p)).
elim H2; intros.
elim (H4 i); try (unfold Zsucc; omega).
intros.
elim H8; intros.
elim H9; intros.
rewrite H11.
apply H6; unfold Zsucc in H10; omega.
unfold Zsucc; omega.
Qed.
|