-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/


-- | Non-negative numbers
--   
--   Provides a class for non-negative numbers, a wrapper which can turn
--   any ordered numeric type into a member of that class, and a lazy
--   number type for non-negative numbers (a generalization of Peano
--   numbers). This library is used by the <tt>event-list</tt> package.
@package non-negative
@version 0.1.2


-- | A type class for non-negative numbers. Prominent instances are
--   <a>T</a> and peano numbers. Types from <a>Data.Word</a> would also be
--   candidates. However, there is no <a>Monoid</a> instance for
--   <a>Word</a> 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 (<a>take</a>, '(!!)', ...) should have
--   this type class constraint.
module Numeric.NonNegative.Class

-- | Instances of this class must ensure non-negative values. We cannot
--   enforce this by types, but the type class constraint
--   <tt>NonNegative.C</tt> avoids accidental usage of types which allow
--   for negative numbers.
--   
--   The Monoid superclass contributes a zero and an addition.
class (Ord a, Monoid a) => C a

-- | <tt>split x y == (m,(b,d))</tt> means that <tt>b == (x&lt;=y)</tt>,
--   <tt>m == min x y</tt>, <tt>d == max x y - min x y</tt>, that is <tt>d
--   == abs(x-y)</tt>.
--   
--   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
--   
--   <pre>
--   if x&lt;=y
--     then f(x-y)
--     else g(y-x)
--   </pre>
--   
--   that lead to a memory leak for peano numbers. We have choosen the
--   simple check <tt>x&lt;=y</tt> instead of a full-blown
--   <tt>compare</tt>, since we want <tt>Zero &lt;= undefined</tt> for
--   peano numbers. Because of undefined values <a>split</a> is in general
--   not commutative in the sense
--   
--   <pre>
--   let (m0,(b0,d0)) = split x y
--       (m1,(b1,d1)) = split y x
--   in  m0==m1 &amp;&amp; d0==d1
--   </pre>
--   
--   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 <tt>b</tt>
--   and <tt>d</tt> and ignore <tt>m</tt>. This is demonstrated by test
--   cases Chunky.splitSpaceLeak3 and Chunky.splitSpaceLeak4.
split :: C a => a -> a -> (a, (Bool, a))

-- | Default implementation for wrapped types of <a>Ord</a> and <a>Num</a>
--   class.
splitDefault :: (Ord b, Num b) => (a -> b) -> (b -> a) -> a -> a -> (a, (Bool, a))

-- | <pre>
--   x -| y == max 0 (x-y)
--   </pre>
--   
--   The default implementation is not efficient, because it compares the
--   values and then subtracts, again, if safe. <tt>max 0 (x-y)</tt> is
--   more elegant and efficient but not possible in the general case, since
--   <tt>x-y</tt> may already yield a negative number.
(-|) :: C a => a -> a -> a
zero :: C a => a
add :: C a => a -> a -> a
infixl 6 `add`
sum :: C a => [a] -> a

-- | Left biased maximum of a list of numbers that can also be empty. It
--   holds
--   
--   <pre>
--   maximum [] == zero
--   </pre>
maximum :: C a => [a] -> a

-- | In <tt>switchDifferenceNegative x y branchXminusY branchYminusX</tt>
--   the function <tt>branchXminusY</tt> is applied to <tt>x-y</tt> if this
--   difference is non-negative, otherwise <tt>branchYminusX</tt> is
--   applied to <tt>y-x</tt>.
switchDifferenceNegative :: C a => a -> a -> (a -> b) -> (a -> b) -> b

-- | In <tt>switchDifferenceOrdering x y branchZero branchXminusY
--   branchYminusX</tt>
switchDifferenceOrdering :: C a => a -> a -> b -> (a -> b) -> (a -> b) -> b


-- | A lazy number type, which is a generalization of lazy Peano numbers.
--   Comparisons can be made lazy and thus computations are possible which
--   are impossible with strict number types, e.g. you can compute <tt>let
--   y = min (1+y) 2 in y</tt>. You can even work with infinite values.
--   However, depending on the granularity, the memory consumption is
--   higher than that for strict number types. This number type is of
--   interest for the merge operation of event lists, which allows for
--   co-recursive merges.
module Numeric.NonNegative.Chunky

