HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleghc-internal-9.1003.0Haskell2010

GHC.Internal.Num

The Num class and the Integer type.

  • 4 types
  • 1 class
  • 183 values
classclass Num a where
#

Basic numeric class.

The Haskell Report defines no laws for Num. However, (+) and (*) are customarily expected to define a ring and have the following properties:

Associativity of (+)

(x + y) + z

=

x + (y + z)

Commutativity of (+)

x + y

=

y + x

fromInteger 0 is the additive identity

x + fromInteger 0

=

x

negate gives the additive inverse

x + negate x

=

fromInteger 0

Associativity of (*)

(x * y) * z

=

x * (y * z)

fromInteger 1 is the multiplicative identity

x * fromInteger 1

=

x

and

fromInteger 1 * x

=

x

Distributivity of (*) with respect to (+)

a * (b + c)

=

(a * b) + (a * c)

and

(b + c) * a

=

(b * a) + (c * a)

Coherence with toInteger

if the type also implements

GHC.Real.Integral

, then

fromInteger

is a left inverse for

toInteger

, i.e.

fromInteger (toInteger i) == i

Note that it isn't customarily expected that a type instance of both Num and Ord implement an ordered ring. Indeed, in base only Integer and Data.Ratio.Rational do.

Methods

  • (+) :: a -> a -> ainfixl 6
  • (-) :: a -> a -> ainfixl 6
  • (*) :: a -> a -> ainfixl 7
  • negate :: a -> a

    Unary negation.

  • abs :: a -> a

    Absolute value.

  • signum :: a -> a

    Sign of a number. The functions abs and signum should satisfy the law:

    abs x * signum x == x

    For real numbers, the signum is either -1 (negative), 0 (zero) or 1 (positive).

  • fromInteger :: Integer -> a

    Conversion from an Integer. An integer literal represents the application of the function fromInteger to the appropriate value of type Integer, so such literals have type (Num a) => a.

