Skip to content

Commit

Permalink
Simplified DivP instance, now with derivation.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbm committed Feb 10, 2012
1 parent 98c40a7 commit cb17378
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Numeric/NumType/TF.lhs
Expand Up @@ -243,12 +243,14 @@ necessary to ensure bad division terminates with a proper error
instead of overflowing the context stack (more confusing).

> type family DivP n m -- n / m.
> type instance DivP Z (S n) = Z -- Trivially.

The recursive instance for division is quite complex and in fact I
do not recall how I derived it. But it works (I promise!).
The instances for division is based on the identities:

0 / y = 0, for y >= 1.
x / y = (x - y) / y + 1, for x >= y >= 1.

> type instance DivP (S n) (S n') = S (DivP (Pred (Sub (S n) n')) (S n')) -- Oh my!
> type instance DivP Z (S n) = Z -- Trivially.
> type instance DivP (S n) (S m) = S (DivP (Sub (S n) (S m)) (S m)) -- Oh my!

Now we can generalize division to negative numbers too, building on
top of 'DivP'. A trivial but tedious exercise.
Expand Down

0 comments on commit cb17378

Please sign in to comment.