-- | A chunky non-negative number is a list of non-negative numbers. It
--   represents the sum of the list elements. It is possible to represent a
--   finite number with infinitely many chunks by using an infinite number
--   of zeros.
--   
--   Note the following problems:
--   
--   Addition is commutative only for finite representations. E.g. <tt>let
--   y = min (1+y) 2 in y</tt> is defined, <tt>let y = min (y+1) 2 in
--   y</tt> is not.
data T a
fromChunks :: C a => [a] -> T a
fromNumber :: C a => a -> T a

-- | This routine exposes the inner structure of the lazy number.
toChunks :: T a -> [a]
toNumber :: C a => T a -> a
zero :: T a

-- | Remove zero chunks.
normalize :: C a => T a -> T a
isNull :: C a => T a -> Bool
isPositive :: C a => T a -> Bool
divModStrict :: (Integral a, C a) => T a -> a -> (T a, a)


-- | A type for non-negative numbers. It performs a run-time check at
--   construction time (i.e. at run-time) and is a member of the
--   non-negative number type class <a>C</a>.
module Numeric.NonNegative.Wrapper
data T a

-- | Convert a number to a non-negative number. If a negative number is
--   given, an error is raised.
fromNumber :: (Ord a, Num a) => a -> T a
fromNumberMsg :: (Ord a, Num a) => String -> a -> T a

-- | Convert a number to a non-negative number. A negative number will be
--   replaced by zero. Use this function with care since it may hide bugs.
fromNumberClip :: (Ord a, Num a) => a -> T a

-- | Wrap a number into a non-negative number without doing checks. This
--   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.
fromNumberUnsafe :: a -> T a
toNumber :: T a -> a
type Int = T Int
type Integer = T Integer
type Float = T Float
type Double = T Double
type Ratio a = T (Ratio a)
type Rational = T Rational
instance GHC.Classes.Ord a => GHC.Classes.Ord (Numeric.NonNegative.Wrapper.T a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (Numeric.NonNegative.Wrapper.T a)
instance GHC.Show.Show a => GHC.Show.Show (Numeric.NonNegative.Wrapper.T a)
instance GHC.Num.Num a => Data.Semigroup.Semigroup (Numeric.NonNegative.Wrapper.T a)
instance GHC.Num.Num a => GHC.Base.Monoid (Numeric.NonNegative.Wrapper.T a)
instance (GHC.Classes.Ord a, GHC.Num.Num a) => Numeric.NonNegative.Class.C (Numeric.NonNegative.Wrapper.T a)
instance (GHC.Classes.Ord a, GHC.Num.Num a) => GHC.Num.Num (Numeric.NonNegative.Wrapper.T a)
instance GHC.Real.Real a => GHC.Real.Real (Numeric.NonNegative.Wrapper.T a)
instance (GHC.Classes.Ord a, GHC.Num.Num a, GHC.Enum.Enum a) => GHC.Enum.Enum (Numeric.NonNegative.Wrapper.T a)
instance (GHC.Classes.Ord a, GHC.Num.Num a, GHC.Enum.Bounded a) => GHC.Enum.Bounded (Numeric.NonNegative.Wrapper.T a)
instance GHC.Real.Integral a => GHC.Real.Integral (Numeric.NonNegative.Wrapper.T a)
instance (GHC.Classes.Ord a, GHC.Real.Fractional a) => GHC.Real.Fractional (Numeric.NonNegative.Wrapper.T a)
instance GHC.Real.RealFrac a => GHC.Real.RealFrac (Numeric.NonNegative.Wrapper.T a)
instance (GHC.Classes.Ord a, GHC.Float.Floating a) => GHC.Float.Floating (Numeric.NonNegative.Wrapper.T a)
instance (GHC.Num.Num a, Test.QuickCheck.Arbitrary.Arbitrary a) => Test.QuickCheck.Arbitrary.Arbitrary (Numeric.NonNegative.Wrapper.T a)
