úÎ@,=s       Haskell 98stablehaskell@henning-thielemann.de 9Instances of this class must ensure non-negative values. ?We cannot enforce this by types, but the type class constraint  NonNegative.C Cavoids accidental usage of types which allow for negative numbers. :The Monoid superclass contributes a zero and an addition. 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  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 2 in which they are generated for Peano numbers. 6 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 6 Chunky.splitSpaceLeak3 and Chunky.splitSpaceLeak4. ,Default implementation for wrapped types of   and ! class. ALeft biased maximum of a list of numbers that can also be empty.  It holds  maximum [] == zero In 8switchDifferenceNegative 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. In CswitchDifferenceOrdering x y branchZero branchXminusY branchYminusX  x -| y == max 0 (x-y)-The default implementation is not efficient, Cbecause 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.      Haskell 98stablehaskell@henning-thielemann.de "#+Convert a number to a non-negative number. 3If a negative number is given, an error is raised. >name of the calling function to be used in the error message $+Convert a number to a non-negative number. ,A negative number will be replaced by zero. 4Use this function with care since it may hide bugs. ?Wrap a number into a non-negative number without doing checks. 4This routine exists entirely for efficiency reasons >and must be used only in cases where you are absolutely sure, 'that the input number is non-negative. %(Results are not checked for positivity. &'(Results are not checked for positivity.    Haskell 98stablehaskell@henning-thielemann.de@A chunky non-negative number is a list of non-negative numbers. ,It represents the sum of the list elements. HIt is possible to represent a finite number with infinitely many chunks &by using an infinite number of zeros. Note the following problems: 9Addition is commutative only for finite representations. E.g. let y = min (1+y) 2 in y is defined, let y = min (y+1) 2 in y is not. ()=This routine exposes the inner structure of the lazy number. *Remove zero chunks. +,-./0123<This routine exposes the inner structure of the lazy number and is actually the same as . It was considered dangerous, 'but you can observe the lazy structure 'in tying-the-knot applications anyway. @So the explicit revelation of the chunks seems not to be worse. 4@This instance is not correct with respect to the equality check -if the involved numbers contain zero chunks. 23 23 Haskell 98stablehaskell@henning-thielemann.de  5      !"#!$%&'()*+&,+-./01234567non-negative-0.1Numeric.NonNegative.ClassNumeric.NonNegative.WrapperNumeric.NonNegative.Chunky!Numeric.NonNegative.ChunkyPrivateCsplit splitDefaultzeroaddsummaximumswitchDifferenceNegativeswitchDifferenceOrdering-|DoubleFloatRationalRatioIntegerIntT fromNumber fromNumberMsgfromNumberClipfromNumberUnsafetoNumber fromChunkstoChunks normalizeisNull isPositive divModStrictbase GHC.ClassesOrdGHC.NumNumConsunwrapfromNumberWrapliftliftWraplift2decons isNullListcheckglue equalList compareListminListmaxListfromChunksUnsafetoChunksUnsafe$fCT