base-4.14.0.0: Basic libraries
Copyright(c) The University of Glasgow 1994-2002
Licensesee libraries/base/LICENSE
Maintainercvs-ghc@haskell.org
Stabilityinternal
Portabilitynon-portable (GHC Extensions)
Safe HaskellTrustworthy
LanguageHaskell2010

GHC.Num

Description

The Num class and the Integer type.

Synopsis

Documentation

class 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)

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

Minimal complete definition

(+), (*), abs, signum, fromInteger, (negate | (-))

Methods

(+) :: a -> a -> a infixl 6 #

(-) :: a -> a -> a infixl 6 #

(*) :: a -> a -> a infixl 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.

Instances

Instances details
Num Double #

Note that due to the presence of NaN, not all elements of Double have an additive inverse.

>>> 0/0 + (negate 0/0 :: Double)
NaN

Also note that due to the presence of -0, Double's Num instance doesn't have an additive identity

>>> 0 + (-0 :: Double)
0.0

Since: base-2.1

Instance details

Defined in GHC.Float

Num Float #

Note that due to the presence of NaN, not all elements of Float have an additive inverse.

>>> 0/0 + (negate 0/0 :: Float)
NaN

Also note that due to the presence of -0, Float's Num instance doesn't have an additive identity

>>> 0 + (-0 :: Float)
0.0

Since: base-2.1

Instance details

Defined in GHC.Float

Num Int #

Since: base-2.1

Instance details

Defined in GHC.Num

Methods

(+) :: Int -> Int -> Int #

(-) :: Int -> Int -> Int #

(*) :: Int -> Int -> Int #

negate :: Int -> Int #

abs :: Int -> Int #

signum :: Int -> Int #

fromInteger :: Integer -> Int #

Num Int8 #

Since: base-2.1

Instance details

Defined in GHC.Int

Methods

(+) :: Int8 -> Int8 -> Int8 #

(-) :: Int8 -> Int8 -> Int8 #

(*) :: Int8 -> Int8 -> Int8 #

negate :: Int8 -> Int8 #

abs :: Int8 -> Int8 #

signum :: Int8 -> Int8 #

fromInteger :: Integer -> Int8 #

Num Int16 #

Since: base-2.1

Instance details

Defined in GHC.Int

Num Int32 #

Since: base-2.1

Instance details

Defined in GHC.Int

Num Int64 #

Since: base-2.1

Instance details

Defined in GHC.Int

Num Integer #

Since: base-2.1

Instance details

Defined in GHC.Num

Num Natural #

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

Since: base-4.8.0.0

Instance details

Defined in GHC.Num

Num Word #

Since: base-2.1

Instance details

Defined in GHC.Num

Methods

(+) :: Word -> Word -> Word #

(-) :: Word -> Word -> Word #

(*) :: Word -> Word -> Word #

negate :: Word -> Word #

abs :: Word -> Word #

signum :: Word -> Word #

fromInteger :: Integer -> Word #

Num Word8 #

Since: base-2.1

Instance details

Defined in GHC.Word

Num Word16 #

Since: base-2.1

Instance details

Defined in GHC.Word

Num Word32 #

Since: base-2.1

Instance details

Defined in GHC.Word

Num Word64 #

Since: base-2.1

Instance details

Defined in GHC.Word

Num IntPtr # 
Instance details

Defined in Foreign.Ptr

Num WordPtr # 
Instance details

Defined in Foreign.Ptr

Num CUIntMax # 
Instance details

Defined in Foreign.C.Types

Num CIntMax # 
Instance details

Defined in Foreign.C.Types

Num CUIntPtr # 
Instance details

Defined in Foreign.C.Types

Num CIntPtr # 
Instance details

Defined in Foreign.C.Types

Num CSUSeconds # 
Instance details

Defined in Foreign.C.Types

Num CUSeconds # 
Instance details

Defined in Foreign.C.Types

Num CTime # 
Instance details

Defined in Foreign.C.Types

