GHC.Natural

base-4.16.1.0: Basic libraries

Safe Haskell | Trustworthy
Language | Haskell2010

GHC.Natural

Contents

- Arithmetic
- Bits
- Conversions
- Modular arithmetic

Description

Compatibility module for pre ghc-bignum code.

Synopsis

- data Natural where
  - pattern NatS# :: Word# -> Natural
  - pattern NatJ# :: BigNat -> Natural
- data BigNat = BN# {
  - unBigNat :: BigNat#
  }
- mkNatural :: [Word] -> Natural
- isValidNatural :: Natural -> Bool
- plusNatural :: Natural -> Natural -> Natural
- minusNatural :: Natural -> Natural -> Natural
- minusNaturalMaybe :: Natural -> Natural -> Maybe Natural
- timesNatural :: Natural -> Natural -> Natural
- negateNatural :: Natural -> Natural
- signumNatural :: Natural -> Natural
- quotRemNatural :: Natural -> Natural -> (Natural, Natural)
- quotNatural :: Natural -> Natural -> Natural
- remNatural :: Natural -> Natural -> Natural
- gcdNatural :: Natural -> Natural -> Natural
- lcmNatural :: Natural -> Natural -> Natural
- andNatural :: Natural -> Natural -> Natural
- orNatural :: Natural -> Natural -> Natural
- xorNatural :: Natural -> Natural -> Natural
- bitNatural :: Int# -> Natural
- testBitNatural :: Natural -> Int -> Bool
- popCountNatural :: Natural -> Int
- shiftLNatural :: Natural -> Int -> Natural
- shiftRNatural :: Natural -> Int -> Natural
- naturalToInteger :: Natural -> Integer
- naturalToWord :: Natural -> Word
- naturalToWordMaybe :: Natural -> Maybe Word
- wordToNatural :: Word -> Natural
- wordToNatural# :: Word -> Natural
- naturalFromInteger :: Integer -> Natural
- powModNatural :: Natural -> Natural -> Natural -> Natural

# Documentation

data Natural where Source #

Natural number

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

Bundled Patterns

pattern NatS# :: Word# -> Natural |
pattern NatJ# :: BigNat -> Natural |

#### Instances

Instances details

Data Natural Source # | Since: base-4.8.0.0
Instance details Defined in Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Natural -> c Natural Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Natural Source # toConstr :: Natural -> Constr Source # dataTypeOf :: Natural -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Natural) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Natural) Source # gmapT :: (forall b. Data b => b -> b) -> Natural -> Natural Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Natural -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Natural -> r Source # gmapQ :: (forall d. Data d => d -> u) -> Natural -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> Natural -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Natural -> m Natural Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Natural -> m Natural Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Natural -> m Natural Source #
Bits Natural Source # | Since: base-4.8.0
Instance details Defined in GHC.Bits Methods (.&.) :: Natural -> Natural -> Natural Source # (.|.) :: Natural -> Natural -> Natural Source # xor :: Natural -> Natural -> Natural Source # complement :: Natural -> Natural Source # shift :: Natural -> Int -> Natural Source # rotate :: Natural -> Int -> Natural Source # zeroBits :: Natural Source # bit :: Int -> Natural Source # setBit :: Natural -> Int -> Natural Source # clearBit :: Natural -> Int -> Natural Source # complementBit :: Natural -> Int -> Natural Source # testBit :: Natural -> Int -> Bool Source # bitSizeMaybe :: Natural -> Maybe Int Source # bitSize :: Natural -> Int Source # isSigned :: Natural -> Bool Source # shiftL :: Natural -> Int -> Natural Source # unsafeShiftL :: Natural -> Int -> Natural Source # shiftR :: Natural -> Int -> Natural Source # unsafeShiftR :: Natural -> Int -> Natural Source # rotateL :: Natural -> Int -> Natural Source # rotateR :: Natural -> Int -> Natural Source # popCount :: Natural -> Int Source #
Enum Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Enum Methods succ :: Natural -> Natural Source # pred :: Natural -> Natural Source # toEnum :: Int -> Natural Source # fromEnum :: Natural -> Int Source # enumFrom :: Natural -> [Natural] Source # enumFromThen :: Natural -> Natural -> [Natural] Source # enumFromTo :: Natural -> Natural -> [Natural] Source # enumFromThenTo :: Natural -> Natural -> Natural -> [Natural] Source #
Ix Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Ix Methods range :: (Natural, Natural) -> [Natural] Source # index :: (Natural, Natural) -> Natural -> Int Source # unsafeIndex :: (Natural, Natural) -> Natural -> Int Source # inRange :: (Natural, Natural) -> Natural -> Bool Source # rangeSize :: (Natural, Natural) -> Int Source # unsafeRangeSize :: (Natural, Natural) -> Int Source #
Num Natural Source # | Note that `Natural`'s `Num` instance isn't a ring: no element but 0 has an additive inverse. It is a semiring though. Since: base-4.8.0.0
Instance details Defined in GHC.Num Methods (+) :: Natural -> Natural -> Natural Source # (-) :: Natural -> Natural -> Natural Source # (*) :: Natural -> Natural -> Natural Source # negate :: Natural -> Natural Source # abs :: Natural -> Natural Source # signum :: Natural -> Natural Source # fromInteger :: Integer -> Natural Source #
Read Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Read Methods readsPrec :: Int -> ReadS Natural Source # readList :: ReadS [Natural] Source # readPrec :: ReadPrec Natural Source # readListPrec :: ReadPrec [Natural] Source #
Integral Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Real Methods quot :: Natural -> Natural -> Natural Source # rem :: Natural -> Natural -> Natural Source # div :: Natural -> Natural -> Natural Source # mod :: Natural -> Natural -> Natural Source # quotRem :: Natural -> Natural -> (Natural, Natural) Source # divMod :: Natural -> Natural -> (Natural, Natural) Source # toInteger :: Natural -> Integer Source #
Real Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Real Methods toRational :: Natural -> Rational Source #
Show Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Show Methods showsPrec :: Int -> Natural -> ShowS Source # show :: Natural -> String Source # showList :: [Natural] -> ShowS Source #
PrintfArg Natural Source # | Since: base-4.8.0.0
Instance details Defined in Text.Printf Methods formatArg :: Natural -> FieldFormatter Source # parseFormat :: Natural -> ModifierParser Source #
Eq Natural |
Instance details Defined in GHC.Num.Natural Methods (==) :: Natural -> Natural -> Bool Source # (/=) :: Natural -> Natural -> Bool Source #
Ord Natural |
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 #
KnownNat n => HasResolution (n :: Nat) Source # | For example, `Fixed 1000` will give you a `Fixed` with a resolution of 1000.
Instance details Defined in Data.Fixed Methods resolution :: p n -> Integer Source #
type Compare (a :: Natural) (b :: Natural) Source # |
Instance details Defined in Data.Type.Ord type Compare (a :: Natural) (b :: Natural) = CmpNat a b

