| 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 |
|
| 11 |
(** * This module proves the validity of |
| 12 |
- well-founded recursion (also known as course of values) |
| 13 |
- well-founded induction |
| 14 |
from a well-founded ordering on a given set *) |
| 15 |
|
| 16 |
Set Implicit Arguments. |
| 17 |
|
| 18 |
Require Import Notations. |
| 19 |
Require Import Ltac. |
| 20 |
Require Import Logic. |
| 21 |
Require Import Datatypes. |
| 22 |
|
| 23 |
(** Well-founded induction principle on [Prop] *) |
| 24 |
|
| 25 |
Section Well_founded. |
| 26 |
|
| 27 |
Variable A : Type. |
| 28 |
Variable R : A -> A -> Prop. |
| 29 |
|
| 30 |
(** The accessibility predicate is defined to be non-informative *) |
| 31 |
(** (Acc_rect is automatically defined because Acc is a singleton type) *) |
| 32 |
|
| 33 |
Inductive Acc (x: A) : Prop := |
| 34 |
Acc_intro : (forall y:A, R y x -> Acc y) -> Acc x. |
| 35 |
|
| 36 |
Register Acc as core.wf.acc. |
| 37 |
|
| 38 |
Lemma Acc_inv : forall x:A, Acc x -> forall y:A, R y x -> Acc y. |
| 39 |
destruct 1; trivial. |
| 40 |
Defined. |
| 41 |
|
| 42 |
Global Arguments Acc_inv [x] _ [y] _, [x] _ y _. |
| 43 |
Register Acc_inv as core.wf.acc_inv. |
| 44 |
|
| 45 |
(** A relation is well-founded if every element is accessible *) |
| 46 |
|
| 47 |
Definition well_founded := forall a:A, Acc a. |
| 48 |
|
| 49 |
Register well_founded as core.wf.well_founded. |
| 50 |
|
| 51 |
(** Well-founded induction on [Set] and [Prop] *) |
| 52 |
|
| 53 |
Hypothesis Rwf : well_founded. |
| 54 |
|
| 55 |
Theorem well_founded_induction_type : |
| 56 |
forall P:A -> Type, |
| 57 |
(forall x:A, (forall y:A, R y x -> P y) -> P x) -> forall a:A, P a. |
| 58 |
Proof. |
| 59 |
intros; apply Acc_rect; auto. |
| 60 |
Defined. |
| 61 |
|
| 62 |
Theorem well_founded_induction : |
| 63 |
forall P:A -> Set, |
| 64 |
(forall x:A, (forall y:A, R y x -> P y) -> P x) -> forall a:A, P a. |
| 65 |
Proof. |
| 66 |
exact (fun P:A -> Set => well_founded_induction_type P). |
| 67 |
Defined. |
| 68 |
|
| 69 |
Theorem well_founded_ind : |
| 70 |
forall P:A -> Prop, |
| 71 |
(forall x:A, (forall y:A, R y x -> P y) -> P x) -> forall a:A, P a. |
| 72 |
Proof. |
| 73 |
exact (fun P:A -> Prop => well_founded_induction_type P). |
| 74 |
Defined. |
| 75 |
|
| 76 |
(** Well-founded fixpoints *) |
| 77 |
|
| 78 |
Section FixPoint. |
| 79 |
|
| 80 |
Variable P : A -> Type. |
| 81 |
Variable F : forall x:A, (forall y:A, R y x -> P y) -> P x. |
| 82 |
|
| 83 |
Fixpoint Fix_F (x:A) (a:Acc x) : P x := |
| 84 |
F (fun (y:A) (h:R y x) => Fix_F (Acc_inv a h)). |
| 85 |
|
| 86 |
Scheme Acc_inv_dep := Induction for Acc Sort Prop. |
| 87 |
|
| 88 |
Lemma Fix_F_eq (x:A) (r:Acc x) : |
| 89 |
F (fun (y:A) (p:R y x) => Fix_F (x:=y) (Acc_inv r p)) = Fix_F (x:=x) r. |
| 90 |
Proof. |
| 91 |
destruct r using Acc_inv_dep; auto. |
| 92 |
Qed. |
| 93 |
|
| 94 |
Definition Fix (x:A) := Fix_F (Rwf x). |
| 95 |
|
| 96 |
Section FixPointGen. |
| 97 |
Hypothesis |
| 98 |
F_ext : |
| 99 |
forall (x:A) (f g:forall y:A, R y x -> P y), |
| 100 |
(forall (y:A) (p q:R y x), f y p = g y q) -> F f = F g. |
| 101 |
|
| 102 |
Lemma Fix_F_inv_gen : forall (x:A) (r s:Acc x), Fix_F r = Fix_F s. |
| 103 |
Proof. |
| 104 |
intro x; induction (Rwf x); intros r s. |
| 105 |
rewrite <- (Fix_F_eq r); rewrite <- (Fix_F_eq s); intros. |
| 106 |
apply F_ext; auto. |
| 107 |
Qed. |
| 108 |
|
| 109 |
Lemma Fix_eq_gen : forall x:A, Fix x = F (fun (y:A) (p:R y x) => Fix y). |
| 110 |
Proof. |
| 111 |
intro x; unfold Fix. |
| 112 |
rewrite <- Fix_F_eq. |
| 113 |
apply F_ext; intros. |
| 114 |
apply Fix_F_inv_gen. |
| 115 |
Qed. |
| 116 |
End FixPointGen. |
| 117 |
|
| 118 |
(** Proof that [well_founded_induction] satisfies the fixpoint equation. |
| 119 |
It requires an extra property of the functional *) |
| 120 |
|
| 121 |
Hypothesis |
| 122 |
F_ext : |
| 123 |
forall (x:A) (f g:forall y:A, R y x -> P y), |
| 124 |
(forall (y:A) (p:R y x), f y p = g y p) -> F f = F g. |
| 125 |
|
| 126 |
Lemma Fix_F_inv : forall (x:A) (r s:Acc x), Fix_F r = Fix_F s. |
| 127 |
Proof. |
| 128 |
auto using Fix_F_inv_gen. |
| 129 |
Qed. |
| 130 |
|
| 131 |
Lemma Fix_eq : forall x:A, Fix x = F (fun (y:A) (p:R y x) => Fix y). |
| 132 |
Proof. |
| 133 |
auto using Fix_eq_gen. |
| 134 |
Qed. |
| 135 |
|
| 136 |
End FixPoint. |
| 137 |
|
| 138 |
End Well_founded. |
| 139 |
|
| 140 |
(** Well-founded fixpoints over pairs *) |
| 141 |
|
| 142 |
Section Well_founded_2. |
| 143 |
|
| 144 |
Variables A B : Type. |
| 145 |
Variable R : A * B -> A * B -> Prop. |
| 146 |
|
| 147 |
Variable P : A -> B -> Type. |
| 148 |
|
| 149 |
Section FixPoint_2. |
| 150 |
|
| 151 |
Variable |
| 152 |
F : |
| 153 |
forall (x:A) (x':B), |
| 154 |
(forall (y:A) (y':B), R (y, y') (x, x') -> P y y') -> P x x'. |
| 155 |
|
| 156 |
Fixpoint Fix_F_2 (x:A) (x':B) (a:Acc R (x, x')) : P x x' := |
| 157 |
F |
| 158 |
(fun (y:A) (y':B) (h:R (y, y') (x, x')) => |
| 159 |
Fix_F_2 (x:=y) (x':=y') (Acc_inv a (y,y') h)). |
| 160 |
|
| 161 |
End FixPoint_2. |
| 162 |
|
| 163 |
Hypothesis Rwf : well_founded R. |
| 164 |
|
| 165 |
Theorem well_founded_induction_type_2 : |
| 166 |
(forall (x:A) (x':B), |
| 167 |
(forall (y:A) (y':B), R (y, y') (x, x') -> P y y') -> P x x') -> |
| 168 |
forall (a:A) (b:B), P a b. |
| 169 |
Proof. |
| 170 |
intros; apply Fix_F_2; auto. |
| 171 |
Defined. |
| 172 |
|
| 173 |
End Well_founded_2. |
| 174 |
|
| 175 |
Abbreviation Acc_iter := Fix_F (only parsing). (* compatibility *) |
| 176 |
Abbreviation Acc_iter_2 := Fix_F_2 (only parsing). (* compatibility *) |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
(* Added by Julien Forest on 13/11/20 |
| 181 |
|
| 182 |
This construction is originally by Georges Gonthier, see |
| 183 |
https://sympa.inria.fr/sympa/arc/coq-club/2007-07/msg00013.html *) |
| 184 |
|
| 185 |
Section Acc_generator. |
| 186 |
Variable A : Type. |
| 187 |
Variable R : A -> A -> Prop. |
| 188 |
|
| 189 |
(* *Lazily* add 2^n - 1 Acc_intro on top of wf. |
| 190 |
Needed for fast reductions using Function and Program Fixpoint |
| 191 |
and probably using Fix and Fix_F_2 |
| 192 |
*) |
| 193 |
Fixpoint Acc_intro_generator n (wf : well_founded R) := |
| 194 |
match n with |
| 195 |
| O => wf |
| 196 |
| S n => fun x => Acc_intro x (fun y _ => Acc_intro_generator n (Acc_intro_generator n wf) y) |
| 197 |
end. |
| 198 |
|
| 199 |
|
| 200 |
End Acc_generator. |