GHC.Num.Natural

ghc-bignum-1.3: GHC BigNum library

Safe Haskell | Safe-Inferred
Language | Haskell2010

GHC.Num.Natural

Synopsis

- data Natural
  - = NS !Word#
  - | NB !BigNat#
- naturalCheck# :: Natural -> Bool#
- naturalCheck :: Natural -> Bool
- naturalZero :: Natural
- naturalOne :: Natural
- naturalIsZero :: Natural -> Bool
- naturalIsOne :: Natural -> Bool
- naturalIsPowerOf2# :: Natural -> (# (# #) | Word# #)
- naturalFromBigNat# :: BigNat# -> Natural
- naturalToBigNat# :: Natural -> BigNat#
- naturalFromWord# :: Word# -> Natural
- naturalFromWord2# :: Word# -> Word# -> Natural
- naturalFromWord :: Word -> Natural
- naturalFromWordList :: [Word] -> Natural
- naturalToWord# :: Natural -> Word#
- naturalToWord :: Natural -> Word
- naturalToWordClamp# :: Natural -> Word#
- naturalToWordClamp :: Natural -> Word
- naturalToWordMaybe# :: Natural -> (# (# #) | Word# #)
- naturalEncodeDouble# :: Natural -> Int# -> Double#
- naturalEncodeFloat# :: Natural -> Int# -> Float#
- naturalEq# :: Natural -> Natural -> Bool#
- naturalEq :: Natural -> Natural -> Bool
- naturalNe# :: Natural -> Natural -> Bool#
- naturalNe :: Natural -> Natural -> Bool
- naturalGe# :: Natural -> Natural -> Bool#
- naturalGe :: Natural -> Natural -> Bool
- naturalLe# :: Natural -> Natural -> Bool#
- naturalLe :: Natural -> Natural -> Bool
- naturalGt# :: Natural -> Natural -> Bool#
- naturalGt :: Natural -> Natural -> Bool
- naturalLt# :: Natural -> Natural -> Bool#
- naturalLt :: Natural -> Natural -> Bool
- naturalCompare :: Natural -> Natural -> Ordering
- naturalPopCount# :: Natural -> Word#
- naturalPopCount :: Natural -> Word
- naturalShiftR# :: Natural -> Word# -> Natural
- naturalShiftR :: Natural -> Word -> Natural
- naturalShiftL# :: Natural -> Word# -> Natural
- naturalShiftL :: Natural -> Word -> Natural
- naturalAdd :: Natural -> Natural -> Natural
- naturalSub :: Natural -> Natural -> (# (# #) | Natural #)
- naturalSubThrow :: Natural -> Natural -> Natural
- naturalSubUnsafe :: Natural -> Natural -> Natural
- naturalMul :: Natural -> Natural -> Natural
- naturalSqr :: Natural -> Natural
- naturalSignum :: Natural -> Natural
- naturalNegate :: Natural -> Natural
- naturalQuotRem# :: Natural -> Natural -> (# Natural, Natural #)
- naturalQuotRem :: Natural -> Natural -> (Natural, Natural)
- naturalQuot :: Natural -> Natural -> Natural
- naturalRem :: Natural -> Natural -> Natural
- naturalAnd :: Natural -> Natural -> Natural
- naturalAndNot :: Natural -> Natural -> Natural
- naturalOr :: Natural -> Natural -> Natural
- naturalXor :: Natural -> Natural -> Natural
- naturalTestBit# :: Natural -> Word# -> Bool#
- naturalTestBit :: Natural -> Word -> Bool
- naturalBit# :: Word# -> Natural
- naturalBit :: Word -> Natural
- naturalSetBit# :: Natural -> Word# -> Natural
- naturalSetBit :: Natural -> Word -> Natural
- naturalClearBit# :: Natural -> Word# -> Natural
- naturalClearBit :: Natural -> Word -> Natural
- naturalComplementBit# :: Natural -> Word# -> Natural
- naturalComplementBit :: Natural -> Word -> Natural
- naturalGcd :: Natural -> Natural -> Natural
- naturalLcm :: Natural -> Natural -> Natural
- naturalLog2# :: Natural -> Word#
- naturalLog2 :: Natural -> Word
- naturalLogBaseWord# :: Word# -> Natural -> Word#
- naturalLogBaseWord :: Word -> Natural -> Word
- naturalLogBase# :: Natural -> Natural -> Word#
- naturalLogBase :: Natural -> Natural -> Word
- naturalPowMod :: Natural -> Natural -> Natural -> Natural
- naturalSizeInBase# :: Word# -> Natural -> Word#
- naturalToAddr# :: Natural -> Addr# -> Bool# -> State# s -> (# State# s, Word# #)
- naturalToAddr :: Natural -> Addr# -> Bool# -> IO Word
- naturalFromAddr# :: Word# -> Addr# -> Bool# -> State# s -> (# State# s, Natural #)
- naturalFromAddr :: Word# -> Addr# -> Bool# -> IO Natural
- naturalToMutableByteArray# :: Natural -> MutableByteArray# s -> Word# -> Bool# -> State# s -> (# State# s, Word# #)
- naturalFromByteArray# :: Word# -> ByteArray# -> Word# -> Bool# -> State# s -> (# State# s, Natural #)

# Documentation

data Natural Source #

Natural number

Invariant: numbers <= 0xffffffffffffffff use the `NS` constructor

Constructors

NS !Word# |
NB !BigNat# |

#### Instances

Instances details

Eq Natural Source # |
Instance details Defined in GHC.Num.Natural Methods (==) :: Natural -> Natural -> Bool Source # (/=) :: Natural -> Natural -> Bool Source #
Ord Natural Source # |
Instance details Defined in GHC.Num.Natural Methods compare :: Natural -> Natural -> Ordering Source # (<) :: Natural -> Natural -> Bool Source # (<=) :: Natural -> Natural -> Bool Source # (>) :: Natural -> Natural -> Bool Source # (>=) :: Natural -> Natural -> Bool Source # max :: Natural -> Natural -> Natural Source # min :: Natural -> Natural -> Natural Source #

naturalCheck# :: Natural -> Bool# Source #

Check Natural invariants

naturalCheck :: Natural -> Bool Source #

Check Natural invariants

naturalZero :: Natural Source #

Zero Natural

naturalOne :: Natural Source #

One Natural

naturalIsZero :: Natural -> Bool Source #

Test Zero Natural

naturalIsOne :: Natural -> Bool Source #

Test One Natural

naturalIsPowerOf2# :: Natural -> (# (# #) | Word# #) Source #

Indicate if the value is a power of two and which one

naturalFromBigNat# :: BigNat# -> Natural Source #

Create a Natural from a BigNat# (respect the invariants)

naturalToBigNat# :: Natural -> BigNat# Source #

Convert a Natural into a BigNat#

naturalFromWord# :: Word# -> Natural Source #

Create a Natural from a Word#

naturalFromWord2# :: Word# -> Word# -> Natural Source #

Convert two Word# (most-significant first) into a Natural

naturalFromWord :: Word -> Natural Source #

Create a Natural from a Word

naturalFromWordList :: [Word] -> Natural Source #

Create a Natural from a list of Word

naturalToWord# :: Natural -> Word# Source #

Convert the lower bits of a Natural into a Word#

naturalToWord :: Natural -> Word Source #

Convert the lower bits of a Natural into a Word

naturalToWordClamp# :: Natural -> Word# Source #

Convert a Natural into a Word# clamping to (maxBound :: Word#).

naturalToWordClamp :: Natural -> Word Source #

Convert a Natural into a Word# clamping to (maxBound :: Word).

naturalToWordMaybe# :: Natural -> (# (# #) | Word# #) Source #

Try downcasting `Natural` to `Word` value. Returns `(##)` if value doesn't fit in `Word`.

naturalEncodeDouble# :: Natural -> Int# -> Double# Source #

Encode (# Natural mantissa, Int# exponent #) into a Double#

naturalEncodeFloat# :: Natural -> Int# -> Float# Source #

Encode (# Natural mantissa, Int# exponent #) into a Float#

TODO: Not sure if it's worth to write `Float` optimized versions here

naturalEq# :: Natural -> Natural -> Bool# Source #

Equality test for Natural

naturalEq :: Natural -> Natural -> Bool Source #

Equality test for Natural

naturalNe# :: Natural -> Natural -> Bool# Source #

Inequality test for Natural

naturalNe :: Natural -> Natural -> Bool Source #

Inequality test for Natural

naturalGe# :: Natural -> Natural -> Bool# Source #

Greater or equal test for Natural

naturalGe :: Natural -> Natural -> Bool Source #

Greater or equal test for Natural

naturalLe# :: Natural -> Natural -> Bool# Source #

Lower or equal test for Natural

naturalLe :: Natural -> Natural -> Bool Source #

Lower or equal test for Natural

naturalGt# :: Natural -> Natural -> Bool# Source #

Greater test for Natural

naturalGt :: Natural -> Natural -> Bool Source #

Greater test for Natural

naturalLt# :: Natural -> Natural -> Bool# Source #

Lower test for Natural

naturalLt :: Natural -> Natural -> Bool Source #

Lower test for Natural

naturalCompare :: Natural -> Natural -> Ordering Source #

Compare two Natural

naturalPopCount# :: Natural -> Word# Source #

PopCount for Natural

naturalPopCount :: Natural -> Word Source #

PopCount for Natural

naturalShiftR# :: Natural -> Word# -> Natural Source #

Right shift for Natural

naturalShiftR :: Natural -> Word -> Natural Source #

Right shift for Natural

naturalShiftL# :: Natural -> Word# -> Natural Source #

Left shift

naturalShiftL :: Natural -> Word -> Natural Source #

Left shift

naturalAdd :: Natural -> Natural -> Natural Source #

Add two naturals

naturalSub :: Natural -> Natural -> (# (# #) | Natural #) Source #

Sub two naturals

naturalSubThrow :: Natural -> Natural -> Natural Source #

Sub two naturals

Throw an Underflow exception if x < y

naturalSubUnsafe :: Natural -> Natural -> Natural Source #

Sub two naturals

Unsafe: don't check that x >= y Undefined results if it happens

naturalMul :: Natural -> Natural -> Natural Source #

Multiplication

naturalSqr :: Natural -> Natural Source #

Square a Natural

naturalSignum :: Natural -> Natural Source #

Signum for Natural

naturalNegate :: Natural -> Natural Source #

Negate for Natural

naturalQuotRem# :: Natural -> Natural -> (# Natural, Natural #) Source #

Return division quotient and remainder

Division by zero is handled by BigNat

naturalQuotRem :: Natural -> Natural -> (Natural, Natural) Source #

Return division quotient and remainder

naturalQuot :: Natural -> Natural -> Natural Source #

Return division quotient

naturalRem :: Natural -> Natural -> Natural Source #

Return division remainder

naturalAnd :: Natural -> Natural -> Natural Source #

naturalAndNot :: Natural -> Natural -> Natural Source #

naturalOr :: Natural -> Natural -> Natural Source #

naturalXor :: Natural -> Natural -> Natural Source #

naturalTestBit# :: Natural -> Word# -> Bool# Source #

naturalTestBit :: Natural -> Word -> Bool Source #

naturalBit# :: Word# -> Natural Source #

naturalBit :: Word -> Natural Source #

naturalSetBit# :: Natural -> Word# -> Natural Source #

Since: ghc-bignum-1.3

naturalSetBit :: Natural -> Word -> Natural Source #

Since: ghc-bignum-1.3

naturalClearBit# :: Natural -> Word# -> Natural Source #

Since: ghc-bignum-1.3

naturalClearBit :: Natural -> Word -> Natural Source #

Since: ghc-bignum-1.3

naturalComplementBit# :: Natural -> Word# -> Natural Source #

Since: ghc-bignum-1.3

naturalComplementBit :: Natural -> Word -> Natural Source #

Since: ghc-bignum-1.3

naturalGcd :: Natural -> Natural -> Natural Source #

Compute greatest common divisor.

naturalLcm :: Natural -> Natural -> Natural Source #

Compute least common multiple.

naturalLog2# :: Natural -> Word# Source #

Base 2 logarithm

naturalLog2 :: Natural -> Word Source #

Base 2 logarithm

naturalLogBaseWord# :: Word# -> Natural -> Word# Source #

Logarithm for an arbitrary base

naturalLogBaseWord :: Word -> Natural -> Word Source #

Logarithm for an arbitrary base

naturalLogBase# :: Natural -> Natural -> Word# Source #

Logarithm for an arbitrary base

naturalLogBase :: Natural -> Natural -> Word Source #

Logarithm for an arbitrary base

naturalPowMod :: Natural -> Natural -> Natural -> Natural Source #

"``naturalPowMod` b e m`" computes base `b` raised to exponent `e` modulo `m`.

naturalSizeInBase# :: Word# -> Natural -> Word# Source #

Compute the number of digits of the Natural in the given base.

`base` must be > 1

naturalToAddr# :: Natural -> Addr# -> Bool# -> State# s -> (# State# s, Word# #) Source #

Write a `Natural` to `addr` in base-256 representation and return the number of bytes written.

The endianness is selected with the Bool# parameter: write most significant byte first (big-endian) if `1#` or least significant byte first (little-endian) if `0#`.

naturalToAddr :: Natural -> Addr# -> Bool# -> IO Word Source #

Write a `Natural` to `addr` in base-256 representation and return the number of bytes written.

The endianness is selected with the Bool# parameter: write most significant byte first (big-endian) if `1#` or least significant byte first (little-endian) if `0#`.

naturalFromAddr# :: Word# -> Addr# -> Bool# -> State# s -> (# State# s, Natural #) Source #

Read a Natural in base-256 representation from an Addr#.

The size is given in bytes.

The endianness is selected with the Bool# parameter: most significant byte first (big-endian) if `1#` or least significant byte first (little-endian) if `0#`.

Null higher limbs are automatically trimed.

naturalFromAddr :: Word# -> Addr# -> Bool# -> IO Natural Source #

Read a Natural in base-256 representation from an Addr#.

The size is given in bytes.

The endianness is selected with the Bool# parameter: most significant byte first (big-endian) if `1#` or least significant byte first (little-endian) if `0#`.

Null higher limbs are automatically trimed.

naturalToMutableByteArray# :: Natural -> MutableByteArray# s -> Word# -> Bool# -> State# s -> (# State# s, Word# #) Source #

Write a Natural in base-256 representation and return the number of bytes written.

The endianness is selected with the Bool# parameter: most significant byte first (big-endian) if `1#` or least significant byte first (little-endian) if `0#`.

naturalFromByteArray# :: Word# -> ByteArray# -> Word# -> Bool# -> State# s -> (# State# s, Natural #) Source #

Read a Natural in base-256 representation from a ByteArray#.

The size is given in bytes.

The endianness is selected with the Bool# parameter: most significant byte first (big-endian) if `1#` or least significant byte first (little-endian) if `0#`.

Null higher limbs are automatically trimed.