-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBin0.agda
More file actions
701 lines (575 loc) · 26 KB
/
Bin0.agda
File metadata and controls
701 lines (575 loc) · 26 KB
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
{-
This file is a part of the library Binary-4.
Copyright © 2018 Program Systems Institute of Russian Academy of Sciences.
Binary-4 is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License.
See license.txt.
-}
module Bin0 where
open import Level using () renaming (zero to 0ℓ)
open import Function using (id; _∘_; case_of_)
open import Relation.Nullary using (yes; no)
open import Relation.Nullary.Negation using (contradiction)
open import Relation.Binary using (Rel; Setoid; DecSetoid; IsDecEquivalence)
renaming (Decidable to Decidable₂)
open import Relation.Binary.PropositionalEquality as PE
using (_≡_; _≢_; _≗_; refl; sym; trans;
cong; cong₂; isEquivalence)
open PE.≡-Reasoning
open import Algebra using (Semigroup; Monoid; CommutativeMonoid)
open import Algebra.Structures using (IsSemigroup; IsMonoid;
IsCommutativeMonoid)
open import Algebra.FunctionProperties as FuncProp using (Op₂)
open import Data.Sum using (_⊎_; inj₁; inj₂)
open import Data.Product using (_×_; _,_; proj₁; ∃)
open import Data.List using (List; []; _∷_)
open import Data.Nat using (ℕ)
renaming (suc to 1+_; pred to pred'; _+_ to _+'_;
_*_ to _*'_; _∸_ to _∸'_; _≤_ to _≤'_
)
-- Renaming foo -> foo' is applied in order to distinguish the
-- corresponding operators from their Bin versions.
--
open import Data.Nat.Properties as NatP using (module ≤-Reasoning)
renaming (+-comm to +'-comm; +-assoc to +'-assoc;
*-distribˡ-+ to *'-lDistrib; *-cancelˡ-≡ to *'-lCancel
)
open ≤-Reasoning using () renaming (begin_ to ≤begin_; _∎ to _≤end;
_≡⟨_⟩_ to _≡≤[_]_; _≤⟨_⟩_ to _≤[_]_)
-- of application ---
open import BPrelude using (≢sym; Injective; Surjective)
open import Nat0 using (k+[m+n]≡m+[k+n])
--****************************************************************************
data Bin : Set where
0# : Bin
2suc : Bin → Bin -- \n→ 2*(1+n) arbitrary nonzero even
suc2* : Bin → Bin -- \n→ 1 + 2n arbitrary odd
--
-- This representation is unique for each natural number.
-- Decidable equality on Bin -------------------------------------------------
2suc-injective : Injective 2suc
2suc-injective {0#} {0#} _ = refl
2suc-injective {0#} {2suc _} ()
2suc-injective {0#} {suc2* _} ()
2suc-injective {2suc _} {0#} ()
2suc-injective {2suc _} {2suc _} refl = refl
2suc-injective {2suc _} {suc2* _} ()
2suc-injective {suc2* _} {0#} ()
2suc-injective {suc2* _} {2suc _} ()
2suc-injective {suc2* _} {suc2* _} refl = refl
suc2*-injective : Injective suc2*
suc2*-injective {0#} {0#} _ = refl
suc2*-injective {0#} {2suc _} ()
suc2*-injective {0#} {suc2* _} ()
suc2*-injective {2suc _} {0#} ()
suc2*-injective {2suc _} {2suc _} refl = refl
suc2*-injective {2suc _} {suc2* _} ()
suc2*-injective {suc2* _} {0#} ()
suc2*-injective {suc2* _} {2suc _} ()
suc2*-injective {suc2* _} {suc2* _} refl = refl
_≟_ : Decidable₂ (_≡_ {A = Bin})
0# ≟ 0# = yes refl
0# ≟ (2suc _) = no λ()
0# ≟ (suc2* _) = no λ()
(2suc _) ≟ 0# = no λ()
(2suc x) ≟ (2suc y) = case x ≟ y
of \
{ (yes eq) → yes (cong 2suc eq)
; (no ne) → no (\x'≡y' → ne (2suc-injective x'≡y'))
}
(2suc _) ≟ (suc2* _) = no λ()
(suc2* _) ≟ 0# = no λ()
(suc2* _) ≟ (2suc _) = no λ()
(suc2* x) ≟ (suc2* y) = case x ≟ y
of \
{ (yes eq) → yes (cong suc2* eq)
; (no ne) → no (\x'≡y' → ne (suc2*-injective x'≡y'))
}
2suc≢0 : {x : Bin} → 2suc x ≢ 0#
2suc≢0 ()
suc2*≢0 : {x : Bin} → suc2* x ≢ 0#
suc2*≢0 ()
----------------------------------------------------
size : Bin → ℕ -- (number of constructors) - 1
size 0# = 0
size (2suc x) = 1+ (size x)
size (suc2* x) = 1+ (size x)
|x|≡0⇒x≡0 : {x : Bin} → size x ≡ 0 → x ≡ 0#
|x|≡0⇒x≡0 {0#} _ = refl
|x|≡0⇒x≡0 {2suc _} ()
|x|≡0⇒x≡0 {suc2* _} ()
--============================================================================
-- Arithmetic operations on Bin and their properties.
suc : Bin → Bin
suc 0# = suc2* 0#
suc (2suc x) = suc2* (suc x) -- 1 + 2(1+x)
suc (suc2* x) = 2suc x -- 1 + 1 + 2x = 2*(1+x)
1# = suc 0#; 2# = suc 1#; 3# = suc 2#; 4# = suc 3#; 5# = suc 4#
---------------------------
suc≢0 : ∀ {x} → suc x ≢ 0#
suc≢0 {0#} ()
suc≢0 {2suc _} ()
suc≢0 {suc2* _} ()
------------
infixl 6 _+_
_+_ : Op₂ Bin
0# + y = y
x + 0# = x
(2suc x) + (2suc y) = 2suc (suc (x + y))
-- 2(1+x) + 2(1+y) = 2(1 + 1+x+y)
(2suc x) + (suc2* y) = suc (2suc (x + y))
-- 2(1+x) + 1 + 2y = 1 + 2(1+x+y)
(suc2* x) + (2suc y) = suc (2suc (x + y))
(suc2* x) + (suc2* y) = suc (suc2* (x + y))
-- 1+2x + 1+2y = 2 + 2(x+y) = 1 + 1 + 2(x+y)
--------------------
sum : List Bin → Bin
sum [] = 0#
sum (x ∷ xs) = x + (sum xs)
--------------
2* : Bin → Bin
2* 0# = 0#
2* (2suc x) = 2suc (suc2* x)
-- 2(1+x) + 2(1+x) = 2(1+x + 1+x) = 2(1 + 1+2x)
2* (suc2* x) = 2suc (2* x)
----------------
pred : Bin → Bin
pred 0# = 0#
pred (2suc x) = suc2* x -- 2(1+x) - 1 = 1+2x
pred (suc2* x) = 2* x -- 1 + 2x -1 = 2x
---------------------------
2suc-as∘ : 2suc ≗ 2* ∘ suc
2suc-as∘ 0# = refl
2suc-as∘ (2suc x) =
begin 2suc (2suc x) ≡⟨ cong 2suc (2suc-as∘ x) ⟩
2suc (2* (suc x)) ≡⟨ refl ⟩
2* (suc2* (suc x)) ≡⟨ refl ⟩
2* (suc (2suc x))
∎
2suc-as∘ (suc2* x) = refl
-- suc2*∘suc≗suc∘2suc : suc2* ∘ suc ≗ suc ∘ 2suc -- is by const refl
suc2*-as∘ : suc2* ≗ suc ∘ 2*
suc2*-as∘ 0# = refl
suc2*-as∘ (2suc x) = refl
suc2*-as∘ (suc2* x) =
begin suc2* (suc2* x) ≡⟨ cong suc2* (suc2*-as∘ x) ⟩
suc2* (suc 2x) ≡⟨ refl ⟩
suc (2suc 2x) ≡⟨ cong suc (2suc-as∘ 2x) ⟩
suc (2* (suc 2x)) ≡⟨ cong (suc ∘ 2*) (sym (suc2*-as∘ x)) ⟩
suc (2* (suc2* x))
∎
where
2x = 2* x
---------------------------
pred∘suc : pred ∘ suc ≗ id
pred∘suc 0# = refl
pred∘suc (2suc x) = sym (2suc-as∘ x)
pred∘suc (suc2* x) = refl
suc∘pred : (x : Bin) → x ≢ 0# → suc (pred x) ≡ x
suc∘pred 0# 0≢0 = contradiction refl 0≢0
suc∘pred (2suc _) _ = refl
suc∘pred (suc2* x) _ = begin suc (pred (suc2* x)) ≡⟨ refl ⟩
suc (2* x) ≡⟨ sym (suc2*-as∘ x) ⟩
suc2* x
∎
----------------------------
0+ : (x : Bin) → 0# + x ≡ x
0+ _ = refl
+0 : (x : Bin) → x + 0# ≡ x
+0 0# = refl
+0 (2suc _) = refl
+0 (suc2* _) = refl
-------------
2^ : ℕ → Bin
2^ 0 = 1#
2^ (1+ n) = 2* (2^ n)
------------
infixl 7 _*_
_*_ : Op₂ Bin
0# * _ = 0#
_ * 0# = 0#
(2suc x) * (2suc y) = 2* (2suc (x + (y + x * y)))
(2suc x) * (suc2* y) = 2suc (x + y * (2suc x))
--
-- 2(1+x) * (1+2y) = 2(1 + 2y + x + 2xy)
-- = 2(1 + x + y*2(1 + x))
(suc2* x) * (2suc y) = 2suc (y + x * (2suc y))
(suc2* x) * (suc2* y) = suc2* (x + y * (suc2* x))
--
-- (1 + 2x)(1 + 2y) = 1 + (2y + 2x + 4xy)
-- 1 + 2(x + y * (1 + 2x))
-----------------------------
infixl 8 _^_
_^_ : Bin → ℕ → Bin
_ ^ 0 = 1#
x ^ (1+ n) = x * (x ^ n)
-----------------------------
0* : (x : Bin) → 0# * x ≡ 0#
0* _ = refl
*0 : (x : Bin) → x * 0# ≡ 0#
*0 0# = refl
*0 (2suc _) = refl
*0 (suc2* _) = refl
x*y≡0⇒⊎ : {x y : Bin} → x * y ≡ 0# → x ≡ 0# ⊎ y ≡ 0#
x*y≡0⇒⊎ {0#} {_ } _ = inj₁ refl
x*y≡0⇒⊎ {_} {0#} _ = inj₂ refl
x*y≡0⇒⊎ {2suc _} {2suc _} ()
x*y≡0⇒⊎ {2suc _} {suc2* _} ()
x*y≡0⇒⊎ {suc2* _} {2suc _} ()
x*y≡0⇒⊎ {suc2* _} {suc2* _} ()
nz*nz : {x y : Bin} → x ≢ 0# → y ≢ 0# → x * y ≢ 0#
nz*nz x≢0 y≢0 xy≡0 =
case x*y≡0⇒⊎ xy≡0 of \ { (inj₁ x≡0) → x≢0 x≡0
; (inj₂ y≡0) → y≢0 y≡0 }
infix 4 _∣_
_∣_ : Rel Bin 0ℓ
x ∣ y = ∃ (\q → x * q ≡ y)
2*≢1 : {x : Bin} → 2* x ≢ 1#
2*≢1 {0#} ()
2*≢1 {2suc _} ()
2*≢1 {suc2* _} ()
------------------------------------------------------------------------------
suc≗1+ : suc ≗ (1# +_)
suc≗1+ 0# = refl
suc≗1+ (2suc _) = refl
suc≗1+ (suc2* _) = refl
----------------------------------------------------
suc+assoc : (x y : Bin) → (suc x) + y ≡ suc (x + y)
suc+assoc 0# y = begin suc 0# + y ≡⟨ refl ⟩
1# + y ≡⟨ sym (suc≗1+ y) ⟩
suc y ≡⟨ refl ⟩
suc (0# + y)
∎
suc+assoc x 0# = begin suc x + 0# ≡⟨ +0 (suc x) ⟩
suc x ≡⟨ cong suc (sym (+0 x)) ⟩
suc (x + 0#)
∎
suc+assoc (2suc x) (2suc y) =
begin
(suc (2suc x)) + (2suc y) ≡⟨ refl ⟩
suc2* (suc x) + (2suc y) ≡⟨ refl ⟩
suc (2suc (suc x + y)) ≡⟨ cong (suc ∘ 2suc) (suc+assoc x y) ⟩
suc (2suc (suc (x + y))) ≡⟨ refl ⟩
suc ((2suc x) + (2suc y))
∎
suc+assoc (2suc x) (suc2* y) =
begin
(suc (2suc x)) + (suc2* y) ≡⟨ refl ⟩
(suc2* (suc x)) + (suc2* y) ≡⟨ refl ⟩
suc (suc2* (suc x + y)) ≡⟨ cong (suc ∘ suc2*) (suc+assoc x y) ⟩
suc (suc2* (suc (x + y))) ≡⟨ refl ⟩
suc (suc (2suc (x + y))) ≡⟨ refl ⟩
suc ((2suc x) + (suc2* y))
∎
suc+assoc (suc2* x) (2suc y) = refl
suc+assoc (suc2* x) (suc2* y) = refl
1+≗suc : (1# +_) ≗ suc
1+≗suc = suc+assoc 0#
-------------
infixr 7 2*'_
toℕ : Bin → ℕ
toℕ 0# = 0
toℕ (2suc x) = 2 *' (1+ (toℕ x))
toℕ (suc2* x) = 1+ (2 *' (toℕ x))
2*'_ : ℕ → ℕ
2*'_ = (2 *'_)
toℕ∘2* : (x : Bin) → toℕ (2* x) ≡ 2*' (toℕ x)
toℕ∘2* 0# = refl
toℕ∘2* (2suc x) =
begin
toℕ (2* (2suc x)) ≡⟨ refl ⟩
2*' (2 +' 2m) ≡⟨ cong (2*'_ ∘ (_+' 2m)) (sym (Nat0.*1 2)) ⟩
2*' (2*' 1 +' 2m) ≡⟨ cong 2*'_ (sym (*'-lDistrib 2 1 m)) ⟩
2*' (2*' (1+ m)) ≡⟨ refl ⟩
2*' (toℕ (2suc x))
∎
where
m = toℕ x; 2m = 2*' m
toℕ∘2* (suc2* x) = cong (2*'_ ∘ 1+_) (toℕ∘2* x)
-----------------------------------------------
toℕ∘suc : (x : Bin) → toℕ (suc x) ≡ 1+ (toℕ x)
toℕ∘suc 0# = refl
toℕ∘suc (2suc x) = cong (1+_ ∘ (2 *'_)) (toℕ∘suc x)
toℕ∘suc (suc2* x) = *'-lDistrib 2 1 (toℕ x)
toℕ∘pred : (x : Bin) → toℕ (pred x) ≡ pred' (toℕ x)
toℕ∘pred x
with x ≟ 0#
... | yes x≡0 = begin toℕ (pred x) ≡⟨ cong (toℕ ∘ pred) x≡0 ⟩
0 ≡⟨ sym (cong (pred' ∘ toℕ) x≡0) ⟩
pred' (toℕ x)
∎
... | no x≢0 =
begin toℕ (pred x) ≡⟨ cong (toℕ ∘ pred) (sym suc-px≡x) ⟩
toℕ (pred (suc px)) ≡⟨ cong toℕ (pred∘suc px) ⟩
toℕ px ≡⟨ refl ⟩
pred' (1+ (toℕ px)) ≡⟨ cong pred' (sym (toℕ∘suc px)) ⟩
pred' (toℕ (suc px)) ≡⟨ cong (pred' ∘ toℕ) suc-px≡x ⟩
pred' (toℕ x)
∎
where
px = pred x; suc-px≡x = suc∘pred x x≢0
------------------------------------------------------------------------------
toℕ+homo : (x y : Bin) → toℕ (x + y) ≡ toℕ x +' toℕ y
toℕ+homo 0# _ = refl
toℕ+homo x 0# = begin toℕ (x + 0#) ≡⟨ cong toℕ (+0 x) ⟩
toℕ x ≡⟨ sym (+'-comm (toℕ x) 0) ⟩
toℕ x +' 0 ≡⟨ refl ⟩
toℕ x +' toℕ 0#
∎
toℕ+homo (2suc x) (2suc y) =
begin
toℕ ((2suc x) + (2suc y)) ≡⟨ refl ⟩
toℕ (2suc (suc (x + y))) ≡⟨ refl ⟩
2 *' (1+ (toℕ (suc (x + y)))) ≡⟨ cong (2*'_ ∘ 1+_) (toℕ∘suc (x + y)) ⟩
2 *' (1+ (1+ toℕ (x + y))) ≡⟨ cong (2*'_ ∘ 1+_ ∘ 1+_)
(toℕ+homo x y) ⟩
2 *' ((1+ (1+ (m +' n)))) ≡⟨ cong (2*'_ ∘ 1+_)
(sym (+'-assoc 1 m n))⟩
2 *' ((1+ ((1+ m) +' n))) ≡⟨ cong (2*'_ ∘ 1+_ ∘ (_+' n))
(+'-comm 1 m) ⟩
2 *' ((1+ ((m +' 1) +' n))) ≡⟨ cong (2*'_ ∘ 1+_) (+'-assoc m 1 n) ⟩
2 *' ((1+ ((m +' (1+ n))))) ≡⟨ cong 2*'_
(sym (+'-assoc 1 m (1+ n))) ⟩
2 *' ((1+ m) +' (1+ n)) ≡⟨ *'-lDistrib 2 (1+ m) (1+ n) ⟩
(2*' (1+ m)) +' (2*' (1+ n)) ≡⟨ refl ⟩
toℕ (2suc x) +' toℕ (2suc y)
∎
where
m = toℕ x; n = toℕ y
toℕ+homo (2suc x) (suc2* y) =
begin
toℕ ((2suc x) + (suc2* y)) ≡⟨ refl ⟩
toℕ (suc (2suc (x + y))) ≡⟨ toℕ∘suc (2suc (x + y)) ⟩
1+ (toℕ (2suc (x + y))) ≡⟨ refl ⟩
1+ (2*' (1+ (toℕ (x + y)))) ≡⟨ cong (1+_ ∘ 2*'_ ∘ 1+_) (toℕ+homo x y) ⟩
1+ (2*' (1+ (m +' n))) ≡⟨ cong (1+_ ∘ 2*'_)
(sym (+'-assoc 1 m n)) ⟩
1+ (2*' (1+m +' n)) ≡⟨ cong 1+_ (*'-lDistrib 2 1+m n) ⟩
1+ ((2*' 1+m) +' 2*' n) ≡⟨ k+[m+n]≡m+[k+n] 1 _ (2*' n) ⟩
(2*' 1+m) +' (1+ (2*' n)) ≡⟨ refl ⟩
toℕ (2suc x) +' toℕ (suc2* y)
∎
where
m = toℕ x; n = toℕ y; 1+m = 1+ m
toℕ+homo (suc2* x) (2suc y) =
begin
toℕ ((suc2* x) + (2suc y)) ≡⟨ refl ⟩
toℕ (suc (2suc (x + y))) ≡⟨ toℕ∘suc (2suc (x + y)) ⟩
1+ (toℕ (2suc (x + y))) ≡⟨ refl ⟩
1+ (2*' (1+ (toℕ (x + y)))) ≡⟨ cong (1+_ ∘ 2*'_ ∘ 1+_) (toℕ+homo x y)
⟩
1+ (2*' (1+ (m +' n))) ≡⟨ cong (1+_ ∘ 2*'_)
(k+[m+n]≡m+[k+n] 1 m n) ⟩
1+ (2*' (m +' 1+n)) ≡⟨ cong 1+_ (*'-lDistrib 2 m 1+n) ⟩
1+ (2*' m +' 2*' 1+n) ≡⟨ sym (+'-assoc 1 (2*' m) (2*' 1+n)) ⟩
(1+ (2*' m)) +' (2*' (1+ n)) ≡⟨ refl ⟩
toℕ (suc2* x) +' toℕ (2suc y)
∎
where
m = toℕ x; n = toℕ y; 1+n = 1+ n
toℕ+homo (suc2* x) (suc2* y) =
begin
toℕ ((suc2* x) + (suc2* y)) ≡⟨ refl ⟩
toℕ (suc (suc2* (x + y))) ≡⟨ toℕ∘suc (suc2* (x + y)) ⟩
1+ (toℕ (suc2* (x + y))) ≡⟨ refl ⟩
1+ (1+ (2*' (toℕ (x + y)))) ≡⟨ cong (1+_ ∘ 1+_ ∘ 2*'_) (toℕ+homo x y) ⟩
1+ (1+ (2*' (m +' n))) ≡⟨ cong (1+_ ∘ 1+_) (*'-lDistrib 2 m n) ⟩
1+ (1+ (2*' m +' 2*' n)) ≡⟨ cong 1+_ (sym (+'-assoc 1 (2*' m) _)) ⟩
1+ ((1+ (2*' m) +' (2*' n))) ≡⟨ k+[m+n]≡m+[k+n] 1 (1+ (2*' m)) _ ⟩
(1+ (2*' m)) +' (1+ (2*' n)) ≡⟨ refl ⟩
toℕ (suc2* x) +' toℕ (suc2* y)
∎
where
m = toℕ x; n = toℕ y
-------------------------------------
fromℕ : (n : ℕ) → ∃ (\x → toℕ x ≡ n) -- Mind: it costs O(n)
fromℕ 0 = (0# , refl)
fromℕ (1+ n) = aux (fromℕ n)
where
aux : ∃ (\x → toℕ x ≡ n) → ∃ (\y → toℕ y ≡ 1+ n)
aux (x , toℕ-x≡n) = (suc x , toℕ-suc-x≡1+n)
where
toℕ-suc-x≡1+n = begin toℕ (suc x) ≡⟨ toℕ∘suc x ⟩
1+ (toℕ x) ≡⟨ cong 1+_ toℕ-x≡n ⟩
1+ n
∎
fromℕ₁ = proj₁ ∘ fromℕ
-- fromℕ₁∘1+ : fromℕ₁ ∘ 1+_ ≗ suc ∘ fromℕ₁
-- fromℕ₁∘1+ _ = refl
test : -- fromℕ₁ 1 ≡ suc2* 0#, 2 -> 2suc 0# , 3 -> suc2* (suc2* 0#)
fromℕ₁ 4 ≡ 2suc (suc2* 0#)
test = refl
-----------------------------------------------------------------
fromℕ₁+homo : (m n : ℕ ) → fromℕ₁ (m +' n) ≡ fromℕ₁ m + fromℕ₁ n
fromℕ₁+homo 0 _ = refl
fromℕ₁+homo (1+ m) n =
begin
fromℕ₁ ((1+ m) +' n) ≡⟨ refl ⟩
fromℕ₁ (1+ (m +' n)) ≡⟨ refl ⟩
suc (fromℕ₁ (m +' n)) ≡⟨ cong suc (fromℕ₁+homo m n) ⟩
suc (a + b) ≡⟨ sym (suc+assoc a b) ⟩
(suc a) + b ≡⟨ refl ⟩
(fromℕ₁ (1+ m)) + (fromℕ₁ n)
∎
where
a = fromℕ₁ m; b = fromℕ₁ n
-------------------------------
toℕ∘fromℕ₁ : toℕ ∘ fromℕ₁ ≗ id
toℕ∘fromℕ₁ 0 = refl
toℕ∘fromℕ₁ (1+ n) =
begin toℕ (fromℕ₁ (1+ n)) ≡⟨ refl ⟩
toℕ (suc (fromℕ₁ n)) ≡⟨ toℕ∘suc (fromℕ₁ n) ⟩
1+ (toℕ (fromℕ₁ n)) ≡⟨ cong 1+_ (toℕ∘fromℕ₁ n) ⟩
1+ n
∎
------------------------------
toℕ-injective : Injective toℕ
toℕ-injective {0#} {0#} _ = refl
toℕ-injective {0#} {2suc _} ()
toℕ-injective {0#} {suc2* _} ()
toℕ-injective {2suc _} {0#} ()
toℕ-injective {2suc x} {2suc y} 2[1+xN]≡2[1+yN] = cong 2suc x≡y
where
xN = toℕ x
yN = toℕ y
1+xN≡1+yN = *'-lCancel {1+ xN} {1+ yN} 1 2[1+xN]≡2[1+yN]
xN≡yN = cong pred' 1+xN≡1+yN
x≡y = toℕ-injective xN≡yN
toℕ-injective {2suc x} {suc2* y} 2[1+xN]≡1+2yN =
contradiction 2[1+xN]≡1+2yN (Nat0.2m≢1+2n 1+xN yN)
where
1+xN = 1+ (toℕ x); yN = toℕ y
toℕ-injective {suc2* _} {0#} ()
toℕ-injective {suc2* x} {2suc y} 1+2xN≡2[1+yN] =
contradiction (sym 1+2xN≡2[1+yN]) (Nat0.2m≢1+2n 1+yN xN)
where
xN = toℕ x; 1+yN = 1+ (toℕ y)
toℕ-injective {suc2* x} {suc2* y} 1+2xN≡1+2yN = cong suc2* x≡y
where
xN = toℕ x; yN = toℕ y; 2xN≡2yN = cong pred' 1+2xN≡1+2yN
xN≡yN = *'-lCancel {xN} {yN} 1 2xN≡2yN; x≡y = toℕ-injective xN≡yN
-------------------------------
toℕ-surjective : Surjective toℕ
toℕ-surjective 0 = (0# , refl)
toℕ-surjective (1+ n) = let (a , toℕ-a≡n) = toℕ-surjective n
toℕ-suc-a≡1+n =
begin toℕ (suc a) ≡⟨ toℕ∘suc a ⟩
1+ (toℕ a) ≡⟨ cong 1+_ toℕ-a≡n ⟩
1+ n
∎
in
(suc a , toℕ-suc-a≡1+n)
-------------------------------
fromℕ₁∘toℕ : fromℕ₁ ∘ toℕ ≗ id
fromℕ₁∘toℕ a =
let (b , toℕ-b≡toℕ-a) = fromℕ (toℕ a)
in
begin fromℕ₁ (toℕ a) ≡⟨ refl ⟩
b ≡⟨ toℕ-injective toℕ-b≡toℕ-a ⟩
a
∎
-- Summary:
-- 1) toℕ : Bin → ℕ is an isomorphism by _+_,
-- 2) fromℕ₁ is a homomorphisms by _+_ mutually inverse with toℕ.
------------------------------------------------------------------------------
-- Commutativity and associativity for _+_ are proved by using the isomorphism
-- to ℕ.
module FP-Bin = FuncProp (_≡_ {A = Bin})
+-comm : FP-Bin.Commutative _+_
+-comm a b =
begin a + b ≡⟨ sym (fromℕ₁∘toℕ (a + b)) ⟩
fromℕ₁ (toℕ (a + b)) ≡⟨ cong fromℕ₁ (toℕ+homo a b) ⟩
fromℕ₁ (toℕ a +' toℕ b) ≡⟨ cong fromℕ₁ (+'-comm (toℕ a) (toℕ b)) ⟩
fromℕ₁ (toℕ b +' toℕ a) ≡⟨ cong fromℕ₁ (sym (toℕ+homo b a)) ⟩
fromℕ₁ (toℕ (b + a)) ≡⟨ fromℕ₁∘toℕ (b + a) ⟩
b + a
∎
+-assoc : FP-Bin.Associative _+_
+-assoc a b c =
begin
(a + b) + c ≡⟨ sym (fromℕ₁∘toℕ ((a + b) + c)) ⟩
fromℕ₁ (toℕ ((a + b) + c)) ≡⟨ cong fromℕ₁ (toℕ+homo (a + b) c) ⟩
fromℕ₁ (toℕ (a + b) +' cN) ≡⟨ cong (fromℕ₁ ∘ (_+' cN)) (toℕ+homo a b) ⟩
fromℕ₁ ((aN +' bN) +' cN) ≡⟨ cong fromℕ₁ (+'-assoc aN bN cN) ⟩
fromℕ₁ (aN +' (bN +' cN)) ≡⟨ cong (fromℕ₁ ∘ (aN +'_))
(sym (toℕ+homo b c)) ⟩
fromℕ₁ (aN +' toℕ (b + c)) ≡⟨ cong fromℕ₁ (sym (toℕ+homo a (b + c))) ⟩
fromℕ₁ (toℕ (a + (b + c))) ≡⟨ fromℕ₁∘toℕ (a + (b + c)) ⟩
(a + (b + c))
∎
where
aN = toℕ a; bN = toℕ b; cN = toℕ c
x+[y+z]≡y+[x+z] : ∀ x y z → x + (y + z) ≡ y + (x + z)
x+[y+z]≡y+[x+z] x y z =
begin x + (y + z) ≡⟨ sym (+-assoc x y z) ⟩
(x + y) + z ≡⟨ cong (_+ z) (+-comm x y) ⟩
(y + x) + z ≡⟨ +-assoc y x z ⟩
y + (x + z)
∎
[x+y]+z≡y+[x+z] : ∀ x y z → (x + y) + z ≡ y + (x + z)
[x+y]+z≡y+[x+z] x y z =
begin (x + y) + z ≡⟨ cong (_+ z) (+-comm x y) ⟩
(y + x) + z ≡⟨ +-assoc y x z ⟩
y + (x + z)
∎
--------------------------------------------
+-lCancel : ∀ x y z → x + y ≡ x + z → y ≡ z -- the proof is via toℕ
+-lCancel x y z x+y≡x+z =
begin y ≡⟨ sym (fromℕ₁∘toℕ y) ⟩
fromℕ₁ m ≡⟨ cong fromℕ₁ m≡n ⟩
fromℕ₁ n ≡⟨ fromℕ₁∘toℕ z ⟩
z
∎
where
k = toℕ x; m = toℕ y; n = toℕ z
eq = begin k +' m ≡⟨ sym (toℕ+homo x y) ⟩
toℕ (x + y) ≡⟨ cong toℕ x+y≡x+z ⟩
toℕ (x + z) ≡⟨ toℕ+homo x z ⟩
k +' n
∎
m≡n = begin m ≡⟨ sym (NatP.m+n∸n≡m m k)⟩
(m +' k) ∸' k ≡⟨ cong (_∸' k) (+'-comm m k) ⟩
(k +' m) ∸' k ≡⟨ cong (_∸' k) eq ⟩
(k +' n) ∸' k ≡⟨ cong (_∸' k) (+'-comm k n) ⟩
(n +' k) ∸' k ≡⟨ NatP.m+n∸n≡m n k ⟩
n
∎
+-rCancel : ∀ x y z → y + x ≡ z + x → y ≡ z
+-rCancel x y z y+x≡z+x =
+-lCancel x y z x+y≡x+z
where
x+y≡x+z = begin x + y ≡⟨ +-comm x y ⟩
y + x ≡⟨ y+x≡z+x ⟩
z + x ≡⟨ +-comm z x ⟩
x + z
∎
------------------------------------------------------------------------------
setoid : Setoid 0ℓ 0ℓ
setoid = PE.setoid Bin
decEquivalence : IsDecEquivalence (_≡_ {A = Bin})
decEquivalence = record{ isEquivalence = isEquivalence; _≟_ = _≟_ }
decSetoid : DecSetoid 0ℓ 0ℓ
decSetoid = record{ Carrier = Bin
; _≈_ = _≡_
; isDecEquivalence = decEquivalence }
+-isSemigroup : IsSemigroup _≡_ _+_
+-isSemigroup = record{ isEquivalence = isEquivalence
; assoc = +-assoc
; ∙-cong = cong₂ _+_ }
+-semigroup : Semigroup 0ℓ 0ℓ
+-semigroup =
record{ Carrier = Bin; _≈_ = _≡_; _∙_ = _+_;
isSemigroup = +-isSemigroup }
+-isMonoid : IsMonoid _≡_ _+_ 0#
+-isMonoid = record{ isSemigroup = +-isSemigroup; identity = (0+ , +0) }
+-monoid : Monoid 0ℓ 0ℓ
+-monoid = record{ Carrier = Bin;
_≈_ = _≡_; _∙_ = _+_; ε = 0#; isMonoid = +-isMonoid }
+-isCommutativeMonoid : IsCommutativeMonoid _≡_ _+_ 0#
+-isCommutativeMonoid = record{ isSemigroup = +-isSemigroup
; identityˡ = 0+
; comm = +-comm }
+-commutativeMonoid : CommutativeMonoid 0ℓ 0ℓ
+-commutativeMonoid =
record{ Carrier = Bin; _≈_ = _≡_; _∙_ = _+_; ε = 0#;
isCommutativeMonoid = +-isCommutativeMonoid }