HORIZON HASKELLDocslts/ghc-10.0.x04404a62026-10-19Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 04404a6 · 2026-10-19

Moduleghc-internal-10.0.0Haskell2010

GHC.Internal.Bignum.Integer

The Integer type.

Contents
  1. Useful constants
  2. Conversion with...
  3. Int
  4. BigNat
  5. Word
  6. Natural
  7. Int64/Word64
  8. Floating-point
  9. Addr#
  10. Limbs
  11. Predicates
  12. Comparison
  13. Arithmetic
  14. Bit operations
  15. Miscellaneous
Synopsis
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-10.0.0 · GHC.Internal.Enum
  • Eq IntegerDefined in ghc-internal-10.0.0 · GHC.Internal.Bignum.Integer
  • Integral IntegerDefined in ghc-internal-10.0.0 · GHC.Internal.Real
  • Data IntegerDefined in ghc-internal-10.0.0 · GHC.Internal.Data.Data
  • Num IntegerDefined in ghc-internal-10.0.0 · GHC.Internal.Num
  • Ord IntegerDefined in ghc-internal-10.0.0 · GHC.Internal.Bignum.Integer
  • Read IntegerDefined in ghc-internal-10.0.0 · GHC.Internal.Read
  • Real IntegerDefined in ghc-internal-10.0.0 · GHC.Internal.Real
  • Show IntegerDefined in ghc-internal-10.0.0 · GHC.Internal.Show
  • Ix IntegerDefined in ghc-internal-10.0.0 · GHC.Internal.Ix
  • Bits IntegerDefined in ghc-internal-10.0.0 · GHC.Internal.Bits
  • Lift IntegerDefined in ghc-internal-10.0.0 · GHC.Internal.TH.Lift

Useful constants

Conversion with...

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#.

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.

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

Comparison

Arithmetic

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

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

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)

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