foundation-0.0.3: Alternative prelude with batteries and no dependencies

License | BSD-style
Maintainer | Vincent Hanquez <vincent@snarc.org>
Stability | experimental
Portability | portable
Safe Haskell | None
Language | Haskell2010

Foundation.Numerical

Description

Compared to the Haskell hierarchy of number classes this provide a more flexible approach that is closer to the mathematical foundation (group, field, etc)

This try to only provide one feature per class, at the expense of the number of classes.

Synopsis

- class (Enum a, Eq a, Ord a, Integral a) => IsIntegral a where
- class (Enum a, Eq a, Ord a, Integral a, IsIntegral a) => IsNatural a where
- class Signed a where
- class Additive a where
- class Subtractive a where
  - type Difference a
- class Multiplicative a where
- class (Additive a, Multiplicative a) => IDivisible a where
- class Multiplicative a => Divisible a where
- data Sign
  - = SignNegative
  - | SignZero
  - | SignPositive
- recip :: Divisible a => a -> a
- class IntegralRounding a where
- class FloatingPoint a where

# Documentation

class (Enum a, Eq a, Ord a, Integral a) => IsIntegral a where #

Number literals, convertible through the generic Integer type.

all number are Enum'erable, meaning that you can move to next element

Minimal complete definition

toInteger

Methods

toInteger :: a -> Integer #

Instances

IsIntegral Int # |
Methods toInteger :: Int -> Integer #
IsIntegral Int8 # |
Methods toInteger :: Int8 -> Integer #
IsIntegral Int16 # |
Methods toInteger :: Int16 -> Integer #
IsIntegral Int32 # |
Methods toInteger :: Int32 -> Integer #
IsIntegral Int64 # |
Methods toInteger :: Int64 -> Integer #
IsIntegral Integer # |
Methods toInteger :: Integer -> Integer #
IsIntegral Word # |
Methods toInteger :: Word -> Integer #
IsIntegral Word8 # |
Methods toInteger :: Word8 -> Integer #
IsIntegral Word16 # |
Methods toInteger :: Word16 -> Integer #
IsIntegral Word32 # |
Methods toInteger :: Word32 -> Integer #
IsIntegral Word64 # |
Methods toInteger :: Word64 -> Integer #
IsIntegral Natural # |
Methods toInteger :: Natural -> Integer #
IsIntegral (Size ty) # |
Methods toInteger :: Size ty -> Integer #
IsIntegral (Offset ty) # |
Methods toInteger :: Offset ty -> Integer #

class (Enum a, Eq a, Ord a, Integral a, IsIntegral a) => IsNatural a where #

Minimal complete definition

toNatural

Methods

toNatural :: a -> Natural #

Instances

IsNatural Word # |
Methods toNatural :: Word -> Natural #
IsNatural Word8 # |
Methods toNatural :: Word8 -> Natural #
IsNatural Word16 # |
Methods toNatural :: Word16 -> Natural #
IsNatural Word32 # |
Methods toNatural :: Word32 -> Natural #
IsNatural Word64 # |
Methods toNatural :: Word64 -> Natural #
IsNatural Natural # |
Methods toNatural :: Natural -> Natural #
IsNatural (Size ty) # |
Methods toNatural :: Size ty -> Natural #
IsNatural (Offset ty) # |
Methods toNatural :: Offset ty -> Natural #

class Signed a where #

types that have sign and can be made absolute

Minimal complete definition

abs, signum

Methods

abs :: a -> a #

signum :: a -> Sign #

Instances

Signed Int # |
Methods abs :: Int -> Int # signum :: Int -> Sign #
Signed Int8 # |
Methods abs :: Int8 -> Int8 # signum :: Int8 -> Sign #
Signed Int16 # |
Methods abs :: Int16 -> Int16 # signum :: Int16 -> Sign #
Signed Int32 # |
Methods abs :: Int32 -> Int32 # signum :: Int32 -> Sign #
Signed Int64 # |
Methods abs :: Int64 -> Int64 # signum :: Int64 -> Sign #
Signed Integer # |
Methods abs :: Integer -> Integer # signum :: Integer -> Sign #

class Additive a where #

Represent class of things that can be added together, contains a neutral element and is commutative.

    x + azero = x
    azero + x = x
    x + y = y + x

Minimal complete definition

azero, (+)

Methods

azero :: a #

(+) :: a -> a -> a infixl 6 #

scale :: IsNatural n => n -> a -> a #

Instances

