2014-12-01 04:34:12 +00:00
|
|
|
/-
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
-/
|
|
|
|
prelude
|
|
|
|
import init.relation
|
2014-11-06 22:49:53 +00:00
|
|
|
|
|
|
|
inductive acc {A : Type} (R : A → A → Prop) : A → Prop :=
|
|
|
|
intro : ∀x, (∀ y, R y x → acc R y) → acc R x
|
|
|
|
|
2014-11-07 18:18:24 +00:00
|
|
|
namespace acc
|
|
|
|
variables {A : Type} {R : A → A → Prop}
|
|
|
|
|
|
|
|
definition inv {x y : A} (H₁ : acc R x) (H₂ : R y x) : acc R y :=
|
|
|
|
acc.rec_on H₁ (λ x₁ ac₁ iH H₂, ac₁ y H₂) H₂
|
|
|
|
end acc
|
|
|
|
|
|
|
|
inductive well_founded [class] {A : Type} (R : A → A → Prop) : Prop :=
|
|
|
|
intro : (∀ a, acc R a) → well_founded R
|
2014-11-06 22:49:53 +00:00
|
|
|
|
2014-11-11 01:51:46 +00:00
|
|
|
namespace well_founded
|
2014-11-11 04:14:19 +00:00
|
|
|
definition apply [coercion] {A : Type} {R : A → A → Prop} (wf : well_founded R) : ∀a, acc R a :=
|
|
|
|
take a, well_founded.rec_on wf (λp, p) a
|
|
|
|
|
2014-11-07 18:18:24 +00:00
|
|
|
context
|
2014-11-06 22:49:53 +00:00
|
|
|
parameters {A : Type} {R : A → A → Prop}
|
|
|
|
infix `≺`:50 := R
|
|
|
|
|
2014-11-07 18:18:24 +00:00
|
|
|
hypothesis [Hwf : well_founded R]
|
2014-11-06 22:49:53 +00:00
|
|
|
|
2014-11-07 18:18:24 +00:00
|
|
|
theorem recursion {C : A → Type} (a : A) (H : Πx, (Πy, y ≺ x → C y) → C x) : C a :=
|
2014-11-06 22:49:53 +00:00
|
|
|
acc.rec_on (Hwf a) (λ x₁ ac₁ iH, H x₁ iH)
|
|
|
|
|
2014-11-22 21:10:38 +00:00
|
|
|
theorem induction {C : A → Prop} (a : A) (H : ∀x, (∀y, y ≺ x → C y) → C x) : C a :=
|
2014-11-07 18:18:24 +00:00
|
|
|
recursion a H
|
2014-11-06 22:49:53 +00:00
|
|
|
|
|
|
|
variable {C : A → Type}
|
|
|
|
variable F : Πx, (Πy, y ≺ x → C y) → C x
|
|
|
|
|
|
|
|
definition fix_F (x : A) (a : acc R x) : C x :=
|
|
|
|
acc.rec_on a (λ x₁ ac₁ iH, F x₁ iH)
|
|
|
|
|
|
|
|
theorem fix_F_eq (x : A) (r : acc R x) :
|
2014-11-07 18:18:24 +00:00
|
|
|
fix_F F x r = F x (λ (y : A) (p : y ≺ x), fix_F F y (acc.inv r p)) :=
|
|
|
|
have gen : Π r : acc R x, fix_F F x r = F x (λ (y : A) (p : y ≺ x), fix_F F y (acc.inv r p)), from
|
2014-11-06 22:49:53 +00:00
|
|
|
acc.rec_on r
|
|
|
|
(λ x₁ ac iH (r₁ : acc R x₁),
|
|
|
|
-- The proof is straightforward after we replace r₁ with acc.intro (to "unblock" evaluation).
|
|
|
|
calc fix_F F x₁ r₁
|
|
|
|
= fix_F F x₁ (acc.intro x₁ ac) : proof_irrel r₁
|
2014-11-07 18:18:24 +00:00
|
|
|
... = F x₁ (λ y ay, fix_F F y (acc.inv r₁ ay)) : rfl),
|
2014-11-06 22:49:53 +00:00
|
|
|
gen r
|
2014-11-07 18:18:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
variables {A : Type} {C : A → Type} {R : A → A → Prop}
|
|
|
|
|
|
|
|
-- Well-founded fixpoint
|
|
|
|
definition fix [Hwf : well_founded R] (F : Πx, (Πy, R y x → C y) → C x) (x : A) : C x :=
|
|
|
|
fix_F F x (Hwf x)
|
|
|
|
|
|
|
|
-- Well-founded fixpoint satisfies fixpoint equation
|
|
|
|
theorem fix_eq [Hwf : well_founded R] (F : Πx, (Πy, R y x → C y) → C x) (x : A) :
|
|
|
|
fix F x = F x (λy h, fix F y) :=
|
|
|
|
calc
|
|
|
|
-- The proof is straightforward, it just uses fix_F_eq and proof irrelevance
|
|
|
|
fix F x
|
|
|
|
= F x (λy h, fix_F F y (acc.inv (Hwf x) h)) : fix_F_eq F x (Hwf x)
|
|
|
|
... = F x (λy h, fix F y) : rfl -- proof irrelevance is used here
|
2014-11-06 22:49:53 +00:00
|
|
|
|
|
|
|
end well_founded
|
2014-11-11 01:51:46 +00:00
|
|
|
|
2014-11-11 04:14:19 +00:00
|
|
|
open well_founded
|
|
|
|
|
2014-11-11 01:51:46 +00:00
|
|
|
-- Empty relation is well-founded
|
2014-11-19 01:32:22 +00:00
|
|
|
definition empty.wf {A : Type} : well_founded empty_relation :=
|
2014-11-11 01:51:46 +00:00
|
|
|
well_founded.intro (λ (a : A),
|
|
|
|
acc.intro a (λ (b : A) (lt : false), false.rec _ lt))
|
|
|
|
|
|
|
|
-- Subrelation of a well-founded relation is well-founded
|
2014-11-19 01:32:22 +00:00
|
|
|
namespace subrelation
|
2014-11-11 01:51:46 +00:00
|
|
|
context
|
|
|
|
parameters {A : Type} {R Q : A → A → Prop}
|
2014-11-19 01:32:22 +00:00
|
|
|
parameters (H₁ : subrelation Q R)
|
2014-11-11 01:51:46 +00:00
|
|
|
parameters (H₂ : well_founded R)
|
|
|
|
|
|
|
|
definition accessible {a : A} (ac : acc R a) : acc Q a :=
|
|
|
|
acc.rec_on ac
|
|
|
|
(λ (x : A) (ax : _) (iH : ∀ (y : A), R y x → acc Q y),
|
|
|
|
acc.intro x (λ (y : A) (lt : Q y x), iH y (H₁ lt)))
|
|
|
|
|
|
|
|
definition wf : well_founded Q :=
|
|
|
|
well_founded.intro (λ a, accessible (H₂ a))
|
|
|
|
end
|
2014-11-19 01:32:22 +00:00
|
|
|
end subrelation
|
2014-11-11 01:51:46 +00:00
|
|
|
|
|
|
|
-- The inverse image of a well-founded relation is well-founded
|
|
|
|
namespace inv_image
|
|
|
|
context
|
|
|
|
parameters {A B : Type} {R : B → B → Prop}
|
|
|
|
parameters (f : A → B)
|
|
|
|
parameters (H : well_founded R)
|
|
|
|
|
|
|
|
definition accessible {a : A} (ac : acc R (f a)) : acc (inv_image R f) a :=
|
|
|
|
have gen : ∀x, f x = f a → acc (inv_image R f) x, from
|
|
|
|
acc.rec_on ac
|
2014-11-11 05:19:38 +00:00
|
|
|
(λx acx (iH : ∀y, R y x → (∀z, f z = y → acc (inv_image R f) z))
|
|
|
|
(z : A) (eq₁ : f z = x),
|
2014-11-11 01:51:46 +00:00
|
|
|
acc.intro z (λ (y : A) (lt : R (f y) (f z)),
|
|
|
|
iH (f y) (eq.rec_on eq₁ lt) y rfl)),
|
|
|
|
gen a rfl
|
|
|
|
|
|
|
|
definition wf : well_founded (inv_image R f) :=
|
|
|
|
well_founded.intro (λ a, accessible (H (f a)))
|
|
|
|
end
|
|
|
|
end inv_image
|
2014-11-11 05:06:15 +00:00
|
|
|
|
|
|
|
-- The transitive closure of a well-founded relation is well-founded
|
|
|
|
namespace tc
|
|
|
|
context
|
|
|
|
parameters {A : Type} {R : A → A → Prop}
|
2014-11-15 01:37:05 +00:00
|
|
|
notation `R⁺` := tc R
|
2014-11-11 05:06:15 +00:00
|
|
|
|
|
|
|
definition accessible {z} (ac: acc R z) : acc R⁺ z :=
|
|
|
|
acc.rec_on ac
|
2014-11-11 05:19:38 +00:00
|
|
|
(λ x acx (iH : ∀y, R y x → acc R⁺ y),
|
2014-11-11 05:06:15 +00:00
|
|
|
acc.intro x (λ (y : A) (lt : R⁺ y x),
|
2014-11-11 05:19:38 +00:00
|
|
|
have gen : x = x → acc R⁺ y, from
|
2014-11-11 05:06:15 +00:00
|
|
|
tc.rec_on lt
|
|
|
|
(λa b (H : R a b) (Heq : b = x),
|
|
|
|
iH a (eq.rec_on Heq H))
|
|
|
|
(λa b c (H₁ : R⁺ a b) (H₂ : R⁺ b c)
|
2014-11-11 05:19:38 +00:00
|
|
|
(iH₁ : b = x → acc R⁺ a)
|
|
|
|
(iH₂ : c = x → acc R⁺ b)
|
2014-11-11 05:06:15 +00:00
|
|
|
(Heq : c = x),
|
|
|
|
acc.inv (iH₂ Heq) H₁),
|
|
|
|
gen rfl))
|
|
|
|
|
|
|
|
definition wf (H : well_founded R) : well_founded R⁺ :=
|
|
|
|
well_founded.intro (λ a, accessible (H a))
|
|
|
|
|
|
|
|
end
|
|
|
|
end tc
|