| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Well-founded induction |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Induction.WellFounded where |
| 10 |
|
| 11 |
open import Data.Product.Base using (Σ; _,_; proj₁; proj₂) |
| 12 |
open import Function.Base using (_∘_; flip; _on_) |
| 13 |
open import Induction |
| 14 |
using (RecStruct; RecursorBuilder; Recursor; build |
| 15 |
; SubsetRecursorBuilder; SubsetRecursor; subsetBuild) |
| 16 |
open import Level using (Level; _⊔_) |
| 17 |
open import Relation.Binary.Core using (Rel) |
| 18 |
open import Relation.Binary.Definitions |
| 19 |
using (Symmetric; Asymmetric; Irreflexive |
| 20 |
; _Respects₂_; _Respectsʳ_; _Respects_) |
| 21 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl) |
| 22 |
open import Relation.Binary.Consequences using (asym⇒irr) |
| 23 |
open import Relation.Unary |
| 24 |
using (Pred; _⊆′_; _⊆_; _⇒_; Universal; IUniversal; Stable; Empty) |
| 25 |
open import Relation.Nullary.Negation.Core using (¬_) |
| 26 |
|
| 27 |
private |
| 28 |
variable |
| 29 |
a b ℓ ℓ₁ ℓ₂ r : Level |
| 30 |
A : Set a |
| 31 |
B : Set b |
| 32 |
|
| 33 |
------------------------------------------------------------------------ |
| 34 |
-- Definitions |
| 35 |
|
| 36 |
-- When using well-founded recursion you can recurse arbitrarily, as |
| 37 |
-- long as the arguments become smaller, and "smaller" is |
| 38 |
-- well-founded. |
| 39 |
|
| 40 |
WfRec : Rel A r → ∀ {ℓ} → RecStruct A ℓ _ |
| 41 |
WfRec _<_ P x = ∀ {y} → y < x → P y |
| 42 |
|
| 43 |
-- The accessibility predicate: x is accessible if everything which is |
| 44 |
-- smaller than x is also accessible (inductively). |
| 45 |
|
| 46 |
data Acc {A : Set a} (_<_ : Rel A ℓ) (x : A) : Set (a ⊔ ℓ) where |
| 47 |
acc : (rs : WfRec _<_ (Acc _<_) x) → Acc _<_ x |
| 48 |
|
| 49 |
-- The accessibility predicate encodes what it means to be |
| 50 |
-- well-founded; if all elements are accessible, then _<_ is |
| 51 |
-- well-founded. |
| 52 |
|
| 53 |
WellFounded : Rel A ℓ → Set _ |
| 54 |
WellFounded _<_ = ∀ x → Acc _<_ x |
| 55 |
|
| 56 |
------------------------------------------------------------------------ |
| 57 |
-- Basic properties |
| 58 |
|
| 59 |
acc-inverse : ∀ {_<_ : Rel A ℓ} {x : A} (q : Acc _<_ x) → |
| 60 |
WfRec _<_ (Acc _<_) x |
| 61 |
acc-inverse (acc rs) y<x = rs y<x |
| 62 |
|
| 63 |
module _ {_≈_ : Rel A ℓ₁} {_<_ : Rel A ℓ₂} where |
| 64 |
|
| 65 |
Acc-resp-flip-≈ : _<_ Respectsʳ (flip _≈_) → (Acc _<_) Respects _≈_ |
| 66 |
Acc-resp-flip-≈ respʳ x≈y (acc rec) = acc λ z<y → rec (respʳ x≈y z<y) |
| 67 |
|
| 68 |
Acc-resp-≈ : Symmetric _≈_ → _<_ Respectsʳ _≈_ → (Acc _<_) Respects _≈_ |
| 69 |
Acc-resp-≈ sym respʳ x≈y wf = Acc-resp-flip-≈ (respʳ ∘ sym) x≈y wf |
| 70 |
|
| 71 |
------------------------------------------------------------------------ |
| 72 |
-- Well-founded induction for the subset of accessible elements: |
| 73 |
|
| 74 |
module Some {_<_ : Rel A r} {ℓ} where |
| 75 |
|
| 76 |
wfRecBuilder : SubsetRecursorBuilder (Acc _<_) (WfRec _<_ {ℓ = ℓ}) |
| 77 |
wfRecBuilder P f x (acc rs) = λ y<x → f _ (wfRecBuilder P f _ (rs y<x)) |
| 78 |
|
| 79 |
wfRec : SubsetRecursor (Acc _<_) (WfRec _<_) |
| 80 |
wfRec = subsetBuild wfRecBuilder |
| 81 |
|
| 82 |
unfold-wfRec : (P : Pred A ℓ) (f : WfRec _<_ P ⊆′ P) {x : A} (q : Acc _<_ x) → |
| 83 |
wfRec P f x q ≡ f x λ y<x → wfRec P f _ (acc-inverse q y<x) |
| 84 |
unfold-wfRec P f (acc rs) = refl |
| 85 |
|
| 86 |
|
| 87 |
------------------------------------------------------------------------ |
| 88 |
-- Well-founded induction for all elements, assuming they are all |
| 89 |
-- accessible: |
| 90 |
|
| 91 |
module All {_<_ : Rel A r} (wf : WellFounded _<_) ℓ where |
| 92 |
|
| 93 |
wfRecBuilder : RecursorBuilder (WfRec _<_ {ℓ = ℓ}) |
| 94 |
wfRecBuilder P f x = Some.wfRecBuilder P f x (wf x) |
| 95 |
|
| 96 |
wfRec : Recursor (WfRec _<_) |
| 97 |
wfRec = build wfRecBuilder |
| 98 |
|
| 99 |
wfRec-builder = wfRecBuilder |
| 100 |
|
| 101 |
module FixPoint |
| 102 |
{_<_ : Rel A r} (wf : WellFounded _<_) |
| 103 |
(P : Pred A ℓ) (f : WfRec _<_ P ⊆′ P) |
| 104 |
(f-ext : (x : A) {IH IH′ : WfRec _<_ P x} → |
| 105 |
(∀ {y} y<x → IH {y} y<x ≡ IH′ y<x) → |
| 106 |
f x IH ≡ f x IH′) |
| 107 |
where |
| 108 |
|
| 109 |
some-wfrec-Irrelevant : Pred A _ |
| 110 |
some-wfrec-Irrelevant x = ∀ q q′ → Some.wfRec P f x q ≡ Some.wfRec P f x q′ |
| 111 |
|
| 112 |
some-wfRec-irrelevant : ∀ x → some-wfrec-Irrelevant x |
| 113 |
some-wfRec-irrelevant = All.wfRec wf _ some-wfrec-Irrelevant |
| 114 |
λ { x IH (acc rs) (acc rs′) → f-ext x λ y<x → IH y<x (rs y<x) (rs′ y<x) } |
| 115 |
|
| 116 |
open All wf ℓ |
| 117 |
|
| 118 |
wfRecBuilder-wfRec : ∀ {x y} y<x → wfRecBuilder P f x y<x ≡ wfRec P f y |
| 119 |
wfRecBuilder-wfRec {x} {y} y<x with acc rs ← wf x |
| 120 |
= some-wfRec-irrelevant y (rs y<x) (wf y) |
| 121 |
|
| 122 |
unfold-wfRec : ∀ {x} → wfRec P f x ≡ f x λ _ → wfRec P f _ |
| 123 |
unfold-wfRec {x} = f-ext x wfRecBuilder-wfRec |
| 124 |
|
| 125 |
------------------------------------------------------------------------ |
| 126 |
-- Well-founded relations are asymmetric and irreflexive. |
| 127 |
|
| 128 |
module _ {_<_ : Rel A r} where |
| 129 |
acc⇒asym : ∀ {x y} → Acc _<_ x → x < y → ¬ (y < x) |
| 130 |
acc⇒asym {x} hx = |
| 131 |
Some.wfRec (λ x → ∀ {y} → x < y → ¬ (y < x)) (λ _ hx x<y y<x → hx y<x y<x x<y) _ hx |
| 132 |
|
| 133 |
wf⇒asym : WellFounded _<_ → Asymmetric _<_ |
| 134 |
wf⇒asym wf = acc⇒asym (wf _) |
| 135 |
|
| 136 |
wf⇒irrefl : {_≈_ : Rel A ℓ} → _<_ Respects₂ _≈_ → |
| 137 |
Symmetric _≈_ → WellFounded _<_ → Irreflexive _≈_ _<_ |
| 138 |
wf⇒irrefl r s wf = asym⇒irr r s (wf⇒asym wf) |
| 139 |
|
| 140 |
------------------------------------------------------------------------ |
| 141 |
-- It might be useful to establish proofs of Acc or Well-founded using |
| 142 |
-- combinators such as the ones below (see, for instance, |
| 143 |
-- "Constructing Recursion Operators in Intuitionistic Type Theory" by |
| 144 |
-- Lawrence C Paulson). |
| 145 |
|
| 146 |
module Subrelation {_<₁_ : Rel A ℓ₁} {_<₂_ : Rel A ℓ₂} |
| 147 |
(<₁⇒<₂ : ∀ {x y} → x <₁ y → x <₂ y) where |
| 148 |
|
| 149 |
accessible : Acc _<₂_ ⊆ Acc _<₁_ |
| 150 |
accessible (acc rs) = acc λ y<x → accessible (rs (<₁⇒<₂ y<x)) |
| 151 |
|
| 152 |
wellFounded : WellFounded _<₂_ → WellFounded _<₁_ |
| 153 |
wellFounded wf = λ x → accessible (wf x) |
| 154 |
|
| 155 |
|
| 156 |
-- DEPRECATED in v1.4. |
| 157 |
-- Please use proofs in `Relation.Binary.Construct.On` instead. |
| 158 |
module InverseImage {_<_ : Rel B ℓ} (f : A → B) where |
| 159 |
|
| 160 |
accessible : ∀ {x} → Acc _<_ (f x) → Acc (_<_ on f) x |
| 161 |
accessible (acc rs) = acc λ fy<fx → accessible (rs fy<fx) |
| 162 |
|
| 163 |
wellFounded : WellFounded _<_ → WellFounded (_<_ on f) |
| 164 |
wellFounded wf = λ x → accessible (wf (f x)) |
| 165 |
|
| 166 |
well-founded = wellFounded |
| 167 |
{-# WARNING_ON_USAGE accessible |
| 168 |
"Warning: accessible was deprecated in v1.4. |
| 169 |
\ \Please use accessible from `Relation.Binary.Construct.On` instead." |
| 170 |
#-} |
| 171 |
{-# WARNING_ON_USAGE wellFounded |
| 172 |
"Warning: wellFounded was deprecated in v1.4. |
| 173 |
\ \Please use wellFounded from `Relation.Binary.Construct.On` instead." |
| 174 |
#-} |
| 175 |
|
| 176 |
|
| 177 |
-- DEPRECATED in v1.5. |
| 178 |
-- Please use `TransClosure` from `Relation.Binary.Construct.Closure.Transitive` instead. |
| 179 |
module TransitiveClosure {A : Set a} (_<_ : Rel A ℓ) where |
| 180 |
|
| 181 |
infix 4 _<⁺_ |
| 182 |
|
| 183 |
data _<⁺_ : Rel A (a ⊔ ℓ) where |
| 184 |
[_] : ∀ {x y} (x<y : x < y) → x <⁺ y |
| 185 |
trans : ∀ {x y z} (x<y : x <⁺ y) (y<z : y <⁺ z) → x <⁺ z |
| 186 |
|
| 187 |
downwardsClosed : ∀ {x y} → Acc _<⁺_ y → x <⁺ y → Acc _<⁺_ x |
| 188 |
downwardsClosed (acc rs) x<y = acc λ z<x → rs (trans z<x x<y) |
| 189 |
|
| 190 |
mutual |
| 191 |
|
| 192 |
accessible : ∀ {x} → Acc _<_ x → Acc _<⁺_ x |
| 193 |
accessible acc-x = acc (accessible′ acc-x) |
| 194 |
|
| 195 |
accessible′ : ∀ {x} → Acc _<_ x → WfRec _<⁺_ (Acc _<⁺_) x |
| 196 |
accessible′ (acc rs) [ y<x ] = accessible (rs y<x) |
| 197 |
accessible′ acc-x (trans y<z z<x) = |
| 198 |
downwardsClosed (accessible′ acc-x z<x) y<z |
| 199 |
|
| 200 |
wellFounded : WellFounded _<_ → WellFounded _<⁺_ |
| 201 |
wellFounded wf = λ x → accessible (wf x) |
| 202 |
|
| 203 |
{-# WARNING_ON_USAGE _<⁺_ |
| 204 |
"Warning: _<⁺_ was deprecated in v1.5. |
| 205 |
\ \Please use TransClosure from Relation.Binary.Construct.Closure.Transitive instead." |
| 206 |
#-} |
| 207 |
|
| 208 |
|
| 209 |
-- DEPRECATED in v1.3. |
| 210 |
-- Please use `×-Lex` from `Data.Product.Relation.Binary.Lex.Strict` instead. |
| 211 |
module Lexicographic {A : Set a} {B : A → Set b} |
| 212 |
(RelA : Rel A ℓ₁) |
| 213 |
(RelB : ∀ x → Rel (B x) ℓ₂) where |
| 214 |
|
| 215 |
infix 4 _<_ |
| 216 |
data _<_ : Rel (Σ A B) (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂) where |
| 217 |
left : ∀ {x₁ y₁ x₂ y₂} (x₁<x₂ : RelA x₁ x₂) → (x₁ , y₁) < (x₂ , y₂) |
| 218 |
right : ∀ {x y₁ y₂} (y₁<y₂ : RelB x y₁ y₂) → (x , y₁) < (x , y₂) |
| 219 |
|
| 220 |
mutual |
| 221 |
|
| 222 |
accessible : ∀ {x y} → |
| 223 |
Acc RelA x → (∀ {x} → WellFounded (RelB x)) → |
| 224 |
Acc _<_ (x , y) |
| 225 |
accessible accA wfB = acc (accessible′ accA (wfB _) wfB) |
| 226 |
|
| 227 |
|
| 228 |
accessible′ : |
| 229 |
∀ {x y} → |
| 230 |
Acc RelA x → Acc (RelB x) y → (∀ {x} → WellFounded (RelB x)) → |
| 231 |
WfRec _<_ (Acc _<_) (x , y) |
| 232 |
accessible′ (acc rsA) _ wfB (left x′<x) = accessible (rsA x′<x) wfB |
| 233 |
accessible′ accA (acc rsB) wfB (right y′<y) = |
| 234 |
acc (accessible′ accA (rsB y′<y) wfB) |
| 235 |
|
| 236 |
wellFounded : WellFounded RelA → (∀ {x} → WellFounded (RelB x)) → |
| 237 |
WellFounded _<_ |
| 238 |
wellFounded wfA wfB p = accessible (wfA (proj₁ p)) wfB |
| 239 |
|
| 240 |
well-founded = wellFounded |
| 241 |
|
| 242 |
{-# WARNING_ON_USAGE _<_ |
| 243 |
"Warning: _<_ was deprecated in v1.3. |
| 244 |
\ \Please use `×-Lex` from `Data.Product.Relation.Binary.Lex.Strict` instead." |
| 245 |
#-} |
| 246 |
{-# WARNING_ON_USAGE accessible |
| 247 |
"Warning: accessible was deprecated in v1.3." |
| 248 |
#-} |
| 249 |
{-# WARNING_ON_USAGE accessible′ |
| 250 |
"Warning: accessible′ was deprecated in v1.3." |
| 251 |
#-} |
| 252 |
{-# WARNING_ON_USAGE wellFounded |
| 253 |
"Warning: wellFounded was deprecated in v1.3. |
| 254 |
\ \Please use `×-wellFounded` from `Data.Product.Relation.Binary.Lex.Strict` instead." |
| 255 |
#-} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
------------------------------------------------------------------------ |
| 260 |
-- DEPRECATED NAMES |
| 261 |
------------------------------------------------------------------------ |
| 262 |
-- Please use the new names as continuing support for the old names is |
| 263 |
-- not guaranteed. |
| 264 |
|
| 265 |
-- Version 1.0 |
| 266 |
|
| 267 |
module Inverse-image = InverseImage |
| 268 |
module Transitive-closure = TransitiveClosure |