ghc-internal-9.1001.0: Basic libraries

GHC.Internal.Num

Description

The Num class and the Integer type.

Synopsis

# Documentation

class Num a where Source #

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

Minimal complete definition

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

Methods

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

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

(*) :: a -> a -> a infixl 7 Source #

negate :: a -> a Source #

Unary negation.

abs :: a -> a Source #

Absolute value.

signum :: a -> a Source #

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

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
 Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods(+) :: CInt -> CInt -> CInt Source #(-) :: CInt -> CInt -> CInt Source #(*) :: CInt -> CInt -> CInt Source # Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.C.Types Methods Source # Instance detailsDefined in GHC.Internal.Foreign.Ptr Methods Source # Instance detailsDefined in GHC.Internal.Foreign.Ptr Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Int Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Int Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Int Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Int Methods(+) :: Int8 -> Int8 -> Int8 Source #(-) :: Int8 -> Int8 -> Int8 Source #(*) :: Int8 -> Int8 -> Int8 Source # Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods(+) :: CCc -> CCc -> CCc Source #(-) :: CCc -> CCc -> CCc Source #(*) :: CCc -> CCc -> CCc Source #abs :: CCc -> CCc Source # Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods(+) :: CDev -> CDev -> CDev Source #(-) :: CDev -> CDev -> CDev Source #(*) :: CDev -> CDev -> CDev Source # Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods(+) :: CGid -> CGid -> CGid Source #(-) :: CGid -> CGid -> CGid Source #(*) :: CGid -> CGid -> CGid Source # Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods(+) :: CId -> CId -> CId Source #(-) :: CId -> CId -> CId Source #(*) :: CId -> CId -> CId Source #abs :: CId -> CId Source # Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods(+) :: CIno -> CIno -> CIno Source #(-) :: CIno -> CIno -> CIno Source #(*) :: CIno -> CIno -> CIno Source # Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods(+) :: CKey -> CKey -> CKey Source #(-) :: CKey -> CKey -> CKey Source #(*) :: CKey -> CKey -> CKey Source # Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods(+) :: COff -> COff -> COff Source #(-) :: COff -> COff -> COff Source #(*) :: COff -> COff -> COff Source # Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods(+) :: CPid -> CPid -> CPid Source #(-) :: CPid -> CPid -> CPid Source #(*) :: CPid -> CPid -> CPid Source # Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods(+) :: CUid -> CUid -> CUid Source #(-) :: CUid -> CUid -> CUid Source #(*) :: CUid -> CUid -> CUid Source # Source # Instance detailsDefined in GHC.Internal.System.Posix.Types Methods(+) :: Fd -> Fd -> Fd Source #(-) :: Fd -> Fd -> Fd Source #(*) :: Fd -> Fd -> Fd Source #abs :: Fd -> Fd Source # Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Word Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Word Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Word Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Word Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Num Methods Source # 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 detailsDefined in GHC.Internal.Num Methods Source # @since base-2.01This instance implements IEEE 754 standard with all its usual pitfalls about NaN, infinities and negative zero. Neither addition nor multiplication are associative or distributive:>>> (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.3 False >>> (0.1 * 0.1) * 0.3 == 0.1 * (0.1 * 0.3) False  Instance detailsDefined in GHC.Internal.Float Methods Source # @since base-2.01This instance implements IEEE 754 standard with all its usual pitfalls about NaN, infinities and negative zero. Neither addition nor multiplication are associative or distributive:>>> (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.9 False >>> (0.1 * 0.1 :: Float) * 0.9 == 0.1 * (0.1 * 0.9) False  Instance detailsDefined in GHC.Internal.Float Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Num Methods(+) :: Int -> Int -> Int Source #(-) :: Int -> Int -> Int Source #(*) :: Int -> Int -> Int Source #abs :: Int -> Int Source # Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Num Methods(+) :: Word -> Word -> Word Source #(-) :: Word -> Word -> Word Source #(*) :: Word -> Word -> Word Source # Num a => Num (Identity a) Source # @since base-4.9.0.0 Instance detailsDefined in GHC.Internal.Data.Functor.Identity Methods(+) :: Identity a -> Identity a -> Identity a Source #(-) :: Identity a -> Identity a -> Identity a Source #(*) :: Identity a -> Identity a -> Identity a Source #abs :: Identity a -> Identity a Source # Num a => Num (Down a) Source # @since base-4.11.0.0 Instance detailsDefined in GHC.Internal.Data.Ord Methods(+) :: Down a -> Down a -> Down a Source #(-) :: Down a -> Down a -> Down a Source #(*) :: Down a -> Down a -> Down a Source #negate :: Down a -> Down a Source #abs :: Down a -> Down a Source #signum :: Down a -> Down a Source # Num a => Num (Product a) Source # @since base-4.7.0.0 Instance detailsDefined in GHC.Internal.Data.Semigroup.Internal Methods(+) :: Product a -> Product a -> Product a Source #(-) :: Product a -> Product a -> Product a Source #(*) :: Product a -> Product a -> Product a Source #negate :: Product a -> Product a Source #abs :: Product a -> Product a Source #signum :: Product a -> Product a Source # Num a => Num (Sum a) Source # @since base-4.7.0.0 Instance detailsDefined in GHC.Internal.Data.Semigroup.Internal Methods(+) :: Sum a -> Sum a -> Sum a Source #(-) :: Sum a -> Sum a -> Sum a Source #(*) :: Sum a -> Sum a -> Sum a Source #negate :: Sum a -> Sum a Source #abs :: Sum a -> Sum a Source #signum :: Sum a -> Sum a Source # Integral a => Num (Ratio a) Source # @since base-2.0.1 Instance detailsDefined in GHC.Internal.Real Methods(+) :: Ratio a -> Ratio a -> Ratio a Source #(-) :: Ratio a -> Ratio a -> Ratio a Source #(*) :: Ratio a -> Ratio a -> Ratio a Source #negate :: Ratio a -> Ratio a Source #abs :: Ratio a -> Ratio a Source #signum :: Ratio a -> Ratio a Source # Num a => Num (Const a b) Source # @since base-4.9.0.0 Instance detailsDefined in GHC.Internal.Data.Functor.Const Methods(+) :: Const a b -> Const a b -> Const a b Source #(-) :: Const a b -> Const a b -> Const a b Source #(*) :: Const a b -> Const a b -> Const a b Source #negate :: Const a b -> Const a b Source #abs :: Const a b -> Const a b Source #signum :: Const a b -> Const a b Source # (Applicative f, Num a) => Num (Ap f a) Source # 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:>>> 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:>>> Ap [] + negate (Ap []) Ap {getAp = []} >>> fromInteger 0 :: Ap [] Int Ap {getAp = [0]} Distributivity:>>> Ap [1,2] * (3 + 4) Ap {getAp = [7,14]} >>> (Ap [1,2] * 3) + (Ap [1,2] * 4) Ap {getAp = [7,11,10,14]} @since base-4.12.0.0 Instance detailsDefined in GHC.Internal.Data.Monoid Methods(+) :: Ap f a -> Ap f a -> Ap f a Source #(-) :: Ap f a -> Ap f a -> Ap f a Source #(*) :: Ap f a -> Ap f a -> Ap f a Source #negate :: Ap f a -> Ap f a Source #abs :: Ap f a -> Ap f a Source #signum :: Ap f a -> Ap f a Source #fromInteger :: Integer -> Ap f a Source # Num (f a) => Num (Alt f a) Source # @since base-4.8.0.0 Instance detailsDefined in GHC.Internal.Data.Semigroup.Internal Methods(+) :: Alt f a -> Alt f a -> Alt f a Source #(-) :: Alt f a -> Alt f a -> Alt f a Source #(*) :: Alt f a -> Alt f a -> Alt f a Source #negate :: Alt f a -> Alt f a Source #abs :: Alt f a -> Alt f a Source #signum :: Alt f a -> Alt f a Source #fromInteger :: Integer -> Alt f a Source #