data BigNat Source #

A lifted BigNat

Represented as an array of limbs (Word#) stored in little-endian order (Word# themselves use machine order).

Invariant (canonical representation): higher Word# is non-zero.

As a consequence, zero is represented with a WordArray# whose size is 0.

Constructors

BN# |
Fields unBigNat :: BigNat#

#### Instances

Instances details

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

mkNatural :: [Word] -> Natural Source #

Construct `Natural` value from list of `Word`s.

isValidNatural :: Natural -> Bool Source #

Test whether all internal invariants are satisfied by `Natural` value

This operation is mostly useful for test-suites and/or code which constructs `Integer` values directly.

Since: base-4.8.0.0

# Arithmetic

plusNatural :: Natural -> Natural -> Natural Source #

`Natural` Addition

minusNatural :: Natural -> Natural -> Natural Source #

`Natural` subtraction. May ``throw` `Underflow``.

minusNaturalMaybe :: Natural -> Natural -> Maybe Natural Source #

`Natural` subtraction. Returns `Nothing`s for non-positive results.

Since: base-4.8.0.0

timesNatural :: Natural -> Natural -> Natural Source #

`Natural` multiplication

negateNatural :: Natural -> Natural Source #

signumNatural :: Natural -> Natural Source #

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

quotNatural :: Natural -> Natural -> Natural Source #

remNatural :: Natural -> Natural -> Natural Source #

gcdNatural :: Natural -> Natural -> Natural Source #

Compute greatest common divisor.

lcmNatural :: Natural -> Natural -> Natural Source #

Compute least common multiple.

# Bits

andNatural :: Natural -> Natural -> Natural Source #

orNatural :: Natural -> Natural -> Natural Source #

xorNatural :: Natural -> Natural -> Natural Source #

bitNatural :: Int# -> Natural Source #

testBitNatural :: Natural -> Int -> Bool Source #

popCountNatural :: Natural -> Int Source #

shiftLNatural :: Natural -> Int -> Natural Source #

shiftRNatural :: Natural -> Int -> Natural Source #

# Conversions

naturalToInteger :: Natural -> Integer Source #

Since: base-4.12.0.0

naturalToWord :: Natural -> Word Source #

naturalToWordMaybe :: Natural -> Maybe Word Source #

Try downcasting `Natural` to `Word` value. Returns `Nothing` if value doesn't fit in `Word`.

Since: base-4.8.0.0

wordToNatural :: Word -> Natural Source #

Construct `Natural` from `Word` value.

Since: base-4.8.0.0

wordToNatural# :: Word -> Natural Source #

naturalFromInteger :: Integer -> Natural Source #

Since: base-4.10.0.0

# Modular arithmetic

powModNatural :: Natural -> Natural -> Natural -> Natural Source #

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

Since: base-4.8.0.0