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 thatb == (x<=y),m == min x y,d == max x y - min x y, that isd == 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<=yinstead of a full-blowncompare, since we wantZero <= undefinedfor peano numbers. Because of undefined values split is in general not commutative in the senselet (m0,(b0,d0)) = split x y (m1,(b1,d1)) = split y x in m0==m1 && d0==d1The 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
banddand ignorem. This is demonstrated by test cases Chunky.splitSpaceLeak3 and Chunky.splitSpaceLeak4.