subtract :: Num a => a -> a -> a Source #

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.

data Natural Source #

Natural number

Invariant: numbers <= 0xffffffffffffffff use the NS constructor

#### Instances

Instances details
 Source # @since base-4.8.0 Instance detailsDefined in GHC.Internal.Bits Methods Source # @since base-4.8.0.0 Instance detailsDefined in GHC.Internal.Data.Data Methodsgfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Natural -> c Natural Source #gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Natural Source #dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Natural) Source #dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Natural) Source #gmapT :: (forall b. Data b => b -> b) -> Natural -> Natural Source #gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Natural -> r Source #gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Natural -> r Source #gmapQ :: (forall d. Data d => d -> u) -> Natural -> [u] Source #gmapQi :: Int -> (forall d. Data d => d -> u) -> Natural -> u Source #gmapM :: Monad m => (forall d. Data d => d -> m d) -> Natural -> m Natural Source #gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Natural -> m Natural Source #gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Natural -> m Natural Source # Source # @since base-4.8.0.0 Instance detailsDefined in GHC.Internal.Enum Methods Source # @since base-4.8.0.0 Instance detailsDefined in GHC.Internal.Ix Methodsrange :: (Natural, Natural) -> [Natural] Source #index :: (Natural, Natural) -> Natural -> Int Source #unsafeIndex :: (Natural, Natural) -> Natural -> Int Source #inRange :: (Natural, Natural) -> Natural -> Bool Source #rangeSize :: (Natural, Natural) -> Int Source # Source # 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 detailsDefined in GHC.Internal.Num Methods Source # @since base-4.8.0.0 Instance detailsDefined in GHC.Internal.Read Methods Source # @since base-4.8.0.0 Instance detailsDefined in GHC.Internal.Real Methods Source # @since base-4.8.0.0 Instance detailsDefined in GHC.Internal.Real Methods Source # @since base-4.8.0.0 Instance detailsDefined in GHC.Internal.Show MethodsshowList :: [Natural] -> ShowS Source # Instance detailsDefined in GHC.Num.Natural Methods Instance detailsDefined in GHC.Num.Natural Methods Source # @since base-4.18.0.0 Instance detailsDefined in GHC.Internal.TypeNats MethodstestCoercion :: forall (a :: Nat) (b :: Nat). SNat a -> SNat b -> Maybe (Coercion a b) Source # Source # @since base-4.18.0.0 Instance detailsDefined in GHC.Internal.TypeNats MethodstestEquality :: forall (a :: Nat) (b :: Nat). SNat a -> SNat b -> Maybe (a :~: b) Source # type Compare (a :: Natural) (b :: Natural) Source # Instance detailsDefined in GHC.Internal.Data.Type.Ord type Compare (a :: Natural) (b :: Natural) = CmpNat a b

