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), and implement a division procedure that works for positive denominators.

The result of division isn’t a single number, but rather a pair of numbers q,rq, r such that a=qb+ra = qb + r. The number qq is the quotient a/nba /_n b, and the number rr is the remainder a%ba \% b.

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.

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 aa is smaller than 1+a1+a — that we have already computed numbers q,rq', r' with a=qb+ra = q'b + r'. Since the ordering on N\mathbb{N} is trichotomous, we can proceed by cases on whether (1+r)<b(1 + r') < b.

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<b1 + r' < b, i.e., 1+r1 + r' can serve as a remainder for the division of (1+a)/b(1 + a) / b. In that case, we have our divisor! It remains to show, by a slightly annoying computation, that

(1+a)=qb+1+r (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=b1 + 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+qq = 1 + q' and r=0r = 0, which works out because (0<b0 < b and) of some arithmetic. See for yourself:

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+rb < 1 + r'. It’s impossible because, by the definition of division, we have r<br' < b, meaning (1+r)b(1 + r') \le b.

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 s)))

As a finishing touch, we define short operators to produce the result of applying divide-pos 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/0x/0 and x%0x \% 0. The canonical choice is to yield 00 in both cases.

_%_ : 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 α

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 β