Num CClock # 
Instance details

Defined in Foreign.C.Types

Num CSigAtomic # 
Instance details

Defined in Foreign.C.Types

Num CWchar # 
Instance details

Defined in Foreign.C.Types

Num CSize # 
Instance details

Defined in Foreign.C.Types

Num CPtrdiff # 
Instance details

Defined in Foreign.C.Types

Num CDouble # 
Instance details

Defined in Foreign.C.Types

Num CFloat # 
Instance details

Defined in Foreign.C.Types

Num CBool # 
Instance details

Defined in Foreign.C.Types

Num CULLong # 
Instance details

Defined in Foreign.C.Types

Num CLLong # 
Instance details

Defined in Foreign.C.Types

Num CULong # 
Instance details

Defined in Foreign.C.Types

Num CLong # 
Instance details

Defined in Foreign.C.Types

Num CUInt # 
Instance details

Defined in Foreign.C.Types

Num CInt # 
Instance details

Defined in Foreign.C.Types

Methods

(+) :: CInt -> CInt -> CInt #

(-) :: CInt -> CInt -> CInt #

(*) :: CInt -> CInt -> CInt #

negate :: CInt -> CInt #

abs :: CInt -> CInt #

signum :: CInt -> CInt #

fromInteger :: Integer -> CInt #

Num CUShort # 
Instance details

Defined in Foreign.C.Types

Num CShort # 
Instance details

Defined in Foreign.C.Types

Num CUChar # 
Instance details

Defined in Foreign.C.Types

Num CSChar # 
Instance details

Defined in Foreign.C.Types

Num CChar # 
Instance details

Defined in Foreign.C.Types

Num Fd # 
Instance details

Defined in System.Posix.Types

Methods

(+) :: Fd -> Fd -> Fd #

(-) :: Fd -> Fd -> Fd #

(*) :: Fd -> Fd -> Fd #

negate :: Fd -> Fd #

abs :: Fd -> Fd #

signum :: Fd -> Fd #

fromInteger :: Integer -> Fd #

Num CNfds # 
Instance details

Defined in System.Posix.Types

Num CSocklen # 
Instance details

Defined in System.Posix.Types

Num CKey # 
Instance details

Defined in System.Posix.Types

Methods

(+) :: CKey -> CKey -> CKey #

(-) :: CKey -> CKey -> CKey #

(*) :: CKey -> CKey -> CKey #

negate :: CKey -> CKey #

abs :: CKey -> CKey #

signum :: CKey -> CKey #

fromInteger :: Integer -> CKey #

Num CId # 
Instance details

Defined in System.Posix.Types

Methods

(+) :: CId -> CId -> CId #

(-) :: CId -> CId -> CId #

(*) :: CId -> CId -> CId #

negate :: CId -> CId #

abs :: CId -> CId #

signum :: CId -> CId #

fromInteger :: Integer -> CId #

Num CFsFilCnt # 
Instance details

Defined in System.Posix.Types

Num CFsBlkCnt # 
Instance details

Defined in System.Posix.Types

Num CClockId # 
Instance details

Defined in System.Posix.Types

Num CBlkCnt # 
Instance details

Defined in System.Posix.Types

Num CBlkSize # 
Instance details

Defined in System.Posix.Types

Num CRLim # 
Instance details

Defined in System.Posix.Types

Num CTcflag # 
Instance details

Defined in System.Posix.Types

Num CSpeed # 
Instance details

Defined in System.Posix.Types

Num CCc # 
Instance details

Defined in System.Posix.Types

Methods

(+) :: CCc -> CCc -> CCc #

(-) :: CCc -> CCc -> CCc #

(*) :: CCc -> CCc -> CCc #

negate :: CCc -> CCc #

abs :: CCc -> CCc #

signum :: CCc -> CCc #

fromInteger :: Integer -> CCc #

Num CUid # 
Instance details

Defined in System.Posix.Types

Methods

(+) :: CUid -> CUid -> CUid #

