{-# OPTIONS --cubical-compatible --safe #-}
open import Equality
module Accessibility {e⁺} (eq : ∀ {a p} → Equality-with-J a p e⁺) where
open Derived-definitions-and-properties eq
open import Prelude
open import Extensionality eq
open import Function-universe eq hiding (id; _∘_)
open import H-level.Closure eq
import Nat eq as Nat
open import Preimage eq using (_⁻¹_)
private
variable
a b ℓ p q r r₁ r₂ : Level
A : Type a
P : A → Type p
x z : A
data Acc {A : Type a} (_<_ : A → A → Type r) (x : A) :
Type (a ⊔ r) where
acc : (∀ y → y < x → Acc _<_ y) → Acc _<_ x
Well-founded : {A : Type a} → (A → A → Type r) → Type (a ⊔ r)
Well-founded _<_ = ∀ x → Acc _<_ x
Acc-propositional :
{A : Type a} {_<_ : A → A → Type r} {x : A} →
Extensionality (a ⊔ r) (a ⊔ r) →
Is-proposition (Acc _<_ x)
Acc-propositional {a} {r} ext (acc f) (acc g) =
cong acc $
apply-ext (lower-extensionality r lzero ext) λ y →
apply-ext (lower-extensionality a lzero ext) λ y<x →
Acc-propositional ext (f y y<x) (g y y<x)
Well-founded-propositional :
{A : Type a} {_<_ : A → A → Type r} →
Extensionality (a ⊔ r) (a ⊔ r) →
Is-proposition (Well-founded _<_)
Well-founded-propositional {r} ext =
Π-closure (lower-extensionality r lzero ext) 1 λ _ →
Acc-propositional ext
Well-founded-⊥ : Well-founded (λ (_ _ : A) → ⊥ {ℓ = ℓ})
Well-founded-⊥ _ = acc λ _ ()
Well-founded-ℕ : Well-founded Nat._<_
Well-founded-ℕ m = acc (helper m)
where
helper : ∀ m n → n Nat.< m → Acc Nat._<_ n
helper zero n (Nat.≤-refl′ eq) = ⊥-elim (Nat.0≢+ (sym eq))
helper zero n (Nat.≤-step′ _ eq) = ⊥-elim (Nat.0≢+ (sym eq))
helper (suc m) n (Nat.≤-refl′ eq) =
subst (Acc Nat._<_) (sym (Nat.cancel-suc eq)) (Well-founded-ℕ m)
helper (suc m) n (Nat.≤-step′ p eq) =
helper m n (subst (n Nat.<_) (Nat.cancel-suc eq) p)
_<W_ :
{A : Type a} {P : A → Type p} →
W A P → W A P → Type (a ⊔ p)
x <W sup _ f = f ⁻¹ x
Well-founded-W : Well-founded {A = W A P} _<W_
Well-founded-W (sup _ f) = acc λ where
(sup x g) (y , eq) → subst (Acc _) eq (Well-founded-W (f y))
infix 4 _[_]⋆_
infixr 5 _∷_
data _[_]⋆_
{A : Type a}
(x : A)
(_<_ : A → A → Type r)
(y : A) :
Type (a ⊔ r) where
[_] : x < y → x [ _<_ ]⋆ y
_∷_ : x < z → z [ _<_ ]⋆ y → x [ _<_ ]⋆ y
Cycle : {A : Type a} → (A → A → Type r) → Type (a ⊔ r)
Cycle _<_ = ∃ λ x → x [ _<_ ]⋆ x
Acc-map :
{@0 A : Type a} {x : A}
{@0 _<₁_ : A → A → Type r₁}
{@0 _<₂_ : A → A → Type r₂} →
(∀ {x y} → x <₂ y → x <₁ y) →
Acc _<₁_ x → Acc _<₂_ x
Acc-map f (acc g) = acc λ y y<₂x → Acc-map f (g y (f y<₂x))
Well-founded-map :
{@0 A : Type a}
{@0 _<₁_ : A → A → Type r₁}
{@0 _<₂_ : A → A → Type r₂} →
(∀ {x y} → x <₂ y → x <₁ y) →
Well-founded _<₁_ → Well-founded _<₂_
Well-founded-map f wf x = Acc-map f (wf x)
Acc-on :
{@0 A : Type a} {@0 B : Type b} {f : A → B}
{@0 _<_ : B → B → Type r} {@0 x : A} →
Acc _<_ (f x) → Acc (_<_ on f) x
Acc-on {f} (acc g) = acc λ y fy<fx → Acc-on (g (f y) fy<fx)
Well-founded-on :
{@0 A : Type a} {@0 B : Type b} {f : A → B}
{@0 _<_ : B → B → Type r} →
Well-founded _<_ → Well-founded (_<_ on f)
Well-founded-on {f} wf x = Acc-on (wf (f x))
Acc-⋆ :
{@0 A : Type a} {@0 _<_ : A → A → Type r} {@0 x : A} →
Acc _<_ x → Acc _[ _<_ ]⋆_ x
Acc-⋆ {_<_} {x} (acc f) = acc helper
where
helper : ∀ y → y [ _<_ ]⋆ x → Acc _[ _<_ ]⋆_ y
helper y [ y<x ] = Acc-⋆ (f y y<x)
helper y (y<z ∷ z<⋆x) = case helper _ z<⋆x of λ where
(acc f) → f y [ y<z ]
Well-founded-⋆ :
{@0 A : Type a} {@0 _<_ : A → A → Type r} →
Well-founded _<_ →
Well-founded _[ _<_ ]⋆_
Well-founded-⋆ wf x = Acc-⋆ (wf x)
Acc-↑ :
∀ {A : Type a} {_<_ : A → A → Type r} {x} →
Acc _<_ x ↝[ a ⊔ ℓ ⊔ r ∣ a ⊔ ℓ ⊔ r ]
Acc (λ x y → ↑ ℓ (x < y)) x
Acc-↑ {a} {r} {ℓ} {_<_} =
generalise-ext?
(record { to = to; from = from })
(λ ext → to-from ext , from-to ext)
where
to : ∀ {@0 x} → Acc _<_ x → Acc (λ x y → ↑ ℓ (x < y)) x
to (acc f) = acc λ y y<x → to (f y (lower y<x))
from : ∀ {@0 x} → Acc (λ x y → ↑ ℓ (x < y)) x → Acc _<_ x
from (acc f) = acc λ y y<x → from (f y (lift y<x))
module _ (ext : Extensionality (a ⊔ ℓ ⊔ r) (a ⊔ ℓ ⊔ r)) where
to-from : (p : Acc (λ x y → ↑ ℓ (x < y)) x) → to (from p) ≡ p
to-from (acc f) =
cong acc $
apply-ext (lower-extensionality (ℓ ⊔ r) lzero ext) λ y →
apply-ext (lower-extensionality a lzero ext) λ y<x →
to-from (f y y<x)
from-to : (p : Acc _<_ x) → from (to p) ≡ p
from-to (acc f) =
cong acc $
apply-ext (lower-extensionality (ℓ ⊔ r) ℓ ext) λ y →
apply-ext (lower-extensionality (a ⊔ ℓ) ℓ ext) λ y<x →
from-to (f y y<x)
Well-founded-↑ :
∀ {A : Type a} {_<_ : A → A → Type r} →
Well-founded _<_ ↝[ a ⊔ ℓ ⊔ r ∣ a ⊔ ℓ ⊔ r ]
Well-founded (λ x y → ↑ ℓ (x < y))
Well-founded-↑ {r} {ℓ} {_<_} {k} ext =
(∀ x → Acc _<_ x) ↝⟨ (∀-cong (lower-extensionality? k (ℓ ⊔ r) lzero ext) λ _ → Acc-↑ ext) ⟩□
(∀ x → Acc (λ x y → ↑ ℓ (x < y)) x) □
<→¬-Acc :
{@0 A : Type a} {@0 _<_ : A → A → Type r} {x : A} →
x < x → ¬ Acc _<_ x
<→¬-Acc x<x (acc f) = <→¬-Acc x<x (f _ x<x)
<→¬-Well-founded :
{@0 A : Type a} {@0 _<_ : A → A → Type r} {x : A} →
x < x → ¬ Well-founded _<_
<→¬-Well-founded x<x wf = <→¬-Acc x<x (wf _)
Cycle→¬-Well-founded :
{@0 A : Type a} {@0 _<_ : A → A → Type r} →
Cycle _<_ → ¬ Well-founded _<_
Cycle→¬-Well-founded {_<_} (x , x<⋆x) =
$⟨ x<⋆x ⟩
x [ _<_ ]⋆ x →⟨ <→¬-Well-founded ⟩
¬ Well-founded _[ _<_ ]⋆_ →⟨ _∘ (λ wf → Well-founded-⋆ wf) ⟩□
¬ Well-founded _<_ □