fd98c19f20f7e02d2c4187065c2b20e55a817ca1
[ghc.git] / libraries / base / GHC / Num.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE NoImplicitPrelude, MagicHash, UnboxedTuples #-}
3 {-# OPTIONS_HADDOCK hide #-}
4
5 -----------------------------------------------------------------------------
6 -- |
7 -- Module : GHC.Num
8 -- Copyright : (c) The University of Glasgow 1994-2002
9 -- License : see libraries/base/LICENSE
10 --
11 -- Maintainer : cvs-ghc@haskell.org
12 -- Stability : internal
13 -- Portability : non-portable (GHC Extensions)
14 --
15 -- The 'Num' class and the 'Integer' type.
16 --
17 -----------------------------------------------------------------------------
18
19 module GHC.Num (module GHC.Num, module GHC.Integer) where
20
21 import GHC.Base
22 import GHC.Integer
23
24 infixl 7 *
25 infixl 6 +, -
26
27 default () -- Double isn't available yet,
28 -- and we shouldn't be using defaults anyway
29
30 -- | Basic numeric class.
31 class Num a where
32 {-# MINIMAL (+), (*), abs, signum, fromInteger, (negate | (-)) #-}
33
34 (+), (-), (*) :: a -> a -> a
35 -- | Unary negation.
36 negate :: a -> a
37 -- | Absolute value.
38 abs :: a -> a
39 -- | Sign of a number.
40 -- The functions 'abs' and 'signum' should satisfy the law:
41 --
42 -- > abs x * signum x == x
43 --
44 -- For real numbers, the 'signum' is either @-1@ (negative), @0@ (zero)
45 -- or @1@ (positive).
46 signum :: a -> a
47 -- | Conversion from an 'Integer'.
48 -- An integer literal represents the application of the function
49 -- 'fromInteger' to the appropriate value of type 'Integer',
50 -- so such literals have type @('Num' a) => a@.
51 fromInteger :: Integer -> a
52
53 {-# INLINE (-) #-}
54 {-# INLINE negate #-}
55 x - y = x + negate y
56 negate x = 0 - x
57
58 -- | the same as @'flip' ('-')@.
59 --
60 -- Because @-@ is treated specially in the Haskell grammar,
61 -- @(-@ /e/@)@ is not a section, but an application of prefix negation.
62 -- However, @('subtract'@ /exp/@)@ is equivalent to the disallowed section.
63 {-# INLINE subtract #-}
64 subtract :: (Num a) => a -> a -> a
65 subtract x y = y - x
66
67 -- | @since 2.01
68 instance Num Int where
69 I# x + I# y = I# (x +# y)
70 I# x - I# y = I# (x -# y)
71 negate (I# x) = I# (negateInt# x)
72 I# x * I# y = I# (x *# y)
73 abs n = if n `geInt` 0 then n else negate n
74
75 signum n | n `ltInt` 0 = negate 1
76 | n `eqInt` 0 = 0
77 | otherwise = 1
78
79 {-# INLINE fromInteger #-} -- Just to be sure!
80 fromInteger i = I# (integerToInt i)
81
82 -- | @since 2.01
83 instance Num Word where
84 (W# x#) + (W# y#) = W# (x# `plusWord#` y#)
85 (W# x#) - (W# y#) = W# (x# `minusWord#` y#)
86 (W# x#) * (W# y#) = W# (x# `timesWord#` y#)
87 negate (W# x#) = W# (int2Word# (negateInt# (word2Int# x#)))
88 abs x = x
89 signum 0 = 0
90 signum _ = 1
91 fromInteger i = W# (integerToWord i)
92
93 -- | @since 2.01
94 instance Num Integer where
95 (+) = plusInteger
96 (-) = minusInteger
97 (*) = timesInteger
98 negate = negateInteger
99 fromInteger x = x
100
101 abs = absInteger
102 signum = signumInteger