Additive Double # |
Methods azero :: Double # (+) :: Double -> Double -> Double # scale :: IsNatural n => n -> Double -> Double #
Additive Float # |
Methods azero :: Float # (+) :: Float -> Float -> Float # scale :: IsNatural n => n -> Float -> Float #
Additive Int # |
Methods azero :: Int # (+) :: Int -> Int -> Int # scale :: IsNatural n => n -> Int -> Int #
Additive Int8 # |
Methods azero :: Int8 # (+) :: Int8 -> Int8 -> Int8 # scale :: IsNatural n => n -> Int8 -> Int8 #
Additive Int16 # |
Methods azero :: Int16 # (+) :: Int16 -> Int16 -> Int16 # scale :: IsNatural n => n -> Int16 -> Int16 #
Additive Int32 # |
Methods azero :: Int32 # (+) :: Int32 -> Int32 -> Int32 # scale :: IsNatural n => n -> Int32 -> Int32 #
Additive Int64 # |
Methods azero :: Int64 # (+) :: Int64 -> Int64 -> Int64 # scale :: IsNatural n => n -> Int64 -> Int64 #
Additive Integer # |
Methods azero :: Integer # (+) :: Integer -> Integer -> Integer # scale :: IsNatural n => n -> Integer -> Integer #
Additive Word # |
Methods azero :: Word # (+) :: Word -> Word -> Word # scale :: IsNatural n => n -> Word -> Word #
Additive Word8 # |
Methods azero :: Word8 # (+) :: Word8 -> Word8 -> Word8 # scale :: IsNatural n => n -> Word8 -> Word8 #
Additive Word16 # |
Methods azero :: Word16 # (+) :: Word16 -> Word16 -> Word16 # scale :: IsNatural n => n -> Word16 -> Word16 #
Additive Word32 # |
Methods azero :: Word32 # (+) :: Word32 -> Word32 -> Word32 # scale :: IsNatural n => n -> Word32 -> Word32 #
Additive Word64 # |
Methods azero :: Word64 # (+) :: Word64 -> Word64 -> Word64 # scale :: IsNatural n => n -> Word64 -> Word64 #
Additive Natural # |
Methods azero :: Natural # (+) :: Natural -> Natural -> Natural # scale :: IsNatural n => n -> Natural -> Natural #
Additive (Size ty) # |
Methods azero :: Size ty # (+) :: Size ty -> Size ty -> Size ty # scale :: IsNatural n => n -> Size ty -> Size ty #
Additive (Offset ty) # |
Methods azero :: Offset ty # (+) :: Offset ty -> Offset ty -> Offset ty # scale :: IsNatural n => n -> Offset ty -> Offset ty #

class Subtractive a where #

Represent class of things that can be subtracted.

Note that the result is not necessary of the same type as the operand depending on the actual type.

For example:

    (-) :: Int -> Int -> Int
    (-) :: DateTime -> DateTime -> Seconds
    (-) :: Ptr a -> Ptr a -> PtrDiff
    (-) :: Natural -> Natural -> Maybe Natural

Minimal complete definition

(-)

Associated Types

type Difference a #

Methods

(-) :: a -> a -> Difference a infixl 6 #

Instances

Subtractive Double # |
Associated Types type Difference Double :: * # Methods (-) :: Double -> Double -> Difference Double #
Subtractive Float # |
Associated Types type Difference Float :: * # Methods (-) :: Float -> Float -> Difference Float #
Subtractive Int # |
Associated Types type Difference Int :: * # Methods (-) :: Int -> Int -> Difference Int #
Subtractive Int8 # |
Associated Types type Difference Int8 :: * # Methods (-) :: Int8 -> Int8 -> Difference Int8 #
Subtractive Int16 # |
Associated Types type Difference Int16 :: * # Methods (-) :: Int16 -> Int16 -> Difference Int16 #
Subtractive Int32 # |
Associated Types type Difference Int32 :: * # Methods (-) :: Int32 -> Int32 -> Difference Int32 #
Subtractive Int64 # |
Associated Types type Difference Int64 :: * # Methods (-) :: Int64 -> Int64 -> Difference Int64 #
Subtractive Integer # |
Associated Types type Difference Integer :: * # Methods (-) :: Integer -> Integer -> Difference Integer #
Subtractive Word # |
Associated Types type Difference Word :: * # Methods (-) :: Word -> Word -> Difference Word #
Subtractive Word8 # |
Associated Types type Difference Word8 :: * # Methods (-) :: Word8 -> Word8 -> Difference Word8 #
Subtractive Word16 # |
Associated Types type Difference Word16 :: * # Methods (-) :: Word16 -> Word16 -> Difference Word16 #
Subtractive Word32 # |
Associated Types type Difference Word32 :: * # Methods (-) :: Word32 -> Word32 -> Difference Word32 #
Subtractive Word64 # |
Associated Types type Difference Word64 :: * # Methods (-) :: Word64 -> Word64 -> Difference Word64 #
Subtractive Natural # |
Associated Types type Difference Natural :: * # Methods (-) :: Natural -> Natural -> Difference Natural #
Subtractive (Size ty) # |
Associated Types type Difference (Size ty) :: * # Methods (-) :: Size ty -> Size ty -> Difference (Size ty) #
Subtractive (Offset ty) # |
Associated Types type Difference (Offset ty) :: * # Methods (-) :: Offset ty -> Offset ty -> Difference (Offset ty) #

