HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulenon-negative-0.1.2Haskell98

Numeric.NonNegative.Class

A type class for non-negative numbers. Prominent instances are T and peano numbers. Types from Data.Word would also be candidates. However, there is no Monoid instance for Word types and they have wrap-around semantics. This class cannot do any checks, but it let you show to the user what arguments your function expects. Thus you must define class instances with care. In fact many standard functions (take, (!!), ...) should have this type class constraint.

  • 1 class
  • 8 values
classclass (Ord a, Monoid a) => C a where
#

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.

The Monoid superclass contributes a zero and an addition.

Methods

  • split :: a -> a -> (a, (Bool, a))

    split x y == (m,(b,d)) means that b == (x<=y), m == min x y, d == max x y - min x y, that is d == abs(x-y).

    We have chosen this function as base function, since it provides comparison and subtraction in one go, which is important for replacing common structures like

    if x<=y
      then f(x-y)
      else g(y-x)

    that lead to a memory leak for peano numbers. We have choosen the simple check x<=y instead of a full-blown compare, since we want Zero <= undefined for peano numbers. Because of undefined values split is in general not commutative in the sense

    let (m0,(b0,d0)) = split x y
        (m1,(b1,d1)) = split y x
    in  m0==m1 && d0==d1

    The result values are in the order in which they are generated for Peano numbers. We have chosen the nested pair instead of a triple in order to prevent a memory leak that occurs if you only use b and d and ignore m. This is demonstrated by test cases Chunky.splitSpaceLeak3 and Chunky.splitSpaceLeak4.

Instances2C
  • C a => C (T a)Defined in non-negative-0.1.2 · Numeric.NonNegative.ChunkyPrivate

    This instance is not correct with respect to the equality check if the involved numbers contain zero chunks.

  • (Ord a, Num a) => C (T a)Defined in non-negative-0.1.2 · Numeric.NonNegative.Wrapper
valuesplitDefault
  1. :: (Ord b, Num b)
  2. => a -> b
  3. -> b -> a
  4. -> a
  5. -> a
  6. -> (a, (Bool, a))
#

Default implementation for wrapped types of Ord and Num class.

value(-|) :: C a => a -> a -> a
#
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.

valueadd :: C a => a -> a -> a
#
valuesum :: C a => [a] -> a
#
valuemaximum :: C a => [a] -> a
#

Left biased maximum of a list of numbers that can also be empty. It holds

maximum [] == zero
valueswitchDifferenceNegative :: C a => a -> a -> (a -> b) -> (a -> b) -> b
#

In switchDifferenceNegative x y branchXminusY branchYminusX the function branchXminusY is applied to x-y if this difference is non-negative, otherwise branchYminusX is applied to y-x.

valueswitchDifferenceOrdering :: C a => a -> a -> b -> (a -> b) -> (a -> b) -> b
#

In switchDifferenceOrdering x y branchZero branchXminusY branchYminusX