| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- An abstraction of various forms of recursion/induction |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- The idea underlying Induction.* comes from Epigram 1, see Section 4 |
| 8 |
-- of "The view from the left" by McBride and McKinna. |
| 9 |
|
| 10 |
-- Note: The types in this module can perhaps be easier to understand |
| 11 |
-- if they are normalised. Note also that Agda can do the |
| 12 |
-- normalisation for you. |
| 13 |
|
| 14 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 15 |
|
| 16 |
module Induction where |
| 17 |
|
| 18 |
open import Level |
| 19 |
open import Relation.Unary |
| 20 |
open import Relation.Unary.PredicateTransformer using (PT) |
| 21 |
|
| 22 |
private |
| 23 |
variable |
| 24 |
a ℓ ℓ₁ ℓ₂ : Level |
| 25 |
A : Set a |
| 26 |
Q : Pred A ℓ |
| 27 |
Rec : PT A A ℓ₁ ℓ₂ |
| 28 |
|
| 29 |
|
| 30 |
------------------------------------------------------------------------ |
| 31 |
-- A RecStruct describes the allowed structure of recursion. The |
| 32 |
-- examples in Data.Nat.Induction should explain what this is all |
| 33 |
-- about. |
| 34 |
|
| 35 |
RecStruct : Set a → (ℓ₁ ℓ₂ : Level) → Set _ |
| 36 |
RecStruct A = PT A A |
| 37 |
|
| 38 |
-- A recursor builder constructs an instance of a recursion structure |
| 39 |
-- for a given input. |
| 40 |
|
| 41 |
RecursorBuilder : RecStruct A ℓ₁ ℓ₂ → Set _ |
| 42 |
RecursorBuilder Rec = ∀ P → Rec P ⊆′ P → Universal (Rec P) |
| 43 |
|
| 44 |
-- A recursor can be used to actually compute/prove something useful. |
| 45 |
|
| 46 |
Recursor : RecStruct A ℓ₁ ℓ₂ → Set _ |
| 47 |
Recursor Rec = ∀ P → Rec P ⊆′ P → Universal P |
| 48 |
|
| 49 |
-- And recursors can be constructed from recursor builders. |
| 50 |
|
| 51 |
build : RecursorBuilder Rec → Recursor Rec |
| 52 |
build builder P f x = f x (builder P f x) |
| 53 |
|
| 54 |
-- We can repeat the exercise above for subsets of the type we are |
| 55 |
-- recursing over. |
| 56 |
|
| 57 |
SubsetRecursorBuilder : Pred A ℓ → RecStruct A ℓ₁ ℓ₂ → Set _ |
| 58 |
SubsetRecursorBuilder Q Rec = ∀ P → Rec P ⊆′ P → Q ⊆′ Rec P |
| 59 |
|
| 60 |
SubsetRecursor : Pred A ℓ → RecStruct A ℓ₁ ℓ₂ → Set _ |
| 61 |
SubsetRecursor Q Rec = ∀ P → Rec P ⊆′ P → Q ⊆′ P |
| 62 |
|
| 63 |
subsetBuild : SubsetRecursorBuilder Q Rec → SubsetRecursor Q Rec |
| 64 |
subsetBuild builder P f x q = f x (builder P f x q) |