{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP, NoImplicitPrelude, MagicHash, UnboxedTuples, BangPatterns #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_HADDOCK not-home #-}
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 )
import GHC.Num.BigNat (gcdInt,gcdWord)
infixr 8 ^, ^^
infixl 7 /, `quot`, `rem`, `div`, `mod`
infixl 7 %
default ()
{-# NOINLINE divZeroError #-}
divZeroError :: a
divZeroError :: forall a. a
divZeroError = forall b a. b -> a
raise# SomeException
divZeroException
{-# NOINLINE ratioZeroDenominatorError #-}
ratioZeroDenominatorError :: a
ratioZeroDenominatorError :: forall a. a
ratioZeroDenominatorError = forall b a. b -> a
raise# SomeException
ratioZeroDenomException
{-# NOINLINE overflowError #-}
overflowError :: a
overflowError :: forall a. a
overflowError = forall b a. b -> a
raise# SomeException
overflowException
{-# NOINLINE underflowError #-}
underflowError :: a
underflowError :: forall a. a
underflowError = forall b a. b -> a
raise# SomeException
underflowException
data Ratio a = !a :% !a deriving Ratio a -> Ratio a -> Bool
forall a. Eq a => Ratio a -> Ratio a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Ratio a -> Ratio a -> Bool
$c/= :: forall a. Eq a => Ratio a -> Ratio a -> Bool
== :: Ratio a -> Ratio a -> Bool
$c== :: forall a. Eq a => Ratio a -> Ratio a -> Bool
Eq
type Rational = Ratio Integer
ratioPrec, ratioPrec1 :: Int
ratioPrec :: Int
ratioPrec = Int
7
ratioPrec1 :: Int
ratioPrec1 = Int
ratioPrec forall a. Num a => a -> a -> a
+ Int
1
infinity, notANumber :: Rational
infinity :: Rational
infinity = Integer
1 forall a. a -> a -> Ratio a
:% Integer
0
notANumber :: Rational
notANumber = Integer
0 forall a. a -> a -> Ratio a
:% Integer
0
{-# SPECIALISE (%) :: Integer -> Integer -> Rational #-}
(%) :: (Integral a) => a -> a -> Ratio a
numerator :: Ratio a -> a
denominator :: Ratio a -> a
reduce :: (Integral a) => a -> a -> Ratio a
{-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
reduce :: forall a. Integral a => a -> a -> Ratio a
reduce a
_ a
0 = forall a. a
ratioZeroDenominatorError
reduce a
x a
y = (a
x forall a. Integral a => a -> a -> a
`quot` a
d) forall a. a -> a -> Ratio a
:% (a
y forall a. Integral a => a -> a -> a
`quot` a
d)
where d :: a
d = forall a. Integral a => a -> a -> a
gcd a
x a
y
a
x % :: forall a. Integral a => a -> a -> Ratio a
% a
y = forall a. Integral a => a -> a -> Ratio a
reduce (a
x forall a. Num a => a -> a -> a
* forall a. Num a => a -> a
signum a
y) (forall a. Num a => a -> a
abs a
y)
numerator :: forall a. Ratio a -> a
numerator (a
x :% a
_) = a
x
denominator :: forall a. Ratio a -> a
denominator (a
_ :% a
y) = a
y
class (Num a, Ord a) => Real a where
toRational :: a -> Rational
class (Real a, Enum a) => Integral a where
quot :: a -> a -> a
rem :: a -> a -> a
div :: a -> a -> a
mod :: a -> a -> a
quotRem :: a -> a -> (a,a)
divMod :: a -> a -> (a,a)
toInteger :: a -> Integer
{-# INLINE quot #-}
{-# INLINE rem #-}
{-# INLINE div #-}
{-# INLINE mod #-}
a
n `quot` a
d = a
q where (a
q,a
_) = forall a. Integral a => a -> a -> (a, a)
quotRem a
n a
d
a
n `rem` a
d = a
r where (a
_,a
r) = forall a. Integral a => a -> a -> (a, a)
quotRem a
n a
d
a
n `div` a
d = a
q where (a
q,a
_) = forall a. Integral a => a -> a -> (a, a)
divMod a
n a
d
a
n `mod` a
d = a
r where (a
_,a
r) = forall a. Integral a => a -> a -> (a, a)
divMod a
n a
d
divMod a
n a
d = if forall a. Num a => a -> a
signum a
r forall a. Eq a => a -> a -> Bool
== forall a. Num a => a -> a
negate (forall a. Num a => a -> a
signum a
d) then (a
qforall a. Num a => a -> a -> a
-a
1, a
rforall a. Num a => a -> a -> a
+a
d) else (a, a)
qr
where qr :: (a, a)
qr@(a
q,a
r) = forall a. Integral a => a -> a -> (a, a)
quotRem a
n a
d
class (Num a) => Fractional a where
{-# MINIMAL fromRational, (recip | (/)) #-}
(/) :: a -> a -> a
recip :: a -> a
fromRational :: Rational -> a
{-# INLINE recip #-}
{-# INLINE (/) #-}
recip a
x = a
1 forall a. Fractional a => a -> a -> a
/ a
x
a
x / a
y = a
x forall a. Num a => a -> a -> a
* forall a. Fractional a => a -> a
recip a
y
class (Real a, Fractional a) => RealFrac a where
properFraction :: (Integral b) => a -> (b,a)
truncate :: (Integral b) => a -> b
round :: (Integral b) => a -> b
ceiling :: (Integral b) => a -> b
floor :: (Integral b) => a -> b
{-# INLINE truncate #-}
truncate a
x = b
m where (b
m,a
_) = forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction a
x
round a
x = let (b
n,a
r) = forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction a
x
m :: b
m = if a
r forall a. Ord a => a -> a -> Bool
< a
0 then b
n forall a. Num a => a -> a -> a
- b
1 else b
n forall a. Num a => a -> a -> a
+ b
1
in case forall a. Num a => a -> a
signum (forall a. Num a => a -> a
abs a
r forall a. Num a => a -> a -> a
- a
0.5) of
-1 -> b
n
a
0 -> if forall a. Integral a => a -> Bool
even b
n then b
n else b
m
a
1 -> b
m
a
_ -> forall a. [Char] -> a
errorWithoutStackTrace [Char]
"round default defn: Bad value"
ceiling a
x = if a
r forall a. Ord a => a -> a -> Bool
> a
0 then b
n forall a. Num a => a -> a -> a
+ b
1 else b
n
where (b
n,a
r) = forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction a
x
floor a
x = if a
r forall a. Ord a => a -> a -> Bool
< a
0 then b
n forall a. Num a => a -> a -> a
- b
1 else b
n
where (b
n,a
r) = forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction a
x
numericEnumFrom :: (Fractional a) => a -> [a]
numericEnumFrom :: forall a. Fractional a => a -> [a]
numericEnumFrom a
n = a -> [a]
go a
0
where
go :: a -> [a]
go !a
k = let !n' :: a
n' = a
n forall a. Num a => a -> a -> a
+ a
k
in a
n' forall a. a -> [a] -> [a]
: a -> [a]
go (a
k forall a. Num a => a -> a -> a
+ a
1)
numericEnumFromThen :: (Fractional a) => a -> a -> [a]
numericEnumFromThen :: forall a. Fractional a => a -> a -> [a]
numericEnumFromThen a
n a
m = a -> [a]
go a
0
where
step :: a
step = a
m forall a. Num a => a -> a -> a
- a
n
go :: a -> [a]
go !a
k = let !n' :: a
n' = a
n forall a. Num a => a -> a -> a
+ a
k forall a. Num a => a -> a -> a
* a
step
in a
n' forall a. a -> [a] -> [a]
: a -> [a]
go (a
k forall a. Num a => a -> a -> a
+ a
1)
numericEnumFromTo :: (Ord a, Fractional a) => a -> a -> [a]
numericEnumFromTo :: forall a. (Ord a, Fractional a) => a -> a -> [a]
numericEnumFromTo a
n a
m = forall a. (a -> Bool) -> [a] -> [a]
takeWhile (forall a. Ord a => a -> a -> Bool
<= a
m forall a. Num a => a -> a -> a
+ a
1forall a. Fractional a => a -> a -> a
/a
2) (forall a. Fractional a => a -> [a]
numericEnumFrom a
n)
numericEnumFromThenTo :: (Ord a, Fractional a) => a -> a -> a -> [a]
numericEnumFromThenTo :: forall a. (Ord a, Fractional a) => a -> a -> a -> [a]
numericEnumFromThenTo a
e1 a
e2 a
e3
= forall a. (a -> Bool) -> [a] -> [a]
takeWhile a -> Bool
predicate (forall a. Fractional a => a -> a -> [a]
numericEnumFromThen a
e1 a
e2)
where
mid :: a
mid = (a
e2 forall a. Num a => a -> a -> a
- a
e1) forall a. Fractional a => a -> a -> a
/ a
2
predicate :: a -> Bool
predicate | a
e2 forall a. Ord a => a -> a -> Bool
>= a
e1 = (forall a. Ord a => a -> a -> Bool
<= a
e3 forall a. Num a => a -> a -> a
+ a
mid)
| Bool
otherwise = (forall a. Ord a => a -> a -> Bool
>= a
e3 forall a. Num a => a -> a -> a
+ a
mid)
instance Real Int where
toRational :: Int -> Rational
toRational Int
x = forall a. Integral a => a -> Integer
toInteger Int
x forall a. a -> a -> Ratio a
:% Integer
1
instance Integral Int where
toInteger :: Int -> Integer
toInteger (I# Int#
i) = Int# -> Integer
IS Int#
i
Int
a quot :: Int -> Int -> Int
`quot` Int
b
| Int
b forall a. Eq a => a -> a -> Bool
== Int
0 = forall a. a
divZeroError
| Int
b forall a. Eq a => a -> a -> Bool
== (-Int
1) Bool -> Bool -> Bool
&& Int
a forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
minBound = forall a. a
overflowError
| Bool
otherwise = Int
a Int -> Int -> Int
`quotInt` Int
b
!Int
a rem :: Int -> Int -> Int
`rem` Int
b
| Int
b forall a. Eq a => a -> a -> Bool
== Int
0 = forall a. a
divZeroError
| Int
b forall a. Eq a => a -> a -> Bool
== (-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 forall a. Eq a => a -> a -> Bool
== Int
0 = forall a. a
divZeroError
| Int
b forall a. Eq a => a -> a -> Bool
== (-Int
1) Bool -> Bool -> Bool
&& Int
a forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
minBound = forall a. a
overflowError
| Bool
otherwise = Int
a Int -> Int -> Int
`divInt` Int
b
!Int
a mod :: Int -> Int -> Int
`mod` Int
b
| Int
b forall a. Eq a => a -> a -> Bool
== Int
0 = forall a. a
divZeroError
| Int
b forall a. Eq a => a -> a -> Bool
== (-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 forall a. Eq a => a -> a -> Bool
== Int
0 = forall a. a
divZeroError
| Int
b forall a. Eq a => a -> a -> Bool
== (-Int
1) Bool -> Bool -> Bool
&& Int
a forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
minBound = (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 forall a. Eq a => a -> a -> Bool
== Int
0 = forall a. a
divZeroError
| Int
b forall a. Eq a => a -> a -> Bool
== (-Int
1) Bool -> Bool -> Bool
&& Int
a forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
minBound = (forall a. a
overflowError, Int
0)
| Bool
otherwise = Int
a Int -> Int -> (Int, Int)
`divModInt` Int
b
instance Real Word where
toRational :: Word -> Rational
toRational Word
x = forall a. Integral a => a -> Integer
toInteger Word
x forall a. Integral a => a -> a -> Ratio a
% Integer
1
instance Integral Word where
quot :: Word -> Word -> Word
quot (W# Word#
x#) y :: Word
y@(W# Word#
y#)
| Word
y forall a. Eq a => a -> a -> Bool
/= Word
0 = Word# -> Word
W# (Word#
x# Word# -> Word# -> Word#
`quotWord#` Word#
y#)
| Bool
otherwise = forall a. a
divZeroError
rem :: Word -> Word -> Word
rem (W# Word#
x#) y :: Word
y@(W# Word#
y#)
| Word
y forall a. Eq a => a -> a -> Bool
/= Word
0 = Word# -> Word
W# (Word#
x# Word# -> Word# -> Word#
`remWord#` Word#
y#)
| Bool
otherwise = forall a. a
divZeroError
div :: Word -> Word -> Word
div (W# Word#
x#) y :: Word
y@(W# Word#
y#)
| Word
y forall a. Eq a => a -> a -> Bool
/= Word
0 = Word# -> Word
W# (Word#
x# Word# -> Word# -> Word#
`quotWord#` Word#
y#)
| Bool
otherwise = forall a. a
divZeroError
mod :: Word -> Word -> Word
mod (W# Word#
x#) y :: Word
y@(W# Word#
y#)
| Word
y forall a. Eq a => a -> a -> Bool
/= Word
0 = Word# -> Word
W# (Word#
x# Word# -> Word# -> Word#
`remWord#` Word#
y#)
| Bool
otherwise = forall a. a
divZeroError
quotRem :: Word -> Word -> (Word, Word)
quotRem (W# Word#
x#) y :: Word
y@(W# Word#
y#)
| Word
y forall a. Eq a => a -> a -> Bool
/= 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 = forall a. a
divZeroError
divMod :: Word -> Word -> (Word, Word)
divMod (W# Word#
x#) y :: Word
y@(W# Word#
y#)
| Word
y forall a. Eq a => a -> a -> Bool
/= 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 = forall a. a
divZeroError
toInteger :: Word -> Integer
toInteger (W# Word#
x#) = Word# -> Integer
integerFromWord# Word#
x#
instance Real Integer where
toRational :: Integer -> Rational
toRational Integer
x = Integer
x forall a. a -> a -> Ratio a
:% Integer
1
instance Real Natural where
toRational :: Natural -> Rational
toRational Natural
n = Natural -> Integer
integerFromNatural Natural
n forall a. a -> a -> Ratio a
:% Integer
1
instance Integral Integer where
toInteger :: Integer -> Integer
toInteger Integer
n = Integer
n
{-# INLINE quot #-}
Integer
_ quot :: Integer -> Integer -> Integer
`quot` Integer
0 = forall a. a
divZeroError
Integer
n `quot` Integer
d = Integer
n Integer -> Integer -> Integer
`integerQuot` Integer
d
{-# INLINE rem #-}
Integer
_ rem :: Integer -> Integer -> Integer
`rem` Integer
0 = forall a. a
divZeroError
Integer
n `rem` Integer
d = Integer
n Integer -> Integer -> Integer
`integerRem` Integer
d
{-# INLINE div #-}
Integer
_ div :: Integer -> Integer -> Integer
`div` Integer
0 = forall a. a
divZeroError
Integer
n `div` Integer
d = Integer
n Integer -> Integer -> Integer
`integerDiv` Integer
d
{-# INLINE mod #-}
Integer
_ mod :: Integer -> Integer -> Integer
`mod` Integer
0 = forall a. a
divZeroError
Integer
n `mod` Integer
d = Integer
n Integer -> Integer -> Integer
`integerMod` Integer
d
{-# INLINE divMod #-}
Integer
_ divMod :: Integer -> Integer -> (Integer, Integer)
`divMod` Integer
0 = forall a. a
divZeroError
Integer
n `divMod` Integer
d = Integer
n Integer -> Integer -> (Integer, Integer)
`integerDivMod` Integer
d
{-# INLINE quotRem #-}
Integer
_ quotRem :: Integer -> Integer -> (Integer, Integer)
`quotRem` Integer
0 = forall a. a
divZeroError
Integer
n `quotRem` Integer
d = Integer
n Integer -> Integer -> (Integer, Integer)
`integerQuotRem` Integer
d
instance Integral Natural where
toInteger :: Natural -> Integer
toInteger Natural
x = Natural -> Integer
integerFromNatural Natural
x
{-# INLINE quot #-}
Natural
_ quot :: Natural -> Natural -> Natural
`quot` Natural
0 = forall a. a
divZeroError
Natural
n `quot` Natural
d = Natural
n Natural -> Natural -> Natural
`naturalQuot` Natural
d
{-# INLINE rem #-}
Natural
_ rem :: Natural -> Natural -> Natural
`rem` Natural
0 = forall a. a
divZeroError
Natural
n `rem` Natural
d = Natural
n Natural -> Natural -> Natural
`naturalRem` Natural
d
{-# INLINE div #-}
Natural
_ div :: Natural -> Natural -> Natural
`div` Natural
0 = forall a. a
divZeroError
Natural
n `div` Natural
d = Natural
n Natural -> Natural -> Natural
`naturalQuot` Natural
d
{-# INLINE mod #-}
Natural
_ mod :: Natural -> Natural -> Natural
`mod` Natural
0 = forall a. a
divZeroError
Natural
n `mod` Natural
d = Natural
n Natural -> Natural -> Natural
`naturalRem` Natural
d
{-# INLINE divMod #-}
Natural
_ divMod :: Natural -> Natural -> (Natural, Natural)
`divMod` Natural
0 = forall a. a
divZeroError
Natural
n `divMod` Natural
d = Natural
n Natural -> Natural -> (Natural, Natural)
`naturalQuotRem` Natural
d
{-# INLINE quotRem #-}
Natural
_ quotRem :: Natural -> Natural -> (Natural, Natural)
`quotRem` Natural
0 = forall a. a
divZeroError
Natural
n `quotRem` Natural
d = Natural
n Natural -> Natural -> (Natural, Natural)
`naturalQuotRem` Natural
d
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 forall a. Num a => a -> a -> a
* a
y' forall a. Ord a => a -> a -> Bool
<= a
x' forall a. Num a => a -> a -> a
* a
y
(a
x:%a
y) < :: Ratio a -> Ratio a -> Bool
< (a
x':%a
y') = a
x forall a. Num a => a -> a -> a
* a
y' forall a. Ord a => a -> a -> Bool
< a
x' forall a. Num a => a -> a -> a
* a
y
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') = forall a. Integral a => a -> a -> Ratio a
reduce (a
xforall a. Num a => a -> a -> a
*a
y' forall a. Num a => a -> a -> a
+ a
x'forall a. Num a => a -> a -> a
*a
y) (a
yforall a. Num a => a -> a -> a
*a
y')
(a
x:%a
y) - :: Ratio a -> Ratio a -> Ratio a
- (a
x':%a
y') = forall a. Integral a => a -> a -> Ratio a
reduce (a
xforall a. Num a => a -> a -> a
*a
y' forall a. Num a => a -> a -> a
- a
x'forall a. Num a => a -> a -> a
*a
y) (a
yforall a. Num a => a -> a -> a
*a
y')
(a
x:%a
y) * :: Ratio a -> Ratio a -> Ratio a
* (a
x':%a
y') = forall a. Integral a => a -> a -> Ratio a
reduce (a
x forall a. Num a => a -> a -> a
* a
x') (a
y forall a. Num a => a -> a -> a
* a
y')
negate :: Ratio a -> Ratio a
negate (a
x:%a
y) = (-a
x) forall a. a -> a -> Ratio a
:% a
y
abs :: Ratio a -> Ratio a
abs (a
x:%a
y) = forall a. Num a => a -> a
abs a
x forall a. a -> a -> Ratio a
:% a
y
signum :: Ratio a -> Ratio a
signum (a
x:%a
_) = forall a. Num a => a -> a
signum a
x forall a. a -> a -> Ratio a
:% a
1
fromInteger :: Integer -> Ratio a
fromInteger Integer
x = forall a. Num a => Integer -> a
fromInteger Integer
x forall a. a -> a -> Ratio a
:% a
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
xforall a. Num a => a -> a -> a
*a
y') forall a. Integral a => a -> a -> Ratio a
% (a
yforall a. Num a => a -> a -> a
*a
x')
recip :: Ratio a -> Ratio a
recip (a
0:%a
_) = forall a. a
ratioZeroDenominatorError
recip (a
x:%a
y)
| a
x forall a. Ord a => a -> a -> Bool
< a
0 = forall a. Num a => a -> a
negate a
y forall a. a -> a -> Ratio a
:% forall a. Num a => a -> a
negate a
x
| Bool
otherwise = a
y forall a. a -> a -> Ratio a
:% a
x
fromRational :: Rational -> Ratio a
fromRational (Integer
x:%Integer
y) = forall a. Num a => Integer -> a
fromInteger Integer
x forall a. Integral a => a -> a -> Ratio a
% forall a. Num a => Integer -> a
fromInteger Integer
y
instance (Integral a) => Real (Ratio a) where
{-# SPECIALIZE instance Real Rational #-}
toRational :: Ratio a -> Rational
toRational (a
x:%a
y) = forall a. Integral a => a -> Integer
toInteger a
x forall a. a -> a -> Ratio a
:% forall a. Integral a => a -> Integer
toInteger a
y
instance (Integral a) => RealFrac (Ratio a) where
{-# SPECIALIZE instance RealFrac Rational #-}
properFraction :: forall b. Integral b => Ratio a -> (b, Ratio a)
properFraction (a
x:%a
y) = (forall a. Num a => Integer -> a
fromInteger (forall a. Integral a => a -> Integer
toInteger a
q), a
rforall a. a -> a -> Ratio a
:%a
y)
where (a
q,a
r) = forall a. Integral a => a -> a -> (a, a)
quotRem a
x a
y
round :: forall b. Integral b => Ratio a -> b
round Ratio a
r =
let
(b
n, Ratio a
f) = forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction Ratio a
r
x :: b
x = if Ratio a
r forall a. Ord a => a -> a -> Bool
< Ratio a
0 then -b
1 else b
1
in
case (forall a. Ord a => a -> a -> Ordering
compare (forall a. Num a => a -> a
abs Ratio a
f) Ratio a
0.5, forall a. Integral a => a -> Bool
odd b
n) of
(Ordering
LT, Bool
_) -> b
n
(Ordering
EQ, Bool
False) -> b
n
(Ordering
EQ, Bool
True) -> b
n forall a. Num a => a -> a -> a
+ b
x
(Ordering
GT, Bool
_) -> b
n forall a. Num a => a -> a -> a
+ b
x
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 forall a. Ord a => a -> a -> Bool
> Int
ratioPrec) forall a b. (a -> b) -> a -> b
$
forall a. Show a => Int -> a -> ShowS
showsPrec Int
ratioPrec1 a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> ShowS
showString [Char]
" % " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a. Show a => Int -> a -> ShowS
showsPrec Int
ratioPrec1 a
y
instance (Integral a) => Enum (Ratio a) where
{-# SPECIALIZE instance Enum Rational #-}
succ :: Ratio a -> Ratio a
succ Ratio a
x = Ratio a
x forall a. Num a => a -> a -> a
+ Ratio a
1
pred :: Ratio a -> Ratio a
pred Ratio a
x = Ratio a
x forall a. Num a => a -> a -> a
- Ratio a
1
toEnum :: Int -> Ratio a
toEnum Int
n = forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n forall a. a -> a -> Ratio a
:% a
1
fromEnum :: Ratio a -> Int
fromEnum = forall a. Num a => Integer -> a
fromInteger forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (RealFrac a, Integral b) => a -> b
truncate
enumFrom :: Ratio a -> [Ratio a]
enumFrom = forall a. Fractional a => a -> [a]
numericEnumFrom
enumFromThen :: Ratio a -> Ratio a -> [Ratio a]
enumFromThen = forall a. Fractional a => a -> a -> [a]
numericEnumFromThen
enumFromTo :: Ratio a -> Ratio a -> [Ratio a]
enumFromTo = forall a. (Ord a, Fractional a) => a -> a -> [a]
numericEnumFromTo
enumFromThenTo :: Ratio a -> Ratio a -> Ratio a -> [Ratio a]
enumFromThenTo = forall a. (Ord a, Fractional a) => a -> a -> a -> [a]
numericEnumFromThenTo
{-# INLINE fromIntegral #-}
fromIntegral :: (Integral a, Num b) => a -> b
fromIntegral :: forall a b. (Integral a, Num b) => a -> b
fromIntegral = forall a. Num a => Integer -> a
fromInteger forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
toInteger
realToFrac :: (Real a, Fractional b) => a -> b
{-# NOINLINE [1] realToFrac #-}
realToFrac :: forall a b. (Real a, Fractional b) => a -> b
realToFrac = forall a. Fractional a => Rational -> a
fromRational forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Real a => a -> Rational
toRational
showSigned :: (Real a)
=> (a -> ShowS)
-> Int
-> a
-> ShowS
showSigned :: forall a. Real a => (a -> ShowS) -> Int -> a -> ShowS
showSigned a -> ShowS
showPos Int
p a
x
| a
x forall a. Ord a => a -> a -> Bool
< a
0 = Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
6) (Char -> ShowS
showChar Char
'-' 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 :: forall a. Integral a => a -> Bool
even a
n = a
n forall a. Integral a => a -> a -> a
`rem` a
2 forall a. Eq a => a -> a -> Bool
== a
0
odd :: forall a. Integral a => a -> Bool
odd = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Bool
even
{-# INLINABLE even #-}
{-# INLINABLE odd #-}
{-# SPECIALISE [1] (^) ::
Integer -> Integer -> Integer,
Integer -> Int -> Integer,
Int -> Int -> Int #-}
{-# INLINABLE [1] (^) #-}
(^) :: (Num a, Integral b) => a -> b -> a
a
x0 ^ :: forall a b. (Num a, Integral b) => a -> b -> a
^ b
y0 | b
y0 forall a. Ord a => a -> a -> Bool
< b
0 = forall a. [Char] -> a
errorWithoutStackTrace [Char]
"Negative exponent"
| b
y0 forall a. Eq a => a -> a -> Bool
== b
0 = a
1
| Bool
otherwise = forall {a} {t}. (Integral a, Num t) => t -> a -> t
f a
x0 b
y0
where
f :: t -> a -> t
f t
x a
y | forall a. Integral a => a -> Bool
even a
y = t -> a -> t
f (t
x forall a. Num a => a -> a -> a
* t
x) (a
y forall a. Integral a => a -> a -> a
`quot` a
2)
| a
y forall a. Eq a => a -> a -> Bool
== a
1 = t
x
| Bool
otherwise = forall {a} {t}. (Integral a, Num t) => t -> a -> t -> t
g (t
x forall a. Num a => a -> a -> a
* t
x) (a
y forall a. Integral a => a -> a -> a
`quot` a
2) t
x
g :: t -> a -> t -> t
g t
x a
y t
z | forall a. Integral a => a -> Bool
even a
y = t -> a -> t -> t
g (t
x forall a. Num a => a -> a -> a
* t
x) (a
y forall a. Integral a => a -> a -> a
`quot` a
2) t
z
| a
y forall a. Eq a => a -> a -> Bool
== a
1 = t
x forall a. Num a => a -> a -> a
* t
z
| Bool
otherwise = t -> a -> t -> t
g (t
x forall a. Num a => a -> a -> a
* t
x) (a
y forall a. Integral a => a -> a -> a
`quot` a
2) (t
x forall a. Num a => a -> a -> a
* t
z)
(^^) :: (Fractional a, Integral b) => a -> b -> a
{-# INLINABLE [1] (^^) #-}
a
x ^^ :: forall a b. (Fractional a, Integral b) => a -> b -> a
^^ b
n = if b
n forall a. Ord a => a -> a -> Bool
>= b
0 then a
xforall a b. (Num a, Integral b) => a -> b -> a
^b
n else forall a. Fractional a => a -> a
recip (a
xforall a b. (Num a, Integral b) => a -> b -> a
^(forall a. Num a => a -> a
negate b
n))
{-# 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
#-}
{-# RULES "(^)/Rational" (^) = (^%^) #-}
(^%^) :: Integral a => Rational -> a -> Rational
(Integer
n :% Integer
d) ^%^ :: forall a. Integral a => Rational -> a -> Rational
^%^ a
e
| a
e forall a. Ord a => a -> a -> Bool
< a
0 = forall a. [Char] -> a
errorWithoutStackTrace [Char]
"Negative exponent"
| a
e forall a. Eq a => a -> a -> Bool
== a
0 = Integer
1 forall a. a -> a -> Ratio a
:% Integer
1
| Bool
otherwise = (Integer
n forall a b. (Num a, Integral b) => a -> b -> a
^ a
e) forall a. a -> a -> Ratio a
:% (Integer
d forall a b. (Num a, Integral b) => a -> b -> a
^ a
e)
{-# RULES "(^^)/Rational" (^^) = (^^%^^) #-}
(^^%^^) :: Integral a => Rational -> a -> Rational
(Integer
n :% Integer
d) ^^%^^ :: forall a. Integral a => Rational -> a -> Rational
^^%^^ a
e
| a
e forall a. Ord a => a -> a -> Bool
> a
0 = (Integer
n forall a b. (Num a, Integral b) => a -> b -> a
^ a
e) forall a. a -> a -> Ratio a
:% (Integer
d forall a b. (Num a, Integral b) => a -> b -> a
^ a
e)
| a
e forall a. Eq a => a -> a -> Bool
== a
0 = Integer
1 forall a. a -> a -> Ratio a
:% Integer
1
| Integer
n forall a. Ord a => a -> a -> Bool
> Integer
0 = (Integer
d forall a b. (Num a, Integral b) => a -> b -> a
^ (forall a. Num a => a -> a
negate a
e)) forall a. a -> a -> Ratio a
:% (Integer
n forall a b. (Num a, Integral b) => a -> b -> a
^ (forall a. Num a => a -> a
negate a
e))
| Integer
n forall a. Eq a => a -> a -> Bool
== Integer
0 = forall a. a
ratioZeroDenominatorError
| Bool
otherwise = let nn :: Integer
nn = Integer
d forall a b. (Num a, Integral b) => a -> b -> a
^ (forall a. Num a => a -> a
negate a
e)
dd :: Integer
dd = (forall a. Num a => a -> a
negate Integer
n) forall a b. (Num a, Integral b) => a -> b -> a
^ (forall a. Num a => a -> a
negate a
e)
in if forall a. Integral a => a -> Bool
even a
e then (Integer
nn forall a. a -> a -> Ratio a
:% Integer
dd) else (forall a. Num a => a -> a
negate Integer
nn forall a. a -> a -> Ratio a
:% Integer
dd)
gcd :: (Integral a) => a -> a -> a
{-# NOINLINE [1] gcd #-}
gcd :: forall a. Integral a => a -> a -> a
gcd a
x a
y = forall a. Integral a => a -> a -> a
gcd' (forall a. Num a => a -> a
abs a
x) (forall a. Num a => a -> 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 forall a. Integral a => a -> a -> a
`rem` t
b)
lcm :: (Integral a) => a -> a -> a
{-# SPECIALISE lcm :: Int -> Int -> Int #-}
{-# SPECIALISE lcm :: Word -> Word -> Word #-}
{-# NOINLINE [1] lcm #-}
lcm :: forall a. Integral a => a -> a -> a
lcm a
_ a
0 = a
0
lcm a
0 a
_ = a
0
lcm a
x a
y = forall a. Num a => a -> a
abs ((a
x forall a. Integral a => a -> a -> a
`quot` (forall a. Integral a => a -> a -> a
gcd a
x a
y)) forall a. Num a => a -> a -> a
* a
y)
{-# RULES
"gcd/Integer->Integer->Integer" gcd = integerGcd
"lcm/Integer->Integer->Integer" lcm = integerLcm
"gcd/Natural->Natural->Natural" gcd = naturalGcd
"lcm/Natural->Natural->Natural" lcm = naturalLcm
#-}
{-# RULES
"gcd/Int->Int->Int" gcd = gcdInt
"gcd/Word->Word->Word" gcd = gcdWord
#-}
{-# INLINABLE integralEnumFrom #-}
integralEnumFrom :: (Integral a, Bounded a) => a -> [a]
integralEnumFrom :: forall a. (Integral a, Bounded a) => a -> [a]
integralEnumFrom a
n = forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger [forall a. Integral a => a -> Integer
toInteger a
n .. forall a. Integral a => a -> Integer
toInteger (forall a. Bounded a => a
maxBound forall a. a -> a -> a
`asTypeOf` a
n)]
{-# INLINABLE integralEnumFromThen #-}
integralEnumFromThen :: (Integral a, Bounded a) => a -> a -> [a]
integralEnumFromThen :: forall a. (Integral a, Bounded a) => a -> a -> [a]
integralEnumFromThen a
n1 a
n2
| Integer
i_n2 forall a. Ord a => a -> a -> Bool
>= Integer
i_n1 = forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger [Integer
i_n1, Integer
i_n2 .. forall a. Integral a => a -> Integer
toInteger (forall a. Bounded a => a
maxBound forall a. a -> a -> a
`asTypeOf` a
n1)]
| Bool
otherwise = forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger [Integer
i_n1, Integer
i_n2 .. forall a. Integral a => a -> Integer
toInteger (forall a. Bounded a => a
minBound forall a. a -> a -> a
`asTypeOf` a
n1)]
where
i_n1 :: Integer
i_n1 = forall a. Integral a => a -> Integer
toInteger a
n1
i_n2 :: Integer
i_n2 = forall a. Integral a => a -> Integer
toInteger a
n2
{-# INLINABLE integralEnumFromTo #-}
integralEnumFromTo :: Integral a => a -> a -> [a]
integralEnumFromTo :: forall a. Integral a => a -> a -> [a]
integralEnumFromTo a
n a
m = forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger [forall a. Integral a => a -> Integer
toInteger a
n .. forall a. Integral a => a -> Integer
toInteger a
m]
{-# INLINABLE integralEnumFromThenTo #-}
integralEnumFromThenTo :: Integral a => a -> a -> a -> [a]
integralEnumFromThenTo :: forall a. Integral a => a -> a -> a -> [a]
integralEnumFromThenTo a
n1 a
n2 a
m
= forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger [forall a. Integral a => a -> Integer
toInteger a
n1, forall a. Integral a => a -> Integer
toInteger a
n2 .. forall a. Integral a => a -> Integer
toInteger a
m]
data FractionalExponentBase
= Base2
| Base10
deriving (Int -> FractionalExponentBase -> ShowS
[FractionalExponentBase] -> ShowS
FractionalExponentBase -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [FractionalExponentBase] -> ShowS
$cshowList :: [FractionalExponentBase] -> ShowS
show :: FractionalExponentBase -> [Char]
$cshow :: FractionalExponentBase -> [Char]
showsPrec :: Int -> FractionalExponentBase -> ShowS
$cshowsPrec :: Int -> FractionalExponentBase -> ShowS
Show)
mkRationalBase2 :: Rational -> Integer -> Rational
mkRationalBase2 :: Rational -> Integer -> Rational
mkRationalBase2 Rational
r Integer
e = Rational -> Integer -> FractionalExponentBase -> Rational
mkRationalWithExponentBase Rational
r Integer
e FractionalExponentBase
Base2
mkRationalBase10 :: Rational -> Integer -> Rational
mkRationalBase10 :: Rational -> Integer -> Rational
mkRationalBase10 Rational
r Integer
e = Rational -> Integer -> FractionalExponentBase -> Rational
mkRationalWithExponentBase Rational
r Integer
e FractionalExponentBase
Base10
mkRationalWithExponentBase :: Rational -> Integer
-> FractionalExponentBase -> Rational
mkRationalWithExponentBase :: Rational -> Integer -> FractionalExponentBase -> Rational
mkRationalWithExponentBase Rational
r Integer
e FractionalExponentBase
feb = Rational
r forall a. Num a => a -> a -> a
* (Rational
eb forall a b. (Fractional a, Integral b) => a -> b -> a
^^ Integer
e)
where eb :: Rational
eb = case FractionalExponentBase
feb of FractionalExponentBase
Base2 -> Rational
2 ; FractionalExponentBase
Base10 -> Rational
10