| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Lexicographic induction |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Induction.Lexicographic where |
| 10 |
|
| 11 |
open import Data.Product.Base using (Σ; _,_; _×_) |
| 12 |
open import Induction using (RecStruct; RecursorBuilder; build) |
| 13 |
open import Level using (Level; _⊔_) |
| 14 |
|
| 15 |
-- The structure of lexicographic induction. |
| 16 |
|
| 17 |
Σ-Rec : ∀ {a b ℓ₁ ℓ₂ ℓ₃} {A : Set a} {B : A → Set b} → |
| 18 |
RecStruct A (ℓ₁ ⊔ b) ℓ₂ → (∀ x → RecStruct (B x) ℓ₁ ℓ₃) → |
| 19 |
RecStruct (Σ A B) _ _ |
| 20 |
Σ-Rec RecA RecB P (x , y) = |
| 21 |
-- Either x is constant and y is "smaller", ... |
| 22 |
RecB x (λ y′ → P (x , y′)) y |
| 23 |
× |
| 24 |
-- ...or x is "smaller" and y is arbitrary. |
| 25 |
RecA (λ x′ → ∀ y′ → P (x′ , y′)) x |
| 26 |
|
| 27 |
infixr 2 _⊗_ |
| 28 |
|
| 29 |
_⊗_ : ∀ {a b ℓ₁ ℓ₂ ℓ₃} {A : Set a} {B : Set b} → |
| 30 |
RecStruct A (ℓ₁ ⊔ b) ℓ₂ → RecStruct B ℓ₁ ℓ₃ → |
| 31 |
RecStruct (A × B) _ _ |
| 32 |
RecA ⊗ RecB = Σ-Rec RecA (λ _ → RecB) |
| 33 |
|
| 34 |
-- Constructs a recursor builder for lexicographic induction. |
| 35 |
|
| 36 |
Σ-rec-builder : |
| 37 |
∀ {a b ℓ₁ ℓ₂ ℓ₃} {A : Set a} {B : A → Set b} |
| 38 |
{RecA : RecStruct A (ℓ₁ ⊔ b) ℓ₂} |
| 39 |
{RecB : ∀ x → RecStruct (B x) ℓ₁ ℓ₃} → |
| 40 |
RecursorBuilder RecA → (∀ x → RecursorBuilder (RecB x)) → |
| 41 |
RecursorBuilder (Σ-Rec RecA RecB) |
| 42 |
Σ-rec-builder {RecA = RecA} {RecB = RecB} recA recB P f (x , y) = |
| 43 |
(p₁ x y p₂x , p₂x) |
| 44 |
where |
| 45 |
p₁ : ∀ x y → |
| 46 |
RecA (λ x′ → ∀ y′ → P (x′ , y′)) x → |
| 47 |
RecB x (λ y′ → P (x , y′)) y |
| 48 |
p₁ x y x-rec = recB x |
| 49 |
(λ y′ → P (x , y′)) |
| 50 |
(λ y y-rec → f (x , y) (y-rec , x-rec)) |
| 51 |
y |
| 52 |
|
| 53 |
p₂ : ∀ x → RecA (λ x′ → ∀ y′ → P (x′ , y′)) x |
| 54 |
p₂ = recA (λ x → ∀ y → P (x , y)) |
| 55 |
(λ x x-rec y → f (x , y) (p₁ x y x-rec , x-rec)) |
| 56 |
|
| 57 |
p₂x = p₂ x |
| 58 |
|
| 59 |
[_⊗_] : ∀ {a b ℓ₁ ℓ₂ ℓ₃} {A : Set a} {B : Set b} |
| 60 |
{RecA : RecStruct A (ℓ₁ ⊔ b) ℓ₂} {RecB : RecStruct B ℓ₁ ℓ₃} → |
| 61 |
RecursorBuilder RecA → RecursorBuilder RecB → |
| 62 |
RecursorBuilder (RecA ⊗ RecB) |
| 63 |
[ recA ⊗ recB ] = Σ-rec-builder recA (λ _ → recB) |
| 64 |
|
| 65 |
------------------------------------------------------------------------ |
| 66 |
-- Example |
| 67 |
|
| 68 |
private |
| 69 |
|
| 70 |
open import Data.Nat.Base |
| 71 |
open import Data.Nat.Induction as ℕ |
| 72 |
|
| 73 |
-- The Ackermann function à la Rózsa Péter. |
| 74 |
|
| 75 |
ackermann : ℕ → ℕ → ℕ |
| 76 |
ackermann m n = |
| 77 |
build [ ℕ.recBuilder ⊗ ℕ.recBuilder ] |
| 78 |
(λ _ → ℕ) |
| 79 |
(λ { (zero , n) _ → 1 + n |
| 80 |
; (suc m , zero) (_ , ackm•) → ackm• 1 |
| 81 |
; (suc m , suc n) (ack[1+m]n , ackm•) → ackm• ack[1+m]n |
| 82 |
}) |
| 83 |
(m , n) |