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

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

Moduleghc-internal-9.1401.0Haskell2010

GHC.Internal.Num

The Num class and the Integer type.

  • 2 types
  • 1 class
  • 2 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.

Instances81Num, …
  • Num IntegerDefined in ghc-internal-9.1401.0 · GHC.Internal.Num
  • Num NaturalDefined in ghc-internal-9.1401.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.1401.0 · GHC.Internal.Event.EPoll
  • Num EventDefined in ghc-internal-9.1401.0 · GHC.Internal.Event.Poll
  • Num UniqueDefined in ghc-internal-9.1401.0 · GHC.Internal.Event.Unique
  • Num CBoolDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CCharDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CClockDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CDoubleDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CFloatDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CIntDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CIntMaxDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CIntPtrDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CLLongDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CLongDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CPtrdiffDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CSCharDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CSUSecondsDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CShortDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CSigAtomicDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CSizeDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CTimeDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CUCharDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CUIntDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CUIntMaxDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CUIntPtrDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CULLongDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CULongDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CUSecondsDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CUShortDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num CWcharDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.C.Types
  • Num IntPtrDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.Ptr
  • Num WordPtrDefined in ghc-internal-9.1401.0 · GHC.Internal.Foreign.Ptr
  • Num HalfWordDefined in ghc-internal-9.1401.0 · GHC.Internal.Heap.InfoTable.Types
  • Num Int16Defined in ghc-internal-9.1401.0 · GHC.Internal.Int
  • Num Int32Defined in ghc-internal-9.1401.0 · GHC.Internal.Int
  • Num Int64Defined in ghc-internal-9.1401.0 · GHC.Internal.Int
  • Num Int8Defined in ghc-internal-9.1401.0 · GHC.Internal.Int
  • Num ByteOffsetDefined in ghc-internal-9.1401.0 · GHC.Internal.Stack.Constants
  • Num WordOffsetDefined in ghc-internal-9.1401.0 · GHC.Internal.Stack.Constants
  • Num ByteOffsetDefined in ghc-internal-9.1401.0 · GHC.Internal.Stack.ConstantsProf
  • Num WordOffsetDefined in ghc-internal-9.1401.0 · GHC.Internal.Stack.ConstantsProf
  • Num CBlkCntDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CBlkSizeDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CCcDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CClockIdDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CDevDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CFsBlkCntDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CFsFilCntDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CGidDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CIdDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CInoDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CKeyDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CModeDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CNfdsDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CNlinkDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num COffDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CPidDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CRLimDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CSocklenDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CSpeedDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CSsizeDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CTcflagDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num CUidDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num FdDefined in ghc-internal-9.1401.0 · GHC.Internal.System.Posix.Types
  • Num DoubleDefined in ghc-internal-9.1401.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.1401.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.1401.0 · GHC.Internal.Num
  • Num WordDefined in ghc-internal-9.1401.0 · GHC.Internal.Num
  • Num Word16Defined in ghc-internal-9.1401.0 · GHC.Internal.Word
  • Num Word32Defined in ghc-internal-9.1401.0 · GHC.Internal.Word
  • Num Word64Defined in ghc-internal-9.1401.0 · GHC.Internal.Word
  • Num Word8Defined in ghc-internal-9.1401.0 · GHC.Internal.Word
  • Num a => Num (Identity a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.Identity
  • Num a => Num (Down a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Ord
  • Num a => Num (Product a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.Internal
  • Num a => Num (Sum a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.Internal
  • Integral a => Num (Ratio a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Real
  • Num (f a) => Num (Alt f a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.Internal
  • Num a => Num (Const a b)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.Const
  • (Applicative f, Num a) => Num (Ap f a)Defined in ghc-internal-9.1401.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 Natural
#

Natural number

Invariant: numbers <= 0xffffffffffffffff use the NS constructor

Instances15Enum, Eq, Integral, Data, Num, Ord, …
  • Enum NaturalDefined in ghc-internal-9.1401.0 · GHC.Internal.Enum
  • Eq NaturalDefined in ghc-internal-9.1401.0 · GHC.Internal.Bignum.Natural
  • Integral NaturalDefined in ghc-internal-9.1401.0 · GHC.Internal.Real
  • Data NaturalDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Data
  • Num NaturalDefined in ghc-internal-9.1401.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-internal-9.1401.0 · GHC.Internal.Bignum.Natural
  • Read NaturalDefined in ghc-internal-9.1401.0 · GHC.Internal.Read
  • Real NaturalDefined in ghc-internal-9.1401.0 · GHC.Internal.Real
  • Show NaturalDefined in ghc-internal-9.1401.0 · GHC.Internal.Show
  • Ix NaturalDefined in ghc-internal-9.1401.0 · GHC.Internal.Ix
  • Bits NaturalDefined in ghc-internal-9.1401.0 · GHC.Internal.Bits
  • Lift NaturalDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.Lift
  • TestCoercion SNatDefined in ghc-internal-9.1401.0 · GHC.Internal.TypeNats
  • TestEquality SNatDefined in ghc-internal-9.1401.0 · GHC.Internal.TypeNats
  • type Compare a b = CmpNat a bDefined in ghc-internal-9.1401.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.

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