/
DivMod.lagda.md
121 lines (97 loc) · 3.99 KB
/
DivMod.lagda.md
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
```agda
open import 1Lab.Prelude
open import Data.Nat.Properties
open import Data.Nat.Solver
open import Data.Nat.Order
open import Data.Dec.Base
open import Data.Nat.Base
open import Data.Sum.Base
module Data.Nat.DivMod where
```
# Natural division
This module implements the basics of the theory of **division** (not
[divisibility], see there) for the natural numbers. In particular, we
define what it means to divide in the naturals (the type
`DivMod`{.Agda}), and implement a division procedure that works for
positive denominators.
[divisibility]: Data.Nat.Divisible.html
The result of division isn't a single number, but rather a **pair** of
numbers $q, r$ such that $a = qb + r$. The number $q$ is the
**quotient** $a /_n b$, and the number $r$ is the **remainder**
$a \% b$.
```agda
record DivMod (a b : Nat) : Type where
constructor divmod
field
quot : Nat
rem : Nat
.quotient : a ≡ quot * b + rem
.smaller : rem < b
```
The easy case is to divide zero by anything, in which case the result is
zero with remainder zero. The more interesting case comes when we have
some successor, and we want to divide it.
```agda
divide-pos : ∀ a b → .⦃ _ : Positive b ⦄ → DivMod a b
divide-pos zero (suc b) = divmod 0 0 refl (s≤s 0≤x)
```
It suffices to assume --- since $a$ is smaller than $1+a$ --- that we
have already computed numbers $q', r'$ with $a = q'b + r'$. Since the
ordering on $\NN$ is trichotomous, we can proceed by cases on whether
$(1 + r') < b$.
```agda
divide-pos (suc a) b with divide-pos a b
divide-pos (suc a) b | divmod q′ r′ p s with ≤-split (suc r′) b
```
First, suppose that $1 + r' < b$, i.e., $1 + r'$ _can_ serve as a
remainder for the division of $(1 + a) / b$. In that case, we have our
divisor! It remains to show, by a slightly annoying computation, that
$$
(1 + a) = q'b + 1 + r
$$
```
divide-pos (suc a) b | divmod q′ r′ p s | inl r′+1<b =
divmod q′ (suc r′) (ap suc p ∙ sym (+-sucr (q′ * b) r′)) r′+1<b
```
The other case --- that in which $1 + r' = b$ --- is more interesting.
Then, rather than incrementing the remainder, our remainder has
"overflown", and we have to increment the _quotient_ instead. We take,
in this case, $q = 1 + q'$ and $r = 0$, which works out because ($0 < b$
and) of some arithmetic. See for yourself:
```agda
divide-pos (suc a) (suc b′) | divmod q′ r′ p s | inr (inr r′+1=b) =
divmod (suc q′) 0
( suc a ≡⟨ ap suc p ⟩
suc (q′ * (suc b′) + r′) ≡˘⟨ ap (λ e → suc (q′ * e + r′)) r′+1=b ⟩
suc (q′ * (suc r′) + r′) ≡⟨ nat! ⟩
suc (r′ + q′ * (suc r′) + zero) ≡⟨ ap (λ e → e + q′ * e + 0) r′+1=b ⟩
(suc b′) + q′ * (suc b′) + 0 ∎ )
(s≤s 0≤x)
```
Note that we actually have one more case to consider -- or rather,
discard -- that in which $b < 1 + r'$. It's impossible because, by the
definition of division, we have $r' < b$, meaning $(1 + r') \le b$.
```agda
divide-pos (suc a) (suc b′) | divmod q′ r′ p s | inr (inl b<r′+1) =
absurd $ <-not-equal b<r′+1
(≤-antisym (≤-sucr (≤-peel b<r′+1)) (recover (≤-dec _ _) s))
```
As a finishing touch, we define short operators to produce the result of
applying `divide-pos`{.Agda} to a pair of numbers. Note that the
procedure above only works when the denominator is nonzero, so we have a
degree of freedom when defining $x/0$ and $x \% 0$. The canonical choice
is to yield $0$ in both cases.
```agda
_%_ : Nat → Nat → Nat
a % zero = zero
a % suc b = divide-pos a (suc b) .DivMod.rem
_/ₙ_ : Nat → Nat → Nat
a /ₙ zero = zero
a /ₙ suc b = divide-pos a (suc b) .DivMod.quot
is-divmod : ∀ x y → .⦃ _ : Positive y ⦄ → x ≡ (x /ₙ y) * y + x % y
is-divmod x (suc y) with divide-pos x (suc y)
... | divmod q r α β = recover (Discrete-Nat _ _) α
x%y<y : ∀ x y → .⦃ _ : Positive y ⦄ → (x % y) < y
x%y<y x (suc y) with divide-pos x (suc y)
... | divmod q r α β = recover (≤-dec _ _) β
```