| 1 |
(************************************************************************) |
| 2 |
(* * The Rocq Prover / The Rocq Development Team *) |
| 3 |
(* v * Copyright INRIA, CNRS and contributors *) |
| 4 |
(* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 |
(* \VV/ **************************************************************) |
| 6 |
(* // * This file is distributed under the terms of the *) |
| 7 |
(* * GNU Lesser General Public License Version 2.1 *) |
| 8 |
(* * (see LICENSE file for the text of the license) *) |
| 9 |
(************************************************************************) |
| 10 |
(** Reformulation of the Wf module using subsets where possible, providing |
| 11 |
the support for [Program]'s treatment of well-founded definitions. *) |
| 12 |
|
| 13 |
Require Import Corelib.Init.Wf. |
| 14 |
Require Import Corelib.Program.Utils. |
| 15 |
|
| 16 |
Local Open Scope program_scope. |
| 17 |
|
| 18 |
Section Well_founded. |
| 19 |
Variable A : Type. |
| 20 |
Variable R : A -> A -> Prop. |
| 21 |
Hypothesis Rwf : well_founded R. |
| 22 |
|
| 23 |
Variable P : A -> Type. |
| 24 |
|
| 25 |
Variable F_sub : forall x:A, (forall y: { y : A | R y x }, P (proj1_sig y)) -> P x. |
| 26 |
|
| 27 |
Fixpoint Fix_F_sub (x : A) (r : Acc R x) : P x := |
| 28 |
F_sub x (fun y: { y : A | R y x} => Fix_F_sub (proj1_sig y) |
| 29 |
(Acc_inv r (proj2_sig y))). |
| 30 |
|
| 31 |
Definition Fix_sub (x : A) := Fix_F_sub x (Rwf x). |
| 32 |
|
| 33 |
Register Fix_sub as program.wf.fix_sub. |
| 34 |
|
| 35 |
(* Abbreviation Fix_F := (Fix_F_sub P F_sub) (only parsing). (* alias *) *) |
| 36 |
(* Definition Fix (x:A) := Fix_F_sub P F_sub x (Rwf x). *) |
| 37 |
|
| 38 |
Hypothesis F_ext : |
| 39 |
forall (x:A) (f g:forall y:{y:A | R y x}, P (`y)), |
| 40 |
(forall y:{y : A | R y x}, f y = g y) -> F_sub x f = F_sub x g. |
| 41 |
|
| 42 |
Lemma Fix_F_eq : |
| 43 |
forall (x:A) (r:Acc R x), |
| 44 |
F_sub x (fun y:{y:A | R y x} => Fix_F_sub (`y) (Acc_inv r (proj2_sig y))) = Fix_F_sub x r. |
| 45 |
Proof. |
| 46 |
intros x r; destruct r using Acc_inv_dep; auto. |
| 47 |
Qed. |
| 48 |
|
| 49 |
Lemma Fix_F_inv : forall (x:A) (r s:Acc R x), Fix_F_sub x r = Fix_F_sub x s. |
| 50 |
Proof. |
| 51 |
intro x; induction (Rwf x); intros. |
| 52 |
rewrite <- 2 Fix_F_eq; intros. apply F_ext; intros []; auto. |
| 53 |
Qed. |
| 54 |
|
| 55 |
Lemma Fix_eq : forall x:A, Fix_sub x = F_sub x (fun y:{ y:A | R y x} => Fix_sub (proj1_sig y)). |
| 56 |
Proof. |
| 57 |
intro x; unfold Fix_sub. |
| 58 |
rewrite <- (Fix_F_eq ). |
| 59 |
apply F_ext; intros. |
| 60 |
apply Fix_F_inv. |
| 61 |
Qed. |
| 62 |
|
| 63 |
Lemma fix_sub_eq : |
| 64 |
forall x : A, |
| 65 |
Fix_sub x = |
| 66 |
let f_sub := F_sub in |
| 67 |
f_sub x (fun y: {y : A | R y x} => Fix_sub (`y)). |
| 68 |
Proof. |
| 69 |
exact Fix_eq. |
| 70 |
Qed. |
| 71 |
|
| 72 |
End Well_founded. |
| 73 |
|
| 74 |
Set Implicit Arguments. |
| 75 |
|
| 76 |
(** Reasoning about well-founded fixpoints on measures. *) |
| 77 |
|
| 78 |
Section Measure_well_founded. |
| 79 |
|
| 80 |
(* Measure relations are well-founded if the underlying relation is well-founded. *) |
| 81 |
|
| 82 |
Variables T M: Type. |
| 83 |
Variable R: M -> M -> Prop. |
| 84 |
Hypothesis wf: well_founded R. |
| 85 |
Variable m: T -> M. |
| 86 |
|
| 87 |
Definition MR (x y: T): Prop := R (m x) (m y). |
| 88 |
|
| 89 |
Register MR as program.wf.mr. |
| 90 |
|
| 91 |
Lemma measure_wf: well_founded MR. |
| 92 |
Proof. |
| 93 |
unfold well_founded. |
| 94 |
cut (forall (a: M) (a0: T), m a0 = a -> Acc MR a0). |
| 95 |
+ intros H a. |
| 96 |
apply (H (m a)); auto. |
| 97 |
+ apply (@well_founded_ind M R wf (fun mm => forall a, m a = mm -> Acc MR a)). |
| 98 |
intros ? H ? H0. |
| 99 |
apply Acc_intro. |
| 100 |
intros y H1. |
| 101 |
unfold MR in H1. |
| 102 |
rewrite H0 in H1. |
| 103 |
apply (H (m y)); auto. |
| 104 |
Defined. |
| 105 |
|
| 106 |
End Measure_well_founded. |
| 107 |
|
| 108 |
#[global] |
| 109 |
Hint Resolve measure_wf : core. |
| 110 |
|
| 111 |
Section Fix_rects. |
| 112 |
|
| 113 |
Variable A: Type. |
| 114 |
Variable P: A -> Type. |
| 115 |
Variable R : A -> A -> Prop. |
| 116 |
Variable Rwf : well_founded R. |
| 117 |
Variable f: forall (x : A), (forall y: { y: A | R y x }, P (proj1_sig y)) -> P x. |
| 118 |
|
| 119 |
Lemma F_unfold x r: |
| 120 |
Fix_F_sub A R P f x r = |
| 121 |
f (fun y => Fix_F_sub A R P f (proj1_sig y) (Acc_inv r (proj2_sig y))). |
| 122 |
Proof. intros. case r; auto. Qed. |
| 123 |
|
| 124 |
(* Fix_F_sub_rect lets one prove a property of |
| 125 |
functions defined using Fix_F_sub by showing |
| 126 |
that property to be invariant over single application of the |
| 127 |
function body (f in our case). *) |
| 128 |
|
| 129 |
Lemma Fix_F_sub_rect |
| 130 |
(Q: forall x, P x -> Type) |
| 131 |
(inv: forall x: A, |
| 132 |
(forall (y: A) (H: R y x) (a: Acc R y), |
| 133 |
Q y (Fix_F_sub A R P f y a)) -> |
| 134 |
forall (a: Acc R x), |
| 135 |
Q x (f (fun y: {y: A | R y x} => |
| 136 |
Fix_F_sub A R P f (proj1_sig y) (Acc_inv a (proj2_sig y))))) |
| 137 |
: forall x a, Q _ (Fix_F_sub A R P f x a). |
| 138 |
Proof. |
| 139 |
set (R' := fun (x: A) => forall a, Q _ (Fix_F_sub A R P f x a)). |
| 140 |
cut (forall x, R' x); auto. |
| 141 |
apply (well_founded_induction_type Rwf). |
| 142 |
subst R'. |
| 143 |
simpl. |
| 144 |
intros. |
| 145 |
rewrite F_unfold; auto. |
| 146 |
Qed. |
| 147 |
|
| 148 |
(* Let's call f's second parameter its "lowers" function, since it |
| 149 |
provides it access to results for inputs with a lower measure. |
| 150 |
|
| 151 |
In preparation of lemma similar to Fix_F_sub_rect, but |
| 152 |
for Fix_sub, we first |
| 153 |
need an extra hypothesis stating that the function body has the |
| 154 |
same result for different "lowers" functions (g and h below) as long |
| 155 |
as those produce the same results for lower inputs, regardless |
| 156 |
of the lt proofs. *) |
| 157 |
|
| 158 |
Hypothesis equiv_lowers: |
| 159 |
forall x0 (g h: forall x: {y: A | R y x0}, P (proj1_sig x)), |
| 160 |
(forall x p p', g (exist (fun y: A => R y x0) x p) = h (exist (*FIXME shouldn't be needed *) (fun y => R y x0) x p')) -> |
| 161 |
f g = f h. |
| 162 |
|
| 163 |
(* From equiv_lowers, it follows that |
| 164 |
[Fix_F_sub A R P f x] applications do not not |
| 165 |
depend on the Acc proofs. *) |
| 166 |
|
| 167 |
Lemma eq_Fix_F_sub x (a a': Acc R x): |
| 168 |
Fix_F_sub A R P f x a = |
| 169 |
Fix_F_sub A R P f x a'. |
| 170 |
Proof. |
| 171 |
revert a'. |
| 172 |
pattern x, (Fix_F_sub A R P f x a). |
| 173 |
apply Fix_F_sub_rect. |
| 174 |
intros ? H **. |
| 175 |
rewrite F_unfold. |
| 176 |
apply equiv_lowers. |
| 177 |
intros. |
| 178 |
apply H. |
| 179 |
assumption. |
| 180 |
Qed. |
| 181 |
|
| 182 |
(* Finally, Fix_F_rect lets one prove a property of |
| 183 |
functions defined using Fix_F_sub by showing that |
| 184 |
property to be invariant over single application of the function |
| 185 |
body (f). *) |
| 186 |
|
| 187 |
Lemma Fix_sub_rect |
| 188 |
(Q: forall x, P x -> Type) |
| 189 |
(inv: forall |
| 190 |
(x: A) |
| 191 |
(H: forall (y: A), R y x -> Q y (Fix_sub A R Rwf P f y)) |
| 192 |
(a: Acc R x), |
| 193 |
Q x (f (fun y: {y: A | R y x} => Fix_sub A R Rwf P f (proj1_sig y)))) |
| 194 |
: forall x, Q _ (Fix_sub A R Rwf P f x). |
| 195 |
Proof. |
| 196 |
unfold Fix_sub. |
| 197 |
intros x. |
| 198 |
apply Fix_F_sub_rect. |
| 199 |
intros x0 H a. |
| 200 |
assert (forall y: A, R y x0 -> Q y (Fix_F_sub A R P f y (Rwf y))) as X0; auto. |
| 201 |
set (q := inv x0 X0 a). clearbody q. |
| 202 |
rewrite <- (equiv_lowers (fun y: {y: A | R y x0} => |
| 203 |
Fix_F_sub A R P f (proj1_sig y) (Rwf (proj1_sig y))) |
| 204 |
(fun y: {y: A | R y x0} => Fix_F_sub A R P f (proj1_sig y) (Acc_inv a (proj2_sig y)))); auto. |
| 205 |
intros. |
| 206 |
apply eq_Fix_F_sub. |
| 207 |
Qed. |
| 208 |
|
| 209 |
End Fix_rects. |
| 210 |
|
| 211 |
(** Tactic to fold a definition based on [Fix_measure_sub]. *) |
| 212 |
|
| 213 |
Ltac fold_sub f := |
| 214 |
match goal with |
| 215 |
| [ |- ?T ] => |
| 216 |
match T with |
| 217 |
context C [ @Fix_sub _ _ _ _ _ ?arg ] => |
| 218 |
let app := context C [ f arg ] in |
| 219 |
change app |
| 220 |
end |
| 221 |
end. |