{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP, NoImplicitPrelude, MagicHash, UnboxedTuples, BangPatterns #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_HADDOCK not-home #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.Real
-- Copyright   :  (c) The University of Glasgow, 1994-2002
-- License     :  see libraries/base/LICENSE
--
-- Maintainer  :  cvs-ghc@haskell.org
-- Stability   :  internal
-- Portability :  non-portable (GHC Extensions)
--
-- The types 'Ratio' and 'Rational', and the classes 'Real', 'Fractional',
-- 'Integral', and 'RealFrac'.
--
-----------------------------------------------------------------------------

module GHC.Real where

#include "MachDeps.h"

import GHC.Base
import GHC.Num
import GHC.List
import GHC.Enum
import GHC.Show
import {-# SOURCE #-} GHC.Exception( divZeroException, overflowException
                                   , underflowException
                                   , ratioZeroDenomException )

#if defined(MIN_VERSION_integer_gmp)
import GHC.Integer.GMP.Internals
#endif

infixr 8  ^, ^^
infixl 7  /, `quot`, `rem`, `div`, `mod`
infixl 7  %

default ()              -- Double isn't available yet,
                        -- and we shouldn't be using defaults anyway

------------------------------------------------------------------------
-- Divide by zero and arithmetic overflow
------------------------------------------------------------------------

-- We put them here because they are needed relatively early
-- in the libraries before the Exception type has been defined yet.

{-# NOINLINE divZeroError #-}
divZeroError :: a
divZeroError :: a
divZeroError = SomeException -> a
forall b a. b -> a
raise# SomeException
divZeroException

{-# NOINLINE ratioZeroDenominatorError #-}
ratioZeroDenominatorError :: a
ratioZeroDenominatorError :: a
ratioZeroDenominatorError = SomeException -> a
forall b a. b -> a
raise# SomeException
ratioZeroDenomException

{-# NOINLINE overflowError #-}
overflowError :: a
overflowError :: a
overflowError = SomeException -> a
forall b a. b -> a
raise# SomeException
overflowException

{-# NOINLINE underflowError #-}
underflowError :: a
underflowError :: a
underflowError = SomeException -> a
forall b a. b -> a
raise# SomeException
underflowException


--------------------------------------------------------------
-- The Ratio and Rational types
--------------------------------------------------------------

-- | Rational numbers, with numerator and denominator of some 'Integral' type.
--
-- Note that `Ratio`'s instances inherit the deficiencies from the type
-- parameter's. For example, @Ratio Natural@'s 'Num' instance has similar
-- problems to `Numeric.Natural.Natural`'s.
data  Ratio a = !a :% !a  deriving Eq -- ^ @since 2.01

-- | Arbitrary-precision rational numbers, represented as a ratio of
-- two 'Integer' values.  A rational number may be constructed using
-- the '%' operator.
type  Rational          =  Ratio Integer

ratioPrec, ratioPrec1 :: Int
ratioPrec :: Int
ratioPrec  = Int
7  -- Precedence of ':%' constructor
ratioPrec1 :: Int
ratioPrec1 = Int
ratioPrec Int -> Int -> Int
forall a. Num a => a -> a -> a
External instance of the constraint type Num Int
+ Int
1

infinity, notANumber :: Rational
infinity :: Rational
infinity   = Integer
1 Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
0
notANumber :: Rational
notANumber = Integer
0 Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
0

-- Use :%, not % for Inf/NaN; the latter would
-- immediately lead to a runtime error, because it normalises.

-- | Forms the ratio of two integral numbers.
{-# SPECIALISE (%) :: Integer -> Integer -> Rational #-}
(%)                     :: (Integral a) => a -> a -> Ratio a

-- | Extract the numerator of the ratio in reduced form:
-- the numerator and denominator have no common factor and the denominator
-- is positive.
numerator       :: Ratio a -> a

-- | Extract the denominator of the ratio in reduced form:
-- the numerator and denominator have no common factor and the denominator
-- is positive.
denominator     :: Ratio a -> a


-- | 'reduce' is a subsidiary function used only in this module.
-- It normalises a ratio by dividing both numerator and denominator by
-- their greatest common divisor.
reduce ::  (Integral a) => a -> a -> Ratio a
{-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
reduce :: a -> a -> Ratio a
reduce a
_ a
0              =  Ratio a
forall a. a
ratioZeroDenominatorError
reduce a
x a
y              =  (a
x a -> a -> a
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral a
`quot` a
d) a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% (a
y a -> a -> a
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral a
`quot` a
d)
                           where d :: a
d = a -> a -> a
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral a
gcd a
x a
y

a
x % :: a -> a -> Ratio a
% a
y                   =  a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
Evidence bound by a type signature of the constraint type Integral a
reduce (a
x a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
* a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
signum a
y) (a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
abs a
y)

numerator :: Ratio a -> a
numerator   (a
x :% a
_)    =  a
x
denominator :: Ratio a -> a
denominator (a
_ :% a
y)    =  a
y

--------------------------------------------------------------
-- Standard numeric classes
--------------------------------------------------------------

class  (Num a, Ord a) => Real a  where
    -- | the rational equivalent of its real argument with full precision
    toRational          ::  a -> Rational

-- | Integral numbers, supporting integer division.
--
-- The Haskell Report defines no laws for 'Integral'. However, 'Integral'
-- instances are customarily expected to define a Euclidean domain and have the
-- following properties for the 'div'\/'mod' and 'quot'\/'rem' pairs, given
-- suitable Euclidean functions @f@ and @g@:
--
-- * @x@ = @y * quot x y + rem x y@ with @rem x y@ = @fromInteger 0@ or
-- @g (rem x y)@ < @g y@
-- * @x@ = @y * div x y + mod x y@ with @mod x y@ = @fromInteger 0@ or
-- @f (mod x y)@ < @f y@
--
-- An example of a suitable Euclidean function, for 'Integer'\'s instance, is
-- 'abs'.
class  (Real a, Enum a) => Integral a  where
    -- | integer division truncated toward zero
    quot                :: a -> a -> a
    -- | integer remainder, satisfying
    --
    -- > (x `quot` y)*y + (x `rem` y) == x
    rem                 :: a -> a -> a
    -- | integer division truncated toward negative infinity
    div                 :: a -> a -> a
    -- | integer modulus, satisfying
    --
    -- > (x `div` y)*y + (x `mod` y) == x
    mod                 :: a -> a -> a
    -- | simultaneous 'quot' and 'rem'
    quotRem             :: a -> a -> (a,a)
    -- | simultaneous 'div' and 'mod'
    divMod              :: a -> a -> (a,a)
    -- | conversion to 'Integer'
    toInteger           :: a -> Integer

    {-# INLINE quot #-}
    {-# INLINE rem #-}
    {-# INLINE div #-}
    {-# INLINE mod #-}
    a
n `quot` a
d          =  a
q  where (a
q,a
_) = a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
Evidence bound by a type signature of the constraint type Integral a
quotRem a
n a
d
    a
n `rem` a
d           =  a
r  where (a
_,a
r) = a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
Evidence bound by a type signature of the constraint type Integral a
quotRem a
n a
d
    a
n `div` a
d           =  a
q  where (a
q,a
_) = a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
Evidence bound by a type signature of the constraint type Integral a
divMod a
n a
d
    a
n `mod` a
d           =  a
r  where (a
_,a
r) = a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
Evidence bound by a type signature of the constraint type Integral a
divMod a
n a
d

    divMod a
n a
d          =  if a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
signum a
r a -> a -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type forall a. Ord a => Eq a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
== a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
negate (a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
signum a
d) then (a
qa -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
-a
1, a
ra -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
+a
d) else (a, a)
qr
                           where qr :: (a, a)
qr@(a
q,a
r) = a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
Evidence bound by a type signature of the constraint type Integral a
quotRem a
n a
d

-- | Fractional numbers, supporting real division.
--
-- The Haskell Report defines no laws for 'Fractional'. However, @('+')@ and
-- @('*')@ are customarily expected to define a division ring and have the
-- following properties:
--
-- [__'recip' gives the multiplicative inverse__]:
-- @x * recip x@ = @recip x * x@ = @fromInteger 1@
--
-- Note that it /isn't/ customarily expected that a type instance of
-- 'Fractional' implement a field. However, all instances in @base@ do.
class  (Num a) => Fractional a  where
    {-# MINIMAL fromRational, (recip | (/)) #-}

    -- | Fractional division.
    (/)                 :: a -> a -> a
    -- | Reciprocal fraction.
    recip               :: a -> a
    -- | Conversion from a 'Rational' (that is @'Ratio' 'Integer'@).
    -- A floating literal stands for an application of 'fromRational'
    -- to a value of type 'Rational', so such literals have type
    -- @('Fractional' a) => a@.
    fromRational        :: Rational -> a

    {-# INLINE recip #-}
    {-# INLINE (/) #-}
    recip a
x             =  a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
Evidence bound by a type signature of the constraint type Fractional a
/ a
x
    a
x / a
y               = a
x a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
* a -> a
forall a. Fractional a => a -> a
Evidence bound by a type signature of the constraint type Fractional a
recip a
y

-- | Extracting components of fractions.
class  (Real a, Fractional a) => RealFrac a  where
    -- | The function 'properFraction' takes a real fractional number @x@
    -- and returns a pair @(n,f)@ such that @x = n+f@, and:
    --
    -- * @n@ is an integral number with the same sign as @x@; and
    --
    -- * @f@ is a fraction with the same type and sign as @x@,
    --   and with absolute value less than @1@.
    --
    -- The default definitions of the 'ceiling', 'floor', 'truncate'
    -- and 'round' functions are in terms of 'properFraction'.
    properFraction      :: (Integral b) => a -> (b,a)
    -- | @'truncate' x@ returns the integer nearest @x@ between zero and @x@
    truncate            :: (Integral b) => a -> b
    -- | @'round' x@ returns the nearest integer to @x@;
    --   the even integer if @x@ is equidistant between two integers
    round               :: (Integral b) => a -> b
    -- | @'ceiling' x@ returns the least integer not less than @x@
    ceiling             :: (Integral b) => a -> b
    -- | @'floor' x@ returns the greatest integer not greater than @x@
    floor               :: (Integral b) => a -> b

    {-# INLINE truncate #-}
    truncate a
x          =  b
m  where (b
m,a
_) = a -> (b, a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
Evidence bound by a type signature of the constraint type Integral b
Evidence bound by a type signature of the constraint type RealFrac a
properFraction a
x

    round a
x             =  let (b
n,a
r) = a -> (b, a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
Evidence bound by a type signature of the constraint type Integral b
Evidence bound by a type signature of the constraint type RealFrac a
properFraction a
x
                               m :: b
m     = if a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: RealFrac of the constraint type forall a. RealFrac a => Real a
Evidence bound by a type signature of the constraint type RealFrac a
< a
0 then b
n b -> b -> b
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral b
- b
1 else b
n b -> b -> b
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral b
+ b
1
                           in case a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: RealFrac of the constraint type forall a. RealFrac a => Real a
Evidence bound by a type signature of the constraint type RealFrac a
signum (a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: RealFrac of the constraint type forall a. RealFrac a => Real a
Evidence bound by a type signature of the constraint type RealFrac a
abs a
r a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: RealFrac of the constraint type forall a. RealFrac a => Real a
Evidence bound by a type signature of the constraint type RealFrac a
- a
0.5) of
                                -1 -> b
n
                                a
0  -> if b -> Bool
forall a. Integral a => a -> Bool
Evidence bound by a type signature of the constraint type Integral b
even b
n then b
n else b
m
                                a
1  -> b
m
                                a
_  -> [Char] -> b
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"round default defn: Bad value"

    ceiling a
x           =  if a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: RealFrac of the constraint type forall a. RealFrac a => Real a
Evidence bound by a type signature of the constraint type RealFrac a
> a
0 then b
n b -> b -> b
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral b
+ b
1 else b
n
                           where (b
n,a
r) = a -> (b, a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
Evidence bound by a type signature of the constraint type Integral b
Evidence bound by a type signature of the constraint type RealFrac a
properFraction a
x

    floor a
x             =  if a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: RealFrac of the constraint type forall a. RealFrac a => Real a
Evidence bound by a type signature of the constraint type RealFrac a
< a
0 then b
n b -> b -> b
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral b
- b
1 else b
n
                           where (b
n,a
r) = a -> (b, a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
Evidence bound by a type signature of the constraint type Integral b
Evidence bound by a type signature of the constraint type RealFrac a
properFraction a
x

-- These 'numeric' enumerations come straight from the Report

numericEnumFrom         :: (Fractional a) => a -> [a]
numericEnumFrom :: a -> [a]
numericEnumFrom a
n       = a -> [a]
go a
0
  where
    -- See Note [Numeric Stability of Enumerating Floating Numbers]
    go :: a -> [a]
go !a
k = let !n' :: a
n' = a
n a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
+ a
k
             in a
n' a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a -> [a]
go (a
k a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
+ a
1)

numericEnumFromThen     :: (Fractional a) => a -> a -> [a]
numericEnumFromThen :: a -> a -> [a]
numericEnumFromThen a
n a
m = a -> [a]
go a
0
  where
    step :: a
step = a
m a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
- a
n
    -- See Note [Numeric Stability of Enumerating Floating Numbers]
    go :: a -> [a]
go !a
k = let !n' :: a
n' = a
n a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
+ a
k a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
* a
step
             in a
n' a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a -> [a]
go (a
k a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
+ a
1)

numericEnumFromTo       :: (Ord a, Fractional a) => a -> a -> [a]
numericEnumFromTo :: a -> a -> [a]
numericEnumFromTo a
n a
m   = (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a type signature of the constraint type Ord a
<= a
m a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
+ a
1a -> a -> a
forall a. Fractional a => a -> a -> a
Evidence bound by a type signature of the constraint type Fractional a
/a
2) (a -> [a]
forall a. Fractional a => a -> [a]
Evidence bound by a type signature of the constraint type Fractional a
numericEnumFrom a
n)

numericEnumFromThenTo   :: (Ord a, Fractional a) => a -> a -> a -> [a]
numericEnumFromThenTo :: a -> a -> a -> [a]
numericEnumFromThenTo a
e1 a
e2 a
e3
    = (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile a -> Bool
predicate (a -> a -> [a]
forall a. Fractional a => a -> a -> [a]
Evidence bound by a type signature of the constraint type Fractional a
numericEnumFromThen a
e1 a
e2)
                                where
                                 mid :: a
mid = (a
e2 a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
- a
e1) a -> a -> a
forall a. Fractional a => a -> a -> a
Evidence bound by a type signature of the constraint type Fractional a
/ a
2
                                 predicate :: a -> Bool
predicate | a
e2 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a type signature of the constraint type Ord a
>= a
e1  = (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a type signature of the constraint type Ord a
<= a
e3 a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
+ a
mid)
                                           | Bool
otherwise = (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a type signature of the constraint type Ord a
>= a
e3 a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
+ a
mid)

{- Note [Numeric Stability of Enumerating Floating Numbers]
-----------------------------------------------------------
When enumerate floating numbers, we could add the increment to the last number
at every run (as what we did previously):

    numericEnumFrom n =  n `seq` (n : numericEnumFrom (n + 1))

This approach is concise and really fast, only needs an addition operation.
However when a floating number is large enough, for `n`, `n` and `n+1` will
have the same binary representation. For example (all number has type
`Double`):

    9007199254740990                 is: 0x433ffffffffffffe
    9007199254740990 + 1             is: 0x433fffffffffffff
    (9007199254740990 + 1) + 1       is: 0x4340000000000000
    ((9007199254740990 + 1) + 1) + 1 is: 0x4340000000000000

When we evaluate ([9007199254740990..9007199254740991] :: Double), we would
never reach the condition in `numericEnumFromTo`

    9007199254740990 + 1 + 1 + ... > 9007199254740991 + 1/2

We would fall into infinite loop (as reported in #15081).

To remedy the situation, we record the number of `1` that needed to be added
to the start number, rather than increasing `1` at every time. This approach
can improvement the numeric stability greatly at the cost of a multiplication.

Furthermore, we use the type of the enumerated number, `Fractional a => a`,
as the type of multiplier. In rare situations, the multiplier could be very
large and will lead to the enumeration to infinite loop, too, which should
be very rare. Consider the following example:

    [1..9007199254740994]

We could fix that by using an Integer as multiplier but we don't do that.
The benchmark on T7954.hs shows that this approach leads to significant
degeneration on performance (33% increase allocation and 300% increase on
elapsed time).

See #15081 and Phab:D4650 for the related discussion about this problem.
-}

--------------------------------------------------------------
-- Instances for Int
--------------------------------------------------------------

-- | @since 2.0.1
instance  Real Int  where
    toRational :: Int -> Rational
toRational Int
x        =  Int -> Integer
forall a. Integral a => a -> Integer
Instance of class: Integral of the constraint type Integral Int
toInteger Int
x Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
1

-- | @since 2.0.1
instance  Integral Int  where
    toInteger :: Int -> Integer
toInteger (I# Int#
i) = Int# -> Integer
smallInteger Int#
i

    Int
a quot :: Int -> Int -> Int
`quot` Int
b
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== Int
0                     = Int
forall a. a
divZeroError
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== (-Int
1) Bool -> Bool -> Bool
&& Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== Int
forall a. Bounded a => a
External instance of the constraint type Bounded Int
minBound = Int
forall a. a
overflowError -- Note [Order of tests]
                                                  -- in GHC.Int
     | Bool
otherwise                  =  Int
a Int -> Int -> Int
`quotInt` Int
b

    !Int
a rem :: Int -> Int -> Int
`rem` Int
b -- See Note [Special case of mod and rem is lazy]
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== Int
0                     = Int
forall a. a
divZeroError
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== (-Int
1)                  = Int
0
     | Bool
otherwise                  =  Int
a Int -> Int -> Int
`remInt` Int
b

    Int
a div :: Int -> Int -> Int
`div` Int
b
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== Int
0                     = Int
forall a. a
divZeroError
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== (-Int
1) Bool -> Bool -> Bool
&& Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== Int
forall a. Bounded a => a
External instance of the constraint type Bounded Int
minBound = Int
forall a. a
overflowError -- Note [Order of tests]
                                                  -- in GHC.Int
     | Bool
otherwise                  =  Int
a Int -> Int -> Int
`divInt` Int
b

    !Int
a mod :: Int -> Int -> Int
`mod` Int
b -- See Note [Special case of mod and rem is lazy]
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== Int
0                     = Int
forall a. a
divZeroError
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== (-Int
1)                  = Int
0
     | Bool
otherwise                  =  Int
a Int -> Int -> Int
`modInt` Int
b

    Int
a quotRem :: Int -> Int -> (Int, Int)
`quotRem` Int
b
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== Int
0                     = (Int, Int)
forall a. a
divZeroError
       -- Note [Order of tests] in GHC.Int
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== (-Int
1) Bool -> Bool -> Bool
&& Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== Int
forall a. Bounded a => a
External instance of the constraint type Bounded Int
minBound = (Int
forall a. a
overflowError, Int
0)
     | Bool
otherwise                  =  Int
a Int -> Int -> (Int, Int)
`quotRemInt` Int
b

    Int
a divMod :: Int -> Int -> (Int, Int)
`divMod` Int
b
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== Int
0                     = (Int, Int)
forall a. a
divZeroError
       -- Note [Order of tests] in GHC.Int
     | Int
b Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== (-Int
1) Bool -> Bool -> Bool
&& Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Int
== Int
forall a. Bounded a => a
External instance of the constraint type Bounded Int
minBound = (Int
forall a. a
overflowError, Int
0)
     | Bool
otherwise                  =  Int
a Int -> Int -> (Int, Int)
`divModInt` Int
b

{- Note [Special case of mod and rem is lazy]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The `quotRem`/`divMod` CPU instruction fails for minBound `quotRem` -1, but
minBound `rem` -1 is well-defined (0). We therefore special-case for `b == -1`,
but not for `a == minBound` because of Note [Order of tests] in GHC.Int. But
now we have to make sure the function stays strict in a, to guarantee unboxing.
Hence the bang on a, see #18187.
-}

--------------------------------------------------------------
-- Instances for @Word@
--------------------------------------------------------------

-- | @since 2.01
instance Real Word where
    toRational :: Word -> Rational
toRational Word
x = Word -> Integer
forall a. Integral a => a -> Integer
Instance of class: Integral of the constraint type Integral Word
toInteger Word
x Integer -> Integer -> Rational
forall a. Integral a => a -> a -> Ratio a
Instance of class: Integral of the constraint type Integral Integer
% Integer
1

-- | @since 2.01
instance Integral Word where
    quot :: Word -> Word -> Word
quot    (W# Word#
x#) y :: Word
y@(W# Word#
y#)
        | Word
y Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Word
/= Word
0                = Word# -> Word
W# (Word#
x# Word# -> Word# -> Word#
`quotWord#` Word#
y#)
        | Bool
otherwise             = Word
forall a. a
divZeroError
    rem :: Word -> Word -> Word
rem     (W# Word#
x#) y :: Word
y@(W# Word#
y#)
        | Word
y Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Word
/= Word
0                = Word# -> Word
W# (Word#
x# Word# -> Word# -> Word#
`remWord#` Word#
y#)
        | Bool
otherwise             = Word
forall a. a
divZeroError
    div :: Word -> Word -> Word
div     (W# Word#
x#) y :: Word
y@(W# Word#
y#)
        | Word
y Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Word
/= Word
0                = Word# -> Word
W# (Word#
x# Word# -> Word# -> Word#
`quotWord#` Word#
y#)
        | Bool
otherwise             = Word
forall a. a
divZeroError
    mod :: Word -> Word -> Word
mod     (W# Word#
x#) y :: Word
y@(W# Word#
y#)
        | Word
y Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Word
/= Word
0                = Word# -> Word
W# (Word#
x# Word# -> Word# -> Word#
`remWord#` Word#
y#)
        | Bool
otherwise             = Word
forall a. a
divZeroError
    quotRem :: Word -> Word -> (Word, Word)
quotRem (W# Word#
x#) y :: Word
y@(W# Word#
y#)
        | Word
y Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Word
/= Word
0                = case Word#
x# Word# -> Word# -> (# Word#, Word# #)
`quotRemWord#` Word#
y# of
                                  (# Word#
q, Word#
r #) ->
                                      (Word# -> Word
W# Word#
q, Word# -> Word
W# Word#
r)
        | Bool
otherwise             = (Word, Word)
forall a. a
divZeroError
    divMod :: Word -> Word -> (Word, Word)
divMod  (W# Word#
x#) y :: Word
y@(W# Word#
y#)
        | Word
y Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Word
/= Word
0                = (Word# -> Word
W# (Word#
x# Word# -> Word# -> Word#
`quotWord#` Word#
y#), Word# -> Word
W# (Word#
x# Word# -> Word# -> Word#
`remWord#` Word#
y#))
        | Bool
otherwise             = (Word, Word)
forall a. a
divZeroError
    toInteger :: Word -> Integer
toInteger (W# Word#
x#)           = Word# -> Integer
wordToInteger Word#
x#

--------------------------------------------------------------
-- Instances for Integer
--------------------------------------------------------------

-- | @since 2.0.1
instance  Real Integer  where
    toRational :: Integer -> Rational
toRational Integer
x        =  Integer
x Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
1

-- | @since 4.8.0.0
instance Real Natural where
    toRational :: Natural -> Rational
toRational Natural
n = Natural -> Integer
naturalToInteger Natural
n Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
1

-- Note [Integer division constant folding]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
-- Constant folding of quot, rem, div, mod, divMod and quotRem for
-- Integer arguments depends crucially on inlining. Constant folding
-- rules defined in GHC.Core.Opt.ConstantFold trigger for
-- quotInteger, remInteger and so on. So if calls to quot, rem and so on
-- were not inlined the rules would not fire. The rules would also not
-- fire if calls to quotInteger and so on were inlined, but this does not
-- happen because they are all marked with NOINLINE pragma - see documentation
-- of integer-gmp or integer-simple.

-- | @since 2.0.1
instance  Integral Integer where
    toInteger :: Integer -> Integer
toInteger Integer
n      = Integer
n

    {-# INLINE quot #-}
    Integer
_ quot :: Integer -> Integer -> Integer
`quot` Integer
0 = Integer
forall a. a
divZeroError
    Integer
n `quot` Integer
d = Integer
n Integer -> Integer -> Integer
`quotInteger` Integer
d

    {-# INLINE rem #-}
    Integer
_ rem :: Integer -> Integer -> Integer
`rem` Integer
0 = Integer
forall a. a
divZeroError
    Integer
n `rem` Integer
d = Integer
n Integer -> Integer -> Integer
`remInteger` Integer
d

    {-# INLINE div #-}
    Integer
_ div :: Integer -> Integer -> Integer
`div` Integer
0 = Integer
forall a. a
divZeroError
    Integer
n `div` Integer
d = Integer
n Integer -> Integer -> Integer
`divInteger` Integer
d

    {-# INLINE mod #-}
    Integer
_ mod :: Integer -> Integer -> Integer
`mod` Integer
0 = Integer
forall a. a
divZeroError
    Integer
n `mod` Integer
d = Integer
n Integer -> Integer -> Integer
`modInteger` Integer
d

    {-# INLINE divMod #-}
    Integer
_ divMod :: Integer -> Integer -> (Integer, Integer)
`divMod` Integer
0 = (Integer, Integer)
forall a. a
divZeroError
    Integer
n `divMod` Integer
d = case Integer
n Integer -> Integer -> (# Integer, Integer #)
`divModInteger` Integer
d of
                     (# Integer
x, Integer
y #) -> (Integer
x, Integer
y)

    {-# INLINE quotRem #-}
    Integer
_ quotRem :: Integer -> Integer -> (Integer, Integer)
`quotRem` Integer
0 = (Integer, Integer)
forall a. a
divZeroError
    Integer
n `quotRem` Integer
d = case Integer
n Integer -> Integer -> (# Integer, Integer #)
`quotRemInteger` Integer
d of
                      (# Integer
q, Integer
r #) -> (Integer
q, Integer
r)

-- | @since 4.8.0.0
instance Integral Natural where
    toInteger :: Natural -> Integer
toInteger = Natural -> Integer
naturalToInteger

    divMod :: Natural -> Natural -> (Natural, Natural)
divMod = Natural -> Natural -> (Natural, Natural)
quotRemNatural
    div :: Natural -> Natural -> Natural
div    = Natural -> Natural -> Natural
quotNatural
    mod :: Natural -> Natural -> Natural
mod    = Natural -> Natural -> Natural
remNatural

    quotRem :: Natural -> Natural -> (Natural, Natural)
quotRem = Natural -> Natural -> (Natural, Natural)
quotRemNatural
    quot :: Natural -> Natural -> Natural
quot    = Natural -> Natural -> Natural
quotNatural
    rem :: Natural -> Natural -> Natural
rem     = Natural -> Natural -> Natural
remNatural

--------------------------------------------------------------
-- Instances for @Ratio@
--------------------------------------------------------------

-- | @since 2.0.1
instance  (Integral a)  => Ord (Ratio a)  where
    {-# SPECIALIZE instance Ord Rational #-}
    (a
x:%a
y) <= :: Ratio a -> Ratio a -> Bool
<= (a
x':%a
y')  =  a
x a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
* a
y' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
<= a
x' a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
* a
y
    (a
x:%a
y) < :: Ratio a -> Ratio a -> Bool
<  (a
x':%a
y')  =  a
x a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
* a
y' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
<  a
x' a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
* a
y

-- | @since 2.0.1
instance  (Integral a)  => Num (Ratio a)  where
    {-# SPECIALIZE instance Num Rational #-}
    (a
x:%a
y) + :: Ratio a -> Ratio a -> Ratio a
+ (a
x':%a
y')   =  a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
Evidence bound by a type signature of the constraint type Integral a
reduce (a
xa -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
*a
y' a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
+ a
x'a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
*a
y) (a
ya -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
*a
y')
    (a
x:%a
y) - :: Ratio a -> Ratio a -> Ratio a
- (a
x':%a
y')   =  a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
Evidence bound by a type signature of the constraint type Integral a
reduce (a
xa -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
*a
y' a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
- a
x'a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
*a
y) (a
ya -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
*a
y')
    (a
x:%a
y) * :: Ratio a -> Ratio a -> Ratio a
* (a
x':%a
y')   =  a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
Evidence bound by a type signature of the constraint type Integral a
reduce (a
x a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
* a
x') (a
y a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
* a
y')
    negate :: Ratio a -> Ratio a
negate (a
x:%a
y)       =  (-a
x) a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
y
    abs :: Ratio a -> Ratio a
abs (a
x:%a
y)          =  a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
abs a
x a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
y
    signum :: Ratio a -> Ratio a
signum (a
x:%a
_)       =  a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
signum a
x a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
1
    fromInteger :: Integer -> Ratio a
fromInteger Integer
x       =  Integer -> a
forall a. Num a => Integer -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
fromInteger Integer
x a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
1

-- | @since 2.0.1
{-# RULES "fromRational/id" fromRational = id :: Rational -> Rational #-}
instance  (Integral a)  => Fractional (Ratio a)  where
    {-# SPECIALIZE instance Fractional Rational #-}
    (a
x:%a
y) / :: Ratio a -> Ratio a -> Ratio a
/ (a
x':%a
y')   =  (a
xa -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
*a
y') a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
Evidence bound by a type signature of the constraint type Integral a
% (a
ya -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
*a
x')
    recip :: Ratio a -> Ratio a
recip (a
0:%a
_)        = Ratio a
forall a. a
ratioZeroDenominatorError
    recip (a
x:%a
y)
        | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
< a
0         = a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
negate a
y a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
negate a
x
        | Bool
otherwise     = a
y a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
x
    fromRational :: Rational -> Ratio a
fromRational (Integer
x:%Integer
y) =  Integer -> a
forall a. Num a => Integer -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
fromInteger Integer
x a -> a -> Ratio a
forall a. Integral a => a -> a -> Ratio a
Evidence bound by a type signature of the constraint type Integral a
% Integer -> a
forall a. Num a => Integer -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
fromInteger Integer
y

-- | @since 2.0.1
instance  (Integral a)  => Real (Ratio a)  where
    {-# SPECIALIZE instance Real Rational #-}
    toRational :: Ratio a -> Rational
toRational (a
x:%a
y)   =  a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger a
x Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger a
y

-- | @since 2.0.1
instance  (Integral a)  => RealFrac (Ratio a)  where
    {-# SPECIALIZE instance RealFrac Rational #-}
    properFraction :: Ratio a -> (b, Ratio a)
properFraction (a
x:%a
y) = (Integer -> b
forall a. Num a => Integer -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral b
fromInteger (a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger a
q), a
ra -> a -> Ratio a
forall a. a -> a -> Ratio a
:%a
y)
                          where (a
q,a
r) = a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
Evidence bound by a type signature of the constraint type Integral a
quotRem a
x a
y
    round :: Ratio a -> b
round Ratio a
r =
      let
        (b
n, Ratio a
f) = Ratio a -> (b, Ratio a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
Evidence bound by a type signature of the constraint type Integral b
Instance of class: RealFrac of the constraint type forall a. Integral a => RealFrac (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
properFraction Ratio a
r
        x :: b
x = if Ratio a
r Ratio a -> Ratio a -> Bool
forall a. Ord a => a -> a -> Bool
Instance of class: Ord of the constraint type forall a. Integral a => Ord (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
< Ratio a
0 then -b
1 else b
1
      in
        case (Ratio a -> Ratio a -> Ordering
forall a. Ord a => a -> a -> Ordering
Instance of class: Ord of the constraint type forall a. Integral a => Ord (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
compare (Ratio a -> Ratio a
forall a. Num a => a -> a
Instance of class: Num of the constraint type forall a. Integral a => Num (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
abs Ratio a
f) Ratio a
0.5, b -> Bool
forall a. Integral a => a -> Bool
Evidence bound by a type signature of the constraint type Integral b
odd b
n) of
          (Ordering
LT, Bool
_) -> b
n
          (Ordering
EQ, Bool
False) -> b
n
          (Ordering
EQ, Bool
True) -> b
n b -> b -> b
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral b
+ b
x
          (Ordering
GT, Bool
_) -> b
n b -> b -> b
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral b
+ b
x

-- | @since 2.0.1
instance  (Show a)  => Show (Ratio a)  where
    {-# SPECIALIZE instance Show Rational #-}
    showsPrec :: Int -> Ratio a -> ShowS
showsPrec Int
p (a
x:%a
y)  =  Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
External instance of the constraint type Ord Int
> Int
ratioPrec) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
                           Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
Evidence bound by a type signature of the constraint type Show a
showsPrec Int
ratioPrec1 a
x ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                           [Char] -> ShowS
showString [Char]
" % " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                           -- H98 report has spaces round the %
                           -- but we removed them [May 04]
                           -- and added them again for consistency with
                           -- Haskell 98 [Sep 08, #1920]
                           Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
Evidence bound by a type signature of the constraint type Show a
showsPrec Int
ratioPrec1 a
y

-- | @since 2.0.1
instance  (Integral a)  => Enum (Ratio a)  where
    {-# SPECIALIZE instance Enum Rational #-}
    succ :: Ratio a -> Ratio a
succ Ratio a
x              =  Ratio a
x Ratio a -> Ratio a -> Ratio a
forall a. Num a => a -> a -> a
Instance of class: Num of the constraint type forall a. Integral a => Num (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
+ Ratio a
1
    pred :: Ratio a -> Ratio a
pred Ratio a
x              =  Ratio a
x Ratio a -> Ratio a -> Ratio a
forall a. Num a => a -> a -> a
Instance of class: Num of the constraint type forall a. Integral a => Num (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
- Ratio a
1

    toEnum :: Int -> Ratio a
toEnum Int
n            =  Int -> a
forall a b. (Integral a, Num b) => a -> b
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
Instance of class: Integral of the constraint type Integral Int
fromIntegral Int
n a -> a -> Ratio a
forall a. a -> a -> Ratio a
:% a
1
    fromEnum :: Ratio a -> Int
fromEnum            =  Integer -> Int
forall a. Num a => Integer -> a
External instance of the constraint type Num Int
fromInteger (Integer -> Int) -> (Ratio a -> Integer) -> Ratio a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ratio a -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
Instance of class: Integral of the constraint type Integral Integer
Instance of class: RealFrac of the constraint type forall a. Integral a => RealFrac (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
truncate

    enumFrom :: Ratio a -> [Ratio a]
enumFrom            =  Ratio a -> [Ratio a]
forall a. Fractional a => a -> [a]
Instance of class: Fractional of the constraint type forall a. Integral a => Fractional (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
numericEnumFrom
    enumFromThen :: Ratio a -> Ratio a -> [Ratio a]
enumFromThen        =  Ratio a -> Ratio a -> [Ratio a]
forall a. Fractional a => a -> a -> [a]
Instance of class: Fractional of the constraint type forall a. Integral a => Fractional (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
numericEnumFromThen
    enumFromTo :: Ratio a -> Ratio a -> [Ratio a]
enumFromTo          =  Ratio a -> Ratio a -> [Ratio a]
forall a. (Ord a, Fractional a) => a -> a -> [a]
Instance of class: Fractional of the constraint type forall a. Integral a => Fractional (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
Instance of class: Ord of the constraint type forall a. Integral a => Ord (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
numericEnumFromTo
    enumFromThenTo :: Ratio a -> Ratio a -> Ratio a -> [Ratio a]
enumFromThenTo      =  Ratio a -> Ratio a -> Ratio a -> [Ratio a]
forall a. (Ord a, Fractional a) => a -> a -> a -> [a]
Instance of class: Fractional of the constraint type forall a. Integral a => Fractional (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
Instance of class: Ord of the constraint type forall a. Integral a => Ord (Ratio a)
Evidence bound by a type signature of the constraint type Integral a
numericEnumFromThenTo

--------------------------------------------------------------
-- Coercions
--------------------------------------------------------------

-- | general coercion from integral types
{-# NOINLINE [1] fromIntegral #-}
fromIntegral :: (Integral a, Num b) => a -> b
fromIntegral :: a -> b
fromIntegral = Integer -> b
forall a. Num a => Integer -> a
Evidence bound by a type signature of the constraint type Num b
fromInteger (Integer -> b) -> (a -> Integer) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger

{-# RULES
"fromIntegral/Int->Int" fromIntegral = id :: Int -> Int
    #-}

{-# RULES
"fromIntegral/Int->Word"  fromIntegral = \(I# x#) -> W# (int2Word# x#)
"fromIntegral/Word->Int"  fromIntegral = \(W# x#) -> I# (word2Int# x#)
"fromIntegral/Word->Word" fromIntegral = id :: Word -> Word
    #-}

{-# RULES
"fromIntegral/Natural->Natural"  fromIntegral = id :: Natural -> Natural
"fromIntegral/Natural->Integer"  fromIntegral = toInteger :: Natural->Integer
"fromIntegral/Natural->Word"     fromIntegral = naturalToWord
  #-}

{-# RULES
"fromIntegral/Word->Natural"     fromIntegral = wordToNatural
"fromIntegral/Int->Natural"     fromIntegral = intToNatural
  #-}

-- | general coercion to fractional types
realToFrac :: (Real a, Fractional b) => a -> b
{-# NOINLINE [1] realToFrac #-}
realToFrac :: a -> b
realToFrac = Rational -> b
forall a. Fractional a => Rational -> a
Evidence bound by a type signature of the constraint type Fractional b
fromRational (Rational -> b) -> (a -> Rational) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rational
forall a. Real a => a -> Rational
Evidence bound by a type signature of the constraint type Real a
toRational

--------------------------------------------------------------
-- Overloaded numeric functions
--------------------------------------------------------------

-- | Converts a possibly-negative 'Real' value to a string.
showSigned :: (Real a)
  => (a -> ShowS)       -- ^ a function that can show unsigned values
  -> Int                -- ^ the precedence of the enclosing context
  -> a                  -- ^ the value to show
  -> ShowS
showSigned :: (a -> ShowS) -> Int -> a -> ShowS
showSigned a -> ShowS
showPos Int
p a
x
   | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a type signature of the constraint type Real a
< a
0     = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
External instance of the constraint type Ord Int
> Int
6) (Char -> ShowS
showChar Char
'-' ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
showPos (-a
x))
   | Bool
otherwise = a -> ShowS
showPos a
x

even, odd       :: (Integral a) => a -> Bool
even :: a -> Bool
even a
n          =  a
n a -> a -> a
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral a
`rem` a
2 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type forall a. Ord a => Eq a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
== a
0
odd :: a -> Bool
odd             =  Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
forall a. Integral a => a -> Bool
Evidence bound by a type signature of the constraint type Integral a
even
{-# INLINABLE even #-}
{-# INLINABLE odd  #-}

-------------------------------------------------------
-- | raise a number to a non-negative integral power
{-# SPECIALISE [1] (^) ::
        Integer -> Integer -> Integer,
        Integer -> Int -> Integer,
        Int -> Int -> Int #-}
{-# INLINABLE [1] (^) #-}    -- See Note [Inlining (^)]
(^) :: (Num a, Integral b) => a -> b -> a
a
x0 ^ :: a -> b -> a
^ b
y0 | b
y0 b -> b -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral b
< b
0    = [Char] -> a
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"Negative exponent"
        | b
y0 b -> b -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type forall a. Ord a => Eq a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral b
== b
0   = a
1
        | Bool
otherwise = a -> b -> a
forall {a} {a}. (Integral a, Num a) => a -> a -> a
Evidence bound by a type signature of the constraint type Num a
Evidence bound by a type signature of the constraint type Integral b
f a
x0 b
y0
    where -- f : x0 ^ y0 = x ^ y
          f :: a -> a -> a
f a
x a
y | a -> Bool
forall a. Integral a => a -> Bool
Evidence bound by a type signature of the constraint type Integral a
even a
y    = a -> a -> a
f (a
x a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a type signature of the constraint type Num a
* a
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral a
`quot` a
2)
                | a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type forall a. Ord a => Eq a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
== a
1    = a
x
                | Bool
otherwise = a -> a -> a -> a
forall {a} {a}. (Integral a, Num a) => a -> a -> a -> a
Evidence bound by a type signature of the constraint type Num a
Evidence bound by a type signature of the constraint type Integral a
g (a
x a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a type signature of the constraint type Num a
* a
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral a
`quot` a
2) a
x         -- See Note [Half of y - 1]
          -- g : x0 ^ y0 = (x ^ y) * z
          g :: a -> a -> a -> a
g a
x a
y a
z | a -> Bool
forall a. Integral a => a -> Bool
Evidence bound by a type signature of the constraint type Integral a
even a
y = a -> a -> a -> a
g (a
x a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a type signature of the constraint type Num a
* a
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral a
`quot` a
2) a
z
                  | a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type forall a. Ord a => Eq a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
== a
1 = a
x a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a type signature of the constraint type Num a
* a
z
                  | Bool
otherwise = a -> a -> a -> a
g (a
x a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a type signature of the constraint type Num a
* a
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral a
`quot` a
2) (a
x a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a type signature of the constraint type Num a
* a
z) -- See Note [Half of y - 1]

-- | raise a number to an integral power
(^^)            :: (Fractional a, Integral b) => a -> b -> a
{-# INLINABLE [1] (^^) #-}         -- See Note [Inlining (^)
a
x ^^ :: a -> b -> a
^^ b
n          =  if b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral b
>= b
0 then a
xa -> b -> a
forall a b. (Num a, Integral b) => a -> b -> a
Evidence bound by a type signature of the constraint type Integral b
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
^b
n else a -> a
forall a. Fractional a => a -> a
Evidence bound by a type signature of the constraint type Fractional a
recip (a
xa -> b -> a
forall a b. (Num a, Integral b) => a -> b -> a
Evidence bound by a type signature of the constraint type Integral b
Evidence bound by a superclass of: Fractional of the constraint type forall a. Fractional a => Num a
Evidence bound by a type signature of the constraint type Fractional a
^(b -> b
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral b
negate b
n))

{- Note [Half of y - 1]
   ~~~~~~~~~~~~~~~~~~~~~
   Since y is guaranteed to be odd and positive here,
   half of y - 1 can be computed as y `quot` 2, optimising subtraction away.
-}

{- Note [Inlining (^)
   ~~~~~~~~~~~~~~~~~~~~~
   The INLINABLE pragma allows (^) to be specialised at its call sites.
   If it is called repeatedly at the same type, that can make a huge
   difference, because of those constants which can be repeatedly
   calculated.

   Currently the fromInteger calls are not floated because we get
             \d1 d2 x y -> blah
   after the gentle round of simplification. -}

{- Rules for powers with known small exponent
    see #5237
    For small exponents, (^) is inefficient compared to manually
    expanding the multiplication tree.
    Here, rules for the most common exponent types are given.
    The range of exponents for which rules are given is quite
    arbitrary and kept small to not unduly increase the number of rules.
    0 and 1 are excluded based on the assumption that nobody would
    write x^0 or x^1 in code and the cases where an exponent could
    be statically resolved to 0 or 1 are rare.

    It might be desirable to have corresponding rules also for
    exponents of other types (e. g., Word), but it's doubtful they
    would fire, since the exponents of other types tend to get
    floated out before the rule has a chance to fire.

    Also desirable would be rules for (^^), but I haven't managed
    to get those to fire.

    Note: Trying to save multiplications by sharing the square for
    exponents 4 and 5 does not save time, indeed, for Double, it is
    up to twice slower, so the rules contain flat sequences of
    multiplications.
-}

{-# RULES
"^2/Int"        forall x. x ^ (2 :: Int) = let u = x in u*u
"^3/Int"        forall x. x ^ (3 :: Int) = let u = x in u*u*u
"^4/Int"        forall x. x ^ (4 :: Int) = let u = x in u*u*u*u
"^5/Int"        forall x. x ^ (5 :: Int) = let u = x in u*u*u*u*u
"^2/Integer"    forall x. x ^ (2 :: Integer) = let u = x in u*u
"^3/Integer"    forall x. x ^ (3 :: Integer) = let u = x in u*u*u
"^4/Integer"    forall x. x ^ (4 :: Integer) = let u = x in u*u*u*u
"^5/Integer"    forall x. x ^ (5 :: Integer) = let u = x in u*u*u*u*u
  #-}

-------------------------------------------------------
-- Special power functions for Rational
--
-- see #4337
--
-- Rationale:
-- For a legitimate Rational (n :% d), the numerator and denominator are
-- coprime, i.e. they have no common prime factor.
-- Therefore all powers (n ^ a) and (d ^ b) are also coprime, so it is
-- not necessary to compute the greatest common divisor, which would be
-- done in the default implementation at each multiplication step.
-- Since exponentiation quickly leads to very large numbers and
-- calculation of gcds is generally very slow for large numbers,
-- avoiding the gcd leads to an order of magnitude speedup relatively
-- soon (and an asymptotic improvement overall).
--
-- Note:
-- We cannot use these functions for general Ratio a because that would
-- change results in a multitude of cases.
-- The cause is that if a and b are coprime, their remainders by any
-- positive modulus generally aren't, so in the default implementation
-- reduction occurs.
--
-- Example:
-- (17 % 3) ^ 3 :: Ratio Word8
-- Default:
-- (17 % 3) ^ 3 = ((17 % 3) ^ 2) * (17 % 3)
--              = ((289 `mod` 256) % 9) * (17 % 3)
--              = (33 % 9) * (17 % 3)
--              = (11 % 3) * (17 % 3)
--              = (187 % 9)
-- But:
-- ((17^3) `mod` 256) % (3^3)   = (4913 `mod` 256) % 27
--                              = 49 % 27
--
-- TODO:
-- Find out whether special-casing for numerator, denominator or
-- exponent = 1 (or -1, where that may apply) gains something.

-- Special version of (^) for Rational base
{-# RULES "(^)/Rational"    (^) = (^%^) #-}
(^%^)           :: Integral a => Rational -> a -> Rational
(Integer
n :% Integer
d) ^%^ :: Rational -> a -> Rational
^%^ a
e
    | a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
< a
0     = [Char] -> Rational
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"Negative exponent"
    | a
e a -> a -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type forall a. Ord a => Eq a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
== a
0    = Integer
1 Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
1
    | Bool
otherwise = (Integer
n Integer -> a -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
Evidence bound by a type signature of the constraint type Integral a
External instance of the constraint type Num Integer
^ a
e) Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% (Integer
d Integer -> a -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
Evidence bound by a type signature of the constraint type Integral a
External instance of the constraint type Num Integer
^ a
e)

-- Special version of (^^) for Rational base
{-# RULES "(^^)/Rational"   (^^) = (^^%^^) #-}
(^^%^^)         :: Integral a => Rational -> a -> Rational
(Integer
n :% Integer
d) ^^%^^ :: Rational -> a -> Rational
^^%^^ a
e
    | a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
> a
0     = (Integer
n Integer -> a -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
Evidence bound by a type signature of the constraint type Integral a
External instance of the constraint type Num Integer
^ a
e) Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% (Integer
d Integer -> a -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
Evidence bound by a type signature of the constraint type Integral a
External instance of the constraint type Num Integer
^ a
e)
    | a
e a -> a -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type forall a. Ord a => Eq a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Ord a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
== a
0    = Integer
1 Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
1
    | Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
External instance of the constraint type Ord Integer
> Integer
0     = (Integer
d Integer -> a -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
Evidence bound by a type signature of the constraint type Integral a
External instance of the constraint type Num Integer
^ (a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
negate a
e)) Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% (Integer
n Integer -> a -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
Evidence bound by a type signature of the constraint type Integral a
External instance of the constraint type Num Integer
^ (a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
negate a
e))
    | Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Integer
== Integer
0    = Rational
forall a. a
ratioZeroDenominatorError
    | Bool
otherwise = let nn :: Integer
nn = Integer
d Integer -> a -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
Evidence bound by a type signature of the constraint type Integral a
External instance of the constraint type Num Integer
^ (a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
negate a
e)
                      dd :: Integer
dd = (Integer -> Integer
forall a. Num a => a -> a
External instance of the constraint type Num Integer
negate Integer
n) Integer -> a -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
Evidence bound by a type signature of the constraint type Integral a
External instance of the constraint type Num Integer
^ (a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
negate a
e)
                  in if a -> Bool
forall a. Integral a => a -> Bool
Evidence bound by a type signature of the constraint type Integral a
even a
e then (Integer
nn Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
dd) else (Integer -> Integer
forall a. Num a => a -> a
External instance of the constraint type Num Integer
negate Integer
nn Integer -> Integer -> Rational
forall a. a -> a -> Ratio a
:% Integer
dd)

-------------------------------------------------------
-- | @'gcd' x y@ is the non-negative factor of both @x@ and @y@ of which
-- every common factor of @x@ and @y@ is also a factor; for example
-- @'gcd' 4 2 = 2@, @'gcd' (-4) 6 = 2@, @'gcd' 0 4@ = @4@. @'gcd' 0 0@ = @0@.
-- (That is, the common divisor that is \"greatest\" in the divisibility
-- preordering.)
--
-- Note: Since for signed fixed-width integer types, @'abs' 'minBound' < 0@,
-- the result may be negative if one of the arguments is @'minBound'@ (and
-- necessarily is if the other is @0@ or @'minBound'@) for such types.
gcd             :: (Integral a) => a -> a -> a
{-# NOINLINE [1] gcd #-}
gcd :: a -> a -> a
gcd a
x a
y         =  a -> a -> a
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral a
gcd' (a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
abs a
x) (a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
abs a
y)
                   where gcd' :: t -> t -> t
gcd' t
a t
0  =  t
a
                         gcd' t
a t
b  =  t -> t -> t
gcd' t
b (t
a t -> t -> t
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral t
`rem` t
b)

-- | @'lcm' x y@ is the smallest positive integer that both @x@ and @y@ divide.
lcm             :: (Integral a) => a -> a -> a
{-# SPECIALISE lcm :: Int -> Int -> Int #-}
{-# SPECIALISE lcm :: Word -> Word -> Word #-}
{-# NOINLINE [1] lcm #-}
lcm :: a -> a -> a
lcm a
_ a
0         =  a
0
lcm a
0 a
_         =  a
0
lcm a
x a
y         =  a -> a
forall a. Num a => a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
abs ((a
x a -> a -> a
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral a
`quot` (a -> a -> a
forall a. Integral a => a -> a -> a
Evidence bound by a type signature of the constraint type Integral a
gcd a
x a
y)) a -> a -> a
forall a. Num a => a -> a -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
* a
y)

{-# RULES
"gcd/Integer->Integer->Integer" gcd = gcdInteger
"lcm/Integer->Integer->Integer" lcm = lcmInteger
"gcd/Natural->Natural->Natural" gcd = gcdNatural
"lcm/Natural->Natural->Natural" lcm = lcmNatural
 #-}

#if defined(MIN_VERSION_integer_gmp)
-- GMP defines a more efficient Int# and Word# GCD

gcdInt' :: Int -> Int -> Int
gcdInt' :: Int -> Int -> Int
gcdInt' (I# Int#
x) (I# Int#
y) = Int# -> Int
I# (Int# -> Int# -> Int#
gcdInt Int#
x Int#
y)

gcdWord' :: Word -> Word -> Word
gcdWord' :: Word -> Word -> Word
gcdWord' (W# Word#
x) (W# Word#
y) = Word# -> Word
W# (Word# -> Word# -> Word#
gcdWord Word#
x Word#
y)

{-# RULES
"gcd/Int->Int->Int"             gcd = gcdInt'
"gcd/Word->Word->Word"          gcd = gcdWord'
 #-}

#endif

integralEnumFrom :: (Integral a, Bounded a) => a -> [a]
integralEnumFrom :: a -> [a]
integralEnumFrom a
n = (Integer -> a) -> [Integer] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> a
forall a. Num a => Integer -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
fromInteger [a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger a
n .. a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger (a
forall a. Bounded a => a
Evidence bound by a type signature of the constraint type Bounded a
maxBound a -> a -> a
forall a. a -> a -> a
`asTypeOf` a
n)]

integralEnumFromThen :: (Integral a, Bounded a) => a -> a -> [a]
integralEnumFromThen :: a -> a -> [a]
integralEnumFromThen a
n1 a
n2
  | Integer
i_n2 Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
External instance of the constraint type Ord Integer
>= Integer
i_n1  = (Integer -> a) -> [Integer] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> a
forall a. Num a => Integer -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
fromInteger [Integer
i_n1, Integer
i_n2 .. a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger (a
forall a. Bounded a => a
Evidence bound by a type signature of the constraint type Bounded a
maxBound a -> a -> a
forall a. a -> a -> a
`asTypeOf` a
n1)]
  | Bool
otherwise     = (Integer -> a) -> [Integer] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> a
forall a. Num a => Integer -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
fromInteger [Integer
i_n1, Integer
i_n2 .. a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger (a
forall a. Bounded a => a
Evidence bound by a type signature of the constraint type Bounded a
minBound a -> a -> a
forall a. a -> a -> a
`asTypeOf` a
n1)]
  where
    i_n1 :: Integer
i_n1 = a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger a
n1
    i_n2 :: Integer
i_n2 = a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger a
n2

integralEnumFromTo :: Integral a => a -> a -> [a]
integralEnumFromTo :: a -> a -> [a]
integralEnumFromTo a
n a
m = (Integer -> a) -> [Integer] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> a
forall a. Num a => Integer -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
fromInteger [a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger a
n .. a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger a
m]

integralEnumFromThenTo :: Integral a => a -> a -> a -> [a]
integralEnumFromThenTo :: a -> a -> a -> [a]
integralEnumFromThenTo a
n1 a
n2 a
m
  = (Integer -> a) -> [Integer] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> a
forall a. Num a => Integer -> a
Evidence bound by a superclass of: Real of the constraint type forall a. Real a => Num a
Evidence bound by a superclass of: Integral of the constraint type forall a. Integral a => Real a
Evidence bound by a type signature of the constraint type Integral a
fromInteger [a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger a
n1, a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger a
n2 .. a -> Integer
forall a. Integral a => a -> Integer
Evidence bound by a type signature of the constraint type Integral a
toInteger a
m]