Num -package:haha -package:linear-base -package:svg-tree package:relude
Basic numeric class.
The Haskell Report defines no laws for
Num. However,
(+) and
(*) are customarily expected
to define a ring and have the following properties:
- Associativity of (+) (x + y) +
z = x + (y + z)
- Commutativity of (+) x + y
= y + x
- fromInteger 0 is the additive
identity x + fromInteger 0 = x
- negate gives the additive inverse x +
negate x = fromInteger 0
- Associativity of (*) (x * y) *
z = x * (y * z)
- fromInteger 1 is the multiplicative
identity x * fromInteger 1 = x and
fromInteger 1 * x = x
- Distributivity of (*) with respect to
(+) a * (b + c) = (a * b) + (a *
c) and (b + c) * a = (b * a) + (c * a)
Note that it
isn't customarily expected that a type instance of
both
Num and
Ord implement an ordered ring. Indeed, in
base only
Integer and
Rational do.
Provides numerical data types and functions.
Extract the numerator of the ratio in reduced form: the numerator and
denominator have no common factor and the denominator is positive.
Reexports
Enum related typeclasses and functions. Also
introduces a few useful helpers to work with Enums.
Note: universe,
universeNonEmpty and
inverseMap were previously in the extra modules, but due to
their benefit in different use cases. If you imported
Relude.Extra.Enum module, you can remove it now, as these
functions are reexported in the main
Relude module.
Class
Enum defines operations on sequentially ordered types.
The
enumFrom... methods are used in Haskell's translation of
arithmetic sequences.
Instances of
Enum may be derived for any enumeration type
(types whose constructors have no fields). The nullary constructors
are assumed to be numbered left-to-right by
fromEnum from
0 through
n-1. See Chapter 10 of the
Haskell
Report for more details.
For any type that is an instance of class
Bounded as well as
Enum, the following should hold:
enumFrom x = enumFromTo x maxBound
enumFromThen x y = enumFromThenTo x y bound
where
bound | fromEnum y >= fromEnum x = maxBound
| otherwise = minBound
Used in Haskell's translation of
[n..] with
[n..] =
enumFrom n, a possible implementation being
enumFrom n = n :
enumFrom (succ n). For example:
enumFrom 4 :: [Integer] = [4,5,6,7,...]
enumFrom 6 :: [Int] = [6,7,8,9,...,maxBound ::
Int]
Used in Haskell's translation of
[n,n'..] with
[n,n'..] =
enumFromThen n n', a possible implementation being
enumFromThen n n' = n : n' : worker (f x) (f x n'),
worker s v = v : worker s (s v),
x = fromEnum n' -
fromEnum n and
f n y | n > 0 = f (n - 1) (succ y) | n <
0 = f (n + 1) (pred y) | otherwise = y For example:
enumFromThen 4 6 :: [Integer] = [4,6,8,10...]
enumFromThen 6 2 :: [Int] = [6,2,-2,-6,...,minBound ::
Int]
Used in Haskell's translation of
[n,n'..m] with
[n,n'..m]
= enumFromThenTo n n' m, a possible implementation being
enumFromThenTo n n' m = worker (f x) (c x) n m,
x =
fromEnum n' - fromEnum n,
c x = bool (>=) ((x
0) f n y | n > 0 = f (n - 1) (succ y) | n < 0 = f (n +
1) (pred y) | otherwise = y and
worker s c v m | c v m = v :
worker s c (s v) m | otherwise = [] For example:
enumFromThenTo 4 2 -6 :: [Integer] =
[4,2,0,-2,-4,-6]
enumFromThenTo 6 8 2 :: [Int] = []
Used in Haskell's translation of
[n..m] with
[n..m] =
enumFromTo n m, a possible implementation being
enumFromTo n
m | n <= m = n : enumFromTo (succ n) m | otherwise = []. For
example:
enumFromTo 6 10 :: [Int] = [6,7,8,9,10]
enumFromTo 42 1 :: [Integer] = []
Convert to an
Int. It is implementation-dependent what
fromEnum returns when applied to a value that is too large to
fit in an
Int.
Mini bounded-enum framework inside relude.
Returns
Nothing if given
Int outside range.
>>> safeToEnum @Bool 0
Just False
>>> safeToEnum @Bool 1
Just True
>>> safeToEnum @Bool 2
Nothing
>>> safeToEnum @Bool (-1)
Nothing
Sign of a number. The functions
abs and
signum should
satisfy the law:
abs x * signum x == x
For real numbers, the
signum is either
-1 (negative),
0 (zero) or
1 (positive).