HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleghc-bignum-1.3Haskell2010

GHC.Num.Integer

The Integer type.

  • 1 type
  • 100 values
  • Packageghc-bignum-1.3
  • Exports101
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceInteger.hs
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

  • IS Int#

    iff value in [minBound::Int, maxBound::Int] range

  • IP ByteArray#

    iff value in ]maxBound::Int, +inf[ range

  • IN ByteArray#

    iff value in ]-inf, minBound::Int[ range

Instances2Eq, Ord
  • Eq IntegerDefined in ghc-bignum-1.3 · GHC.Num.Integer
  • Ord IntegerDefined in ghc-bignum-1.3 · GHC.Num.Integer

Useful constants

2 declarations

Conversion with...

0 declarations

Int

BigNat

Word

Natural

Int64/Word64

Floating-point

valueintegerEncodeFloat# :: Integer -> Int# -> Float#
#

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

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

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

valueintegerToMutableByteArray#
  1. :: Integer
  2. -> MutableByteArray# s
  3. -> Word#
  4. -> Bool#
  5. -> State# s
  6. -> (# State# s, Word# #)
#

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

valueintegerToMutableByteArray
  1. :: Integer
  2. -> MutableByteArray# RealWorld
  3. -> Word#
  4. -> Bool#
  5. -> IO Word
#

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

valueintegerFromByteArray#
  1. :: Word#
  2. -> ByteArray#
  3. -> Word#
  4. -> Bool#
  5. -> State# s
  6. -> (# State# s, Integer #)
#

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.

valueintegerFromByteArray :: Word# -> ByteArray# -> Word# -> Bool# -> Integer
#

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
valueintegerPopCount# :: Integer -> Int#
#

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
valueintegerSizeInBase# :: Word# -> Integer -> Word#
#

Compute the number of digits of the Integer (without the sign) in the given base.

base must be > 1