Debian Codegraph @

doc/README/Data/Nat/Induction.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Some examples of how to use non-trivial induction over the natural
5 -- numbers.
6 ------------------------------------------------------------------------
7
8 module README.Data.Nat.Induction where
9
10 open import Data.Nat
11 open import Data.Nat.Induction
12 open import Data.Product.Base using (_,_)
13 open import Function.Base using (_∘_)
14 open import Induction.WellFounded
15 open import Relation.Binary.PropositionalEquality
16
17 private
18
19 n<′1+n : ∀ {n} → n <′ suc n
20 n<′1+n = ≤′-refl
21
22 n<′2+n : ∀ {n} → n <′ suc (suc n)
23 n<′2+n = ≤′-step ≤′-refl
24
25 -- Doubles its input.
26
27 twice : ℕ → ℕ
28 twice = rec _ λ
29 { zero _ → zero
30 ; (suc n) twice-n → suc (suc twice-n)
31 }
32
33 -- Halves its input (rounding downwards).
34 --
35 -- The step function is mentioned in a proof below, so it has been
36 -- given a name. (The mutual keyword is used to avoid having to give
37 -- a type signature for the step function.)
38
39 mutual
40
41 half₁-step = λ
42 { zero _ → zero
43 ; (suc zero) _ → zero
44 ; (suc (suc n)) (_ , half₁n , _) → suc half₁n
45 }
46
47 half₁ : ℕ → ℕ
48 half₁ = cRec _ half₁-step
49
50 -- An alternative implementation of half₁.
51
52 mutual
53
54 half₂-step = λ
55 { zero _ → zero
56 ; (suc zero) _ → zero
57 ; (suc (suc n)) rec → suc (rec n<′2+n)
58 }
59
60 half₂ : ℕ → ℕ
61 half₂ = <′-rec _ half₂-step
62
63 -- The application half₁ (2 + n) is definitionally equal to
64 -- 1 + half₁ n. Perhaps it is instructive to see why.
65
66 half₁-2+ : ∀ n → half₁ (2 + n) ≡ 1 + half₁ n
67 half₁-2+ n = begin
68
69 half₁ (2 + n) ≡⟨⟩
70
71 cRec _ half₁-step (2 + n) ≡⟨⟩
72
73 half₁-step (2 + n) (cRecBuilder _ half₁-step (2 + n)) ≡⟨⟩
74
75 half₁-step (2 + n)
76 (let ih = cRecBuilder _ half₁-step (1 + n) in
77 half₁-step (1 + n) ih , ih) ≡⟨⟩
78
79 half₁-step (2 + n)
80 (let ih = cRecBuilder _ half₁-step n in
81 half₁-step (1 + n) (half₁-step n ih , ih) , half₁-step n ih , ih) ≡⟨⟩
82
83 1 + half₁-step n (cRecBuilder _ half₁-step n) ≡⟨⟩
84
85 1 + cRec _ half₁-step n ≡⟨⟩
86
87 1 + half₁ n ∎
88
89 where open ≡-Reasoning
90
91 -- The application half₂ (2 + n) is definitionally equal to
92 -- 1 + half₂ n. Perhaps it is instructive to see why.
93
94 half₂-2+ : ∀ n → half₂ (2 + n) ≡ 1 + half₂ n
95 half₂-2+ n = begin
96
97 half₂ (2 + n) ≡⟨⟩
98
99 <′-rec _ half₂-step (2 + n) ≡⟨⟩
100
101 half₂-step (2 + n) (<′-recBuilder _ half₂-step (2 + n)) ≡⟨⟩
102
103 1 + <′-recBuilder _ half₂-step (2 + n) n<′2+n ≡⟨⟩
104
105 1 + Some.wfRecBuilder _ half₂-step (2 + n)
106 (<′-wellFounded (2 + n)) n<′2+n ≡⟨⟩
107
108 1 + Some.wfRecBuilder _ half₂-step (2 + n)
109 (acc (<′-wellFounded′ (2 + n))) n<′2+n ≡⟨⟩
110
111 1 + half₂-step n
112 (Some.wfRecBuilder _ half₂-step n
113 (<′-wellFounded′ (2 + n) n<′2+n)) ≡⟨⟩
114
115 1 + half₂-step n
116 (Some.wfRecBuilder _ half₂-step n
117 (<′-wellFounded′ (1 + n) n<′1+n)) ≡⟨⟩
118
119 1 + half₂-step n
120 (Some.wfRecBuilder _ half₂-step n (<′-wellFounded n)) ≡⟨⟩
121
122 1 + half₂-step n (<′-recBuilder _ half₂-step n) ≡⟨⟩
123
124 1 + <′-rec _ half₂-step n ≡⟨⟩
125
126 1 + half₂ n ∎
127
128 where open ≡-Reasoning
129
130 -- Some properties that the functions above satisfy, proved using
131 -- cRec.
132
133 half₁-+₁ : ∀ n → half₁ (twice n) ≡ n
134 half₁-+₁ = cRec _ λ
135 { zero _ → refl
136 ; (suc zero) _ → refl
137 ; (suc (suc n)) (_ , half₁twice-n≡n , _) →
138 cong (suc ∘ suc) half₁twice-n≡n
139 }
140
141 half₂-+₁ : ∀ n → half₂ (twice n) ≡ n
142 half₂-+₁ = cRec _ λ
143 { zero _ → refl
144 ; (suc zero) _ → refl
145 ; (suc (suc n)) (_ , half₁twice-n≡n , _) →
146 cong (suc ∘ suc) half₁twice-n≡n
147 }
148
149 -- Some properties that the functions above satisfy, proved using
150 -- <′-rec.
151
152 half₁-+₂ : ∀ n → half₁ (twice n) ≡ n
153 half₁-+₂ = <′-rec _ λ
154 { zero _ → refl
155 ; (suc zero) _ → refl
156 ; (suc (suc n)) rec →
157 cong (suc ∘ suc) (rec n<′2+n)
158 }
159
160 half₂-+₂ : ∀ n → half₂ (twice n) ≡ n
161 half₂-+₂ = <′-rec _ λ
162 { zero _ → refl
163 ; (suc zero) _ → refl
164 ; (suc (suc n)) rec →
165 cong (suc ∘ suc) (rec n<′2+n)
166 }
167