data Integer Source #

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.

#### Instances

Instances details
 Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Bits Methods Source # @since base-4.0.0.0 Instance detailsDefined in GHC.Internal.Data.Data Methodsgfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Integer -> c Integer Source #gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Integer Source #dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Integer) Source #dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Integer) Source #gmapT :: (forall b. Data b => b -> b) -> Integer -> Integer Source #gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Integer -> r Source #gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Integer -> r Source #gmapQ :: (forall d. Data d => d -> u) -> Integer -> [u] Source #gmapQi :: Int -> (forall d. Data d => d -> u) -> Integer -> u Source #gmapM :: Monad m => (forall d. Data d => d -> m d) -> Integer -> m Integer Source #gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Integer -> m Integer Source #gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Integer -> m Integer Source # Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Enum Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Ix Methodsrange :: (Integer, Integer) -> [Integer] Source #index :: (Integer, Integer) -> Integer -> Int Source #unsafeIndex :: (Integer, Integer) -> Integer -> Int Source #inRange :: (Integer, Integer) -> Integer -> Bool Source #rangeSize :: (Integer, Integer) -> Int Source # Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Num Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Read Methods Source # @since base-2.0.1 Instance detailsDefined in GHC.Internal.Real Methods Source # @since base-2.0.1 Instance detailsDefined in GHC.Internal.Real Methods Source # @since base-2.01 Instance detailsDefined in GHC.Internal.Show MethodsshowList :: [Integer] -> ShowS Source # Instance detailsDefined in GHC.Num.Integer Methods Instance detailsDefined in GHC.Num.Integer Methods