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

Modulebase-4.22.0.0Haskell2010

GHC.Num

The Num class and the Integer type.

  • 1 class
  • 2 values
  • Packagebase-4.22.0.0
  • Exports3
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceNum.hs
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.

Instances87Num, …
  • 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
  • RealFloat a => Num (Complex a)Defined in base-4.22.0.0 · Data.Complex
  • Num a => Num (Max a)Defined in base-4.22.0.0 · Data.Semigroup
  • Num a => Num (Min a)Defined in base-4.22.0.0 · Data.Semigroup
  • 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
  • HasResolution a => Num (Fixed a)Defined in base-4.22.0.0 · Data.Fixed

    Multiplication is not associative or distributive:

    Example1 expression
    (0.2 * 0.6 :: Deci) * 0.9 == 0.2 * (0.6 * 0.9)False
    Example1 expression
    (0.1 + 0.1 :: Deci) * 0.5 == 0.1 * 0.5 + 0.1 * 0.5False
  • Num a => Num (Op a b)Defined in base-4.22.0.0 · Data.Functor.Contravariant
  • 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]}
  • Num (f (g a)) => Num (Compose f g a)Defined in base-4.22.0.0 · Data.Functor.Compose
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.