-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter2-Numbers.agda
341 lines (253 loc) · 7.59 KB
/
Chapter2-Numbers.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
module Chapter2-Numbers where
import Chapter1-Agda
module Definition-Naturals where
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
module Sandbox-Naturals where
open import Data.Nat using (ℕ; zero; suc)
one : ℕ
one = suc zero
two : ℕ
two = suc one
three : ℕ
three = suc two
four : ℕ
four = suc three
open Chapter1-Agda using (Bool; true; false)
n=0? : ℕ → Bool
n=0? zero = true
n=0? (suc x) = false
n=2? : ℕ → Bool
n=2? (suc (suc zero)) = true
n=2? n = false
even? : ℕ → Bool
even? zero = true
even? (suc zero) = false
even? (suc (suc n)) = even? n
data Evenℕ : Set where
zero : Evenℕ
suc-suc : Evenℕ → Evenℕ
toℕ : Evenℕ → ℕ
toℕ zero = zero
toℕ (suc-suc x) = suc (suc (toℕ x))
module Sandbox-Usable where
postulate
Usable : Set
Unusable : Set
IsEven : ℕ → Set
IsEven zero = Usable
IsEven (suc zero) = Unusable
IsEven (suc (suc x)) = IsEven x
data IsEven : ℕ → Set where
zero-even : IsEven zero
suc-suc-even : {n : ℕ} → IsEven n → IsEven (suc (suc n))
four-is-even : IsEven four
four-is-even = suc-suc-even (suc-suc-even zero-even)
-- three-is-even : IsEven three
-- three-is-even = suc-suc-even {! !}
data IsOdd : ℕ → Set where
one-odd : IsOdd (suc zero)
suc-suc-odd : {n : ℕ} → IsOdd n → IsOdd (suc (suc n))
data IsOdd’ : ℕ → Set where
is-odd : {n : ℕ} → IsEven n → IsOdd’ (suc n)
three-is-odd : IsOdd three
three-is-odd = suc-suc-odd one-odd
evenOdd : {n : ℕ} → IsEven n → IsOdd (suc n)
evenOdd zero-even = one-odd
evenOdd (suc-suc-even x) = suc-suc-odd (evenOdd x)
evenOdd’ : {n : ℕ} → IsEven n → IsOdd’ (suc n)
evenOdd’ = is-odd
data Maybe (A : Set) : Set where
just : A → Maybe A
nothing : Maybe A
evenEv : (n : ℕ) → Maybe (IsEven n)
evenEv zero = just zero-even
evenEv (suc zero) = nothing
evenEv (suc (suc n)) with evenEv n
... | just x = just (suc-suc-even x)
... | nothing = nothing
_+_ : ℕ → ℕ → ℕ
zero + y = y
suc x + y = suc (x + y)
infixl 6 _+_
module Example-Silly where
open Chapter1-Agda using (not)
data ℕ’ : Set where
zero : ℕ’
suc : ℕ’ → ℕ’
2suc : ℕ’ → ℕ’
even?’ : ℕ’ → Bool
even?’ zero = true
even?’ (suc n) = not (even?’ n)
even?’ (2suc zero) = true
even?’ (2suc (suc n)) = not (even?’ n)
even?’ (2suc (2suc n)) = even?’ n
_*_ : ℕ → ℕ → ℕ
zero * y = zero
suc x * y = y + (x * y)
infixl 7 _*_
_^_ : ℕ → ℕ → ℕ
x ^ zero = one
x ^ suc y = x * (x ^ y)
_∸_ : ℕ → ℕ → ℕ
x ∸ zero = x
zero ∸ suc y = zero
suc x ∸ suc y = x ∸ y
module Natural-Tests where
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
_ : one + two ≡ three
_ = refl
_ : three ∸ one ≡ two
_ = refl
_ : one ∸ three ≡ zero
_ = refl
_ : two * two ≡ four
_ = refl
-- not from the book
module Naturals-Proofs where
open import Relation.Binary.PropositionalEquality using (_≡_; refl; cong)
+-idl : {x : ℕ} → x + 0 ≡ x
+-idl {zero} = refl
+-idl {suc x} = cong suc +-idl
+-idr : {x : ℕ} → 0 + x ≡ x
+-idr {zero} = refl
+-idr {suc x} = cong suc +-idr
*-idl : {x : ℕ} → x * 1 ≡ x
*-idl {zero} = refl
*-idl {suc x} = cong suc *-idl
*-idr : {x : ℕ} → 1 * x ≡ x
*-idr {zero} = refl
*-idr {suc x} = cong suc *-idr
module Misstep-Integers₁ where
data ℤ : Set where
zero : ℤ
suc : ℤ → ℤ
pred : ℤ → ℤ
normalize : ℤ → ℤ
normalize zero = zero
normalize (suc zero) = suc zero
normalize (suc (suc x)) = suc (normalize (suc x))
normalize (suc (pred x)) = normalize x
normalize (pred zero) = pred zero
normalize (pred (suc x)) = normalize x
normalize (pred (pred x)) = pred (normalize (pred x))
module Counterexample where
open import Relation.Binary.PropositionalEquality
_ : normalize (suc (suc (pred (pred zero)))) ≡ suc (pred zero)
_ = refl
module Misstep-Integers₂ where
import Data.Nat as ℕ
open ℕ using (ℕ; zero; suc)
record ℤ : Set where
constructor mkℤ
field
pos : ℕ
neg : ℕ
normalize : ℤ → ℤ
normalize (mkℤ zero neg) = mkℤ zero neg
normalize (mkℤ (suc pos) zero) = mkℤ (suc pos) zero
normalize (mkℤ (suc pos) (suc neg)) = normalize (mkℤ pos neg)
_+_ : ℤ → ℤ → ℤ
mkℤ p₁ n₁ + mkℤ p₂ n₂ = normalize (mkℤ (p₁ ℕ.+ p₂) (n₁ ℕ.+ n₂))
infixl 5 _+_
_-_ : ℤ → ℤ → ℤ
mkℤ p₁ n₁ - mkℤ p₂ n₂ = normalize (mkℤ (p₁ ℕ.+ n₂) (n₁ ℕ.+ p₂))
infixl 5 _-_
_*_ : ℤ → ℤ → ℤ
mkℤ p₁ n₁ * mkℤ p₂ n₂ = normalize (mkℤ (p₁ ℕ.* p₂ ℕ.+ n₁ ℕ.* n₂) (p₁ ℕ.* n₂ ℕ.+ p₂ ℕ.* n₁))
infixl 6 _*_
module Misstep-Integers₃ where
open import Data.Nat
data ℤ : Set where
+_ : ℕ → ℤ
-_ : ℕ → ℤ
_ : ℤ
_ = - 2
_ : ℤ
_ = + 6
_ : ℤ
_ = + 0
_ : ℤ
_ = - 0
module Sandbox-Integers where
import Data.Nat as ℕ
open ℕ using (ℕ)
data ℤ : Set where
+_ : ℕ → ℤ
-[1+_] : ℕ → ℤ
0ℤ : ℤ
0ℤ = + 0
1ℤ : ℤ
1ℤ = + 1
-1ℤ : ℤ
-1ℤ = -[1+ 0 ]
suc : ℤ → ℤ
suc (+ x) = + ℕ.suc x
suc -[1+ ℕ.zero ] = 0ℤ
suc -[1+ ℕ.suc x ] = -[1+ x ]
pred : ℤ → ℤ
pred (+ ℕ.zero) = -1ℤ
pred (+ ℕ.suc x) = + x
pred -[1+ x ] = -[1+ ℕ.suc x ]
-’_ : ℤ → ℤ
-’ (+ ℕ.zero) = 0ℤ
-’ (+ ℕ.suc x) = -[1+ x ]
-’ -[1+ x ] = + ℕ.suc x
pattern +[1+_] n = + ℕ.suc n
pattern +0 = + ℕ.zero
-_ : ℤ → ℤ
- +0 = +0
- -[1+ x ] = +[1+ x ]
- +[1+ x ] = -[1+ x ]
module Native-Addition where
-- _+_ : ℤ → ℤ → ℤ
-- +0 + y = y
-- +[1+ x ] + +0 = +[1+ x ]
-- +[1+ x ] + +[1+ y ] = +[1+ 1 ℕ.+ x ℕ.+ y ]
-- +[1+ x ] + -[1+ y ] = {! !}
-- -[1+ x ] + +0 = -[1+ x ]
-- -[1+ x ] + +[1+ y ] = {! !}
-- -[1+ x ] + -[1+ y ] = -[1+ 1 ℕ.+ x ℕ.+ y ]
_⊖_ : ℕ → ℕ → ℤ
ℕ.zero ⊖ ℕ.zero = +0
ℕ.zero ⊖ ℕ.suc n = -[1+ n ]
ℕ.suc m ⊖ ℕ.zero = +[1+ m ]
ℕ.suc m ⊖ ℕ.suc n = m ⊖ n
infixl 5 _+_
_+_ : ℤ → ℤ → ℤ
+ x + + y = + (x ℕ.+ y)
+ x + -[1+ y ] = x ⊖ ℕ.suc y
-[1+ x ] + + y = y ⊖ ℕ.suc x
-[1+ x ] + -[1+ y ] = -[1+ x ℕ.+ ℕ.suc y ]
infixl 5 _-_
_-_ : ℤ → ℤ → ℤ
x - y = x + (- y)
infixl 6 _*_
_*_ : ℤ → ℤ → ℤ
x * +0 = +0
x * +[1+ ℕ.zero ] = x
x * -[1+ ℕ.zero ] = - x
x * +[1+ ℕ.suc y ] = (+[1+ y ] * x) + x
x * -[1+ ℕ.suc y ] = (-[1+ y ] * x) - x
module Tests where
open import Relation.Binary.PropositionalEquality
_ : - (+ 2) * - (+ 6) ≡ + 12
_ = refl
_ : (+ 3) - (+ 10) ≡ - (+ 7)
_ = refl
open import Data.Nat
using (ℕ; zero; suc; _+_; _*_; _^_; _∸_)
public
open Sandbox-Naturals
using (one; two; three; four)
public
open Sandbox-Naturals
using (IsEven; IsOdd)
renaming ( zero-even to z-even
; suc-suc-even to ss-even
; one-odd to 1-odd
; suc-suc-odd to ss-odd
)
public
open import Data.Maybe using (Maybe; just; nothing) public