-
Notifications
You must be signed in to change notification settings - Fork 0
/
haskell_ch13.agda
565 lines (517 loc) · 18.9 KB
/
haskell_ch13.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
module haskell_ch13 where
-- for "Programming Haskell" chapter 13.
open import Relation.Binary.PropositionalEquality
open ≡-Reasoning
open import Data.Empty
import Level
open import Relation.Nullary
open import Relation.Binary.Core
open import Data.List hiding (replicate; reverse; _++_; all; map; take; drop)
open import Data.Nat
open import Data.Bool
data Nat : Set where
Zero : Nat
Succ : Nat → Nat
add : Nat → Nat → Nat
add Zero m = m
add (Succ n) m = Succ (add n m)
n+zero≡n : {n : Nat} → add n Zero ≡ n
n+zero≡n {Zero} = refl
n+zero≡n {Succ x} = begin
add (Succ x) Zero
≡⟨ refl ⟩ --{ addの定義 }
Succ (add x Zero)
≡⟨ cong (λ e → Succ e) (n+zero≡n {x}) ⟩ --{ 帰納法の仮定 }
Succ x
∎
assoc-+ : {x y z : Nat} → add x (add y z) ≡ add (add x y) z
assoc-+ {Zero} {y} {z} = begin
add Zero (add y z) --{ addの定義 }
≡⟨ refl ⟩
add y z
≡⟨ sym refl ⟩ --{ addの定義(逆方向) }
add (add Zero y) z
∎
assoc-+ {Succ x} {y} {z} = begin
add (Succ x) (add y z)
≡⟨ refl ⟩ --{ addの定義 }
Succ (add x (add y z))
≡⟨ cong (λ e → Succ e) (assoc-+ {x}) ⟩ --{ 帰納法の仮定 }
Succ (add (add x y) z)
≡⟨ sym refl ⟩ --{ addの定義(逆方向) }
add (Succ (add x y)) z
≡⟨ sym refl ⟩ --{ addの定義(逆方向) }
add (add (Succ x) y) z
∎
replicate : ∀{l}{A : Set l} → ℕ → A → List A
replicate zero _ = []
replicate (suc n) c = c ∷ (replicate n c)
replicate-length : ∀{l}{A : Set l} → {n : ℕ} → {c : A} → length (replicate n c) ≡ n
replicate-length {A = A}{n = zero} {c} = begin
length (replicate zero c)
≡⟨ refl ⟩ --{ replicateの定義 }
zero
∎
replicate-length {n = suc x} {c} = begin
length (replicate (suc x) c)
≡⟨ refl ⟩ --{ replicateの定義 }
length (c ∷ (replicate x c))
≡⟨ refl ⟩ --{ lengthの定義 }
suc (length (replicate x c))
≡⟨ cong (λ e → suc e) (replicate-length {n = x}) ⟩ --{ 帰納法の仮定 }
suc x
∎
_++_ : ∀{l}{A : Set l} → (xs ys : List A) → List A
[] ++ ys = ys
(x ∷ xs) ++ ys = x ∷ (xs ++ ys)
assoc-++ : ∀{l}{A : Set l} → {xs ys zs : List A} → (xs ++ ys) ++ zs ≡ xs ++ (ys ++ zs)
assoc-++ {xs = []} {ys} {zs} = begin
([] ++ ys) ++ zs
≡⟨ refl ⟩ --{ ++の定義 }
ys ++ zs
≡⟨ sym refl ⟩ --{ ++の定義(逆方向) }
[] ++ (ys ++ zs)
∎
assoc-++ {xs = n ∷ ns} {ys} {zs} = begin
((n ∷ ns) ++ ys) ++ zs
≡⟨ refl ⟩ --{ ++の定義 }
(n ∷ (ns ++ ys)) ++ zs
≡⟨ refl ⟩ --{ ++の定義 }
n ∷ ((ns ++ ys) ++ zs)
≡⟨ cong (λ e → n ∷ e) (assoc-++ {xs = ns}) ⟩ --{ 帰納法の仮定 }
n ∷ (ns ++ (ys ++ zs))
≡⟨ sym refl ⟩ --{ ++の定義(逆方向) }
(n ∷ ns) ++ (ys ++ zs)
∎
reverse : ∀{l}{A : Set l} → List A → List A
reverse [] = []
reverse (x ∷ xs) = (reverse xs) ++ (x ∷ [])
-- right identity
rid-++ : ∀{l}{A : Set l} → {xs : List A} → xs ++ [] ≡ xs
rid-++ {xs = []} = begin
[] ++ []
≡⟨ refl ⟩ --{ ++の定義 }
[]
∎
rid-++ {xs = n ∷ ns} = begin
(n ∷ ns) ++ []
≡⟨ refl ⟩ --{ ++の定義 }
n ∷ (ns ++ [])
≡⟨ cong (λ e → n ∷ e) (rid-++ {xs = ns}) ⟩ --{ 帰納法の定義 }
n ∷ ns
∎
dist-rev : ∀{l}{A : Set l} → {xs ys : List A} → reverse (xs ++ ys) ≡ reverse ys ++ reverse xs
dist-rev {xs = []} {ys} = begin
reverse ([] ++ ys)
≡⟨ refl ⟩ --{ ++の定義 }
reverse ys
≡⟨ sym rid-++ ⟩ --{ xs ++ [] ≡ xs }
reverse (ys ++ [])
∎
dist-rev {xs = n ∷ ns} {ys} = begin
reverse ((n ∷ ns) ++ ys)
≡⟨ refl ⟩ --{ ++の定義 }
reverse (n ∷ (ns ++ ys))
≡⟨ refl ⟩ --{ reverseの定義 }
reverse (ns ++ ys) ++ (n ∷ [])
≡⟨ cong (λ e → e ++ (n ∷ [])) (dist-rev {xs = ns}) ⟩ --{ 帰納法の仮定 }
(reverse ys ++ reverse ns) ++ (n ∷ [])
≡⟨ assoc-++ {xs = reverse ys} ⟩ --{ ++の結合律 }
reverse ys ++ (reverse ns ++ (n ∷ []))
≡⟨ sym refl ⟩ --{ ++の定義(逆方向) }
reverse ys ++ reverse (n ∷ ns)
∎
eq-revrev : ∀{l}{A : Set l} → {xs : List A} → reverse (reverse xs) ≡ xs
eq-revrev {xs = []} = begin
reverse (reverse [])
≡⟨ refl ⟩ --{ reverseの定義 }
reverse []
≡⟨ refl ⟩ --{ reverseの定義 }
[]
∎
eq-revrev {xs = n ∷ ns} = begin
reverse (reverse (n ∷ ns))
≡⟨ refl ⟩ --{ reverseの定義 }
reverse ((reverse ns) ++ (n ∷ []))
≡⟨ dist-rev {xs = reverse ns} ⟩ --{ reverseの分配律 }
reverse (n ∷ []) ++ reverse (reverse ns)
≡⟨ cong (λ e → reverse (n ∷ []) ++ e) (eq-revrev {xs = ns}) ⟩ --{ 帰納法の仮定 }
reverse (n ∷ []) ++ ns
≡⟨ refl ⟩ --{ reverseの定義 }
(reverse [] ++ (n ∷ [])) ++ ns
≡⟨ refl ⟩ --{ reverseの定義 }
([] ++ (n ∷ [])) ++ ns
≡⟨ refl ⟩ --{ ++の定義 }
(n ∷ []) ++ ns
≡⟨ refl ⟩ --{ ++の定義 }
n ∷ ([] ++ ns)
≡⟨ refl ⟩ --{ ++の定義 }
n ∷ ns
∎
reverse′ : ∀{l}{A : Set l} → List A → List A → List A
reverse′ [] ys = ys
reverse′ (x ∷ xs) ys = reverse′ xs (x ∷ ys)
rev′≡rev : ∀{l}{A : Set l} → {xs ys : List A} → reverse′ xs ys ≡ (reverse xs) ++ ys
rev′≡rev {xs = []} {ys} = begin
reverse′ [] ys
≡⟨ refl ⟩ --{ reverse′の定義 }
ys
≡⟨ sym refl ⟩ --{ ++の定義(逆方向) }
[] ++ ys
≡⟨ sym refl ⟩ --{ reverseの定義(逆方向) }
(reverse []) ++ ys
∎
rev′≡rev {xs = n ∷ ns} {ys} = begin
reverse′ (n ∷ ns) ys
≡⟨ refl ⟩ --{ reverse′の定義 }
reverse′ ns (n ∷ ys)
≡⟨ rev′≡rev {xs = ns} ⟩ --{ 帰納法の仮定 }
(reverse ns) ++ (n ∷ ys)
≡⟨ sym refl ⟩ --{ ++の定義(逆方向) }
(reverse ns) ++ ([] ++ (n ∷ ys))
≡⟨ sym refl ⟩ --{ ++の定義(逆方向) }
(reverse ns) ++ ((n ∷ []) ++ ys)
≡⟨ sym (assoc-++ {xs = reverse ns}) ⟩ --{ ++の結合律 }
((reverse ns) ++ (n ∷ [])) ++ ys
≡⟨ sym refl ⟩ --{ reverseの定義(逆方向) }
(reverse (n ∷ ns)) ++ ys
∎
data Tree : Set where
Leaf : ℕ → Tree
Node : Tree → Tree → Tree
flatten : Tree → List ℕ
flatten (Leaf n) = n ∷ []
flatten (Node l r) = (flatten l) ++ (flatten r)
flatten′ : Tree → List ℕ → List ℕ
flatten′ (Leaf n) ns = n ∷ ns
flatten′ (Node l r) ns = flatten′ l (flatten′ r ns)
-- flatten′ t ns = (faltten t) ++ ns
fla′≡fla : {t : Tree} → {ns : List ℕ} → flatten′ t ns ≡ (flatten t) ++ ns
fla′≡fla {Leaf n} {ns} = begin
flatten′ (Leaf n) ns
≡⟨ refl ⟩ --{ flatten′の定義 }
n ∷ ns
≡⟨ sym refl ⟩ --{ ++の定義(逆方向) }
[] ++ (n ∷ ns)
≡⟨ sym refl ⟩ --{ ++の定義(逆方向) }
(n ∷ []) ++ ns
≡⟨ sym refl ⟩ --{ flattenの定義(逆方向) }
(flatten (Leaf n)) ++ ns
∎
fla′≡fla {Node l r} {ns} = begin
flatten′ (Node l r) ns
≡⟨ refl ⟩ --{ flatten′の定義 }
flatten′ l (flatten′ r ns)
≡⟨ fla′≡fla {t = l} ⟩ --{ 帰納法の仮定 }
(flatten l) ++ (flatten′ r ns)
≡⟨ cong (λ e → (flatten l) ++ e) (fla′≡fla {t = r}) ⟩ --{ 帰納法の仮定 }
(flatten l) ++ ((flatten r) ++ ns)
≡⟨ sym (assoc-++ {xs = flatten l}) ⟩ --{ ++の結合律 }
((flatten l) ++ (flatten r)) ++ ns
≡⟨ sym refl ⟩ --{ flattenの定義(逆方向) }
(flatten (Node l r)) ++ ns
∎
-- exercise 13.9
-- 1 重複のあるパターンで定義されている標準ライブラリ関数
-- min, max, toLower, toUpper, takeWhile, dropWhile, zip
-- 2
succ-+ : {n m : Nat} → add n (Succ m) ≡ Succ (add n m)
succ-+ {Zero} {m} = begin
add Zero (Succ m)
≡⟨ refl ⟩ --{ addの定義 }
Succ m
≡⟨ sym refl ⟩ --{ addの定義(逆方向) }
Succ (add Zero m)
∎
succ-+ {Succ n} {m} = begin
add (Succ n) (Succ m)
≡⟨ refl ⟩ --{ addの定義 }
Succ (add n (Succ m))
≡⟨ cong (λ e → Succ e) (succ-+ {n}) ⟩ --{ 帰納法の仮定 }
Succ (Succ (add n m))
≡⟨ refl ⟩ --{ addの定義 }
Succ (add (Succ n) m)
∎
-- 3
comm-+ : {n m : Nat} → add n m ≡ add m n
comm-+ {Zero} {m} = begin
add Zero m
≡⟨ refl ⟩ --{ addの定義 }
m
≡⟨ sym n+zero≡n ⟩ --{ add n Zero = n }
add m Zero
∎
comm-+ {Succ n} {m} = begin
add (Succ n) m
≡⟨ refl ⟩ --{ addの定義 }
Succ (add n m)
≡⟨ cong (λ e → Succ e) (comm-+ {n}) ⟩ --{ 帰納法の仮定 }
Succ (add m n)
≡⟨ sym refl ⟩ --{ addの定義(逆方向) }
add (Succ m) n
≡⟨ cong (λ e → add e n) (sym (a+1≡succ-a {m})) ⟩ --{ add a (Succ Zero) = succ a }
add (add m (Succ Zero)) n
≡⟨ sym (assoc-+ {m}) ⟩ --{ addの結合律 }
add m (add (Succ Zero) n)
≡⟨ refl ⟩ --{ addの定義 }
add m (Succ (add Zero n))
≡⟨ refl ⟩ --{ addの定義 }
add m (Succ n)
∎
where a+1≡succ-a : {a : Nat} → add a (Succ Zero) ≡ Succ a
a+1≡succ-a {Zero} = begin
add Zero (Succ Zero)
≡⟨ refl ⟩ --{ addの定義 }
Succ Zero
∎
a+1≡succ-a {Succ a} = begin
add (Succ a) (Succ Zero)
≡⟨ refl ⟩ --{ addの定義 }
Succ (add a (Succ Zero))
≡⟨ cong (λ e → Succ e) a+1≡succ-a ⟩ --{ 帰納法の仮定 }
Succ (Succ a)
∎
{-
別解、問題2で示した add a (Succ b) ≡ Succ (add a b) を用いる。
add (Succ n) m
≡⟨ addの定義 ⟩
Succ (add n m)
≡⟨ 帰納法の仮定 ⟩
Succ (add m n)
≡⟨ 問題2 ⟩
add n (Succ m)
≡⟨ 問題2 ⟩
-}
-- 4
all : ∀{l}{A : Set l} → (A → Bool) → List A → Bool
all _ [] = true
all p (x ∷ xs) = p x ∧ all p xs
{--
replicate-eq : {n : ℕ} → ∀{l}{A : Set l} → {x : A} → all (== x) (replicate n x) ≡ true
replicate-eq = {!!}
一般の型に対して == のような等式は定義できないため、Agdaでの証明は略する。
i) n = 0 のとき
all (== x) (replicate zero x)
≡⟨ replicate の定義 ⟩
all (== x) []
≡⟨ all の定義 ⟩
true
ii) n = m + 1 のとき
all (== x) (replicate (m + 1) x)
≡⟨ replicate の定義 ⟩
all (== x) (x ∷ (replicate m x))
≡⟨ all の定義 ⟩
(x == x) ^ all (== x) (replicate m x)
≡⟨ == の反射律 ⟩
true ^ all (== x) (replicate m x)
≡⟨ ^ の定義 ⟩
all (== x) (replicate m x)
≡⟨ m に対する仮定 ⟩
true
--}
-- 5 すでに証明済み
-- rid-++
-- assoc-++
-- 6
rev-lemma : ∀{l}{A : Set l} → {x : A} → {xs : List A} → reverse (xs ++ (x ∷ [])) ≡ x ∷ reverse xs
rev-lemma {x = x} {xs = []} = begin
reverse ([] ++ (x ∷ []))
≡⟨ refl ⟩ --{ ++の定義 }
reverse (x ∷ [])
≡⟨ refl ⟩ --{ reverseの定義 }
reverse [] ++ (x ∷ [])
≡⟨ refl ⟩ --{ reverseの定義 }
[] ++ (x ∷ [])
≡⟨ refl ⟩ --{ ++の定義 }
x ∷ []
≡⟨ sym refl ⟩ --{ reverseの定義(逆方向) }
x ∷ reverse []
∎
rev-lemma {x = x} {xs = n ∷ ns} = begin
reverse ((n ∷ ns) ++ (x ∷ []))
≡⟨ refl ⟩ --{ ++の定義 }
reverse (n ∷ (ns ++ (x ∷ [])))
≡⟨ refl ⟩ --{ reverseの定義 }
(reverse (ns ++ (x ∷ []))) ++ (n ∷ [])
≡⟨ cong (λ e → e ++ (n ∷ [])) (rev-lemma {xs = ns}) ⟩ --{ 帰納法の仮定 }
(x ∷ reverse ns) ++ (n ∷ [])
≡⟨ refl ⟩ --{ ++の定義 }
x ∷ (reverse ns ++ (n ∷ []))
≡⟨ sym refl ⟩ --{ reverseの定義(逆方向) }
x ∷ reverse (n ∷ ns)
∎
eq-revrev′ : ∀{l}{A : Set l} → {xs : List A} → reverse (reverse xs) ≡ xs
eq-revrev′ {xs = []} = begin
reverse (reverse [])
≡⟨ refl ⟩ --{ reverseの定義 }
reverse []
≡⟨ refl ⟩ --{ reverseの定義 }
[]
∎
eq-revrev′ {xs = n ∷ ns} = begin
reverse (reverse (n ∷ ns))
≡⟨ refl ⟩ --{ reverseの定義 }
reverse ((reverse ns) ++ (n ∷ []))
≡⟨ rev-lemma {xs = reverse ns} ⟩ --{ reverse (xs ++ [x]) ≡ x ∷ reverse xs }
n ∷ (reverse (reverse ns))
≡⟨ cong (λ e → n ∷ e) (eq-revrev′ {xs = ns}) ⟩ --{ 帰納法の仮定 }
n ∷ ns
∎
-- 7
map : ∀{l}{A B : Set l} → (f : A → B) → List A → List B
map f [] = []
map f (x ∷ xs) = f x ∷ map f xs
_∘_ : ∀{l}{A B C : Set l} → (f : B → C) → (g : A → B) → A → C
(f ∘ g) x = f (g x)
map-comp : ∀{l}{A B C : Set l} → {f : B → C} → {g : A → B} → {xs : List A} →
map f (map g xs) ≡ map (f ∘ g) xs
map-comp {f = f} {g} {xs = []} = begin
map f (map g [])
≡⟨ refl ⟩ --{ mapの定義 }
map f []
≡⟨ refl ⟩ --{ mapの定義 }
[]
≡⟨ sym refl ⟩ --{ mapの定義(逆方向) }
map (f ∘ g) []
∎
map-comp {f = f} {g} {xs = n ∷ ns} = begin
map f (map g (n ∷ ns))
≡⟨ refl ⟩ --{ mapの定義 }
map f (g n ∷ map g ns)
≡⟨ refl ⟩ --{ mapの定義 }
f (g n) ∷ map f (map g ns)
≡⟨ cong (λ e → f (g n) ∷ e) (map-comp {xs = ns}) ⟩ --{ 帰納法の仮定 }
f (g n) ∷ map (f ∘ g) ns
≡⟨ sym refl ⟩ --{ ∘の定義(逆方向) }
(f ∘ g) n ∷ map (f ∘ g) ns
≡⟨ sym refl ⟩ --{ mapの定義(逆方向) }
map (f ∘ g) (n ∷ ns)
∎
-- 8
take : ∀{l}{A : Set l} → ℕ → List A → List A
take zero _ = []
take (suc n) [] = []
take (suc n) (x ∷ xs) = x ∷ (take n xs)
drop : ∀{l}{A : Set l} → ℕ → List A → List A
drop zero xs = xs
drop (suc n) [] = []
drop (suc n) (x ∷ xs) = drop n xs
take-drop : ∀{l}{A : Set l} → {n : ℕ} → {xs : List A} → take n xs ++ drop n xs ≡ xs
take-drop {n = zero} {xs = []} = begin
take zero [] ++ drop zero []
≡⟨ refl ⟩ --{ takeの定義 }
[] ++ drop zero []
≡⟨ refl ⟩ --{ dropの定義 }
drop zero []
≡⟨ refl ⟩
[]
∎
take-drop {n = zero} {xs = x ∷ xs} = begin
take zero (x ∷ xs) ++ drop zero (x ∷ xs)
≡⟨ refl ⟩ --{ takeの定義 }
[] ++ drop zero (x ∷ xs)
≡⟨ refl ⟩ --{ ++の定義 }
drop zero (x ∷ xs)
≡⟨ refl ⟩ --{ dropの定義 }
x ∷ xs
∎
take-drop {n = suc n} {xs = []} = begin
take (suc n) [] ++ drop (suc n) []
≡⟨ refl ⟩ --{ takeの定義 }
[] ++ drop (suc n) []
≡⟨ refl ⟩ --{ ++の定義 }
drop (suc n) []
≡⟨ refl ⟩ --{ dropの定義 }
[]
∎
take-drop {n = suc n} {xs = x ∷ xs} = begin
take (suc n) (x ∷ xs) ++ drop (suc n) (x ∷ xs)
≡⟨ refl ⟩ --{ takeの定義 }
(x ∷ take n xs) ++ drop (suc n) (x ∷ xs)
≡⟨ refl ⟩ --{ ++の定義 }
x ∷ (take n xs ++ drop (suc n) (x ∷ xs))
≡⟨ refl ⟩ --{ dropの定義 }
x ∷ (take n xs ++ drop n xs)
≡⟨ cong (λ e → x ∷ e) (take-drop {n = n} {xs = xs}) ⟩ --{ 帰納法の仮定 }
x ∷ xs
∎
-- 9
-- ***** 整数順序の定義、定理 *************************** --
data _<=_ : Rel Nat Level.zero where
z<=n : {n : Nat} → Zero <= n
s<=s : {m n : Nat} (m<=n : m <= n) → Succ m <= Succ n
eq→<= : {x y : Nat} → x ≡ y → x <= y
eq→<= {Zero} {Zero} refl = z<=n
eq→<= {Zero} {Succ y} ()
eq→<= {Succ x} {Zero} ()
eq→<= {Succ x} {Succ y} suc-x≡suc-y = s<=s (eq→<= x≡y)
where ≡-suc : {a b : Nat} → Succ a ≡ Succ b → a ≡ b
≡-suc refl = refl
x≡y : x ≡ y
x≡y = ≡-suc suc-x≡suc-y
<=trans : {x y z : Nat} → x <= y → y <= z → x <= z
<=trans {Zero} {_} {z} x<=y y<=z = z<=n
<=trans {Succ x}{Zero} {z} ()
<=trans {Succ x}{Succ y}{Zero} suc-x<=suc-y ()
<=trans {Succ x}{Succ y}{Succ z} (s<=s x<=y) (s<=s y<=z) = s<=s (<=trans x<=y y<=z)
<=+n : {a b : Nat} → a <= add a b
<=+n {Zero} = z<=n
<=+n {Succ a} = s<=s (<=+n {a})
a+c<=b+d : {a b c d : Nat} → a <= b → c <= d → add a c <= add b d
a+c<=b+d {Zero} {b} {c} {d} z<=n c<=d = <=trans p1 p2
where p1 : c <= add d b
p1 = <=trans (c<=d) (<=+n)
p2 : add d b <= add b d
p2 = eq→<= (comm-+ {d})
a+c<=b+d {Succ a}{Zero} ()
a+c<=b+d {Succ a}{Succ b}{Zero} {d} (s<=s a<=b) z<=n = s<=s (<=trans p2 p1)
where p1 : a <= add b d
p1 = <=trans a<=b <=+n
p2 : add a Zero <= a
p2 = eq→<= n+zero≡n
a+c<=b+d {Succ a}{Succ b}{Succ c}{Zero} _ ()
a+c<=b+d {Succ a}{Succ b}{Succ c}{Succ d} (s<=s a<=b) (s<=s c<=d) = s<=s p4
where p1 : add a c <= add b d
p1 = a+c<=b+d a<=b c<=d
p2 : add a (Succ c) <= Succ (add a c)
p2 = eq→<= (succ-+ {a})
p3 : Succ (add b d) <= add b (Succ d)
p3 = eq→<= (sym (succ-+ {b}))
p4 : add a (Succ c) <= add b (Succ d)
p4 = <=trans p2 (<=trans (s<=s p1) p3)
-- ****************************************************** --
cnt-leaf : Tree → Nat
cnt-leaf (Leaf _) = Succ Zero
cnt-leaf (Node l r) = add (cnt-leaf l) (cnt-leaf r)
cnt-node : Tree → Nat
cnt-node (Leaf _) = Zero
cnt-node (Node l r) = Succ (add (cnt-node l) (cnt-node r))
tree-lemm : {t : Tree} → Succ (cnt-node t) <= cnt-leaf t
tree-lemm {Leaf n} = eq→<= p
where p : Succ (cnt-node (Leaf n)) ≡ cnt-leaf (Leaf n)
p = begin
Succ (cnt-node (Leaf n))
≡⟨ refl ⟩ --{ cnt-nodeの定義 }
Succ Zero
≡⟨ sym refl ⟩ --{ cnt-leafの定義(逆方向) }
cnt-leaf (Leaf n)
∎
tree-lemm {Node l r} = <=trans p4 p3
where for-l : Succ (cnt-node l) <= cnt-leaf l
for-l = tree-lemm {l}
for-r : Succ (cnt-node r) <= cnt-leaf r
for-r = tree-lemm {r}
p1 : add (Succ (cnt-node l)) (Succ (cnt-node r)) <= add (cnt-leaf l) (cnt-leaf r)
p1 = a+c<=b+d for-l for-r
p2 : add (Succ (cnt-node l)) (Succ (cnt-node r)) ≡ Succ (add (Succ (cnt-node l)) (cnt-node r))
p2 = succ-+ {(Succ (cnt-node l))}
p3 : Succ (add (Succ (cnt-node l)) (cnt-node r)) <= add (cnt-leaf l) (cnt-leaf r)
p3 = <=trans (eq→<= (sym p2)) p1
p4 : Succ (cnt-node (Node l r)) <= Succ (add (Succ (cnt-node l)) (cnt-node r))
p4 = eq→<= refl
-- 10
-- これから
-- cong (λ∎
-- ∎
-- ℕ
-- assoc-+ add x (add y z) ≡ add (add x y) z