module NumHask.Data.Integral
( Integral (..),
DivMod (..),
QuotRem (..),
ToIntegral (..),
ToInt,
FromIntegral (..),
FromInt,
FromInteger (..),
even,
odd,
(^^),
(^),
(^+),
)
where
import Data.Int (Int16, Int32, Int64, Int8)
import Data.Ord
import Data.Word (Word, Word16, Word32, Word64, Word8)
import GHC.Natural (Natural (..), naturalFromInteger)
import NumHask.Algebra.Additive
import NumHask.Algebra.Multiplicative
import NumHask.Algebra.Ring
import Prelude (Double, Eq, Float, Int, Integer, Show, uncurry, (.))
import Prelude qualified as P
data DivMod a = DivMod {forall a. DivMod a -> a
div_ :: a, forall a. DivMod a -> a
mod_ :: a}
deriving (DivMod a -> DivMod a -> Bool
(DivMod a -> DivMod a -> Bool)
-> (DivMod a -> DivMod a -> Bool) -> Eq (DivMod a)
forall a. Eq a => DivMod a -> DivMod a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => DivMod a -> DivMod a -> Bool
== :: DivMod a -> DivMod a -> Bool
$c/= :: forall a. Eq a => DivMod a -> DivMod a -> Bool
/= :: DivMod a -> DivMod a -> Bool
Eq, Int -> DivMod a -> ShowS
[DivMod a] -> ShowS
DivMod a -> String
(Int -> DivMod a -> ShowS)
-> (DivMod a -> String) -> ([DivMod a] -> ShowS) -> Show (DivMod a)
forall a. Show a => Int -> DivMod a -> ShowS
forall a. Show a => [DivMod a] -> ShowS
forall a. Show a => DivMod a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> DivMod a -> ShowS
showsPrec :: Int -> DivMod a -> ShowS
$cshow :: forall a. Show a => DivMod a -> String
show :: DivMod a -> String
$cshowList :: forall a. Show a => [DivMod a] -> ShowS
showList :: [DivMod a] -> ShowS
Show)
data QuotRem a = QuotRem {forall a. QuotRem a -> a
quot_ :: a, forall a. QuotRem a -> a
rem_ :: a}
deriving (QuotRem a -> QuotRem a -> Bool
(QuotRem a -> QuotRem a -> Bool)
-> (QuotRem a -> QuotRem a -> Bool) -> Eq (QuotRem a)
forall a. Eq a => QuotRem a -> QuotRem a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => QuotRem a -> QuotRem a -> Bool
== :: QuotRem a -> QuotRem a -> Bool
$c/= :: forall a. Eq a => QuotRem a -> QuotRem a -> Bool
/= :: QuotRem a -> QuotRem a -> Bool
Eq, Int -> QuotRem a -> ShowS
[QuotRem a] -> ShowS
QuotRem a -> String
(Int -> QuotRem a -> ShowS)
-> (QuotRem a -> String)
-> ([QuotRem a] -> ShowS)
-> Show (QuotRem a)
forall a. Show a => Int -> QuotRem a -> ShowS
forall a. Show a => [QuotRem a] -> ShowS
forall a. Show a => QuotRem a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> QuotRem a -> ShowS
showsPrec :: Int -> QuotRem a -> ShowS
$cshow :: forall a. Show a => QuotRem a -> String
show :: QuotRem a -> String
$cshowList :: forall a. Show a => [QuotRem a] -> ShowS
showList :: [QuotRem a] -> ShowS
Show)
class
(Distributive a) =>
Integral a
where
infixl 7 `div`, `mod`
div :: a -> a -> a
div a
a1 a
a2 = DivMod a -> a
forall a. DivMod a -> a
div_ (a -> a -> DivMod a
forall a. Integral a => a -> a -> DivMod a
divMod a
a1 a
a2)
mod :: a -> a -> a
mod a
a1 a
a2 = DivMod a -> a
forall a. DivMod a -> a
mod_ (a -> a -> DivMod a
forall a. Integral a => a -> a -> DivMod a
divMod a
a1 a
a2)
divMod :: a -> a -> DivMod a
quot :: a -> a -> a
quot a
a1 a
a2 = QuotRem a -> a
forall a. QuotRem a -> a
quot_ (a -> a -> QuotRem a
forall a. Integral a => a -> a -> QuotRem a
quotRem a
a1 a
a2)
rem :: a -> a -> a
rem a
a1 a
a2 = QuotRem a -> a
forall a. QuotRem a -> a
rem_ (a -> a -> QuotRem a
forall a. Integral a => a -> a -> QuotRem a
quotRem a
a1 a
a2)
quotRem :: a -> a -> QuotRem a
instance Integral Int where
divMod :: Int -> Int -> DivMod Int
divMod Int
a Int
b = (Int -> Int -> DivMod Int) -> (Int, Int) -> DivMod Int
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> Int -> DivMod Int
forall a. a -> a -> DivMod a
DivMod (Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
P.divMod Int
a Int
b)
quotRem :: Int -> Int -> QuotRem Int
quotRem Int
a Int
b = (Int -> Int -> QuotRem Int) -> (Int, Int) -> QuotRem Int
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> Int -> QuotRem Int
forall a. a -> a -> QuotRem a
QuotRem (Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Int
a Int
b)
instance Integral Integer where
divMod :: Integer -> Integer -> DivMod Integer
divMod Integer
a Integer
b = (Integer -> Integer -> DivMod Integer)
-> (Integer, Integer) -> DivMod Integer
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Integer -> Integer -> DivMod Integer
forall a. a -> a -> DivMod a
DivMod (Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
P.divMod Integer
a Integer
b)
quotRem :: Integer -> Integer -> QuotRem Integer
quotRem Integer
a Integer
b = (Integer -> Integer -> QuotRem Integer)
-> (Integer, Integer) -> QuotRem Integer
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Integer -> Integer -> QuotRem Integer
forall a. a -> a -> QuotRem a
QuotRem (Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Integer
a Integer
b)
instance Integral Natural where
divMod :: Natural -> Natural -> DivMod Natural
divMod Natural
a Natural
b = (Natural -> Natural -> DivMod Natural)
-> (Natural, Natural) -> DivMod Natural
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Natural -> Natural -> DivMod Natural
forall a. a -> a -> DivMod a
DivMod (Natural -> Natural -> (Natural, Natural)
forall a. Integral a => a -> a -> (a, a)
P.divMod Natural
a Natural
b)
quotRem :: Natural -> Natural -> QuotRem Natural
quotRem Natural
a Natural
b = (Natural -> Natural -> QuotRem Natural)
-> (Natural, Natural) -> QuotRem Natural
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Natural -> Natural -> QuotRem Natural
forall a. a -> a -> QuotRem a
QuotRem (Natural -> Natural -> (Natural, Natural)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Natural
a Natural
b)
instance Integral Int8 where
divMod :: Int8 -> Int8 -> DivMod Int8
divMod Int8
a Int8
b = (Int8 -> Int8 -> DivMod Int8) -> (Int8, Int8) -> DivMod Int8
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int8 -> Int8 -> DivMod Int8
forall a. a -> a -> DivMod a
DivMod (Int8 -> Int8 -> (Int8, Int8)
forall a. Integral a => a -> a -> (a, a)
P.divMod Int8
a Int8
b)
quotRem :: Int8 -> Int8 -> QuotRem Int8
quotRem Int8
a Int8
b = (Int8 -> Int8 -> QuotRem Int8) -> (Int8, Int8) -> QuotRem Int8
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int8 -> Int8 -> QuotRem Int8
forall a. a -> a -> QuotRem a
QuotRem (Int8 -> Int8 -> (Int8, Int8)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Int8
a Int8
b)
instance Integral Int16 where
divMod :: Int16 -> Int16 -> DivMod Int16
divMod Int16
a Int16
b = (Int16 -> Int16 -> DivMod Int16) -> (Int16, Int16) -> DivMod Int16
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int16 -> Int16 -> DivMod Int16
forall a. a -> a -> DivMod a
DivMod (Int16 -> Int16 -> (Int16, Int16)
forall a. Integral a => a -> a -> (a, a)
P.divMod Int16
a Int16
b)
quotRem :: Int16 -> Int16 -> QuotRem Int16
quotRem Int16
a Int16
b = (Int16 -> Int16 -> QuotRem Int16)
-> (Int16, Int16) -> QuotRem Int16
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int16 -> Int16 -> QuotRem Int16
forall a. a -> a -> QuotRem a
QuotRem (Int16 -> Int16 -> (Int16, Int16)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Int16
a Int16
b)
instance Integral Int32 where
divMod :: Int32 -> Int32 -> DivMod Int32
divMod Int32
a Int32
b = (Int32 -> Int32 -> DivMod Int32) -> (Int32, Int32) -> DivMod Int32
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int32 -> Int32 -> DivMod Int32
forall a. a -> a -> DivMod a
DivMod (Int32 -> Int32 -> (Int32, Int32)
forall a. Integral a => a -> a -> (a, a)
P.divMod Int32
a Int32
b)
quotRem :: Int32 -> Int32 -> QuotRem Int32
quotRem Int32
a Int32
b = (Int32 -> Int32 -> QuotRem Int32)
-> (Int32, Int32) -> QuotRem Int32
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int32 -> Int32 -> QuotRem Int32
forall a. a -> a -> QuotRem a
QuotRem (Int32 -> Int32 -> (Int32, Int32)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Int32
a Int32
b)
instance Integral Int64 where
divMod :: Int64 -> Int64 -> DivMod Int64
divMod Int64
a Int64
b = (Int64 -> Int64 -> DivMod Int64) -> (Int64, Int64) -> DivMod Int64
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int64 -> Int64 -> DivMod Int64
forall a. a -> a -> DivMod a
DivMod (Int64 -> Int64 -> (Int64, Int64)
forall a. Integral a => a -> a -> (a, a)
P.divMod Int64
a Int64
b)
quotRem :: Int64 -> Int64 -> QuotRem Int64
quotRem Int64
a Int64
b = (Int64 -> Int64 -> QuotRem Int64)
-> (Int64, Int64) -> QuotRem Int64
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int64 -> Int64 -> QuotRem Int64
forall a. a -> a -> QuotRem a
QuotRem (Int64 -> Int64 -> (Int64, Int64)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Int64
a Int64
b)
instance Integral Word where
divMod :: Word -> Word -> DivMod Word
divMod Word
a Word
b = (Word -> Word -> DivMod Word) -> (Word, Word) -> DivMod Word
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Word -> Word -> DivMod Word
forall a. a -> a -> DivMod a
DivMod (Word -> Word -> (Word, Word)
forall a. Integral a => a -> a -> (a, a)
P.divMod Word
a Word
b)
quotRem :: Word -> Word -> QuotRem Word
quotRem Word
a Word
b = (Word -> Word -> QuotRem Word) -> (Word, Word) -> QuotRem Word
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Word -> Word -> QuotRem Word
forall a. a -> a -> QuotRem a
QuotRem (Word -> Word -> (Word, Word)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Word
a Word
b)
instance Integral Word8 where
divMod :: Word8 -> Word8 -> DivMod Word8
divMod Word8
a Word8
b = (Word8 -> Word8 -> DivMod Word8) -> (Word8, Word8) -> DivMod Word8
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Word8 -> Word8 -> DivMod Word8
forall a. a -> a -> DivMod a
DivMod (Word8 -> Word8 -> (Word8, Word8)
forall a. Integral a => a -> a -> (a, a)
P.divMod Word8
a Word8
b)
quotRem :: Word8 -> Word8 -> QuotRem Word8
quotRem Word8
a Word8
b = (Word8 -> Word8 -> QuotRem Word8)
-> (Word8, Word8) -> QuotRem Word8
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Word8 -> Word8 -> QuotRem Word8
forall a. a -> a -> QuotRem a
QuotRem (Word8 -> Word8 -> (Word8, Word8)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Word8
a Word8
b)
instance Integral Word16 where
divMod :: Word16 -> Word16 -> DivMod Word16
divMod Word16
a Word16
b = (Word16 -> Word16 -> DivMod Word16)
-> (Word16, Word16) -> DivMod Word16
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Word16 -> Word16 -> DivMod Word16
forall a. a -> a -> DivMod a
DivMod (Word16 -> Word16 -> (Word16, Word16)
forall a. Integral a => a -> a -> (a, a)
P.divMod Word16
a Word16
b)
quotRem :: Word16 -> Word16 -> QuotRem Word16
quotRem Word16
a Word16
b = (Word16 -> Word16 -> QuotRem Word16)
-> (Word16, Word16) -> QuotRem Word16
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Word16 -> Word16 -> QuotRem Word16
forall a. a -> a -> QuotRem a
QuotRem (Word16 -> Word16 -> (Word16, Word16)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Word16
a Word16
b)
instance Integral Word32 where
divMod :: Word32 -> Word32 -> DivMod Word32
divMod Word32
a Word32
b = (Word32 -> Word32 -> DivMod Word32)
-> (Word32, Word32) -> DivMod Word32
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Word32 -> Word32 -> DivMod Word32
forall a. a -> a -> DivMod a
DivMod (Word32 -> Word32 -> (Word32, Word32)
forall a. Integral a => a -> a -> (a, a)
P.divMod Word32
a Word32
b)
quotRem :: Word32 -> Word32 -> QuotRem Word32
quotRem Word32
a Word32
b = (Word32 -> Word32 -> QuotRem Word32)
-> (Word32, Word32) -> QuotRem Word32
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Word32 -> Word32 -> QuotRem Word32
forall a. a -> a -> QuotRem a
QuotRem (Word32 -> Word32 -> (Word32, Word32)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Word32
a Word32
b)
instance Integral Word64 where
divMod :: Word64 -> Word64 -> DivMod Word64
divMod Word64
a Word64
b = (Word64 -> Word64 -> DivMod Word64)
-> (Word64, Word64) -> DivMod Word64
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Word64 -> Word64 -> DivMod Word64
forall a. a -> a -> DivMod a
DivMod (Word64 -> Word64 -> (Word64, Word64)
forall a. Integral a => a -> a -> (a, a)
P.divMod Word64
a Word64
b)
quotRem :: Word64 -> Word64 -> QuotRem Word64
quotRem Word64
a Word64
b = (Word64 -> Word64 -> QuotRem Word64)
-> (Word64, Word64) -> QuotRem Word64
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Word64 -> Word64 -> QuotRem Word64
forall a. a -> a -> QuotRem a
QuotRem (Word64 -> Word64 -> (Word64, Word64)
forall a. Integral a => a -> a -> (a, a)
P.quotRem Word64
a Word64
b)
instance (Integral b) => Integral (a -> b) where
div :: (a -> b) -> (a -> b) -> a -> b
div a -> b
f a -> b
f' a
a = a -> b
f a
a b -> b -> b
forall a. Integral a => a -> a -> a
`div` a -> b
f' a
a
mod :: (a -> b) -> (a -> b) -> a -> b
mod a -> b
f a -> b
f' a
a = a -> b
f a
a b -> b -> b
forall a. Integral a => a -> a -> a
`mod` a -> b
f' a
a
quot :: (a -> b) -> (a -> b) -> a -> b
quot a -> b
f a -> b
f' a
a = a -> b
f a
a b -> b -> b
forall a. Integral a => a -> a -> a
`quot` a -> b
f' a
a
rem :: (a -> b) -> (a -> b) -> a -> b
rem a -> b
f a -> b
f' a
a = a -> b
f a
a b -> b -> b
forall a. Integral a => a -> a -> a
`rem` a -> b
f' a
a
divMod :: (a -> b) -> (a -> b) -> DivMod (a -> b)
divMod a -> b
f a -> b
f' = (a -> b) -> (a -> b) -> DivMod (a -> b)
forall a. a -> a -> DivMod a
DivMod ((a -> b) -> (a -> b) -> a -> b
forall a. Integral a => a -> a -> a
div a -> b
f a -> b
f') ((a -> b) -> (a -> b) -> a -> b
forall a. Integral a => a -> a -> a
mod a -> b
f a -> b
f')
quotRem :: (a -> b) -> (a -> b) -> QuotRem (a -> b)
quotRem a -> b
f a -> b
f' = (a -> b) -> (a -> b) -> QuotRem (a -> b)
forall a. a -> a -> QuotRem a
QuotRem ((a -> b) -> (a -> b) -> a -> b
forall a. Integral a => a -> a -> a
quot a -> b
f a -> b
f') ((a -> b) -> (a -> b) -> a -> b
forall a. Integral a => a -> a -> a
rem a -> b
f a -> b
f')
even :: (P.Eq a, Integral a) => a -> P.Bool
even :: forall a. (Eq a, Integral a) => a -> Bool
even a
n = a
n a -> a -> a
forall a. Integral a => a -> a -> a
`rem` (a
forall a. Multiplicative a => a
one a -> a -> a
forall a. Additive a => a -> a -> a
+ a
forall a. Multiplicative a => a
one) a -> a -> Bool
forall a. Eq a => a -> a -> Bool
P.== a
forall a. Additive a => a
zero
odd :: (P.Eq a, Integral a) => a -> P.Bool
odd :: forall a. (Eq a, Integral a) => a -> Bool
odd = Bool -> Bool
P.not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
forall a. (Eq a, Integral a) => a -> Bool
even
class ToIntegral a b where
{-# MINIMAL toIntegral #-}
toIntegral :: a -> b
type ToInt a = ToIntegral a Int
instance ToIntegral Integer Integer where
toIntegral :: Integer -> Integer
toIntegral = Integer -> Integer
forall a. a -> a
P.id
instance ToIntegral Int Integer where
toIntegral :: Int -> Integer
toIntegral = Int -> Integer
forall a. Integral a => a -> Integer
P.toInteger
instance ToIntegral Natural Integer where
toIntegral :: Natural -> Integer
toIntegral = Natural -> Integer
forall a. Integral a => a -> Integer
P.toInteger
instance ToIntegral Int8 Integer where
toIntegral :: Int8 -> Integer
toIntegral = Int8 -> Integer
forall a. Integral a => a -> Integer
P.toInteger
instance ToIntegral Int16 Integer where
toIntegral :: Int16 -> Integer
toIntegral = Int16 -> Integer
forall a. Integral a => a -> Integer
P.toInteger
instance ToIntegral Int32 Integer where
toIntegral :: Int32 -> Integer
toIntegral = Int32 -> Integer
forall a. Integral a => a -> Integer
P.toInteger
instance ToIntegral Int64 Integer where
toIntegral :: Int64 -> Integer
toIntegral = Int64 -> Integer
forall a. Integral a => a -> Integer
P.toInteger
instance ToIntegral Word Integer where
toIntegral :: Word -> Integer
toIntegral = Word -> Integer
forall a. Integral a => a -> Integer
P.toInteger
instance ToIntegral Word8 Integer where
toIntegral :: Word8 -> Integer
toIntegral = Word8 -> Integer
forall a. Integral a => a -> Integer
P.toInteger
instance ToIntegral Word16 Integer where
toIntegral :: Word16 -> Integer
toIntegral = Word16 -> Integer
forall a. Integral a => a -> Integer
P.toInteger
instance ToIntegral Word32 Integer where
toIntegral :: Word32 -> Integer
toIntegral = Word32 -> Integer
forall a. Integral a => a -> Integer
P.toInteger
instance ToIntegral Word64 Integer where
toIntegral :: Word64 -> Integer
toIntegral = Word64 -> Integer
forall a. Integral a => a -> Integer
P.toInteger
instance ToIntegral Int Int where
toIntegral :: Int -> Int
toIntegral = Int -> Int
forall a. a -> a
P.id
instance ToIntegral Integer Int where
toIntegral :: Integer -> Int
toIntegral = Integer -> Int
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance ToIntegral Natural Int where
toIntegral :: Natural -> Int
toIntegral = Natural -> Int
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance ToIntegral Int8 Int where
toIntegral :: Int8 -> Int
toIntegral = Int8 -> Int
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance ToIntegral Int16 Int where
toIntegral :: Int16 -> Int
toIntegral = Int16 -> Int
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance ToIntegral Int32 Int where
toIntegral :: Int32 -> Int
toIntegral = Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance ToIntegral Int64 Int where
toIntegral :: Int64 -> Int
toIntegral = Int64 -> Int
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance ToIntegral Word Int where
toIntegral :: Word -> Int
toIntegral = Word -> Int
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance ToIntegral Word8 Int where
toIntegral :: Word8 -> Int
toIntegral = Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance ToIntegral Word16 Int where
toIntegral :: Word16 -> Int
toIntegral = Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance ToIntegral Word32 Int where
toIntegral :: Word32 -> Int
toIntegral = Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance ToIntegral Word64 Int where
toIntegral :: Word64 -> Int
toIntegral = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance ToIntegral Natural Natural where
toIntegral :: Natural -> Natural
toIntegral = Natural -> Natural
forall a. a -> a
P.id
instance ToIntegral Int8 Int8 where
toIntegral :: Int8 -> Int8
toIntegral = Int8 -> Int8
forall a. a -> a
P.id
instance ToIntegral Int16 Int16 where
toIntegral :: Int16 -> Int16
toIntegral = Int16 -> Int16
forall a. a -> a
P.id
instance ToIntegral Int32 Int32 where
toIntegral :: Int32 -> Int32
toIntegral = Int32 -> Int32
forall a. a -> a
P.id
instance ToIntegral Int64 Int64 where
toIntegral :: Int64 -> Int64
toIntegral = Int64 -> Int64
forall a. a -> a
P.id
instance ToIntegral Word Word where
toIntegral :: Word -> Word
toIntegral = Word -> Word
forall a. a -> a
P.id
instance ToIntegral Word8 Word8 where
toIntegral :: Word8 -> Word8
toIntegral = Word8 -> Word8
forall a. a -> a
P.id
instance ToIntegral Word16 Word16 where
toIntegral :: Word16 -> Word16
toIntegral = Word16 -> Word16
forall a. a -> a
P.id
instance ToIntegral Word32 Word32 where
toIntegral :: Word32 -> Word32
toIntegral = Word32 -> Word32
forall a. a -> a
P.id
instance ToIntegral Word64 Word64 where
toIntegral :: Word64 -> Word64
toIntegral = Word64 -> Word64
forall a. a -> a
P.id
class FromIntegral a b where
{-# MINIMAL fromIntegral #-}
fromIntegral :: b -> a
type FromInt a = FromIntegral a Int
instance (FromIntegral a b) => FromIntegral (c -> a) b where
fromIntegral :: b -> c -> a
fromIntegral b
i c
_ = b -> a
forall a b. FromIntegral a b => b -> a
fromIntegral b
i
instance FromIntegral Double Integer where
fromIntegral :: Integer -> Double
fromIntegral = Integer -> Double
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Float Integer where
fromIntegral :: Integer -> Float
fromIntegral = Integer -> Float
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Int Integer where
fromIntegral :: Integer -> Int
fromIntegral = Integer -> Int
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Integer Integer where
fromIntegral :: Integer -> Integer
fromIntegral = Integer -> Integer
forall a. a -> a
P.id
instance FromIntegral Natural Integer where
fromIntegral :: Integer -> Natural
fromIntegral = Integer -> Natural
naturalFromInteger
instance FromIntegral Int8 Integer where
fromIntegral :: Integer -> Int8
fromIntegral = Integer -> Int8
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Int16 Integer where
fromIntegral :: Integer -> Int16
fromIntegral = Integer -> Int16
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Int32 Integer where
fromIntegral :: Integer -> Int32
fromIntegral = Integer -> Int32
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Int64 Integer where
fromIntegral :: Integer -> Int64
fromIntegral = Integer -> Int64
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Word Integer where
fromIntegral :: Integer -> Word
fromIntegral = Integer -> Word
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Word8 Integer where
fromIntegral :: Integer -> Word8
fromIntegral = Integer -> Word8
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Word16 Integer where
fromIntegral :: Integer -> Word16
fromIntegral = Integer -> Word16
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Word32 Integer where
fromIntegral :: Integer -> Word32
fromIntegral = Integer -> Word32
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Word64 Integer where
fromIntegral :: Integer -> Word64
fromIntegral = Integer -> Word64
forall a. Num a => Integer -> a
P.fromInteger
instance FromIntegral Double Int where
fromIntegral :: Int -> Double
fromIntegral = Int -> Double
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Float Int where
fromIntegral :: Int -> Float
fromIntegral = Int -> Float
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Int Int where
fromIntegral :: Int -> Int
fromIntegral = Int -> Int
forall a. a -> a
P.id
instance FromIntegral Integer Int where
fromIntegral :: Int -> Integer
fromIntegral = Int -> Integer
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Natural Int where
fromIntegral :: Int -> Natural
fromIntegral = Int -> Natural
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Int8 Int where
fromIntegral :: Int -> Int8
fromIntegral = Int -> Int8
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Int16 Int where
fromIntegral :: Int -> Int16
fromIntegral = Int -> Int16
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Int32 Int where
fromIntegral :: Int -> Int32
fromIntegral = Int -> Int32
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Int64 Int where
fromIntegral :: Int -> Int64
fromIntegral = Int -> Int64
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Word Int where
fromIntegral :: Int -> Word
fromIntegral = Int -> Word
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Word8 Int where
fromIntegral :: Int -> Word8
fromIntegral = Int -> Word8
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Word16 Int where
fromIntegral :: Int -> Word16
fromIntegral = Int -> Word16
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Word32 Int where
fromIntegral :: Int -> Word32
fromIntegral = Int -> Word32
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Word64 Int where
fromIntegral :: Int -> Word64
fromIntegral = Int -> Word64
forall a b. (Integral a, Num b) => a -> b
P.fromIntegral
instance FromIntegral Natural Natural where
fromIntegral :: Natural -> Natural
fromIntegral = Natural -> Natural
forall a. a -> a
P.id
instance FromIntegral Int8 Int8 where
fromIntegral :: Int8 -> Int8
fromIntegral = Int8 -> Int8
forall a. a -> a
P.id
instance FromIntegral Int16 Int16 where
fromIntegral :: Int16 -> Int16
fromIntegral = Int16 -> Int16
forall a. a -> a
P.id
instance FromIntegral Int32 Int32 where
fromIntegral :: Int32 -> Int32
fromIntegral = Int32 -> Int32
forall a. a -> a
P.id
instance FromIntegral Int64 Int64 where
fromIntegral :: Int64 -> Int64
fromIntegral = Int64 -> Int64
forall a. a -> a
P.id
instance FromIntegral Word Word where
fromIntegral :: Word -> Word
fromIntegral = Word -> Word
forall a. a -> a
P.id
instance FromIntegral Word8 Word8 where
fromIntegral :: Word8 -> Word8
fromIntegral = Word8 -> Word8
forall a. a -> a
P.id
instance FromIntegral Word16 Word16 where
fromIntegral :: Word16 -> Word16
fromIntegral = Word16 -> Word16
forall a. a -> a
P.id
instance FromIntegral Word32 Word32 where
fromIntegral :: Word32 -> Word32
fromIntegral = Word32 -> Word32
forall a. a -> a
P.id
instance FromIntegral Word64 Word64 where
fromIntegral :: Word64 -> Word64
fromIntegral = Word64 -> Word64
forall a. a -> a
P.id
class FromInteger a where
fromInteger :: Integer -> a
instance FromInteger Double where
fromInteger :: Integer -> Double
fromInteger = Integer -> Double
forall a. Num a => Integer -> a
P.fromInteger
instance FromInteger Float where
fromInteger :: Integer -> Float
fromInteger = Integer -> Float
forall a. Num a => Integer -> a
P.fromInteger
instance FromInteger Int where
fromInteger :: Integer -> Int
fromInteger = Integer -> Int
forall a. Num a => Integer -> a
P.fromInteger
instance FromInteger Integer where
fromInteger :: Integer -> Integer
fromInteger = Integer -> Integer
forall a. a -> a
P.id
instance FromInteger Natural where
fromInteger :: Integer -> Natural
fromInteger = Integer -> Natural
naturalFromInteger
instance FromInteger Int8 where
fromInteger :: Integer -> Int8
fromInteger = Integer -> Int8
forall a. Num a => Integer -> a
P.fromInteger
instance FromInteger Int16 where
fromInteger :: Integer -> Int16
fromInteger = Integer -> Int16
forall a. Num a => Integer -> a
P.fromInteger
instance FromInteger Int32 where
fromInteger :: Integer -> Int32
fromInteger = Integer -> Int32
forall a. Num a => Integer -> a
P.fromInteger
instance FromInteger Int64 where
fromInteger :: Integer -> Int64
fromInteger = Integer -> Int64
forall a. Num a => Integer -> a
P.fromInteger
instance FromInteger Word where
fromInteger :: Integer -> Word
fromInteger = Integer -> Word
forall a. Num a => Integer -> a
P.fromInteger
instance FromInteger Word8 where
fromInteger :: Integer -> Word8
fromInteger = Integer -> Word8
forall a. Num a => Integer -> a
P.fromInteger
instance FromInteger Word16 where
fromInteger :: Integer -> Word16
fromInteger = Integer -> Word16
forall a. Num a => Integer -> a
P.fromInteger
instance FromInteger Word32 where
fromInteger :: Integer -> Word32
fromInteger = Integer -> Word32
forall a. Num a => Integer -> a
P.fromInteger
instance FromInteger Word64 where
fromInteger :: Integer -> Word64
fromInteger = Integer -> Word64
forall a. Num a => Integer -> a
P.fromInteger
deriving instance (FromInteger a) => FromInteger (Sum a)
deriving instance (FromInteger a) => FromInteger (Product a)
infixr 8 ^^
(^^) ::
(P.Ord b, Divisive a, Subtractive b, Integral b) =>
a ->
b ->
a
a
x0 ^^ :: forall b a.
(Ord b, Divisive a, Subtractive b, Integral b) =>
a -> b -> a
^^ b
y0 =
case b -> b -> Ordering
forall a. Ord a => a -> a -> Ordering
compare b
y0 b
forall a. Additive a => a
zero of
Ordering
EQ -> a
forall a. Multiplicative a => a
one
Ordering
GT -> a -> b -> a
forall {a} {t}. (Eq a, Integral a, Multiplicative t) => t -> a -> t
f a
x0 b
y0
Ordering
LT -> a -> a
forall a. Divisive a => a -> a
recip (a
x0 a -> b -> a
forall b a.
(Ord b, Divisive a, Subtractive b, Integral b) =>
a -> b -> a
^^ b -> b
forall a. Subtractive a => a -> a
negate b
y0)
where
f :: t -> a -> t
f t
x a
y
| a -> Bool
forall a. (Eq a, Integral a) => a -> Bool
even a
y = t -> a -> t
f (t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
forall a. (Multiplicative a, Additive a) => a
two)
| a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
P.== a
forall a. Multiplicative a => a
one = t
x
| Bool
P.otherwise = t -> a -> t -> t
forall {a} {t}.
(Eq a, Integral a, Multiplicative t) =>
t -> a -> t -> t
g (t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
forall a. (Multiplicative a, Additive a) => a
two) t
x
g :: t -> a -> t -> t
g t
x a
y t
z
| a -> Bool
forall a. (Eq a, Integral a) => a -> Bool
even a
y = t -> a -> t -> t
g (t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
forall a. (Multiplicative a, Additive a) => a
two) t
z
| a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
P.== a
forall a. Multiplicative a => a
one = t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
z
| Bool
P.otherwise = t -> a -> t -> t
g (t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
forall a. (Multiplicative a, Additive a) => a
two) (t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
z)
infixr 8 ^
(^) ::
(Divisive a) => a -> Int -> a
^ :: forall a. Divisive a => a -> Int -> a
(^) a
x Int
n = a
x a -> Int -> a
forall b a.
(Ord b, Divisive a, Subtractive b, Integral b) =>
a -> b -> a
^^ Int
n
infixr 8 ^+
(^+) ::
(Multiplicative a) => a -> Natural -> a
a
x0 ^+ :: forall a. Multiplicative a => a -> Natural -> a
^+ Natural
y0 =
case Natural -> Natural -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Natural
y0 Natural
forall a. Additive a => a
zero of
Ordering
EQ -> a
forall a. Multiplicative a => a
one
Ordering
GT -> a -> Natural -> a
forall {a} {t}. (Eq a, Integral a, Multiplicative t) => t -> a -> t
f a
x0 Natural
y0
Ordering
LT -> String -> a
forall a. HasCallStack => String -> a
P.error String
"(^+): negative exponent"
where
f :: t -> a -> t
f t
x a
y
| a -> Bool
forall a. (Eq a, Integral a) => a -> Bool
even a
y = t -> a -> t
f (t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
forall a. (Multiplicative a, Additive a) => a
two)
| a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
P.== a
forall a. Multiplicative a => a
one = t
x
| Bool
P.otherwise = t -> a -> t -> t
forall {a} {t}.
(Eq a, Integral a, Multiplicative t) =>
t -> a -> t -> t
g (t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
forall a. (Multiplicative a, Additive a) => a
two) t
x
g :: t -> a -> t -> t
g t
x a
y t
z
| a -> Bool
forall a. (Eq a, Integral a) => a -> Bool
even a
y = t -> a -> t -> t
g (t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
forall a. (Multiplicative a, Additive a) => a
two) t
z
| a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
P.== a
forall a. Multiplicative a => a
one = t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
z
| Bool
P.otherwise = t -> a -> t -> t
g (t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
x) (a
y a -> a -> a
forall a. Integral a => a -> a -> a
`quot` a
forall a. (Multiplicative a, Additive a) => a
two) (t
x t -> t -> t
forall a. Multiplicative a => a -> a -> a
* t
z)