class Multiplicative a where #

Represent class of things that can be multiplied together

    x * midentity = x
    midentity * x = x

Minimal complete definition

midentity, (*)

Methods

midentity :: a #

Identity element over multiplication

(*) :: a -> a -> a infixl 7 #

Multiplication of 2 elements that result in another element

(^) :: (IsNatural n, IDivisible n) => a -> n -> a infixr 8 #

Raise to power, repeated multiplication e.g. > a ^ 2 = a * a > a ^ 10 = (a ^ 5) * (a ^ 5) .. (^) :: (IsNatural n) => a -> n -> a

Instances

Multiplicative Double # |
Methods midentity :: Double # (*) :: Double -> Double -> Double # (^) :: (IsNatural n, IDivisible n) => Double -> n -> Double #
Multiplicative Float # |
Methods midentity :: Float # (*) :: Float -> Float -> Float # (^) :: (IsNatural n, IDivisible n) => Float -> n -> Float #
Multiplicative Int # |
Methods midentity :: Int # (*) :: Int -> Int -> Int # (^) :: (IsNatural n, IDivisible n) => Int -> n -> Int #
Multiplicative Int8 # |
Methods midentity :: Int8 # (*) :: Int8 -> Int8 -> Int8 # (^) :: (IsNatural n, IDivisible n) => Int8 -> n -> Int8 #
Multiplicative Int16 # |
Methods midentity :: Int16 # (*) :: Int16 -> Int16 -> Int16 # (^) :: (IsNatural n, IDivisible n) => Int16 -> n -> Int16 #
Multiplicative Int32 # |
Methods midentity :: Int32 # (*) :: Int32 -> Int32 -> Int32 # (^) :: (IsNatural n, IDivisible n) => Int32 -> n -> Int32 #
Multiplicative Int64 # |
Methods midentity :: Int64 # (*) :: Int64 -> Int64 -> Int64 # (^) :: (IsNatural n, IDivisible n) => Int64 -> n -> Int64 #
Multiplicative Integer # |
Methods midentity :: Integer # (*) :: Integer -> Integer -> Integer # (^) :: (IsNatural n, IDivisible n) => Integer -> n -> Integer #
Multiplicative Rational # |
Methods midentity :: Rational # (*) :: Rational -> Rational -> Rational # (^) :: (IsNatural n, IDivisible n) => Rational -> n -> Rational #
Multiplicative Word # |
Methods midentity :: Word # (*) :: Word -> Word -> Word # (^) :: (IsNatural n, IDivisible n) => Word -> n -> Word #
Multiplicative Word8 # |
Methods midentity :: Word8 # (*) :: Word8 -> Word8 -> Word8 # (^) :: (IsNatural n, IDivisible n) => Word8 -> n -> Word8 #
Multiplicative Word16 # |
Methods midentity :: Word16 # (*) :: Word16 -> Word16 -> Word16 # (^) :: (IsNatural n, IDivisible n) => Word16 -> n -> Word16 #
Multiplicative Word32 # |
Methods midentity :: Word32 # (*) :: Word32 -> Word32 -> Word32 # (^) :: (IsNatural n, IDivisible n) => Word32 -> n -> Word32 #
Multiplicative Word64 # |
Methods midentity :: Word64 # (*) :: Word64 -> Word64 -> Word64 # (^) :: (IsNatural n, IDivisible n) => Word64 -> n -> Word64 #
Multiplicative Natural # |
Methods midentity :: Natural # (*) :: Natural -> Natural -> Natural # (^) :: (IsNatural n, IDivisible n) => Natural -> n -> Natural #

class (Additive a, Multiplicative a) => IDivisible a where #

Represent types that supports an euclidian division

    (x ‘div‘ y) * y + (x ‘mod‘ y) == x

Minimal complete definition

div, mod | divMod

Methods

div :: a -> a -> a #

mod :: a -> a -> a #

divMod :: a -> a -> (a, a) #

Instances

