| 1 |
(** * Miscellaneous Well-Foundedness Facts *) |
| 2 |
From Coq Require Import Setoid Program. |
| 3 |
From Coq.Program Require Import Wf. |
| 4 |
From Coq Require Import Wf_nat Morphisms. |
| 5 |
From Coq.Init Require Import Wf. |
| 6 |
From Coq Require Import SetoidList. |
| 7 |
From Coq Require Import PeanoNat. |
| 8 |
Require Export Crypto.Util.FixCoqMistakes. |
| 9 |
From Coq Require EqNat. |
| 10 |
|
| 11 |
Local Set Implicit Arguments. |
| 12 |
Local Unset Uniform Inductive Parameters. |
| 13 |
|
| 14 |
Scheme Induction for Acc Sort Prop. |
| 15 |
Scheme Induction for Acc Sort Set. |
| 16 |
Scheme Induction for Acc Sort Type. |
| 17 |
|
| 18 |
Section wf. |
| 19 |
Section wrap_wf. |
| 20 |
Context {A R} (Rwf : @well_founded A R). |
| 21 |
|
| 22 |
Definition lt_wf_idx_step |
| 23 |
(lt_wf_idx : nat -> well_founded R) |
| 24 |
(n : nat) |
| 25 |
: well_founded R. |
| 26 |
Proof. |
| 27 |
destruct n. |
| 28 |
{ clear -Rwf; apply Rwf. } |
| 29 |
{ constructor; intros; apply lt_wf_idx; assumption. } |
| 30 |
Defined. |
| 31 |
|
| 32 |
Fixpoint lt_wf_idx (n : nat) : well_founded R |
| 33 |
:= lt_wf_idx_step (@lt_wf_idx) n. |
| 34 |
End wrap_wf. |
| 35 |
|
| 36 |
Section rel_hetero. |
| 37 |
Local Notation "R ==> S" |
| 38 |
:= (@respectful_hetero _ _ _ _ R (fun _ _ => S)) |
| 39 |
: signature_scope. |
| 40 |
Local Notation Proper S R |
| 41 |
:= (S%signature R R) (only parsing). |
| 42 |
|
| 43 |
Context {A A'} |
| 44 |
(RA : A -> A' -> Prop) |
| 45 |
(P : A -> Type) (P' : A' -> Type) |
| 46 |
(RP : forall a a', RA a a' -> P a -> P' a' -> Prop) |
| 47 |
{R : relation A} {R' : relation A'} |
| 48 |
(HR : (RA ==> RA ==> Basics.impl)%signature R R') |
| 49 |
(Rwf : well_founded R) (Rwf' : well_founded R') |
| 50 |
(F : forall x, (forall y, R y x -> P y) -> P x) |
| 51 |
(F' : forall x, (forall y, R' y x -> P' y) -> P' x) |
| 52 |
(HF : forall a a' (r : RA a a') |
| 53 |
(f := (fun (y : A) (_ : R y a) => Fix Rwf P F y)) |
| 54 |
(f' := (fun (y' : A') (_ : R' y' a') => Fix Rwf' P' F' y')) |
| 55 |
(Hf : forall y y' (ry : RA y y') (r : R y a) (r' : R' y' a'), |
| 56 |
RP _ _ ry (f y r) (f' y' r')), |
| 57 |
RP a a' r (F a f) (F' a' f')) |
| 58 |
(F_ext : forall x f g, |
| 59 |
(forall y p, f y p = g y p) -> F x f = F x g) |
| 60 |
(F'_ext : forall x f g, |
| 61 |
(forall y p, f y p = g y p) -> F' x f = F' x g). |
| 62 |
|
| 63 |
Lemma Fix_Proper_hetero |
| 64 |
{a a'} |
| 65 |
(Ha : RA a a') |
| 66 |
: @RP a a' Ha (Fix Rwf P F a) (Fix Rwf' P' F' a'). |
| 67 |
Proof. |
| 68 |
revert a' Ha. |
| 69 |
induction (Rwf a) as [a Hlt IHwf]. |
| 70 |
intros a' Ha. |
| 71 |
rewrite (Fix_eq Rwf), (Fix_eq Rwf') by assumption. |
| 72 |
apply HF; intros. |
| 73 |
apply IHwf; assumption. |
| 74 |
Qed. |
| 75 |
End rel_hetero. |
| 76 |
|
| 77 |
Global Instance well_founded_subrelation {A} |
| 78 |
: Proper (flip subrelation ==> impl) (@well_founded A). |
| 79 |
Proof. |
| 80 |
intros R R' HR Rwf a. |
| 81 |
induction (Rwf a) as [a Ra R'a]. |
| 82 |
constructor; intros y Hy. |
| 83 |
apply R'a, HR, Hy. |
| 84 |
Defined. |
| 85 |
|
| 86 |
Fixpoint no_wf_cycle_helper {A} {R : relation A} x y |
| 87 |
(H0 : R x y) (H1 : R y x) (wf : Acc R x) |
| 88 |
: False |
| 89 |
:= match wf with |
| 90 |
| Acc_intro f => @no_wf_cycle_helper A R y x H1 H0 (f _ H1) |
| 91 |
end. |
| 92 |
|
| 93 |
Global Instance no_wf_cycle {A R} (Rwf : @well_founded A R) : Asymmetric R. |
| 94 |
Proof. |
| 95 |
intros x y H0 H1. |
| 96 |
specialize (Rwf x). |
| 97 |
eapply no_wf_cycle_helper; [ .. | eassumption ]; eassumption. |
| 98 |
Qed. |
| 99 |
|
| 100 |
Inductive RT_closure {A} (R : relation A) : relation A := |
| 101 |
| cinject {x y} : R x y -> RT_closure R x y |
| 102 |
| crefl {x} : RT_closure R x x |
| 103 |
| ctrans {x y z} : RT_closure R x y -> RT_closure R y z -> RT_closure R x z. |
| 104 |
|
| 105 |
Fixpoint Acc_subrelation {A} (R1 R2 : relation A) (v : A) (Hacc : Acc R1 v) |
| 106 |
(HR : forall x y, RT_closure R2 y v -> R2 x y -> R1 x y) {struct Hacc} |
| 107 |
: Acc R2 v. |
| 108 |
Proof. |
| 109 |
destruct Hacc as [Hacc]. |
| 110 |
constructor. |
| 111 |
intros y Hy. |
| 112 |
specialize (fun pf => @Acc_subrelation A R1 R2 y (Hacc y pf)). |
| 113 |
specialize (@Acc_subrelation (HR _ _ (@crefl _ _ _) Hy)). |
| 114 |
apply Acc_subrelation; clear -HR Hy. |
| 115 |
intros x y' Hxy Hr2. |
| 116 |
apply HR; clear HR; [ | assumption ]. |
| 117 |
clear -Hy Hxy. |
| 118 |
eapply ctrans; [ eassumption | eapply cinject; eassumption ]. |
| 119 |
Defined. |
| 120 |
|
| 121 |
Section wf_acc_of. |
| 122 |
Context A (RA : relation A). |
| 123 |
|
| 124 |
Definition well_founded_acc_relation_of |
| 125 |
B (f : B -> A) (fA : forall b, Acc RA (f b)) |
| 126 |
: relation B |
| 127 |
:= fun b0 b1 => match fA b1 with |
| 128 |
| Acc_intro fAb1 => exists pf, |
| 129 |
fAb1 (f b0) pf = fA b0 |
| 130 |
end. |
| 131 |
|
| 132 |
|
| 133 |
Lemma well_founded_well_founded_acc_relation_of B f fA |
| 134 |
: well_founded (@well_founded_acc_relation_of B f fA). |
| 135 |
Proof. |
| 136 |
intro b. |
| 137 |
constructor. |
| 138 |
unfold well_founded_acc_relation_of. |
| 139 |
generalize (fA b). |
| 140 |
generalize (f b). |
| 141 |
lazymatch goal with |
| 142 |
| [ |- forall a' wf', @?P a' wf' ] |
| 143 |
=> apply (@Acc_ind_dep A RA P) |
| 144 |
end. |
| 145 |
intros a Ha IH y [pf Hy]. |
| 146 |
constructor. |
| 147 |
intros z Hz. |
| 148 |
specialize (IH (f y) pf z). |
| 149 |
apply IH; clear IH. |
| 150 |
destruct Hy. |
| 151 |
apply Hz. |
| 152 |
Defined. |
| 153 |
|
| 154 |
Fixpoint Acc_RA_of B (f : B -> A) b (ac : Acc RA (f b)) |
| 155 |
: Acc (fun x y => RA (f x) (f y)) b. |
| 156 |
Proof. |
| 157 |
refine match ac with |
| 158 |
| Acc_intro fg => Acc_intro _ (fun y Ry => @Acc_RA_of _ _ _ (fg _ _)) |
| 159 |
end. |
| 160 |
assumption. |
| 161 |
Defined. |
| 162 |
|
| 163 |
Lemma well_founded_RA_of B (f : B -> A) (wf_A : well_founded RA) |
| 164 |
: well_founded (fun x y => RA (f x) (f y)). |
| 165 |
Proof. |
| 166 |
intro a. |
| 167 |
apply Acc_RA_of, wf_A. |
| 168 |
Defined. |
| 169 |
End wf_acc_of. |
| 170 |
|
| 171 |
Section wf_acc_of_option. |
| 172 |
Context A (RA : relation A). |
| 173 |
|
| 174 |
Definition well_founded_acc_relation_of_opt |
| 175 |
B (f : B -> option A) (fA : forall b, match f b with |
| 176 |
| Some fb => Acc RA fb |
| 177 |
| None => True |
| 178 |
end) |
| 179 |
: relation B |
| 180 |
:= fun b0 b1 |
| 181 |
=> match f b1 as fb1 return match fb1 with |
| 182 |
| Some fb => Acc RA fb |
| 183 |
| None => True |
| 184 |
end -> _ |
| 185 |
with |
| 186 |
| Some fb1 |
| 187 |
=> fun fAb |
| 188 |
=> match fAb with |
| 189 |
| Acc_intro fAb1 |
| 190 |
=> match f b0 as fb0 return match fb0 with |
| 191 |
| Some fb => Acc RA fb |
| 192 |
| None => True |
| 193 |
end -> _ |
| 194 |
with |
| 195 |
| Some fb0 |
| 196 |
=> fun fAb0 => exists pf, |
| 197 |
fAb1 fb0 pf = fAb0 |
| 198 |
| None => fun _ => False |
| 199 |
end (fA b0) |
| 200 |
end |
| 201 |
| None => fun _ => False |
| 202 |
end (fA b1). |
| 203 |
|
| 204 |
Lemma well_founded_well_founded_acc_relation_of_opt B f fA |
| 205 |
: well_founded (@well_founded_acc_relation_of_opt B f fA). |
| 206 |
Proof. |
| 207 |
intro b. |
| 208 |
constructor. |
| 209 |
unfold well_founded_acc_relation_of_opt. |
| 210 |
generalize (fA b). |
| 211 |
generalize (f b). |
| 212 |
intros [fb|]. |
| 213 |
{ revert fb. |
| 214 |
lazymatch goal with |
| 215 |
| [ |- forall a' wf', @?P a' wf' ] |
| 216 |
=> apply (@Acc_ind_dep A RA P) |
| 217 |
end. |
| 218 |
intros a Ha IH y. |
| 219 |
constructor. |
| 220 |
generalize dependent (fA y). |
| 221 |
destruct (f y) as [fy|] eqn:Hfy. |
| 222 |
{ intros y0 [pf Hy]. |
| 223 |
intros z Hz. |
| 224 |
specialize (IH fy pf z). |
| 225 |
apply IH; clear IH. |
| 226 |
destruct Hy. |
| 227 |
apply Hz. } |
| 228 |
{ intros ? []. } } |
| 229 |
{ intros ?? []. } |
| 230 |
Defined. |
| 231 |
|
| 232 |
Fixpoint Acc_RA_of_opt B (f : B -> option A) b v (Heq : f b = Some v) |
| 233 |
(ac : Acc RA v) {struct ac} |
| 234 |
: Acc (fun x y => match f x, f y with |
| 235 |
| Some fx, Some fy => RA fx fy |
| 236 |
| _, _ => False |
| 237 |
end) b. |
| 238 |
Proof. |
| 239 |
destruct ac as [fg]. |
| 240 |
constructor. |
| 241 |
intros y Ry. |
| 242 |
specialize (fun v H Rv => Acc_RA_of_opt B f y v H (fg _ Rv)); clear fg. |
| 243 |
destruct (f y) as [fy|] eqn:Hfy. |
| 244 |
{ specialize (Acc_RA_of_opt _ eq_refl). |
| 245 |
destruct (f b) as [fb|] eqn:Hfb. |
| 246 |
{ inversion Heq; clear Heq; subst. |
| 247 |
specialize (Acc_RA_of_opt Ry). |
| 248 |
assumption. } |
| 249 |
{ destruct Ry. } } |
| 250 |
{ destruct Ry. } |
| 251 |
Defined. |
| 252 |
|
| 253 |
Lemma well_founded_RA_of_opt B (f : B -> option A) (wf_A : well_founded RA) |
| 254 |
: well_founded (fun x y => match f x, f y with |
| 255 |
| Some fx, Some fy => RA fx fy |
| 256 |
| _, _ => False |
| 257 |
end). |
| 258 |
Proof. |
| 259 |
intro a. |
| 260 |
destruct (f a) eqn:H. |
| 261 |
{ eapply Acc_RA_of_opt, wf_A; eassumption. } |
| 262 |
{ constructor. |
| 263 |
intro y. |
| 264 |
destruct (f y); [ rewrite H | ]; intros []. } |
| 265 |
Defined. |
| 266 |
End wf_acc_of_option. |
| 267 |
|
| 268 |
Section wf_prodA. |
| 269 |
Context A B (eqA RA : relation A) (RB : relation B). |
| 270 |
|
| 271 |
Definition prod_relationA : relation (A * B) |
| 272 |
:= fun ab a'b' => |
| 273 |
RA (fst ab) (fst a'b') \/ (eqA (fst a'b') (fst ab) /\ RB (snd ab) (snd a'b')). |
| 274 |
|
| 275 |
Context (prod_relationA_Proper |
| 276 |
: forall b'', Proper (eqA ==> impl) (fun a => Acc prod_relationA (a, b''))). |
| 277 |
|
| 278 |
Global Instance prod_relationA_Proper_from_Equivalence |
| 279 |
(RA_Proper : Proper (eq ==> eqA ==> flip impl) RA) |
| 280 |
(eqA_Proper : Proper (eqA ==> eq ==> flip impl) eqA) |
| 281 |
: forall b'', Proper (eqA ==> impl) (fun a => Acc prod_relationA (a, b'')). |
| 282 |
Proof. |
| 283 |
intros b'' a b H wf. |
| 284 |
revert dependent b. |
| 285 |
destruct wf as [f]. |
| 286 |
constructor. |
| 287 |
intros y Hlt; apply f. |
| 288 |
unfold prod_relationA in Hlt |- *; simpl in *. |
| 289 |
destruct Hlt as [Hlt|[Hlt0 Hlt1]]; [ left | right; split; try assumption ]. |
| 290 |
{ eapply RA_Proper; [ reflexivity | exact H | exact Hlt ]. } |
| 291 |
{ eapply eqA_Proper; [ exact H | reflexivity | exact Hlt0 ]. } |
| 292 |
Defined. |
| 293 |
|
| 294 |
Section step. |
| 295 |
Context (well_founded_prod_relationA_helper |
| 296 |
: forall a b (wf_A : Acc RA a) (wf_B : well_founded RB), |
| 297 |
Acc prod_relationA (a, b)). |
| 298 |
|
| 299 |
Definition well_founded_prod_relationA_helper_step |
| 300 |
a b |
| 301 |
(wf_A : Acc RA a) (wf_B : well_founded RB) |
| 302 |
(wf_B_rec : forall (fa : forall y : A, RA y a -> Acc RA y) b' (wf_B' : Acc RB b'), Acc prod_relationA (a, b')) |
| 303 |
: Acc prod_relationA (a, b) |
| 304 |
:= match wf_A with |
| 305 |
| Acc_intro fa => @wf_B_rec fa b (wf_B b) |
| 306 |
end. |
| 307 |
Definition wf_B_rec_step |
| 308 |
(wf_B : well_founded RB) |
| 309 |
a (fa : forall y : A, RA y a -> Acc RA y) |
| 310 |
(wf_B_rec : forall b' (wf_B' : Acc RB b'), Acc prod_relationA (a, b')) |
| 311 |
b' (wf_B' : Acc RB b') |
| 312 |
: Acc prod_relationA (a, b'). |
| 313 |
Proof. |
| 314 |
refine (Acc_intro _ _). |
| 315 |
intros [a'' b''] [pf'|[pfa pfb]]. |
| 316 |
{ refine (@well_founded_prod_relationA_helper |
| 317 |
_ _ |
| 318 |
(fa _ pf') |
| 319 |
wf_B). } |
| 320 |
{ refine match wf_B' with |
| 321 |
| Acc_intro fb => @prod_relationA_Proper |
| 322 |
_ _ _ pfa |
| 323 |
(@wf_B_rec _ (fb _ pfb)) |
| 324 |
end. } |
| 325 |
Defined. |
| 326 |
End step. |
| 327 |
|
| 328 |
Fixpoint well_founded_prod_relationA_helper |
| 329 |
a b |
| 330 |
(wf_A : Acc RA a) (wf_B : well_founded RB) {struct wf_A} |
| 331 |
: Acc prod_relationA (a, b) |
| 332 |
:= @well_founded_prod_relationA_helper_step |
| 333 |
a b wf_A wf_B |
| 334 |
(fun fa |
| 335 |
=> fix wf_B_rec b' (wf_B' : Acc RB b') : Acc prod_relationA (a, b') |
| 336 |
:= @wf_B_rec_step |
| 337 |
(@well_founded_prod_relationA_helper) |
| 338 |
wf_B |
| 339 |
a fa (@wf_B_rec) |
| 340 |
b' wf_B'). |
| 341 |
|
| 342 |
Definition well_founded_prod_relationA : well_founded RA -> well_founded RB -> well_founded prod_relationA. |
| 343 |
Proof. |
| 344 |
intros wf_A wf_B [a b]; hnf in *. |
| 345 |
apply well_founded_prod_relationA_helper; auto. |
| 346 |
Defined. |
| 347 |
End wf_prodA. |
| 348 |
|
| 349 |
Section wf_prod. |
| 350 |
Context A B (RA : relation A) (RB : relation B). |
| 351 |
|
| 352 |
Definition prod_relation : relation (A * B) |
| 353 |
:= Eval cbv [prod_relationA] in prod_relationA eq RA RB. |
| 354 |
|
| 355 |
Definition well_founded_prod_relation : well_founded RA -> well_founded RB -> well_founded prod_relation. |
| 356 |
Proof. |
| 357 |
apply well_founded_prod_relationA. |
| 358 |
intros b'' a b H H'. |
| 359 |
induction H. |
| 360 |
exact H'. |
| 361 |
Defined. |
| 362 |
End wf_prod. |
| 363 |
|
| 364 |
Section wf_sig. |
| 365 |
Context A B (RA : relation A) (RB : forall a : A, relation (B a)). |
| 366 |
|
| 367 |
Definition sigT_relation : relation (sigT B) |
| 368 |
:= fun ab a'b' => |
| 369 |
RA (projT1 ab) (projT1 a'b') \/ (exists pf : projT1 a'b' = projT1 ab, RB (projT2 ab) |
| 370 |
(eq_rect _ B (projT2 a'b') _ pf)). |
| 371 |
|
| 372 |
Fixpoint well_founded_sigT_relation_helper |
| 373 |
a b |
| 374 |
(wf_A : Acc RA a) (wf_B : forall a, well_founded (@RB a)) {struct wf_A} |
| 375 |
: Acc sigT_relation (existT _ a b). |
| 376 |
Proof. |
| 377 |
refine match wf_A with |
| 378 |
| Acc_intro fa => (fix wf_B_rec b' (wf_B' : Acc (@RB a) b') : Acc sigT_relation (existT _ a b') |
| 379 |
:= Acc_intro |
| 380 |
_ |
| 381 |
(fun ab => |
| 382 |
match ab as ab return sigT_relation ab (existT _ a b') -> Acc sigT_relation ab with |
| 383 |
| existT a'' b'' => |
| 384 |
fun pf => |
| 385 |
match pf with |
| 386 |
| or_introl pf' |
| 387 |
=> @well_founded_sigT_relation_helper |
| 388 |
_ _ |
| 389 |
(fa _ pf') |
| 390 |
wf_B |
| 391 |
| or_intror (ex_intro pfa pfb) |
| 392 |
=> match wf_B' with |
| 393 |
| Acc_intro fb |
| 394 |
=> _(*eq_rect |
| 395 |
_ |
| 396 |
(fun a'' => Acc sigT_relation (existT B a'' _(*b''*))) |
| 397 |
(wf_B_rec _ (fb _ _(*pfb*))) |
| 398 |
_ |
| 399 |
pfa*) |
| 400 |
end |
| 401 |
end |
| 402 |
end) |
| 403 |
) b (wf_B a b) |
| 404 |
end; |
| 405 |
simpl in *. |
| 406 |
destruct pfa; simpl in *. |
| 407 |
exact (wf_B_rec _ (fb _ pfb)). |
| 408 |
Defined. |
| 409 |
|
| 410 |
Definition well_founded_sigT_relation : well_founded RA |
| 411 |
-> (forall a, well_founded (@RB a)) |
| 412 |
-> well_founded sigT_relation. |
| 413 |
Proof. |
| 414 |
intros wf_A wf_B [a b]; hnf in *. |
| 415 |
apply well_founded_sigT_relation_helper; auto. |
| 416 |
Defined. |
| 417 |
End wf_sig. |
| 418 |
|
| 419 |
Section wf_projT1. |
| 420 |
Context A (B : A -> Type) (R : relation A). |
| 421 |
|
| 422 |
Definition projT1_relation : relation (sigT B) |
| 423 |
:= fun ab a'b' => |
| 424 |
R (projT1 ab) (projT1 a'b'). |
| 425 |
|
| 426 |
Definition well_founded_projT1_relation : well_founded R -> well_founded projT1_relation. |
| 427 |
Proof. |
| 428 |
intros wf [a b]; hnf in *. |
| 429 |
induction (wf a) as [a H IH]. |
| 430 |
constructor. |
| 431 |
intros y r. |
| 432 |
specialize (IH _ r (projT2 y)). |
| 433 |
destruct y. |
| 434 |
exact IH. |
| 435 |
Defined. |
| 436 |
End wf_projT1. |
| 437 |
|
| 438 |
Section wf_iterated_prod_ofA. |
| 439 |
Context A (eqA R : relation A) (Rwf : well_founded R) |
| 440 |
(RA_Proper : Proper (eq ==> eqA ==> flip impl) R) |
| 441 |
(eqA_Proper : Proper (eqA ==> eq ==> flip impl) eqA). |
| 442 |
|
| 443 |
Fixpoint iterated_prod (n : nat) : Type |
| 444 |
:= match n with |
| 445 |
| 0 => unit |
| 446 |
| S n' => A * iterated_prod n' |
| 447 |
end%type. |
| 448 |
|
| 449 |
Fixpoint iterated_prod_relationA {n} : relation (iterated_prod n) |
| 450 |
:= match n return relation (iterated_prod n) with |
| 451 |
| 0 => fun _ _ => False |
| 452 |
| S n' => prod_relationA eqA R (@iterated_prod_relationA n') |
| 453 |
end. |
| 454 |
|
| 455 |
Fixpoint nat_eq_transfer (P : nat -> Type) (n m : nat) : P n -> (P m) + (Nat.eqb n m = false) |
| 456 |
:= match n, m return P n -> (P m) + (Nat.eqb n m = false) with |
| 457 |
| 0, 0 => fun x => inl x |
| 458 |
| S n', S m' => @nat_eq_transfer (fun v => P (S v)) n' m' |
| 459 |
| _, _ => fun _ => inr eq_refl |
| 460 |
end. |
| 461 |
|
| 462 |
Fixpoint nat_eq_transfer_refl (P : nat -> Type) (n : nat) : forall v : P n, nat_eq_transfer P n n v = inl v |
| 463 |
:= match n return forall v : P n, nat_eq_transfer P n n v = inl v with |
| 464 |
| 0 => fun v => eq_refl |
| 465 |
| S n' => @nat_eq_transfer_refl (fun k => P (S k)) n' |
| 466 |
end. |
| 467 |
|
| 468 |
Fixpoint nat_eq_transfer_neq (P : nat -> Type) (n m : nat) |
| 469 |
: forall v : P n, (if Nat.eqb n m as b return ((P m) + (b = false)) -> Prop |
| 470 |
then fun _ => True |
| 471 |
else fun v => v = inr eq_refl) |
| 472 |
(nat_eq_transfer P n m v) |
| 473 |
:= match n, m return forall v : P n, (if Nat.eqb n m as b return ((P m) + (b = false)) -> Prop |
| 474 |
then fun _ => True |
| 475 |
else fun v => v = inr eq_refl) |
| 476 |
(nat_eq_transfer P n m v) |
| 477 |
with |
| 478 |
| 0, 0 => fun _ => I |
| 479 |
| S n', S m' => @nat_eq_transfer_neq (fun v => P (S v)) n' m' |
| 480 |
| _, _ => fun _ => eq_refl |
| 481 |
end. |
| 482 |
|
| 483 |
Definition iterated_prod_relationA_of |
| 484 |
B (sz : B -> nat) (f : forall b, iterated_prod (sz b)) |
| 485 |
: relation B |
| 486 |
:= fun x y => match nat_eq_transfer _ (sz x) (sz y) (f x) with |
| 487 |
| inl fx => iterated_prod_relationA fx (f y) |
| 488 |
| inr _ => False |
| 489 |
end. |
| 490 |
|
| 491 |
Definition iterated_prod_relationA_of_opt |
| 492 |
B (sz : nat) (f : B -> option (iterated_prod sz)) |
| 493 |
: relation B |
| 494 |
:= fun x y => match f x, f y with |
| 495 |
| Some fx, Some fy => iterated_prod_relationA fx fy |
| 496 |
| _, _ => False |
| 497 |
end. |
| 498 |
|
| 499 |
Lemma well_founded_iterated_prod_relationA {n} : well_founded (@iterated_prod_relationA n). |
| 500 |
Proof. |
| 501 |
induction n as [|n IHn]; simpl. |
| 502 |
{ constructor; intros ? []. } |
| 503 |
{ apply well_founded_prod_relationA; try exact _; assumption. } |
| 504 |
Defined. |
| 505 |
|
| 506 |
Lemma well_founded_iterated_prod_relationA_of_opt {B n f} : well_founded (@iterated_prod_relationA_of_opt B n f). |
| 507 |
Proof. |
| 508 |
unfold iterated_prod_relationA_of_opt. |
| 509 |
apply well_founded_RA_of_opt, well_founded_iterated_prod_relationA. |
| 510 |
Defined. |
| 511 |
|
| 512 |
Local Ltac handle_nat_eq_transfer |
| 513 |
:= repeat lazymatch goal with |
| 514 |
| [ |- forall n0 n1, @?P n0 n1 ] |
| 515 |
=> let n0' := fresh "n" in |
| 516 |
let n1' := fresh "n" in |
| 517 |
let H := fresh in |
| 518 |
let H' := fresh in |
| 519 |
intros n0' n1'; |
| 520 |
destruct (@nat_eq_transfer (P n0') n0' n1') as [H|H]; |
| 521 |
[ clear n1'; revert n0' |
| 522 |
| apply H |
| 523 |
| lazymatch goal with |
| 524 |
| [ |- context[@nat_eq_transfer iterated_prod n1' n0' _] ] |
| 525 |
=> pose proof (@nat_eq_transfer_neq iterated_prod n1' n0') as H'; |
| 526 |
cbv beta in *; |
| 527 |
generalize dependent (nat_eq_transfer iterated_prod n1' n0'); |
| 528 |
let Heq := fresh in |
| 529 |
destruct (Nat.eqb n1' n0') eqn:Heq; |
| 530 |
[ apply Nat.eqb_eq in Heq; subst; rewrite Nat.eqb_refl in H; |
| 531 |
exfalso; clear -H; congruence |
| 532 |
| ] |
| 533 |
| [ |- context[@nat_eq_transfer iterated_prod n0' n1' _] ] |
| 534 |
=> pose proof (@nat_eq_transfer_neq iterated_prod n0' n1') as H'; |
| 535 |
cbv beta in *; |
| 536 |
generalize dependent (nat_eq_transfer iterated_prod n0' n1'); |
| 537 |
rewrite H |
| 538 |
end |
| 539 |
] |
| 540 |
end; |
| 541 |
repeat match goal with |
| 542 |
| _ => reflexivity |
| 543 |
| [ H : False |- _ ] => exfalso; exact H |
| 544 |
| [ H : forall v, _ = inr _ |- _ ] => rewrite H |
| 545 |
| _ => intro |
| 546 |
end. |
| 547 |
|
| 548 |
Lemma RT_closure_same_size B (sz : B -> nat) (f : forall b, iterated_prod (sz b)) |
| 549 |
a b |
| 550 |
(H : RT_closure (iterated_prod_relationA_of sz f) a b) |
| 551 |
: sz a = sz b. |
| 552 |
Proof. |
| 553 |
induction H as [x y H | | ]. |
| 554 |
{ unfold iterated_prod_relationA_of in *. |
| 555 |
generalize dependent (f x). |
| 556 |
generalize dependent (f y). |
| 557 |
generalize dependent (sz x). |
| 558 |
generalize dependent (sz y). |
| 559 |
handle_nat_eq_transfer. } |
| 560 |
{ reflexivity. } |
| 561 |
{ etransitivity; eassumption. } |
| 562 |
Defined. |
| 563 |
|
| 564 |
Lemma well_founded_iterated_prod_relationA_of |
| 565 |
B (sz : B -> nat) (f : forall b, iterated_prod (sz b)) |
| 566 |
: well_founded (@iterated_prod_relationA_of B sz f). |
| 567 |
Proof. |
| 568 |
intro b. |
| 569 |
pose proof (@well_founded_RA_of_opt (iterated_prod (sz b)) iterated_prod_relationA B) as wf. |
| 570 |
specialize (wf (fun b' => match nat_eq_transfer _ (sz b') (sz b) (f b') with |
| 571 |
| inl v => Some v |
| 572 |
| inr _ => None |
| 573 |
end)). |
| 574 |
specialize (wf well_founded_iterated_prod_relationA). |
| 575 |
eapply Acc_subrelation; [ eapply wf | clear wf ]. |
| 576 |
intros x y H. |
| 577 |
apply RT_closure_same_size in H. |
| 578 |
unfold iterated_prod_relationA_of. |
| 579 |
generalize dependent (f b). |
| 580 |
generalize dependent (f x). |
| 581 |
generalize dependent (f y). |
| 582 |
generalize dependent (sz y). |
| 583 |
intros ??; subst. |
| 584 |
clear y. |
| 585 |
generalize dependent (sz b). |
| 586 |
generalize dependent (sz x). |
| 587 |
clear. |
| 588 |
handle_nat_eq_transfer. |
| 589 |
rewrite !nat_eq_transfer_refl in *. |
| 590 |
assumption. |
| 591 |
Defined. |
| 592 |
End wf_iterated_prod_ofA. |
| 593 |
|
| 594 |
Section wf_iterated_prod_of. |
| 595 |
Context A (R : relation A) (Rwf : well_founded R). |
| 596 |
|
| 597 |
Definition iterated_prod_relation {n} : relation (iterated_prod A n) |
| 598 |
:= iterated_prod_relationA eq R. |
| 599 |
|
| 600 |
Definition iterated_prod_relation_of |
| 601 |
: forall B (sz : B -> nat) (f : forall b, iterated_prod A (sz b)), |
| 602 |
relation B |
| 603 |
:= iterated_prod_relationA_of eq R. |
| 604 |
|
| 605 |
Definition iterated_prod_relation_of_opt |
| 606 |
: forall B (sz : nat) (f : B -> option (iterated_prod A sz)), |
| 607 |
relation B |
| 608 |
:= iterated_prod_relationA_of_opt eq R. |
| 609 |
|
| 610 |
Lemma well_founded_iterated_prod_relation {n} : well_founded (@iterated_prod_relation n). |
| 611 |
Proof. |
| 612 |
apply well_founded_iterated_prod_relationA; try exact _; assumption. |
| 613 |
Defined. |
| 614 |
|
| 615 |
Lemma well_founded_iterated_prod_relation_of_opt {B n f} : well_founded (@iterated_prod_relation_of_opt B n f). |
| 616 |
Proof. |
| 617 |
apply well_founded_iterated_prod_relationA_of_opt; try exact _; assumption. |
| 618 |
Defined. |
| 619 |
|
| 620 |
Lemma well_founded_iterated_prod_relation_of |
| 621 |
B (sz : B -> nat) (f : forall b, iterated_prod A (sz b)) |
| 622 |
: well_founded (@iterated_prod_relation_of B sz f). |
| 623 |
Proof. |
| 624 |
apply well_founded_iterated_prod_relationA_of; try exact _; assumption. |
| 625 |
Defined. |
| 626 |
End wf_iterated_prod_of. |
| 627 |
|
| 628 |
Section wf_functionsA. |
| 629 |
Context A (eqA R : relation A) |
| 630 |
(R_Proper : Proper (eq ==> eqA ==> flip impl) R) |
| 631 |
(R_Proper2 : Proper (eq ==> eqA ==> impl) R) |
| 632 |
(eqA_Proper : Proper (eqA ==> eq ==> flip impl) eqA) |
| 633 |
(eqA_Proper2 : Proper (eq ==> eqA ==> flip impl) eqA) |
| 634 |
(eqA_Symmetric : Symmetric eqA). |
| 635 |
|
| 636 |
Local Infix "<" := R. |
| 637 |
Local Notation "x <= y" := (eqA x y \/ x < y). |
| 638 |
|
| 639 |
Context (bot : A) (bot_bot : forall a, bot <= a) |
| 640 |
(Rwf : well_founded R) |
| 641 |
(B C : Type) |
| 642 |
(eqv : relation B) |
| 643 |
{Heqv : Reflexive eqv} |
| 644 |
(F : C -> (B -> A)) |
| 645 |
(F_Proper : forall a b k k', eqv k' k -> eqA (F a k) (F b k) -> eqA (F a k') (F b k')). |
| 646 |
|
| 647 |
Definition is_finite_forA' (ls : list B) (f : B -> A) |
| 648 |
:= forall x, ~SetoidList.InA eqv x ls -> eqA (f x) bot. |
| 649 |
|
| 650 |
Definition is_finite_forA (ls : list B) (c : C) := is_finite_forA' ls (F c). |
| 651 |
|
| 652 |
Definition function_relationA : relation (B -> A) |
| 653 |
:= fun f g => (forall x, f x <= g x) |
| 654 |
/\ exists x, ~(eqA (f x) (g x)). |
| 655 |
|
| 656 |
Definition iterated_prod_of_function_for (ls : list B) (f : B -> A) |
| 657 |
: iterated_prod A (List.length ls) |
| 658 |
:= list_rect (fun ls => iterated_prod A (List.length ls)) |
| 659 |
tt |
| 660 |
(fun x _ v => (f x, v)) |
| 661 |
ls. |
| 662 |
|
| 663 |
Global Instance is_finite_forA'_Proper_fr |
| 664 |
: Proper (eq ==> function_relationA ==> flip impl) is_finite_forA'. |
| 665 |
Proof. |
| 666 |
unfold is_finite_forA', function_relationA. |
| 667 |
intros ls g ?; subst g. |
| 668 |
intros f g [Hle Hne] Hfin x Hnin. |
| 669 |
specialize (Hle x); specialize (Hfin x Hnin). |
| 670 |
destruct Hle as [Heq|Hlt]; [ rewrite Heq; assumption | ]. |
| 671 |
specialize (bot_bot (f x)). |
| 672 |
destruct bot_bot as [Heq|Hlt']; [ symmetry; assumption | ]. |
| 673 |
rewrite Hfin in Hlt. |
| 674 |
clear -Hlt Hlt' Rwf. |
| 675 |
exfalso; eapply no_wf_cycle; eassumption. |
| 676 |
Qed. |
| 677 |
|
| 678 |
Global Instance is_finite_forA_Proper_fr |
| 679 |
: Proper (eq ==> (fun x y => function_relationA (F x) (F y)) ==> flip impl) is_finite_forA. |
| 680 |
Proof. |
| 681 |
unfold is_finite_forA. |
| 682 |
intros ??????. |
| 683 |
eapply is_finite_forA'_Proper_fr; eassumption. |
| 684 |
Qed. |
| 685 |
|
| 686 |
Global Instance is_finite_forA'_Proper_frc |
| 687 |
: Proper (eq ==> RT_closure function_relationA ==> flip impl) is_finite_forA'. |
| 688 |
Proof. |
| 689 |
intros ls ls' ? f g H Hfin. |
| 690 |
induction H as [ ?? H | | ]. |
| 691 |
{ eapply is_finite_forA'_Proper_fr; eassumption. } |
| 692 |
{ subst; assumption. } |
| 693 |
{ subst; eauto with nocore. } |
| 694 |
Qed. |
| 695 |
|
| 696 |
Global Instance is_finite_forA_Proper_frc |
| 697 |
: Proper (eq ==> (fun x y => RT_closure function_relationA (F x) (F y)) ==> flip impl) is_finite_forA. |
| 698 |
Proof. |
| 699 |
unfold is_finite_forA. |
| 700 |
intros ??????. |
| 701 |
eapply is_finite_forA'_Proper_frc; eassumption. |
| 702 |
Qed. |
| 703 |
|
| 704 |
Global Instance is_finite_forA_Proper_frc' |
| 705 |
: Proper (eq ==> RT_closure (fun x y => function_relationA (F x) (F y)) ==> flip impl) is_finite_forA. |
| 706 |
Proof. |
| 707 |
intros ls ls' ? f g H Hfin. |
| 708 |
induction H as [ ?? H | | ]. |
| 709 |
{ eapply is_finite_forA_Proper_fr; eassumption. } |
| 710 |
{ subst; assumption. } |
| 711 |
{ subst; eauto with nocore. } |
| 712 |
Qed. |
| 713 |
|
| 714 |
Lemma function_subrelationA ls c (H : is_finite_forA ls c) |
| 715 |
: forall x y : C, |
| 716 |
RT_closure (fun x0 y0 : C => function_relationA (F x0) (F y0)) y c -> |
| 717 |
function_relationA (F x) (F y) -> |
| 718 |
(fun x0 y0 : C => |
| 719 |
iterated_prod_relationA_of eqA R (fun _ : B -> A => length ls) |
| 720 |
(iterated_prod_of_function_for ls) (F x0) (F y0)) x y. |
| 721 |
Proof. |
| 722 |
intros g h Hhf Hgh; cbv beta. |
| 723 |
assert (Hfinh : is_finite_forA ls h) by (rewrite Hhf; assumption). |
| 724 |
assert (Hfing : is_finite_forA ls g) by (unfold is_finite_forA; rewrite Hgh; assumption). |
| 725 |
unfold iterated_prod_relationA_of, function_relationA, iterated_prod_of_function_for in *. |
| 726 |
rewrite nat_eq_transfer_refl. |
| 727 |
destruct Hgh as [Hle Hne]. |
| 728 |
destruct Hne as [b Hne]. |
| 729 |
assert (Hin : ~~SetoidList.InA eqv b ls). |
| 730 |
{ intro Hnin. |
| 731 |
rewrite Hfinh in Hne by assumption. |
| 732 |
eauto with nocore. } |
| 733 |
rewrite SetoidList.InA_alt in Hin. |
| 734 |
move ls at bottom. |
| 735 |
clear H Hfinh Hfing. |
| 736 |
induction ls as [|x xs IHxs]; |
| 737 |
[ |
| 738 |
| destruct (Hle x) as [Heqx|Hlex]; |
| 739 |
[ right; split; |
| 740 |
[ | apply IHxs; clear IHxs ] |
| 741 |
| left ] ]; |
| 742 |
simpl; unfold prod_relationA; simpl in *; |
| 743 |
repeat match goal with |
| 744 |
| _ => congruence |
| 745 |
| _ => tauto |
| 746 |
| [ H : ex _ |- _ ] => destruct H |
| 747 |
| [ H : and _ _ |- _ ] => destruct H |
| 748 |
| [ H : or _ _ |- _ ] => destruct H |
| 749 |
| _ => progress subst |
| 750 |
| _ => progress eauto |
| 751 |
| _ => solve [ exfalso; eauto ] |
| 752 |
| [ H : ~~(exists x, _ /\ False) |- _ ] |
| 753 |
=> exfalso; apply H; clear |
| 754 |
| _ => intro |
| 755 |
| [ |- and _ _ ] => split |
| 756 |
| [ H : ~~_ |- False ] => apply H; clear H |
| 757 |
end. |
| 758 |
Qed. |
| 759 |
|
| 760 |
Definition function_relationA_of_Acc ls (c : C) (H : is_finite_forA ls c) |
| 761 |
: Acc (fun x y => function_relationA (F x) (F y)) c. |
| 762 |
Proof. |
| 763 |
pose proof (@well_founded_RA_of |
| 764 |
_ _ _ F |
| 765 |
(@well_founded_iterated_prod_relationA_of |
| 766 |
_ eqA R Rwf _ _ _ (fun _ => List.length ls) (iterated_prod_of_function_for ls)) |
| 767 |
c) |
| 768 |
as wf. |
| 769 |
eapply Acc_subrelation; [ eexact wf | ]. |
| 770 |
apply function_subrelationA; assumption. |
| 771 |
Defined. |
| 772 |
End wf_functionsA. |
| 773 |
|
| 774 |
Section wf_functions. |
| 775 |
Context A (R : relation A). |
| 776 |
|
| 777 |
Local Infix "<" := R. |
| 778 |
Local Notation "x <= y" := (x = y \/ x < y). |
| 779 |
|
| 780 |
Context (bot : A) (bot_bot : forall a, bot <= a) |
| 781 |
(Rwf : well_founded R) |
| 782 |
{B : Type}. |
| 783 |
|
| 784 |
Definition function_relation : relation (B -> A) |
| 785 |
:= @function_relationA A eq R B. |
| 786 |
|
| 787 |
Definition is_finite_for (ls : list B) (f : B -> A) |
| 788 |
:= forall x, ~List.In x ls -> f x = bot. |
| 789 |
|
| 790 |
Lemma is_finite_for_iff ls f |
| 791 |
: is_finite_for ls f <-> @is_finite_forA A eq bot B (B -> A) eq (fun f => f) ls f. |
| 792 |
Proof. |
| 793 |
unfold is_finite_for, is_finite_forA, is_finite_forA'. |
| 794 |
setoid_rewrite InA_alt. |
| 795 |
split; intros H x H'; apply H; intro H''; apply H'. |
| 796 |
{ eexists; split; [ reflexivity | assumption ]. } |
| 797 |
{ destruct H'' as [? [? ?]]; subst; assumption. } |
| 798 |
Qed. |
| 799 |
|
| 800 |
Definition function_relation_Acc ls (f : B -> A) (H : is_finite_for ls f) |
| 801 |
: Acc function_relation f. |
| 802 |
Proof. |
| 803 |
rewrite is_finite_for_iff in H. |
| 804 |
apply function_relationA_of_Acc with (bot := bot) (eqv := eq) (ls := ls); |
| 805 |
try exact _; try assumption. |
| 806 |
intros; subst; assumption. |
| 807 |
Defined. |
| 808 |
End wf_functions. |
| 809 |
End wf. |