HORIZON HASKELLDocslts/ghc-9.14.x3e2d5682026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 3e2d568 · 2026-10-11

Moduleghc-internal-9.1401.0Haskell2010

GHC.Internal.Bignum.Integer

The Integer type.

  • 1 type
  • 100 values
datadata Integer
#

Arbitrary precision integers. In contrast with fixed-size integral types such as Int, the Integer type represents the entire infinite range of integers.

Integers are stored in a kind of sign-magnitude form, hence do not expect two's complement form when using bit operations.

If the value is small (i.e., fits into an Int), the IS constructor is used. Otherwise IP and IN constructors are used to store a BigNat representing the positive or the negative value magnitude, respectively.

Invariant: IP and IN are used iff the value does not fit in IS.

Constructors

Instances12Enum, Eq, Integral, Data, Num, Ord, …
  • Enum IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Enum
  • Eq IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Bignum.Integer
  • Integral IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Real
  • Data IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Data
  • Num IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Num
  • Ord IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Bignum.Integer
  • Read IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Read
  • Real IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Real
  • Show IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Show
  • Ix IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Ix
  • Bits IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Bits
  • Lift IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.Lift

Useful constants

2 declarations

Conversion with...

0 declarations

Int

BigNat

Word

Natural

Int64/Word64

Floating-point

Addr#

valueintegerToAddr#
  1. :: Integer
  2. -> Addr#
  3. -> Bool#
  4. -> State# s
  5. -> (# State# s, Word# #)
#

Write an Integer (without sign) 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#.

valueintegerToAddr :: Integer -> Addr# -> Bool# -> IO Word
#

Write an Integer (without sign) 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#.

valueintegerFromAddr#
  1. :: Word#
  2. -> Addr#
  3. -> Bool#
  4. -> State# s
  5. -> (# State# s, Integer #)
#

Read an Integer (without sign) 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.

valueintegerFromAddr :: Word# -> Addr# -> Bool# -> IO Integer
#

Read an Integer (without sign) 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.

Limbs

Read an Integer (without sign) 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.

Read an Integer (without sign) 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.

Predicates

4 declarations

Comparison

13 declarations

Arithmetic

29 declarations

Negate Integer.

One edge-case issue to take into account is that Int's range is not symmetric around 0. I.e. minBound+maxBound = -1

IP is used iff n > maxBound::Int IN is used iff n < minBound::Int

Return -1, 0, and 1 depending on whether argument is negative, zero, or positive, respectively

valueintegerSignum# :: Integer -> Int#
#

Return -1#, 0#, and 1# depending on whether argument is negative, zero, or positive, respectively

valueintegerRecipMod# :: Integer -> Natural -> (# Natural | () #)
#

Computes the modular inverse.

integerRecipMod# x m behaves as follows:

  • If m > 1 and gcd x m = 1, it returns an integer y with 0 < y < m such that x*y is congruent to 1 modulo m.

  • If m > 1 and gcd x m > 1, it fails.

  • If m = 1, it returns 0 for all x. The computation effectively takes place in the zero ring, which has a single element 0 with 0+0 = 0*0 = 0: the element 0 is the multiplicative identity element and is its own multiplicative inverse.

  • If m = 0, it fails.

NB. Successful evaluation returns a value of the form (# n | #); failure is indicated by returning (# | () #).

valueintegerPowMod# :: Integer -> Integer -> Natural -> (# Natural | () #)
#

Computes the modular exponentiation.

integerPowMod# b e m behaves as follows:

  • If m > 1 and e >= 0, it returns an integer y with 0 <= y < m and y congruent to b^e modulo m.

  • If m > 1 and e < 0, it uses integerRecipMod# to try to find a modular multiplicative inverse b' (which only exists if gcd b m = 1) and then caculates (b')^(-e) modulo m (note that -e > 0); if the inverse does not exist then it fails.

  • If m = 1, it returns 0 for all b and e.

  • If m = 0, it fails.

NB. Successful evaluation returns a value of the form (# n | #); failure is indicated by returning (# | () #).

Bit operations

13 declarations

Count number of set bits. For negative arguments returns the negated population count of the absolute value.

valueintegerTestBit :: Integer -> Word -> Bool
#

Test if n-th bit is set. For negative Integers it tests the n-th bit of the negated argument.

Fake 2's complement for negative values (might be slow)

valueintegerShiftL :: Integer -> Word -> Integer
#

Shift-left operation

Remember that bits are stored in sign-magnitude form, hence the behavior of negative Integers is different from negative Int's behavior.

Miscellaneous

1 declaration