(-) :: CUid -> CUid -> CUid #

(*) :: CUid -> CUid -> CUid #

negate :: CUid -> CUid #

abs :: CUid -> CUid #

signum :: CUid -> CUid #

fromInteger :: Integer -> CUid #

Num CNlink # 
Instance details

Defined in System.Posix.Types

Num CGid # 
Instance details

Defined in System.Posix.Types

Methods

(+) :: CGid -> CGid -> CGid #

(-) :: CGid -> CGid -> CGid #

(*) :: CGid -> CGid -> CGid #

negate :: CGid -> CGid #

abs :: CGid -> CGid #

signum :: CGid -> CGid #

fromInteger :: Integer -> CGid #

Num CSsize # 
Instance details

Defined in System.Posix.Types

Num CPid # 
Instance details

Defined in System.Posix.Types

Methods

(+) :: CPid -> CPid -> CPid #

(-) :: CPid -> CPid -> CPid #

(*) :: CPid -> CPid -> CPid #

negate :: CPid -> CPid #

abs :: CPid -> CPid #

signum :: CPid -> CPid #

fromInteger :: Integer -> CPid #

Num COff # 
Instance details

Defined in System.Posix.Types

Methods

(+) :: COff -> COff -> COff #

(-) :: COff -> COff -> COff #

(*) :: COff -> COff -> COff #

negate :: COff -> COff #

abs :: COff -> COff #

signum :: COff -> COff #

fromInteger :: Integer -> COff #

Num CMode # 
Instance details

Defined in System.Posix.Types

Num CIno # 
Instance details

Defined in System.Posix.Types

Methods

(+) :: CIno -> CIno -> CIno #

(-) :: CIno -> CIno -> CIno #

(*) :: CIno -> CIno -> CIno #

negate :: CIno -> CIno #

abs :: CIno -> CIno #

signum :: CIno -> CIno #

fromInteger :: Integer -> CIno #

Num CDev # 
Instance details

Defined in System.Posix.Types

Methods

(+) :: CDev -> CDev -> CDev #

(-) :: CDev -> CDev -> CDev #

(*) :: CDev -> CDev -> CDev #

negate :: CDev -> CDev #

abs :: CDev -> CDev #

signum :: CDev -> CDev #

fromInteger :: Integer -> CDev #

Integral a => Num (Ratio a) #

Since: base-2.0.1

Instance details

Defined in GHC.Real

Methods

(+) :: Ratio a -> Ratio a -> Ratio a #

(-) :: Ratio a -> Ratio a -> Ratio a #

(*) :: Ratio a -> Ratio a -> Ratio a #

negate :: Ratio a -> Ratio a #

abs :: Ratio a -> Ratio a #

signum :: Ratio a -> Ratio a #

fromInteger :: Integer -> Ratio a #

Num a => Num (Down a) #

Since: base-4.11.0.0

Instance details

Defined in Data.Ord

Methods

(+) :: Down a -> Down a -> Down a #

(-) :: Down a -> Down a -> Down a #

(*) :: Down a -> Down a -> Down a #

negate :: Down a -> Down a #

abs :: Down a -> Down a #

signum :: Down a -> Down a #

fromInteger :: Integer -> Down a #

Num a => Num (Product a) #

Since: base-4.7.0.0

Instance details

Defined in Data.Semigroup.Internal

Methods

(+) :: Product a -> Product a -> Product a #

(-) :: Product a -> Product a -> Product a #

(*) :: Product a -> Product a -> Product a #

negate :: Product a -> Product a #

abs :: Product a -> Product a #

signum :: Product a -> Product a #

fromInteger :: Integer -> Product a #

Num a => Num (Sum a) #

Since: base-4.7.0.0

Instance details

Defined in Data.Semigroup.Internal

Methods

(+) :: Sum a -> Sum a -> Sum a #

(-) :: Sum a -> Sum a -> Sum a #

(*) :: Sum a -> Sum a -> Sum a #

