| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Various forms of induction for natural numbers |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Nat.Induction where |
| 10 |
|
| 11 |
open import Data.Nat.Base |
| 12 |
open import Data.Nat.Properties using (<⇒<′) |
| 13 |
open import Data.Product.Base using (_×_; _,_) |
| 14 |
open import Data.Unit.Polymorphic.Base using (⊤) |
| 15 |
open import Induction using (RecStruct; RecursorBuilder; Recursor; build) |
| 16 |
open import Induction.WellFounded as WF |
| 17 |
open import Level using (Level) |
| 18 |
|
| 19 |
private |
| 20 |
variable |
| 21 |
ℓ : Level |
| 22 |
|
| 23 |
------------------------------------------------------------------------ |
| 24 |
-- Re-export accessability |
| 25 |
|
| 26 |
open WF public using (Acc; acc) |
| 27 |
|
| 28 |
------------------------------------------------------------------------ |
| 29 |
-- Ordinary induction |
| 30 |
|
| 31 |
Rec : ∀ ℓ → RecStruct ℕ ℓ ℓ |
| 32 |
Rec ℓ P zero = ⊤ |
| 33 |
Rec ℓ P (suc n) = P n |
| 34 |
|
| 35 |
recBuilder : RecursorBuilder (Rec ℓ) |
| 36 |
recBuilder P f zero = _ |
| 37 |
recBuilder P f (suc n) = f n (recBuilder P f n) |
| 38 |
|
| 39 |
rec : Recursor (Rec ℓ) |
| 40 |
rec = build recBuilder |
| 41 |
|
| 42 |
------------------------------------------------------------------------ |
| 43 |
-- Complete induction |
| 44 |
|
| 45 |
CRec : ∀ ℓ → RecStruct ℕ ℓ ℓ |
| 46 |
CRec ℓ P zero = ⊤ |
| 47 |
CRec ℓ P (suc n) = P n × CRec ℓ P n |
| 48 |
|
| 49 |
cRecBuilder : RecursorBuilder (CRec ℓ) |
| 50 |
cRecBuilder P f zero = _ |
| 51 |
cRecBuilder P f (suc n) = f n ih , ih |
| 52 |
where ih = cRecBuilder P f n |
| 53 |
|
| 54 |
cRec : Recursor (CRec ℓ) |
| 55 |
cRec = build cRecBuilder |
| 56 |
|
| 57 |
------------------------------------------------------------------------ |
| 58 |
-- Complete induction based on _<′_ |
| 59 |
|
| 60 |
<′-Rec : RecStruct ℕ ℓ ℓ |
| 61 |
<′-Rec = WfRec _<′_ |
| 62 |
|
| 63 |
-- mutual definition |
| 64 |
|
| 65 |
<′-wellFounded : WellFounded _<′_ |
| 66 |
<′-wellFounded′ : ∀ n → <′-Rec (Acc _<′_) n |
| 67 |
|
| 68 |
<′-wellFounded n = acc (<′-wellFounded′ n) |
| 69 |
|
| 70 |
<′-wellFounded′ (suc n) <′-base = <′-wellFounded n |
| 71 |
<′-wellFounded′ (suc n) (<′-step m<n) = <′-wellFounded′ n m<n |
| 72 |
|
| 73 |
module _ {ℓ : Level} where |
| 74 |
open WF.All <′-wellFounded ℓ public |
| 75 |
renaming ( wfRecBuilder to <′-recBuilder |
| 76 |
; wfRec to <′-rec |
| 77 |
) |
| 78 |
hiding (wfRec-builder) |
| 79 |
|
| 80 |
------------------------------------------------------------------------ |
| 81 |
-- Complete induction based on _<_ |
| 82 |
|
| 83 |
<-Rec : RecStruct ℕ ℓ ℓ |
| 84 |
<-Rec = WfRec _<_ |
| 85 |
|
| 86 |
<-wellFounded : WellFounded _<_ |
| 87 |
<-wellFounded = Subrelation.wellFounded <⇒<′ <′-wellFounded |
| 88 |
|
| 89 |
-- A version of `<-wellFounded` that cheats by skipping building |
| 90 |
-- the first billion proofs. Use this when you require the function |
| 91 |
-- using the proof of well-foundedness to evaluate fast. |
| 92 |
-- |
| 93 |
-- IMPORTANT: You have to be a little bit careful when using this to |
| 94 |
-- always make the function be strict in some other argument than the |
| 95 |
-- accessibility proof, otherwise you will have neutral terms unfolding |
| 96 |
-- a billion times before getting stuck. |
| 97 |
<-wellFounded-fast : WellFounded _<_ |
| 98 |
<-wellFounded-fast = <-wellFounded-skip 1000000000 |
| 99 |
where |
| 100 |
<-wellFounded-skip : ∀ (k : ℕ) → WellFounded _<_ |
| 101 |
<-wellFounded-skip zero n = <-wellFounded n |
| 102 |
<-wellFounded-skip (suc k) zero = <-wellFounded 0 |
| 103 |
<-wellFounded-skip (suc k) (suc n) = acc λ {m} _ → <-wellFounded-skip k m |
| 104 |
|
| 105 |
module _ {ℓ : Level} where |
| 106 |
open WF.All <-wellFounded ℓ public |
| 107 |
renaming ( wfRecBuilder to <-recBuilder |
| 108 |
; wfRec to <-rec |
| 109 |
) |
| 110 |
hiding (wfRec-builder) |