IDivisible Int # |
Methods div :: Int -> Int -> Int # mod :: Int -> Int -> Int # divMod :: Int -> Int -> (Int, Int) #
IDivisible Int8 # |
Methods div :: Int8 -> Int8 -> Int8 # mod :: Int8 -> Int8 -> Int8 # divMod :: Int8 -> Int8 -> (Int8, Int8) #
IDivisible Int16 # |
Methods div :: Int16 -> Int16 -> Int16 # mod :: Int16 -> Int16 -> Int16 # divMod :: Int16 -> Int16 -> (Int16, Int16) #
IDivisible Int32 # |
Methods div :: Int32 -> Int32 -> Int32 # mod :: Int32 -> Int32 -> Int32 # divMod :: Int32 -> Int32 -> (Int32, Int32) #
IDivisible Int64 # |
Methods div :: Int64 -> Int64 -> Int64 # mod :: Int64 -> Int64 -> Int64 # divMod :: Int64 -> Int64 -> (Int64, Int64) #
IDivisible Integer # |
Methods div :: Integer -> Integer -> Integer # mod :: Integer -> Integer -> Integer # divMod :: Integer -> Integer -> (Integer, Integer) #
IDivisible Word # |
Methods div :: Word -> Word -> Word # mod :: Word -> Word -> Word # divMod :: Word -> Word -> (Word, Word) #
IDivisible Word8 # |
Methods div :: Word8 -> Word8 -> Word8 # mod :: Word8 -> Word8 -> Word8 # divMod :: Word8 -> Word8 -> (Word8, Word8) #
IDivisible Word16 # |
Methods div :: Word16 -> Word16 -> Word16 # mod :: Word16 -> Word16 -> Word16 # divMod :: Word16 -> Word16 -> (Word16, Word16) #
IDivisible Word32 # |
Methods div :: Word32 -> Word32 -> Word32 # mod :: Word32 -> Word32 -> Word32 # divMod :: Word32 -> Word32 -> (Word32, Word32) #
IDivisible Word64 # |
Methods div :: Word64 -> Word64 -> Word64 # mod :: Word64 -> Word64 -> Word64 # divMod :: Word64 -> Word64 -> (Word64, Word64) #
IDivisible Natural # |
Methods div :: Natural -> Natural -> Natural # mod :: Natural -> Natural -> Natural # divMod :: Natural -> Natural -> (Natural, Natural) #

class Multiplicative a => Divisible a where #

Minimal complete definition

(/)

Methods

(/) :: a -> a -> a infixl 7 #

Instances

Divisible Double # |
Methods (/) :: Double -> Double -> Double #
Divisible Float # |
Methods (/) :: Float -> Float -> Float #
Divisible Rational # |
Methods (/) :: Rational -> Rational -> Rational #

data Sign #

Sign of a signed number

Constructors

SignNegative |
SignZero |
SignPositive |

Instances

Eq Sign # |
Methods (==) :: Sign -> Sign -> Bool # (/=) :: Sign -> Sign -> Bool #

recip :: Divisible a => a -> a #

class IntegralRounding a where #

Minimal complete definition

roundUp, roundDown, roundTruncate, roundNearest

Methods

roundUp :: Integral n => a -> n #

Round up, to the next integral.

Also known as `ceiling`

roundDown :: Integral n => a -> n #

Round down, to the previous integral

Also known as `floor`

roundTruncate :: Integral n => a -> n #

Truncate to the closest integral to the fractional number closer to 0.

This is equivalent to roundUp for negative Number and roundDown for positive Number

roundNearest :: Integral n => a -> n #

Round to the nearest integral

    roundNearest 3.6

4 > roundNearest 3.4 3

Instances

IntegralRounding Double # |
Methods roundUp :: Integral n => Double -> n # roundDown :: Integral n => Double -> n # roundTruncate :: Integral n => Double -> n # roundNearest :: Integral n => Double -> n #
IntegralRounding Float # |
Methods roundUp :: Integral n => Float -> n # roundDown :: Integral n => Float -> n # roundTruncate :: Integral n => Float -> n # roundNearest :: Integral n => Float -> n #
IntegralRounding Rational # |
Methods roundUp :: Integral n => Rational -> n # roundDown :: Integral n => Rational -> n # roundTruncate :: Integral n => Rational -> n # roundNearest :: Integral n => Rational -> n #

class FloatingPoint a where #

IEEE754 Floating Point

Minimal complete definition

floatRadix, floatDigits, floatRange, floatDecode, floatEncode

Methods

floatRadix :: Proxy a -> Integer #

floatDigits :: Proxy a -> Int #

floatRange :: Proxy a -> (Int, Int) #

floatDecode :: a -> (Integer, Int) #

floatEncode :: Integer -> Int -> a #