The primary module of the integer-types package is Integer, which exports
the following integer-like types:
| Type | Range |
|---|---|
Integer |
(-∞, ∞) |
Natural |
(0, ∞) |
Positive |
(1, ∞) |
In addition to Integer, there is also an equivalent type called Signed that
is represented as:
data Signed = Zero | NonZero Sign Positive
data Sign = MinusSign | PlusSignSigned also comes with bundled pattern synonyms that allow it to be used as if
it had the following definition:
data Signed = Minus Positive | Zero | Plus PositiveThe following modules contain monomorphic conversion functions:
Integer.IntegerInteger.NaturalInteger.PositiveInteger.Signed
For example, you can convert from Positive to Integer using either
Integer.Positive.toInteger or Integer.Integer.fromPositive, which are two
names for the same function of type Positive -> Integer.
Since not all integers are positive, the corresponding function in the reverse
direction has a Maybe codomain. Integer.Integer.toPositive and
Integer.Positive.fromInteger have the type Integer -> Maybe Positive.
The Integer module exports two polymorphic conversion functions. The first is
for conversions that always succeed, such as Positive -> Integer.
convert :: IntegerConvert a b => a -> bThe second is for conversions that may fail because they convert to a subset of
the domain, such as Integer -> Maybe Positive.
narrow :: IntegerNarrow a b => a -> Maybe bIn addition to the conversion utilities discussed above, this library also
provides some minimal support for converting to/from the Word and Int types.
These are system-dependent finite subsets of Integer that are sometimes used
for performance reasons.
toFinite :: (ConvertWithFinite a, Finite b) => a -> Maybe b
fromFinite :: (ConvertWithFinite a, Finite b) => b -> Maybe aFor example, toFinite may specialize as Positive -> Maybe Int, and
fromFinite may specialize as Int -> Maybe Positive.
For the Integer and Signed types that represent the full range of integers,
the standard arithmetic operations in the Num and Integral classes are
suitable.
For Natural and Positive, which are subsets of the integers, the standard
classes are not entirely appropriate. Consider, for example, subtraction.
(-) :: Num a => a -> a -> aNatural and Positive do belong to the Num class, but subtraction and some
other operations are partial; the expression 1 - 2 throws instead of returning
a value, because the integer result -1 is negative and not representable by
either Natural or Positive.
For this reason, Natural and Positive have their own subtraction functions
that return Signed.
-- from Integer.Positive
subtract :: Positive -> Positive -> Signed
--
3D08
from Integer.Natural
subtract :: Natural -> Natural -> SignedIn addition to the (-) method from the Num class and the subtract
functions for Natural and Positive, there are some polymorphic subtraction
functions in the Integer module. subtractSigned generalizes the two
monomorphic functions discussed in the previous section. Its codomain is
Signed.
subtractSigned :: forall a. Subtraction a =>
a -> a -> SignedsubtractInteger does the same thing, but gives the result as Integer instead
of Signed.
subtractInteger :: forall a. Subtraction a =>
a -> a -> IntegerThe subtract function generalizes further. Its domain is any subtractable type
(Natural, Positive, Integer, or Signed) and its codomain is any type
that can represent the full range of integers (Integer or Signed).
subtract :: forall b a. (Subtraction' b, Subtraction a) =>
a -> a -> b