{- |
Copyright   :  (c) Henning Thielemann 2007

Maintainer  :  haskell@henning-thielemann.de
Stability   :  stable
Portability :  Haskell 98

A type class for non-negative numbers.
Prominent instances are 'Numeric.NonNegative.Wrapper.T' and peano numbers.
This class cannot do any checks,
but it let you show to the user what arguments your function expects.
In fact many standard functions ('take', '(!!)', ...)
should have this type class constraint.
Thus you must define class instances with care.
module Algebra.NonNegative (C(..)) where

import qualified Algebra.Additive as Additive
import qualified Algebra.Real     as Real

infixl 6 -|, -?

{- |
Instances of this class must ensure non-negative values.
We cannot enforce this by types, but the type class constraint @NonNegative.C@
avoids accidental usage of types which allow for negative numbers.
class (Ord a, Additive.C a) => C a where
   {- |
   @x -| y == max 0 (x-y)@

   The default implementation is not efficient,
   because it compares the values and then subtracts, again, if safe.
   @max 0 (x-y)@ is more elegant and efficient
   but not possible in the general case,
   since @x-y@ may already yield a negative number.
   (-|) :: a -> a -> a
   x -| y  =  if x >= y then x Additive.- y else Additive.zero

(-?) :: (Real.C a) => a -> a -> (Bool, a)
(-?) x y  =  (x >= y, Real.abs (x Additive.- y))