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 such that The number is the quotient and the number is the remainder

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 is smaller than β€” that we have already computed numbers with Since the ordering on is trichotomous, we can proceed by cases on whether

  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 i.e., can serve as a remainder for the division of In that case, we have our divisor! It remains to show, by a slightly annoying computation, that

  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 β€” 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, and which works out because ( 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 It’s impossible because, by the definition of division, we have meaning

  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 and The canonical choice is to yield 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 Ξ²

With this, we can decide whether two numbers divide each other by checking whether the remainder is zero!

mod-helper : (k m n j : Nat) β†’ Nat
mod-helper k m  zero    j      = k
mod-helper k m (suc n)  zero   = mod-helper 0       m n m
mod-helper k m (suc n) (suc j) = mod-helper (suc k) m n j

div-helper : (k m n j : Nat) β†’ Nat
div-helper k m  zero    j      = k
div-helper k m (suc n)  zero   = div-helper (suc k) m n m
div-helper k m (suc n) (suc j) = div-helper k       m n j

{-# BUILTIN NATDIVSUCAUX div-helper #-}
{-# BUILTIN NATMODSUCAUX mod-helper #-}

-- _ = {! mod-helper 0 0 4294967296 2  !}

_/β‚™_ : (d1 d2 : Nat) .⦃ _ : Positive d2 ⦄ β†’ Nat
d1 /β‚™ suc d2 = div-helper 0 d2 d1 d2

_%_ : (d1 d2 : Nat) .⦃ _ : Positive d2 ⦄ β†’ Nat
d1 % suc d2 = mod-helper 0 d2 d1 d2

infixl 9 _/β‚™_ _%_

abstract
  private
    div-mod-lemma : βˆ€ am ad d n β†’ am + ad * suc (am + n) + d ≑ mod-helper am (am + n) d n + div-helper ad (am + n) d n * suc (am + n)
    div-mod-lemma am ad zero n = +-zeror _
    div-mod-lemma am ad (suc d) zero rewrite Id≃path.from (+-zeror am) = +-sucr _ d βˆ™ div-mod-lemma zero (suc ad) d am
    div-mod-lemma am ad (suc d) (suc n) rewrite Id≃path.from (+-sucr am n) = +-sucr _ d βˆ™ div-mod-lemma (suc am) ad d n

    mod-le : βˆ€ a d n β†’ mod-helper a (a + n) d n ≀ a + n
    mod-le a zero n = +-≀l a n
    mod-le a (suc d) zero = mod-le zero d (a + 0)
    mod-le a (suc d) (suc n) rewrite Id≃path.from (+-sucr a n) = mod-le (suc a) d n

  is-divmod : βˆ€ x y β†’ .⦃ _ : Positive y ⦄ β†’ x ≑ (x /β‚™ y) * y + x % y
  is-divmod x (suc y) = div-mod-lemma 0 0 x y βˆ™ +-commutative (x % (suc y)) _

  x%y<y : βˆ€ x y β†’ .⦃ _ : Positive y ⦄ β†’ (x % y) < y
  x%y<y x (suc y) = s≀s (mod-le 0 x y)

from-fast-mod : βˆ€ d1 d2 .⦃ _ : Positive d2 ⦄ β†’ d1 % d2 ≑ 0 β†’ d2 ∣ d1
from-fast-mod d1 d2 p = fibreβ†’βˆ£ (d1 /β‚™ d2 , sym (is-divmod d1 d2 βˆ™ ap (d1 /β‚™ d2 * d2 +_) p βˆ™ +-zeror _))

divide-pos : βˆ€ a b β†’ .⦃ _ : Positive b ⦄ β†’ DivMod a b
divide-pos a b = divmod (a /β‚™ b) (a % b) (is-divmod a b) (x%y<y a b)
instance
  Dec-∣ : βˆ€ {n m} β†’ Dec (n ∣ m)
  Dec-∣ {zero} {m} = m ≑? 0
  Dec-∣ n@{suc _} {m} with divide-pos m n
  ... | divmod q 0 Ξ± Ξ² = yes (q , sym (+-zeror _) βˆ™ sym (recover Ξ±))
  ... | divmod q r@(suc _) Ξ± Ξ² = no Ξ» (q' , p) β†’
    let
      n∣r : (q' - q) * n ≑ r
      n∣r = monus-distribr q' q n βˆ™ sym (monus-swapl _ _ _ (sym (p βˆ™ recover Ξ±)))
    in <-≀-asym (recover Ξ²) (m∣snβ†’m≀sn (q' - q , recover n∣r))