-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinus.agda
More file actions
1470 lines (1290 loc) · 65.5 KB
/
Minus.agda
File metadata and controls
1470 lines (1290 loc) · 65.5 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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-
This file is a part of the library Binary-3.0.
Copyright © 2018 Program Systems Institute of Russian Academy of Sciences.
Binary-3.0 is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License.
See license.txt.
-}
{-# OPTIONS --termination-depth=2 #-}
module Minus where
open import Function using (id; _∘_; _$_; case_of_; const)
open import Algebra.FunctionProperties as FuncProp using (Op₂)
open import Relation.Nullary using (Dec; yes; no)
open import Relation.Binary using (Tri; _Preserves₂_⟶_⟶_)
open import Relation.Binary.PropositionalEquality as PE
using (_≡_; _≢_; _≗_; subst; subst₂;
cong; cong₂; refl; sym; trans)
open PE.≡-Reasoning renaming (begin_ to begin≡_; _∎ to _end≡)
open import Data.Empty using (⊥-elim)
open import Data.Sum using (inj₁; inj₂)
open import Data.Product using (_×_; _,_; proj₁; proj₂; ∃)
open import Data.List using (List; []; _∷_; _∷ʳ_; [_]; map)
renaming (length to ln)
open import Data.Digit using (Bit; fromDigits)
open import Data.Nat using (s≤s)
renaming (suc to 1+_; pred to predN; _+_ to _+n_; _<_ to _<n_;
_>_ to _>n_; _≤_ to _≤n_; _*_ to _*n_; _∸_ to _∸n_
)
open import Data.Nat.Properties as NProp using (m≤m+n)
renaming (+-comm to +n-comm; ∸-mono to ∸n-mono;
≤-reflexive to ≤n-reflexive; module ≤-Reasoning to ≤n-Reasoning
)
open ≤n-Reasoning using () renaming (begin_ to ≤nBegin_; _∎ to _≤nEnd;
_≡⟨_⟩_ to _≡≤n[_]_; _≤⟨_⟩_ to _≤n[_]_)
-- of application ---
open import LtReasoning using (module InequalityReasoning) -- by U. Norell
open import NatProp0 using (_≤n?_)
open import List0 using (length-xs:x)
open import Bin0 using (_←→_; Bin; 0b; ⊥b; 1b; _*2; 2^_; _∈_; _∈?_; fromBits;
suc; pred; toℕ; _<_; _>_; _≤_; _≥_; _+_; _*_
)
renaming (1bin to 1'; 2bin to 2')
open import Bin1 using
(0<bs1; 0≢bs1; 1<bbs1; *2≗2bin*; <-trans; <-irrefl; <-asym; <-cmp; _≤?_;
≢sym; ≢0#⇒≡bs1; ≮0; 0≤; ≤⇒≯; <⇒≢; <⇒≱; >⇒≢; ≢0⇒0<; ≤-reflexive; ≤-refl;
≤-trans; <-≤-trans; ≤-<-trans; ≰⇒>; ∣_∣; ∣_∣-mono-≤; 1∉bs⇒fromBits-bs≡0;
1∈bs⇒|fromBits-bs|≤|bs|; |2^n|≡1+n; fromBits-bbs<2^|bbs|
)
open import Bin2 using (0+; +0; +-comm; +-assoc; pred∘suc; suc∘pred;
suc∘pred-for>0; toℕ+homo; fromℕ+homo; suc-even1;
fromℕ; toℕ∘fromℕ; fromℕ∘toℕ; module FP-Bin
)
open import Bin3 using (rDistrib; *2≗*2bin; *2≗+; *2-distrib; toℕ*homo;
fromℕ*homo; partHighest-2^; |pred-2^[1+n]|<|2^[1+n]|)
open import Bin4 using
(0<1+x; x<1+x; x<x+1; x≤x+y; x≤y+x; pred-mono-<; pred-mono-≤; suc-mono-<;
suc-mono-≤; toℕ-mono-≤; fromℕ-mono-≤; +-mono-≤; +-mono-≤-<; +-mono-<-≤;
*bs1-mono-<; *2-mono-≤; *2-mono-<; *-mono-≤; x<y⇒a+x*a≤y*a; +-lCancel;
+-rCancel; pred<; 0<2^; <⇒suc≤; suc≤⇒<
)
--****************************************************************************
-- Subtraction on Bin (minus, _∸_), with certain its properties.
open Bin
open InequalityReasoning _<_ _≤_ (\{x y} → ≤-reflexive {x} {y})
(\{x y z} → <-trans {x} {y} {z})
(\{x y z} → ≤-trans {x} {y} {z})
(\{x y z} → <-≤-trans {x} {y} {z})
(\{x y z} → ≤-<-trans {x} {y} {z})
module _ (c : Bit) (x y : Bin)
where
private c+y = (fromBits [ c ]) + y
data MinusRes (d : Bin) : Set
where
minuend< : x < c+y → d ≡ 0# → MinusRes d
minuend≡ : x ≡ c+y → d ≡ 0# → MinusRes d
minuend> : x > c+y → (x ≡ c+y + d) → MinusRes d
------------------------------------------------------------------------------
minus : (c : Bit) → (x y : Bin) → ∃ (\d → MinusRes c x y d)
minus ⊥b
minus _ ((⊥b ∷ _) 1#)
minus _ _ ((⊥b ∷ _) 1#)
minus 0b 0# 0# = (0# , minuend≡ (refl {x = 0#}) refl)
minus 0b (bs 1#) 0# = (bs1 , minuend> (0<bs1 bs) bs1≡0+0+bs1)
where
bs1 = bs 1#
bs1≡0+0+bs1 = begin≡ bs1 ≡⟨ sym (0+ bs1) ⟩
0# + bs1 ≡⟨ cong (_+ bs1) (sym (+0 0#)) ⟩
(0# + 0#) + bs1
end≡
minus 1b 0# 0# = (0# , minuend< (0<bs1 []) refl)
minus 0b 0# (bs 1#) = (0# , minuend< 0<0+bs1 refl)
where
bs1 = bs 1#
0<0+bs1 = begin 0# <[ 0<bs1 bs ]
bs1 ≡[ sym (0+ bs1) ]
0# + bs1
∎
minus 1b 0# (bs 1#) = (0# , minuend< 0<1+bs1 refl) where
0<1+bs1 = 0<1+x (bs 1#)
minus 1b ([] 1#) 0# = (0# , minuend≡ 1≡<1+0>+0 (refl {x = 0#}))
where
1≡<1+0>+0 : 1' ≡ (1' + 0#) + 0#
1≡<1+0>+0 = begin≡ 1' ≡⟨ sym (+0 1') ⟩
1' + 0# ≡⟨ sym (+0 (1' + 0#)) ⟩
(1' + 0#) + 0#
end≡
minus 0b ([] 1#) ([] 1#) = (0# , minuend≡ (sym (0+ 1')) refl)
minus 0b ([] 1#) ((b ∷ bs) 1#) = (0# , minuend< 1<0+bbs1 refl)
where
bbs1 = (b ∷ bs) 1#
|bs:1| = ln (bs ∷ʳ 1b)
1<1+|bs:1| = ≤nBegin 2 ≤n[ m≤m+n 2 (ln bs) ]
2 +n (ln bs) ≡≤n[ cong 1+_ $
sym (length-xs:x 1b bs) ]
1+ |bs:1|
≤nEnd
1<0+bbs1 : 1' < 0# + bbs1
1<0+bbs1 = begin [] 1# <[ inj₁ 1<1+|bs:1| ]
(b ∷ bs) 1# ≡[ sym (0+ bbs1) ]
0# + (b ∷ bs) 1#
∎
minus 1b ([] 1#) ([] 1#) = (0# , minuend< (x<1+x 1') refl)
minus 1b ([] 1#) ((b ∷ bs) 1#) = (0# , minuend< 1<1+bbs1 (refl {x = 0#}))
where
bbs1 = (b ∷ bs) 1#
1<1+bbs1 = <-trans {1'} {bbs1} {suc bbs1} (1<bbs1 b bs) (x<1+x bbs1)
minus 0b ((b ∷ bs) 1#) ([] 1#) =
(pred-bbs1 , minuend> 0+1<bbs1 bbs1≡<0+1>+pred-bbs1)
where
bbs1 = (b ∷ bs) 1#
pred-bbs1 = pred bbs1
0+1≡1 = 0+ 1'
0+1<bbs1 : 0# + 1' < bbs1
0+1<bbs1 = subst (_< bbs1) (sym (0+ 1')) (1<bbs1 b bs)
bbs1≡<0+1>+pred-bbs1 : bbs1 ≡ (0# + 1') + pred-bbs1
bbs1≡<0+1>+pred-bbs1 =
begin≡
bbs1 ≡⟨ sym (suc∘pred (b ∷ bs)) ⟩
suc pred-bbs1 ≡⟨ refl ⟩
1' + pred-bbs1 ≡⟨ cong (_+ pred-bbs1) (sym (0+ 1')) ⟩
(0# + 1') + pred-bbs1
end≡
minus 1b ((b ∷ bs) 1#) 0# = (d , minuend> 1+0<bbs1 bbs1≡1+0+d)
where
bbs1 = (b ∷ bs) 1#
d = pred bbs1
bbs1≡1+0+d = sym (suc∘pred (b ∷ bs))
1+0<bbs1 = 1<bbs1 b bs
minus 1b ((0b ∷ []) 1#) ([] 1#) = (0# , minuend≡ refl refl)
minus 1b ((1b ∷ []) 1#) ([] 1#) = (1' , minuend> 2<3 refl)
where
2<3 = x<1+x 2'
minus 1b ((b ∷ b' ∷ bs) 1#) ([] 1#) = (d , minuend> 2<bb'bs1 bb'bs1≡2+d)
where
bb'bs = b ∷ b' ∷ bs
bb'bs1 = bb'bs 1#
d = pred (pred bb'bs1)
2<|bb'bs1| : 2 <n ln (bb'bs ∷ʳ 1b)
2<|bb'bs1| =
≤nBegin 3 ≤n[ m≤m+n 3 (ln bs) ]
3 +n (ln bs) ≡≤n[ sym (length-xs:x 1b bb'bs) ]
ln ((b ∷ b' ∷ bs) ∷ʳ 1b)
≤nEnd
2<bb'bs1 = inj₁ 2<|bb'bs1|
1<bb'bs1 = inj₁ (m≤m+n 2 (ln (bs ∷ʳ 1b)))
0<pred-bb'bs1 = pred-mono-< [] bb'bs1 1<bb'bs1
bb'bs1≡2+d : bb'bs1 ≡ 2' + d
bb'bs1≡2+d =
begin≡
bb'bs1 ≡⟨ sym (suc∘pred bb'bs) ⟩
suc (pred bb'bs1) ≡⟨ cong suc (sym (suc∘pred-for>0 {pred bb'bs1}
0<pred-bb'bs1))
⟩
suc (suc (pred (pred bb'bs1))) ≡⟨ sym (+-assoc 1' 1'
(pred (pred bb'bs1))) ⟩
2' + pred (pred bb'bs1)
end≡
minus c ((b ∷ bs) 1#) ((b' ∷ bs') 1#) = aux c b b'
where
bs1 = bs 1#; 0bs1 = (0b ∷ bs) 1#
bs'1 = bs' 1#; 0bs'1 = (0b ∷ bs') 1#
1bs1 = (1b ∷ bs) 1#; 1bs'1 = (1b ∷ bs') 1#
bs'1*2 = bs'1 * 2'
aux : ∀ c b b' → ∃ (\d → MinusRes c ((b ∷ bs) 1#) ((b' ∷ bs') 1#) d)
aux ⊥b
aux _ ⊥b
aux _ _ ⊥b
----------------------------------------------
aux 0b 0b 0b with minus 0b (bs 1#) (bs' 1#) -- no carry
...
| (_ , minuend< bs1<0+bs'1 _) = (0# , minuend< 0bs1<0+0bs'1 refl)
where
bs1<bs'1 : bs1 < bs'1
bs1<bs'1 = subst (bs1 <_) (0+ bs'1) bs1<0+bs'1
0bs1<0+0bs'1 : 0bs1 < 0# + 0bs'1
0bs1<0+0bs'1 =
begin 0bs1 ≡[ *2≗*2bin bs1 ]
bs1 * 2' <[ *bs1-mono-< [ 0b ] {bs1} {bs'1} bs1<bs'1 ]
bs'1 * 2' ≡[ sym (*2≗*2bin bs'1) ]
bs'1 *2 ≡[ sym (0+ 0bs'1) ]
0# + 0bs'1
∎
... | (_ , minuend≡ bs1≡0+bs'1 _) = (0# , minuend≡ 0bs1≡0+0bs'1 refl)
where
0bs1≡0+0bs'1 : (0b ∷ bs) 1# ≡ 0# + (0b ∷ bs') 1#
0bs1≡0+0bs'1 =
begin≡ (0b ∷ bs) 1# ≡⟨ refl ⟩
(bs 1#) *2 ≡⟨ cong _*2 bs1≡0+bs'1 ⟩
(0# + (bs' 1#)) *2 ≡⟨ cong _*2 (0+ (bs' 1#)) ⟩
(bs' 1#) *2 ≡⟨ refl ⟩
(0b ∷ bs') 1# ≡⟨ sym (0+ ((0b ∷ bs') 1#)) ⟩
0# + (0b ∷ bs') 1#
end≡
... | (d , minuend> 0+bs'1<bs1 bs1≡0+bs'1+d) =
(d*2 , minuend> 0+0bs'1<0bs1 0bs1≡0+0bs'1+d*2)
where
d*2 = d *2
bs'1<bs1 = subst (_< bs1) (0+ bs'1) 0+bs'1<bs1
bs1≡bs'1+d = trans bs1≡0+bs'1+d (cong (_+ d) (0+ bs'1))
0+0bs'1<0bs1 : 0# + 0bs'1 < 0bs1
0+0bs'1<0bs1 =
begin
0# + 0bs'1 ≡[ 0+ 0bs'1 ]
bs'1 *2 ≡[ *2≗*2bin bs'1 ]
bs'1 * 2' <[ *bs1-mono-< [ 0b ] {bs'1} {bs1} bs'1<bs1 ]
bs1 * 2' ≡[ sym (*2≗*2bin bs1) ]
bs1 *2 ≡[ refl ]
0bs1
∎
0bs1≡0+0bs'1+d*2 : 0bs1 ≡ (0# + 0bs'1) + d*2
0bs1≡0+0bs'1+d*2 =
begin≡ (0b ∷ bs) 1# ≡⟨ refl ⟩
bs1 *2 ≡⟨ cong _*2 bs1≡bs'1+d ⟩
(bs'1 + d) *2 ≡⟨ *2-distrib bs'1 d ⟩
bs'1 *2 + d *2 ≡⟨ cong (_+ d*2) (sym (0+ 0bs'1)) ⟩
(0# + 0bs'1) + d*2
end≡
---------------------------------------------
aux 0b 0b 1b with minus 1b (bs 1#) (bs' 1#) -- process carry
...
| (_ , minuend< bs1<1+bs'1 _) = (0# , minuend< 0bs1<0+1bs'1 refl)
where
1+bs1≤1+bs'1 = <⇒suc≤ {bs1} {suc bs'1} bs1<1+bs'1
bs1≤bs'1 = begin bs1 ≡[ sym (pred∘suc bs1) ]
pred (suc bs1) ≤[ pred-mono-≤ 1+bs1≤1+bs'1 ]
pred (suc bs'1) ≡[ pred∘suc bs'1 ]
bs'1
∎
0bs1<0+1bs'1 : 0bs1 < 0# + 1bs'1
0bs1<0+1bs'1 =
begin 0bs1 ≡[ refl ]
bs1 *2 ≤[ *2-mono-≤ bs1≤bs'1 ]
bs'1 *2 <[ x<1+x (bs'1 *2) ]
1' + bs'1 *2 ≡[ suc-even1 bs' ]
1bs'1 ≡[ sym (0+ 1bs'1) ]
0# + 1bs'1
∎
... | (_ , minuend≡ bs1≡1+bs'1 _) =
(1' , minuend> 0+1bs'1<0bs1 0bs1≡0+1bs'1+1)
where
0bs1≡0+1bs'1+1 : 0bs1 ≡ (0# + 1bs'1) + 1'
0bs1≡0+1bs'1+1 =
begin≡
bs1 *2 ≡⟨ cong _*2 bs1≡1+bs'1 ⟩
(1' + bs'1) *2 ≡⟨ *2≗*2bin _ ⟩
(1' + bs'1) * 2' ≡⟨ rDistrib 2' 1' bs'1 ⟩
1' * 2' + bs'1 * 2' ≡⟨ refl ⟩
(1' + 1') + bs'1 * 2' ≡⟨ +-assoc 1' 1' (bs'1 * 2') ⟩
1' + (1' + bs'1 * 2') ≡⟨ cong ((1' +_) ∘ (1' +_))
(sym (*2≗*2bin bs'1))
⟩
1' + (1' + bs'1 *2) ≡⟨ +-comm 1' (1' + bs'1 *2) ⟩
(1' + bs'1 *2) + 1' ≡⟨ refl ⟩
(0# + 1bs'1) + 1'
end≡
le = x<x+1 (0# + 1bs'1)
0+1bs'1<0bs1 : 0# + 1bs'1 < 0bs1
0+1bs'1<0bs1 = subst ((0# + 1bs'1) <_) (sym 0bs1≡0+1bs'1+1) le
... | (d , minuend> 1+bs'1<bs1 bs1≡1+bs'1+d) =
(1+d*2 , minuend> 0+1bs'1<0bs1 0bs1≡0+1bs'1+1+d*2)
where
d*2 = d * 2'
1+d*2 = suc d*2
0bs1≡0+1bs'1+1+d*2 : 0bs1 ≡ (0# + 1bs'1) + 1+d*2
0bs1≡0+1bs'1+1+d*2 =
begin≡
0bs1 ≡⟨ refl ⟩
bs1 *2 ≡⟨ *2≗*2bin bs1 ⟩
bs1 * 2' ≡⟨ cong (_* 2') bs1≡1+bs'1+d ⟩
((suc bs'1) + d) * 2' ≡⟨ rDistrib 2' (suc bs'1) d ⟩
(1' + bs'1) * 2' + d*2 ≡⟨ cong (_+ d*2) (rDistrib 2' 1' bs'1)
⟩
((1' + 1') + bs'1*2) + d*2 ≡⟨ cong (_+ d*2) (+-assoc 1' 1' bs'1*2)
⟩
(1' + (suc bs'1*2)) + d*2 ≡⟨ cong (_+ d*2)
(+-comm 1' (suc bs'1*2))
⟩
((suc bs'1*2) + 1') + d*2 ≡⟨ +-assoc (suc bs'1*2) 1' d*2 ⟩
(suc bs'1*2) + 1+d*2 ≡⟨ cong ((_+ 1+d*2) ∘ suc)
(sym (*2≗*2bin bs'1))
⟩
(suc (bs'1 *2)) + 1+d*2 ≡⟨ cong (_+ 1+d*2) (suc-even1 bs') ⟩
1bs'1 + 1+d*2 ≡⟨ cong (_+ 1+d*2) (sym (0+ 1bs'1)) ⟩
(0# + 1bs'1) + 1+d*2
end≡
0+1bs'1<0bs1 : 0# + 1bs'1 < 0bs1
0+1bs'1<0bs1 =
begin
0# + 1bs'1 <[ x<1+x (0# + 1bs'1) ]
suc (0# + 1bs'1) ≡[ +-comm 1' (0# + 1bs'1) ]
(0# + 1bs'1) + 1' ≡[ sym (+0 ((0# + 1bs'1) + 1')) ]
((0# + 1bs'1) + 1') + 0# ≤[ +-mono-≤ (≤-refl {(0# + 1bs'1) + 1'})
(0≤ d*2) ]
((0# + 1bs'1) + 1') + d*2 ≡[ +-assoc (0# + 1bs'1) 1' d*2 ]
(0# + 1bs'1) + 1+d*2 ≡[ sym 0bs1≡0+1bs'1+1+d*2 ]
0bs1
∎
------------
aux 0b 1b 0b with minus 0b (bs 1#) (bs' 1#) -- 1bs1 - 0bs'1, no carry
...
| (_ , minuend< bs1<0+bs'1 _) = (0# , minuend< 1bs1<0+0bs'1 refl)
where
1+bs1≤0+bs'1 : 1' + bs1 ≤ 0# + bs'1
1+bs1≤0+bs'1 = <⇒suc≤ {bs1} {0# + bs'1} bs1<0+bs'1
1bs1<0+0bs'1 : 1bs1 < 0# + 0bs'1
1bs1<0+0bs'1 =
begin
1bs1 ≡[ sym (suc-even1 bs) ]
suc (bs1 *2) ≡[ cong suc (*2≗*2bin bs1) ]
suc (bs1 * 2') <[ x<1+x (suc (bs1 * 2')) ]
1' + (1' + bs1 * 2') ≡[ sym (+-assoc 1' 1' (bs1 * 2')) ]
2' + bs1 * 2' ≡[ refl ]
1' * 2' + bs1 * 2' ≡[ sym (rDistrib 2' 1' bs1) ]
(suc bs1) * 2' ≤[ *-mono-≤ 1+bs1≤0+bs'1 ≤-refl ]
(0# + bs'1) * 2' ≡[ cong (_* 2') (0+ bs'1) ]
bs'1 * 2' ≡[ sym (*2≗*2bin bs'1) ]
0bs'1 ≡[ sym (0+ 0bs'1) ]
0# + 0bs'1
∎
... | (_ , minuend≡ bs1≡0+bs'1 _) =
(1' , minuend> 0+0bs'1<1bs1 1bs1≡0+0bs'1+1)
where
0+0bs'1 = 0# + 0bs'1
bs1≡bs'1 = trans bs1≡0+bs'1 (0+ bs'1)
1bs1≡0+0bs'1+1 : 1bs1 ≡ 0+0bs'1 + 1'
1bs1≡0+0bs'1+1 =
begin≡ 1bs1 ≡⟨ sym (suc-even1 bs) ⟩
suc (bs1 *2) ≡⟨ cong (suc ∘ _*2) bs1≡bs'1 ⟩
suc (bs'1 *2) ≡⟨ refl ⟩
suc 0bs'1 ≡⟨ +-comm 1' 0bs'1 ⟩
0bs'1 + 1' ≡⟨ cong (_+ 1') (sym (0+ 0bs'1)) ⟩
(0# + 0bs'1) + 1'
end≡
0+0bs'1<1bs1 : 0+0bs'1 < 1bs1
0+0bs'1<1bs1 =
begin 0+0bs'1 ≡[ sym (+0 0+0bs'1) ]
0+0bs'1 + 0# <[ +-mono-≤-< {0+0bs'1} {0+0bs'1} {0#} {1'}
(≤-refl {0+0bs'1}) (0<bs1 []) ]
0+0bs'1 + 1' ≡[ sym 1bs1≡0+0bs'1+1 ]
1bs1
∎
... | (d , minuend> 0+bs'1<bs1 bs1≡0+bs'1+d) =
(1+d*2 , minuend> 0+0bs'1<1bs1 1bs1≡0+0bs'1+1+d*2)
where
d*2 = d * 2'; 1+d*2 = suc d*2; 0+0bs'1 = 0# + 0bs'1
1bs1≡0+0bs'1+1+d*2 : 1bs1 ≡ 0+0bs'1 + 1+d*2
1bs1≡0+0bs'1+1+d*2 =
begin≡
1bs1 ≡⟨ sym (suc-even1 bs) ⟩
suc (bs1 *2) ≡⟨ cong (suc ∘ _*2) bs1≡0+bs'1+d ⟩
suc (((0# + bs'1) + d) *2) ≡⟨ cong (\x → suc ((x + d) *2))
(0+ bs'1) ⟩
suc ((bs'1 + d) *2) ≡⟨ cong suc (*2≗*2bin (bs'1 + d)) ⟩
suc ((bs'1 + d) * 2') ≡⟨ cong suc (rDistrib 2' bs'1 d) ⟩
suc (bs'1 * 2' + d*2) ≡⟨ cong (suc ∘ (_+ d*2))
(sym (*2≗*2bin bs'1)) ⟩
1' + (0bs'1 + d*2) ≡⟨ sym (+-assoc 1' 0bs'1 d*2) ⟩
(1' + 0bs'1) + d*2 ≡⟨ cong (_+ d*2) (+-comm 1' 0bs'1) ⟩
(0bs'1 + 1') + d*2 ≡⟨ +-assoc 0bs'1 1' d*2 ⟩
0bs'1 + 1+d*2 ≡⟨ cong (_+ 1+d*2) (sym (0+ 0bs'1)) ⟩
(0# + 0bs'1) + 1+d*2
end≡
0+0bs'1<1bs1 : 0+0bs'1 < 1bs1
0+0bs'1<1bs1 =
begin 0+0bs'1 ≡[ sym (+0 0+0bs'1) ]
0+0bs'1 + 0# <[ +-mono-≤-< {0+0bs'1} {_} {0#} {1'}
(≤-refl {0+0bs'1}) (0<bs1 [])
]
0+0bs'1 + 1' ≤[ x≤x+y (0+0bs'1 + 1') d*2 ]
(0+0bs'1 + 1') + d*2 ≡[ +-assoc 0+0bs'1 1' d*2 ]
0+0bs'1 + 1+d*2 ≡[ sym 1bs1≡0+0bs'1+1+d*2 ]
1bs1
∎
------------
aux 0b 1b 1b with minus 0b (bs 1#) (bs' 1#) -- 1bs1 - 1bs'1, no carry
...
| (_ , minuend< bs1<0+bs'1 _) = (0# , minuend< 1bs1<0+1bs'1 refl)
where
bs1<bs'1 = subst (bs1 <_) (0+ bs'1) bs1<0+bs'1
1bs1<0+1bs'1 : 1bs1 < 0# + 1bs'1
1bs1<0+1bs'1 =
begin 1bs1 ≡[ sym (suc-even1 bs) ]
suc (bs1 *2) <[ suc-mono-< {bs1 *2} {bs'1 *2}
(*2-mono-< {bs1} {bs'1} bs1<bs'1) ]
suc (bs'1 *2) ≡[ suc-even1 bs' ]
1bs'1 ≡[ sym (0+ 1bs'1) ]
0# + 1bs'1
∎
... | (_ , minuend≡ bs1≡0+bs'1 _) = (0# , minuend≡ 1bs1≡0+1bs'1 refl)
where
1bs1≡0+1bs'1 : 1bs1 ≡ 0# + 1bs'1
1bs1≡0+1bs'1 =
begin≡ 1bs1 ≡⟨ sym (suc-even1 bs) ⟩
suc (bs1 *2) ≡⟨ cong (suc ∘ _*2) bs1≡0+bs'1 ⟩
suc ((0# + bs'1) *2) ≡⟨ cong (suc ∘ _*2) (0+ bs'1) ⟩
suc (bs'1 *2) ≡⟨ suc-even1 bs' ⟩
1bs'1 ≡⟨ sym (0+ 1bs'1) ⟩
0# + 1bs'1
end≡
... | (d , minuend> 0+bs'1<bs1 bs1≡0+bs'1+d) =
(d*2 , minuend> 0+1bs'1<1bs1 1bs1≡0+1bs'1+d*2)
where
d*2 = d * 2'
bs'1<bs1 = subst (_< bs1) (0+ bs'1) 0+bs'1<bs1
1bs1≡0+1bs'1+d*2 : 1bs1 ≡ (0# + 1bs'1) + d*2
1bs1≡0+1bs'1+d*2 =
begin≡
1bs1 ≡⟨ sym (suc-even1 bs) ⟩
suc (bs1 *2) ≡⟨ cong (suc ∘ _*2) bs1≡0+bs'1+d ⟩
suc (((0# + bs'1) + d) *2) ≡⟨ cong (\x → suc ((x + d) *2))
(0+ bs'1) ⟩
suc ((bs'1 + d) *2) ≡⟨ cong suc (*2≗*2bin (bs'1 + d)) ⟩
suc ((bs'1 + d) * 2') ≡⟨ cong suc (rDistrib 2' bs'1 d) ⟩
suc (bs'1 * 2' + d*2) ≡⟨ sym (+-assoc 1' (bs'1 * 2') d*2) ⟩
suc (bs'1 * 2') + d*2 ≡⟨ cong ((_+ d*2) ∘ suc)
(sym (*2≗*2bin bs'1)) ⟩
suc (bs'1 *2) + d*2 ≡⟨ cong (_+ d*2) (suc-even1 bs') ⟩
1bs'1 + d*2 ≡⟨ cong (_+ d*2) (sym (0+ 1bs'1)) ⟩
(0# + 1bs'1) + d*2
end≡
0+1bs'1<1bs1 : 0# + 1bs'1 < 1bs1
0+1bs'1<1bs1 =
begin 0# + 1bs'1 ≡[ 0+ _ ]
1bs'1 ≡[ sym (suc-even1 bs') ]
suc (bs'1 *2) <[ suc-mono-< {bs'1 *2} {bs1 *2}
(*2-mono-< {bs'1} {bs1} bs'1<bs1) ]
suc (bs1 *2) ≡[ suc-even1 bs ]
1bs1
∎
------------
aux 1b 0b 0b with minus 1b (bs 1#) (bs' 1#) -- 0bs1 - 0bs'1 with carry
...
| (_ , minuend< bs1<1+bs'1 _) = (0# , minuend< 0bs1<1+0bs'1 refl)
where
1+bs1≤1+bs'1 = <⇒suc≤ {bs1} {1' + bs'1} bs1<1+bs'1
0bs1<1+0bs'1 : 0bs1 < 1' + 0bs'1
0bs1<1+0bs'1 =
suc≤⇒< {0bs1} {suc 0bs'1}
(begin
1' + 0bs1 ≡[ refl ]
1' + bs1 *2 ≡[ sym (pred∘suc (1' + bs1 *2)) ]
pred (suc (1' + bs1 *2)) ≡[ refl ]
pred (1' + (1' + bs1 *2)) ≡[ cong pred $ sym
(+-assoc 1' 1' (bs1 *2)) ]
pred (2' + bs1 *2) ≡[ cong pred (sym (*2-distrib 1' bs1)) ]
pred ((1' + bs1) *2) ≤[ pred-mono-≤ (*2-mono-≤ 1+bs1≤1+bs'1)
]
pred ((1' + bs'1) *2) ≡[ cong pred (*2-distrib 1' bs'1) ]
pred ((suc 1') + bs'1 *2) ≡[ cong pred (+-assoc 1' 1' (bs'1 *2)) ]
pred (suc (1' + bs'1 *2)) ≡[ pred∘suc (1' + bs'1 *2) ]
1' + bs'1 *2 ≡[ refl ]
1' + 0bs'1
∎)
... | (_ , minuend≡ bs1≡1+bs'1 _) =
(1' , minuend> 1+0bs'1<0bs1 0bs1≡1+0bs'1+1)
where
0bs1≡1+0bs'1+1 : 0bs1 ≡ (suc 0bs'1) + 1'
0bs1≡1+0bs'1+1 =
begin≡ 0bs1 ≡⟨ refl ⟩
bs1 *2 ≡⟨ cong _*2 bs1≡1+bs'1 ⟩
(1' + bs'1) *2 ≡⟨ *2-distrib 1' bs'1 ⟩
2' + bs'1 *2 ≡⟨ +-assoc 1' 1' (bs'1 *2) ⟩
1' + (suc (bs'1 *2)) ≡⟨ +-comm 1' (suc (bs'1 *2)) ⟩
(suc 0bs'1) + 1'
end≡
1+0bs'1<0bs1 : suc 0bs'1 < 0bs1
1+0bs'1<0bs1 =
begin suc 0bs'1 <[ x<1+x (suc 0bs'1) ]
1' + (suc 0bs'1) ≡[ +-comm 1' (suc 0bs'1) ]
(suc 0bs'1) + 1' ≡[ sym 0bs1≡1+0bs'1+1 ]
0bs1
∎
... | (d , minuend> 1+bs'1<bs1 bs1≡1+bs'1+d) =
(1+d*2 , minuend> 1+0bs'1<0bs1 0bs1≡1+0bs'1+1+d*2)
where
d*2 = d *2
1+d*2 = suc d*2
0bs1≡1+0bs'1+1+d*2 : 0bs1 ≡ (suc 0bs'1) + 1+d*2
0bs1≡1+0bs'1+1+d*2 =
begin≡
0bs1 ≡⟨ refl ⟩
bs1 *2 ≡⟨ cong _*2 bs1≡1+bs'1+d ⟩
((suc bs'1) + d) *2 ≡⟨ *2-distrib (suc bs'1) d ⟩
(suc bs'1) *2 + d*2 ≡⟨ cong (_+ d*2) (*2-distrib 1' bs'1)
⟩
(2' + bs'1 *2) + d*2 ≡⟨ cong (_+ d*2)
(+-assoc 1' 1' (bs'1 *2))
⟩
(1' + (suc (bs'1 *2))) + d*2 ≡⟨ cong (_+ d*2)
(+-comm 1' (suc (bs'1 *2)))
⟩
(suc (bs'1 *2) + 1') + d*2 ≡⟨ +-assoc (suc (bs'1 *2)) 1' d*2 ⟩
suc (bs'1 *2) + 1+d*2 ≡⟨ refl ⟩
(suc 0bs'1) + 1+d*2
end≡
1+0bs'1<0bs1 : suc 0bs'1 < 0bs1
1+0bs'1<0bs1 =
begin
suc 0bs'1 <[ x<1+x (suc 0bs'1) ]
suc (suc 0bs'1) ≡[ +-comm 1' (suc 0bs'1) ]
(suc 0bs'1) + 1' ≤[ x≤x+y ((suc 0bs'1) + 1') d*2 ]
((suc 0bs'1) + 1') + d*2 ≡[ +-assoc (suc 0bs'1) 1' d*2 ]
(suc 0bs'1) + 1+d*2 ≡[ sym 0bs1≡1+0bs'1+1+d*2 ]
0bs1
∎
------------
aux 1b 0b 1b with minus 1b (bs 1#) (bs' 1#) -- 0bs1 - 1bs'1 with carry
...
| (_ , minuend< bs1<1+bs'1 _) = (0# , minuend< 0bs1<1+1bs'1 refl)
where
1+bs1≤1+bs'1 = <⇒suc≤ {bs1} {suc bs'1} bs1<1+bs'1
0bs1<1+1bs'1 : 0bs1 < suc 1bs'1
0bs1<1+1bs'1 =
begin 0bs1 ≡[ refl ]
bs1 *2 <[ *2-mono-< {bs1} {suc bs1} (x<1+x bs1) ]
(suc bs1) *2 ≤[ *2-mono-≤ 1+bs1≤1+bs'1 ]
(suc bs'1) *2 ≡[ *2-distrib 1' bs'1 ]
2' + (bs'1 *2) ≡[ +-assoc 1' 1' (bs'1 *2) ]
suc (suc (bs'1 *2)) ≡[ cong suc (suc-even1 bs') ]
suc 1bs'1
∎
... | (_ , minuend≡ bs1≡1+bs'1 _) = (0# , minuend≡ 0bs1≡1+1bs'1 refl)
where
0bs1≡1+1bs'1 : 0bs1 ≡ suc 1bs'1
0bs1≡1+1bs'1 =
begin≡ 0bs1 ≡⟨ refl ⟩
bs1 *2 ≡⟨ cong _*2 bs1≡1+bs'1 ⟩
(suc bs'1) *2 ≡⟨ *2-distrib 1' bs'1 ⟩
2' + bs'1 *2 ≡⟨ +-assoc 1' 1' (bs'1 *2) ⟩
suc (suc (bs'1 *2)) ≡⟨ cong suc (suc-even1 bs') ⟩
suc 1bs'1
end≡
... | (d , minuend> 1+bs'1<bs1 bs1≡1+bs'1+d) =
(d*2 , minuend> 1+1bs'1<0bs1 0bs1≡1+1bs'1+d*2)
where
d*2 = d *2
0bs1≡1+1bs'1+d*2 : 0bs1 ≡ (suc 1bs'1) + d*2
0bs1≡1+1bs'1+d*2 =
begin≡
0bs1 ≡⟨ refl ⟩
bs1 *2 ≡⟨ cong _*2 bs1≡1+bs'1+d ⟩
((suc bs'1) + d) *2 ≡⟨ *2-distrib (suc bs'1) d ⟩
(suc bs'1) *2 + d*2 ≡⟨ cong (_+ d*2)
(*2-distrib 1' bs'1) ⟩
(2' + bs'1 *2) + d*2 ≡⟨ cong (_+ d*2)
(+-assoc 1' 1' (bs'1 *2))
⟩
(suc (suc (bs'1 *2))) + d*2 ≡⟨ cong ((_+ d*2) ∘ suc)
(suc-even1 bs') ⟩
(suc 1bs'1) + d*2
end≡
bs'1<bs1 = <-trans {bs'1} {suc bs'1} {bs1} (x<1+x bs'1) 1+bs'1<bs1
1+1+bs'1≤bs1 = <⇒suc≤ {suc bs'1} {bs1} 1+bs'1<bs1
1+1bs'1<0bs1 : suc 1bs'1 < 0bs1
1+1bs'1<0bs1 =
begin
suc 1bs'1 ≡[ cong suc (sym (suc-even1 bs')) ]
suc (suc (bs'1 *2)) ≡[ sym (+-assoc 1' 1' (bs'1 *2)) ]
2' + bs'1 *2 ≡[ cong (2' +_) (*2≗+ bs'1) ]
2' + (bs'1 + bs'1) ≡[ sym (+-assoc 2' bs'1 bs'1) ]
(2' + bs'1) + bs'1 ≡[ cong (_+ bs'1) (+-assoc 1' 1' bs'1)
]
(suc (suc bs'1)) + bs'1 ≤[ +-mono-≤ 1+1+bs'1≤bs1 (≤-refl {bs'1}) ]
bs1 + bs'1 <[ +-mono-≤-< {bs1} {bs1} {bs'1} {bs1}
(≤-refl {bs1}) bs'1<bs1 ]
bs1 + bs1 ≡[ sym (*2≗+ bs1) ]
bs1 *2 ≡[ refl ]
0bs1
∎
------------
aux 1b 1b 0b with minus 0b (bs 1#) (bs' 1#) -- 1bs1 - 0bs'1, no subcarry
...
| (_ , minuend< bs1<0+bs'1 _) = (0# , minuend< 1bs1<1+0bs'1 refl)
where
bs1<bs'1 = subst (bs1 <_) (0+ bs'1) bs1<0+bs'1
1bs1<1+0bs'1 : 1bs1 < suc 0bs'1
1bs1<1+0bs'1 =
begin 1bs1 ≡[ sym (suc-even1 bs) ]
suc (bs1 *2) <[ suc-mono-< {bs1 *2} {bs'1 *2}
(*2-mono-< {bs1} {bs'1} bs1<bs'1) ]
suc (bs'1 *2) ≡[ refl ]
suc 0bs'1
∎
... | (_ , minuend≡ bs1≡0+bs'1 _) = (0# , minuend≡ 1bs1≡1+0bs'1 refl)
where
1bs1≡1+0bs'1 : 1bs1 ≡ suc 0bs'1
1bs1≡1+0bs'1 =
begin≡ 1bs1 ≡⟨ sym (suc-even1 bs) ⟩
suc (bs1 *2) ≡⟨ cong (suc ∘ _*2) bs1≡0+bs'1 ⟩
suc ((0# + bs'1) *2) ≡⟨ cong (suc ∘ _*2) (0+ bs'1) ⟩
suc (bs'1 *2) ≡⟨ refl ⟩
suc 0bs'1
end≡
... | (d , minuend> 0+bs'1<bs1 bs1≡0+bs'1+d) =
(d*2 , minuend> 1+0bs'1<1bs1 1bs1≡1+0bs'1+d*2)
where
d*2 = d *2
bs1≡bs'1+d = trans bs1≡0+bs'1+d (cong (_+ d) (0+ bs'1))
bs'1<bs1 = subst (_< bs1) (0+ bs'1) 0+bs'1<bs1
1bs1≡1+0bs'1+d*2 : 1bs1 ≡ (suc 0bs'1) + d*2
1bs1≡1+0bs'1+d*2 =
begin≡ 1bs1 ≡⟨ sym (suc-even1 bs) ⟩
suc (bs1 *2) ≡⟨ cong (suc ∘ _*2) bs1≡bs'1+d ⟩
suc ((bs'1 + d) *2) ≡⟨ cong suc (*2-distrib bs'1 d) ⟩
suc (bs'1 *2 + d*2) ≡⟨ sym (+-assoc 1' (bs'1 *2) d*2) ⟩
(suc (bs'1 *2)) + d*2 ≡⟨ refl ⟩
(suc 0bs'1) + d*2
end≡
1+0bs'1<1bs1 : suc 0bs'1 < 1bs1
1+0bs'1<1bs1 =
begin suc 0bs'1 ≡[ refl ]
suc (bs'1 *2) <[ suc-mono-< {bs'1 *2} {bs1 *2}
(*2-mono-< {bs'1} {bs1} bs'1<bs1) ]
suc (bs1 *2) ≡[ suc-even1 bs ]
1bs1
∎
------------
aux 1b 1b 1b with minus 1b (bs 1#) (bs' 1#) -- 1bs1 - 1bs'1, subcarry
...
| (_ , minuend< bs1<1+bs'1 _) = (0# , minuend< 1bs1<1+1bs'1 refl)
where
1+bs1≤1+bs'1 = <⇒suc≤ {bs1} {suc bs'1} bs1<1+bs'1
1bs1<1+1bs'1 : 1bs1 < suc 1bs'1
1bs1<1+1bs'1 =
begin 1bs1 ≡[ sym (suc-even1 bs) ]
suc (bs1 *2) <[ x<1+x (suc (bs1 *2)) ]
suc (suc (bs1 *2)) ≡[ sym (+-assoc 1' 1' (bs1 *2)) ]
2' + bs1 *2 ≡[ cong (2' +_) (*2≗*2bin bs1) ]
2' + bs1 * 2' ≤[ x<y⇒a+x*a≤y*a {bs1} {suc bs'1} 2'
bs1<1+bs'1 ]
(suc bs'1) * 2' ≡[ sym (*2≗*2bin (suc bs'1)) ]
(suc bs'1) *2 ≡[ *2-distrib 1' bs'1 ]
2' + bs'1 *2 ≡[ +-assoc 1' 1' (bs'1 *2) ]
suc (1' + bs'1 *2) ≡[ cong suc (suc-even1 bs') ]
suc 1bs'1
∎
... | (_ , minuend≡ bs1≡1+bs'1 _) =
(1' , minuend> 1+1bs'1<1bs1 1bs1≡1+1bs'1+1)
where
1bs1≡1+1bs'1+1 : 1bs1 ≡ (suc 1bs'1) + 1'
1bs1≡1+1bs'1+1 =
begin≡
1bs1 ≡⟨ sym (suc-even1 bs) ⟩
suc (bs1 *2) ≡⟨ cong (suc ∘ _*2) bs1≡1+bs'1 ⟩
suc ((suc bs'1) *2) ≡⟨ cong suc (*2-distrib 1' bs'1) ⟩
suc (2' + bs'1 *2) ≡⟨ cong suc (+-assoc 1' 1' (bs'1 *2)) ⟩
suc (suc (suc (bs'1 *2))) ≡⟨ cong (suc ∘ suc) (suc-even1 bs') ⟩
suc (suc 1bs'1) ≡⟨ +-comm 1' (suc 1bs'1) ⟩
(suc 1bs'1) + 1'
end≡
1+1bs'1<1bs1 : suc 1bs'1 < 1bs1
1+1bs'1<1bs1 =
begin suc 1bs'1 <[ x<1+x (suc 1bs'1) ]
suc (suc 1bs'1) ≡[ +-comm 1' (suc 1bs'1) ]
(suc 1bs'1) + 1' ≡[ sym 1bs1≡1+1bs'1+1 ]
1bs1
∎
... | (d , minuend> 1+bs'1<bs1 bs1≡1+bs'1+d) =
(1+d*2 , minuend> 1+1bs'1<1bs1 1bs1≡1+1bs'1+1+d*2)
where
d*2 = d *2
1+d*2 = suc d*2
1bs1≡1+1bs'1+1+d*2 : 1bs1 ≡ (suc 1bs'1) + 1+d*2
1bs1≡1+1bs'1+1+d*2 =
begin≡
1bs1 ≡⟨ sym (suc-even1 bs) ⟩
suc (bs1 *2) ≡⟨ cong (suc ∘ _*2) bs1≡1+bs'1+d ⟩
suc (((suc bs'1) + d) *2) ≡⟨ cong suc (*2-distrib (suc bs'1) d)
⟩
suc ((suc bs'1) *2 + d*2) ≡⟨ cong (suc ∘ (_+ d*2))
(*2-distrib 1' bs'1) ⟩
suc ((2' + bs'1 *2) + d*2) ≡⟨ sym $ +-assoc 1' (2' + bs'1 *2)
d*2 ⟩
suc (2' + bs'1 *2) + d*2 ≡⟨ cong (_+ d*2)
(+-comm 1' (2' + bs'1 *2)) ⟩
((2' + bs'1 *2) + 1') + d*2 ≡⟨ +-assoc (2' + bs'1 *2) 1' d*2 ⟩
(2' + bs'1 *2) + 1+d*2 ≡⟨ cong (_+ 1+d*2)
(+-assoc 1' 1' (bs'1 *2)) ⟩
(suc (suc (bs'1 *2))) + 1+d*2 ≡⟨ cong ((_+ 1+d*2) ∘ suc)
(suc-even1 bs') ⟩
(suc 1bs'1) + 1+d*2
end≡
1+1bs'1<1bs1 : suc 1bs'1 < 1bs1
1+1bs'1<1bs1 =
begin
suc 1bs'1 ≡[ sym (+0 (suc 1bs'1)) ]
(suc 1bs'1) + 0# <[ +-mono-≤-< {suc 1bs'1} {suc 1bs'1}
{0#} {1+d*2} ≤-refl (0<1+x d*2) ]
(suc 1bs'1) + 1+d*2 ≡[ sym 1bs1≡1+1bs'1+1+d*2 ]
1bs1
∎
------------------------------------------------------------------------------
infixl 6 _∸_
_∸_ : Op₂ Bin
x ∸ y = proj₁ (minus 0b x y)
x≤y←→x∸y≡0 : ∀ {x y} → (x ≤ y ←→ x ∸ y ≡ 0#)
x≤y←→x∸y≡0 {x} {y} =
(to , from)
where
to : x ≤ y → x ∸ y ≡ 0#
to x≤y
with minus 0b x y
... | (_ , minuend< _ d≡0) = d≡0
... | (_ , minuend≡ _ d≡0) = d≡0
... | (_ , minuend> x>0+y _ ) = ⊥-elim (≤⇒≯ x≤y x>y)
where
x>y = subst (x >_) (0+ y) x>0+y
from : x ∸ y ≡ 0# → x ≤ y
from x∸y≡0
with minus 0b x y
... | (_ , minuend< x<0+y _) = inj₁ x<y
where
x<y = subst (x <_) (0+ y) x<0+y
... | (_ , minuend≡ x≡0+y _ ) = inj₂ (trans x≡0+y (0+ y))
... | (0# , minuend> 0+y<x x≡0+y+0) = ⊥-elim (<⇒≢ y<x (sym x≡y))
where
y<x = subst (_< x) (0+ y) 0+y<x
x≡y = begin≡ x ≡⟨ x≡0+y+0 ⟩
(0# + y) + 0# ≡⟨ cong (_+ 0#) (0+ y) ⟩
y + 0# ≡⟨ (+0 y) ⟩
y
end≡
... | (bs 1# , minuend> _ _) = ⊥-elim (0≢bs1 bs (sym x∸y≡0))
x≤y⇒x∸y≡0 : ∀ {x y} → x ≤ y → x ∸ y ≡ 0#
x≤y⇒x∸y≡0 {x} {y} x≤y =
proj₁ (x≤y←→x∸y≡0 {x} {y}) x≤y
x∸y≡0⇒x≤y : ∀ {x y} → x ∸ y ≡ 0# → x ≤ y
x∸y≡0⇒x≤y {x} {y} x∸y≡0 =
proj₂ (x≤y←→x∸y≡0 {x} {y}) x∸y≡0
open Tri
x<y⇒0<y∸x : ∀ {x y} → x < y → 0# < y ∸ x
x<y⇒0<y∸x {x} {y} x<y =
case <-cmp (y ∸ x) 0#
of \
{ (tri> _ _ gt) → gt
; (tri≈ _ y∸x≡0 _ ) → let y≤x = x∸y≡0⇒x≤y {y} {x} y∸x≡0
in ⊥-elim (<⇒≱ x<y y≤x)
; (tri< y∸x<0 _ _ ) → ⊥-elim (≮0 (y ∸ x) y∸x<0)
}
x∸x : ∀ x → x ∸ x ≡ 0#
x∸x x =
x≤y⇒x∸y≡0 (≤-refl {x})
0∸ : ∀ x → 0# ∸ x ≡ 0#
0∸ x = x≤y⇒x∸y≡0 (0≤ x)
------------------------------------------------------------------------------
[x+y]∸y≡x : ∀ x y → (x + y) ∸ y ≡ x
[x+y]∸y≡x x y
with minus 0b (x + y) y
... | (_ , minuend< x+y<0+y _ ) = ⊥-elim (<⇒≱ x+y<0+y 0+y≤x+y)
where
0+y≤x+y = +-mono-≤ (0≤ x) (≤-refl {y})
... | (d , minuend≡ x+y≡0+y d≡0) = trans d≡0 (sym x≡0)
where
x≡0 : x ≡ 0#
x≡0 = +-rCancel y x 0# x+y≡0+y
... | (d , minuend> 0+y<x+y x+y≡0+y+d) = sym x≡d
where
x+y≡d+y = begin≡ x + y ≡⟨ x+y≡0+y+d ⟩
(0# + y) + d ≡⟨ cong (_+ d) (0+ y) ⟩
y + d ≡⟨ +-comm y d ⟩
d + y
end≡
x≡d = +-rCancel y x d x+y≡d+y
------------------
∸0 : (_∸ 0#) ≗ id
∸0 x =
begin≡ x ∸ 0# ≡⟨ cong (_∸ 0#) (sym (+0 x)) ⟩
(x + 0#) ∸ 0# ≡⟨ [x+y]∸y≡x x 0# ⟩
x
end≡
----------------------------------------------
x+[y∸x]≡y : ∀ {x y} → x ≤ y → x + (y ∸ x) ≡ y
x+[y∸x]≡y {x} {y} x≤y =
aux (minus 0b y x)
where
aux : ∃ (\d → MinusRes 0b y x d) → x + (y ∸ x) ≡ y
aux (_ , minuend< y<0+x _) = ⊥-elim (≤⇒≯ x≤y y<x)
where
y<x = subst (y <_) (0+ x) y<0+x
aux (_ , minuend≡ y≡0+x _) = begin≡ x + (y ∸ x) ≡⟨ cong (x +_) y∸x≡0 ⟩
x + 0# ≡⟨ +-comm x 0# ⟩
0# + x ≡⟨ sym y≡0+x ⟩
y
end≡
where
y≡x = trans y≡0+x (0+ x)
y∸x≡0 = x≤y⇒x∸y≡0 {y} {x} (inj₂ y≡x)
aux (d , minuend> 0+x<y y≡0+x+d) =
begin≡
x + (y ∸ x) ≡⟨ cong ((x +_) ∘ (_∸ x)) y≡d+x ⟩
x + ((d + x) ∸ x) ≡⟨ cong (x +_) ([x+y]∸y≡x d x) ⟩
x + d ≡⟨ +-comm x d ⟩
d + x ≡⟨ sym y≡d+x ⟩
y
end≡
where
y≡d+x = begin≡ y ≡⟨ y≡0+x+d ⟩
(0# + x) + d ≡⟨ cong (_+ d) (0+ x) ⟩
x + d ≡⟨ +-comm x d ⟩
d + x
end≡
-------------------------
∸1≗pred : (_∸ 1') ≗ pred
∸1≗pred 0# = refl
∸1≗pred (bs 1#) = begin≡ a ∸ 1' ≡⟨ cong (_∸ 1') (sym 1+pa≡a) ⟩
(1' + pa) ∸ 1' ≡⟨ cong (_∸ 1') (+-comm 1' pa) ⟩
(pa + 1') ∸ 1' ≡⟨ [x+y]∸y≡x pa 1' ⟩
pa
end≡
where
a = bs 1#; pa = pred a; 1+pa≡a = suc∘pred bs
------------------------------------------------------------------------------
toℕ∸homo : ∀ x y → toℕ (x ∸ y) ≡ (toℕ x) ∸n (toℕ y)
toℕ∸homo x y
with <-cmp x y
... | tri< x<y _ _ =
begin≡ toℕ (x ∸ y) ≡⟨ cong toℕ (x≤y⇒x∸y≡0 {x} {y} x≤y) ⟩
toℕ 0# ≡⟨ refl ⟩
0 ≡⟨ sym (NatProp0.≤⇒∸≡0 xN≤yN) ⟩
xN ∸n yN
end≡
where
xN = toℕ x; yN = toℕ y; x≤y = inj₁ x<y
xN≤yN = toℕ-mono-≤ x≤y
... | tri≈ _ x≡y _ =
begin≡ toℕ (x ∸ y) ≡⟨ cong toℕ (x≤y⇒x∸y≡0 {x} {y} x≤y) ⟩
toℕ 0# ≡⟨ refl ⟩
0 ≡⟨ sym (NatProp0.≤⇒∸≡0 xN≤yN) ⟩
xN ∸n yN
end≡
where
xN = toℕ x; yN = toℕ y; x≤y = inj₂ x≡y; xN≤yN = toℕ-mono-≤ x≤y
... | tri> _ _ y<x =
begin≡
toℕ (x ∸ y) ≡⟨ sym (NProp.m+n∸n≡m (toℕ (x ∸ y)) yN) ⟩
((toℕ (x ∸ y)) +n yN) ∸n yN ≡⟨ cong (_∸n yN) eq ⟩
((xN ∸n yN) +n yN) ∸n yN ≡⟨ NProp.m+n∸n≡m (xN ∸n yN) yN ⟩
xN ∸n yN
end≡
where
xN = toℕ x; yN = toℕ y; y≤x = inj₁ y<x
yN≤xN = toℕ-mono-≤ {y} {x} (inj₁ y<x)
eq = begin≡
toℕ (x ∸ y) +n yN ≡⟨ sym (toℕ+homo (x ∸ y) y) ⟩
toℕ ((x ∸ y) + y) ≡⟨ cong toℕ (+-comm (x ∸ y) y) ⟩
toℕ (y + (x ∸ y)) ≡⟨ cong toℕ (x+[y∸x]≡y {y} {x} y≤x) ⟩
xN ≡⟨ sym (NProp.m+n∸m≡n yN≤xN) ⟩
yN +n (xN ∸n yN) ≡⟨ +n-comm yN (xN ∸n yN) ⟩
(xN ∸n yN) +n yN
end≡
------------------------------------------------------------
fromℕ∸homo : ∀ m n → fromℕ (m ∸n n) ≡ (fromℕ m) ∸ (fromℕ n)
fromℕ∸homo m n =
begin≡ fromℕ (m ∸n n) ≡⟨ cong fromℕ (cong₂ _∸n_ m≡xN n≡yN) ⟩
fromℕ (toℕ x ∸n toℕ y) ≡⟨ cong fromℕ (sym (toℕ∸homo x y)) ⟩
fromℕ (toℕ (x ∸ y)) ≡⟨ fromℕ∘toℕ (x ∸ y) ⟩
x ∸ y
end≡