[Haskell-cafe] Defining subtraction for naturals
Henning Thielemann
lemming at henning-thielemann.de
Thu Mar 17 20:13:48 CET 2011
On Thu, 17 Mar 2011, wren ng thornton wrote:
> (1) If the result would drop below zero then throw an overflow error;
>
> (2) Use saturating subtraction, i.e. if the result would drop below zero then
> return zero;
>
> (3) Use type hackery to disallow performing subtraction when the result would
> drop below zero, e.g. by requiring a proof that the left argument is not less
> than the right.
My practical experiences with the non-negative package are: I started with
a saturation subtraction as in (2), but it turned out, that I hardly need
that definition in further code. Thus I switched to
(4) Use a symmetric subtraction that returns the minimum, the order and
the absolute difference of both operands. Precise definitions and reasons
in 'split' function:
http://hackage.haskell.org/packages/archive/non-negative/0.1/doc/html/Numeric-NonNegative-Class.html
More information about the Haskell-Cafe
mailing list