negate :: Sum a -> Sum a #

abs :: Sum a -> Sum a #

signum :: Sum a -> Sum a #

fromInteger :: Integer -> Sum a #

Num a => Num (Identity a) #

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Identity

Num a => Num (Max a) #

Since: base-4.9.0.0

Instance details

Defined in Data.Semigroup

Methods

(+) :: Max a -> Max a -> Max a #

(-) :: Max a -> Max a -> Max a #

(*) :: Max a -> Max a -> Max a #

negate :: Max a -> Max a #

abs :: Max a -> Max a #

signum :: Max a -> Max a #

fromInteger :: Integer -> Max a #

Num a => Num (Min a) #

Since: base-4.9.0.0

Instance details

Defined in Data.Semigroup

Methods

(+) :: Min a -> Min a -> Min a #

(-) :: Min a -> Min a -> Min a #

(*) :: Min a -> Min a -> Min a #

negate :: Min a -> Min a #

abs :: Min a -> Min a #

signum :: Min a -> Min a #

fromInteger :: Integer -> Min a #

RealFloat a => Num (Complex a) #

Since: base-2.1

Instance details

Defined in Data.Complex

Methods

(+) :: Complex a -> Complex a -> Complex a #

(-) :: Complex a -> Complex a -> Complex a #

(*) :: Complex a -> Complex a -> Complex a #

negate :: Complex a -> Complex a #

abs :: Complex a -> Complex a #

signum :: Complex a -> Complex a #

fromInteger :: Integer -> Complex a #

Num a => Num (Op a b) # 
Instance details

Defined in Data.Functor.Contravariant

Methods

(+) :: Op a b -> Op a b -> Op a b #

(-) :: Op a b -> Op a b -> Op a b #

(*) :: Op a b -> Op a b -> Op a b #

negate :: Op a b -> Op a b #

abs :: Op a b -> Op a b #

signum :: Op a b -> Op a b #

fromInteger :: Integer -> Op a b #

HasResolution a => Num (Fixed a) #

Since: base-2.1

Instance details

Defined in Data.Fixed

Methods

(+) :: Fixed a -> Fixed a -> Fixed a #

(-) :: Fixed a -> Fixed a -> Fixed a #

(*) :: Fixed a -> Fixed a -> Fixed a #

negate :: Fixed a -> Fixed a #

abs :: Fixed a -> Fixed a #

signum :: Fixed a -> Fixed a #

fromInteger :: Integer -> Fixed a #

Num (f a) => Num (Alt f a) #

Since: base-4.8.0.0

Instance details

Defined in Data.Semigroup.Internal

Methods

(+) :: Alt f a -> Alt f a -> Alt f a #

(-) :: Alt f a -> Alt f a -> Alt f a #

(*) :: Alt f a -> Alt f a -> Alt f a #

negate :: Alt f a -> Alt f a #

abs :: Alt f a -> Alt f a #

signum :: Alt f a -> Alt f a #

fromInteger :: Integer -> Alt f a #

(Applicative f, Num a) => Num (Ap f a) #

Since: base-4.12.0.0

Instance details

Defined in Data.Monoid

Methods

(+) :: Ap f a -> Ap f a -> Ap f a #

(-) :: Ap f a -> Ap f a -> Ap f a #

(*) :: Ap f a -> Ap f a -> Ap f a #

negate :: Ap f a -> Ap f a #

abs :: Ap f a -> Ap f a #

signum :: Ap f a -> Ap f a #

fromInteger :: Integer -> Ap f a #

Num a => Num (Const a b) #

Since: base-4.9.0.0

Instance details

Defined in Data.Functor.Const

Methods

(+) :: Const a b -> Const a b -> Const a b #

(-) :: Const a b -> Const a b -> Const a b #

(*) :: Const a b -> Const a b -> Const a b #

negate :: Const a b -> Const a b #

abs :: Const a b -> Const a b #

signum :: Const a b -> Const a b #

fromInteger :: Integer -> Const a b #

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