GHC.Natural

base-4.11.0.0: Basic libraries

Copyright | (C) 2014 Herbert Valerio Riedel (C) 2011 Edward Kmett
License | see libraries/base/LICENSE
Maintainer | libraries@haskell.org
Stability | internal
Portability | non-portable (GHC Extensions)
Safe Haskell | Unsafe
Language | Haskell2010

GHC.Natural

Contents

- The `Natural` number type
- Conversions
- Checked subtraction
- Modular arithmetic

Description

The arbitrary-precision `Natural` number type.

Note: This is an internal GHC module with an API subject to change. It's recommended use the Numeric.Natural module to import the `Natural` type.

Since: base-4.8.0.0

Synopsis

- data Natural
  - = NatS# GmpLimb#
  - | NatJ# !BigNat
- isValidNatural :: Natural -> Bool
- naturalFromInteger :: Integer -> Natural
- wordToNatural :: Word -> Natural
- naturalToWordMaybe :: Natural -> Maybe Word
- minusNaturalMaybe :: Natural -> Natural -> Maybe Natural
- powModNatural :: Natural -> Natural -> Natural -> Natural

# The `Natural` number type

Warning: The internal implementation of `Natural` (i.e. which constructors are available) depends on the `Integer` backend used!

data Natural Source #

Type representing arbitrary-precision non-negative integers.

    >>> 2^20 :: Natural
    1267650600228229401496703205376

Operations whose result would be negative ``throw` (`Underflow` :: `ArithException`)`,

    >>> -1 :: Natural
    *** Exception: arithmetic underflow

Since: base-4.8.0.0

Constructors

NatS# GmpLimb# | in `[0, maxBound::Word]`
NatJ# !BigNat | in `]maxBound::Word, +inf[` Invariant: `NatJ#` is used iff value doesn't fit in `NatS#` constructor.

Instances

Enum Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Natural 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 #
Eq Natural Source # |
Instance details Defined in GHC.Natural Methods (==) :: Natural -> Natural -> Bool Source # (/=) :: Natural -> Natural -> Bool Source #
Integral Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Natural 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 #
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 :: (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 #
Num Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Natural 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 #
Ord Natural Source # |
Instance details Defined in GHC.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 #
Read Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Natural Methods readsPrec :: Int -> ReadS Natural Source # readList :: ReadS [Natural] Source # readPrec :: ReadPrec Natural Source # readListPrec :: ReadPrec [Natural] Source #
Real Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Natural Methods toRational :: Natural -> Rational Source #
Show Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Natural Methods showsPrec :: Int -> Natural -> ShowS Source # show :: Natural -> String Source # showList :: [Natural] -> ShowS Source #
Ix Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Natural Methods range :: (Natural, Natural) -> [Natural] Source # index :: (Natural, Natural) -> Natural -> Int Source # unsafeIndex :: (Natural, Natural) -> Natural -> Int inRange :: (Natural, Natural) -> Natural -> Bool Source # rangeSize :: (Natural, Natural) -> Int Source # unsafeRangeSize :: (Natural, Natural) -> Int
Bits Natural Source # | Since: base-4.8.0.0
Instance details Defined in GHC.Natural 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 #
PrintfArg Natural Source # | Since: base-4.8.0.0
Instance details Defined in Text.Printf Methods formatArg :: Natural -> FieldFormatter Source # parseFormat :: Natural -> ModifierParser Source #

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

# Conversions

naturalFromInteger :: Integer -> Natural Source #

Since: base-4.10.0.0

wordToNatural :: Word -> Natural Source #

Construct `Natural` from `Word` value.

Since: base-4.8.0.0

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

# Checked subtraction

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

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

Since: base-4.8.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