Instances76Num, …
  • Num IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Num
  • Num NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Num

    Note that Natural's Num instance isn't a ring: no element but 0 has an additive inverse. It is a semiring though.

  • Num EventTypeDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.EPoll
  • Num EventDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Poll
  • Num UniqueDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Unique
  • Num CBoolDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CClockDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CDoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CFloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CLLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CPtrdiffDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CSCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CSUSecondsDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CSigAtomicDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CTimeDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CUCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CUIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CUIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CUIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CULLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CULongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CUSecondsDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CUShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num CWcharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Num IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Num WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Num Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Num Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Num Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Num Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Num CBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CBlkSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CCcDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CClockIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CDevDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CFsBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CFsFilCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CGidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CInoDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CKeyDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CModeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CNfdsDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CNlinkDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num COffDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CPidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CRLimDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CSocklenDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CSpeedDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CSsizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CTcflagDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num CUidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num FdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Num Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Num Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Num Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Num Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Num DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    This instance implements IEEE 754 standard with all its usual pitfalls about NaN, infinities and negative zero. Neither addition nor multiplication are associative or distributive:

    Example3 expressions
    (0.1 + 0.1) + 0.4 == 0.1 + (0.1 + 0.4)False(0.1 + 0.2) * 0.3 == 0.1 * 0.3 + 0.2 * 0.3False(0.1 * 0.1) * 0.3 == 0.1 * (0.1 * 0.3)False
  • Num FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Float · orphan

    This instance implements IEEE 754 standard with all its usual pitfalls about NaN, infinities and negative zero. Neither addition nor multiplication are associative or distributive:

    Example3 expressions
    (0.1 + 0.1 :: Float) + 0.5 == 0.1 + (0.1 + 0.5)False(0.1 + 0.2 :: Float) * 0.9 == 0.1 * 0.9 + 0.2 * 0.9False(0.1 * 0.1 :: Float) * 0.9 == 0.1 * (0.1 * 0.9)False
  • Num IntDefined in ghc-internal-9.1003.0 · GHC.Internal.Num
  • Num WordDefined in ghc-internal-9.1003.0 · GHC.Internal.Num
  • Num a => Num (Identity a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Identity
  • Num a => Num (Down a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Ord
  • Num a => Num (Product a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Num a => Num (Sum a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Integral a => Num (Ratio a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Num (f a) => Num (Alt f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Semigroup.Internal
  • Num a => Num (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Const
  • (Applicative f, Num a) => Num (Ap f a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Monoid

    Note that even if the underlying Num and Applicative instances are lawful, for most Applicatives, this instance will not be lawful. If you use this instance with the list Applicative, the following customary laws will not hold:

    Commutativity:

    Example2 expressions
    Ap [10,20] + Ap [1,2]Ap {getAp = [11,12,21,22]}Ap [1,2] + Ap [10,20]Ap {getAp = [11,21,12,22]}

    Additive inverse:

    Example2 expressions
    Ap [] + negate (Ap [])Ap {getAp = []}fromInteger 0 :: Ap [] IntAp {getAp = [0]}

    Distributivity:

    Example2 expressions
    Ap [1,2] * (3 + 4)Ap {getAp = [7,14]}(Ap [1,2] * 3) + (Ap [1,2] * 4)Ap {getAp = [7,11,10,14]}
valuesubtract :: Num a => a -> a -> a
#

the same as flip (-).

Because - is treated specially in the Haskell grammar, (- e) is not a section, but an application of prefix negation. However, (subtract exp) is equivalent to the disallowed section.

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

Instances11Enum, Eq, Integral, Data, Num, Ord, …
  • Enum IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Enum
  • Eq IntegerDefined in ghc-bignum-1.3 · GHC.Num.Integer
  • Integral IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Data IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Num
  • Ord IntegerDefined in ghc-bignum-1.3 · GHC.Num.Integer
  • Read IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Show IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Show
  • Ix IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Ix
  • Bits IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits

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

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

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

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.

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

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

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.

valueintegerSignum# :: Integer -> Int#
#

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

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)

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

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

datadata Natural
#

Natural number

Invariant: numbers <= 0xffffffffffffffff use the NS constructor

Constructors

Instances14Enum, Eq, Integral, Data, Num, Ord, …
  • Enum NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Enum
  • Eq NaturalDefined in ghc-bignum-1.3 · GHC.Num.Natural
  • Integral NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Data NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Num

    Note that Natural's Num instance isn't a ring: no element but 0 has an additive inverse. It is a semiring though.

  • Ord NaturalDefined in ghc-bignum-1.3 · GHC.Num.Natural
  • Read NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Show NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Show
  • Ix NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Ix
  • Bits NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • TestCoercion SNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • TestEquality SNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • type Compare a b = CmpNat a bDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Ord
valuenaturalFromAddr :: Word# -> Addr# -> Bool# -> IO Natural
#

Read a Natural 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.

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

Read a Natural 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 a Natural 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.

valuenaturalToAddr :: Natural -> Addr# -> Bool# -> IO Word
#

Write a Natural 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#.

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

Write a Natural 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#.

datadata Natural
#

Natural number

Invariant: numbers <= 0xffffffffffffffff use the NS constructor

Instances14Enum, Eq, Integral, Data, Num, Ord, …
  • Enum NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Enum
  • Eq NaturalDefined in ghc-bignum-1.3 · GHC.Num.Natural
  • Integral NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Data NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Num

    Note that Natural's Num instance isn't a ring: no element but 0 has an additive inverse. It is a semiring though.

  • Ord NaturalDefined in ghc-bignum-1.3 · GHC.Num.Natural
  • Read NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Show NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Show
  • Ix NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Ix
  • Bits NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • TestCoercion SNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • TestEquality SNatDefined in ghc-internal-9.1003.0 · GHC.Internal.TypeNats
  • type Compare a b = CmpNat a bDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Type.Ord
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.

Instances11Enum, Eq, Integral, Data, Num, Ord, …
  • Enum IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Enum
  • Eq IntegerDefined in ghc-bignum-1.3 · GHC.Num.Integer
  • Integral IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Data IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Num
  • Ord IntegerDefined in ghc-bignum-1.3 · GHC.Num.Integer
  • Read IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Real
  • Show IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Show
  • Ix IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Ix
  • Bits IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits