```{-# LANGUAGE ExplicitForAll #-}

module Hextra.Integral where

import Hextra.Function
import Hextra.Bifunctor

toIntegral :: forall i j. (Integral i, Integral j) => i -> j
toIntegral :: i -> j
toIntegral = Integer -> j
forall a. Num a => Integer -> a
fromInteger (Integer -> j) -> (i -> Integer) -> i -> j
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> Integer
forall a. Integral a => a -> Integer
toInteger
-- ^ Converts any Integral to any other Integral.

(\+) :: forall i j. (Integral i, Integral j) => i -> i -> j
\+ :: i -> i -> j
(\+) = i -> j
forall i j. (Integral i, Integral j) => i -> j
toIntegral (i -> j) -> (i -> i -> i) -> i -> i -> j
forall b c a. (b -> c) -> (a -> a -> b) -> a -> a -> c
.> i -> i -> i
forall a. Num a => a -> a -> a
(+)

(\-) :: forall i j. (Integral i, Integral j) => i -> i -> j
\- :: i -> i -> j
(\-) = i -> j
forall i j. (Integral i, Integral j) => i -> j
toIntegral (i -> j) -> (i -> i -> i) -> i -> i -> j
forall b c a. (b -> c) -> (a -> a -> b) -> a -> a -> c
.> (-)

(\*) :: forall i j. (Integral i, Integral j) => i -> i -> j
\* :: i -> i -> j
(\*) = i -> j
forall i j. (Integral i, Integral j) => i -> j
toIntegral (i -> j) -> (i -> i -> i) -> i -> i -> j
forall b c a. (b -> c) -> (a -> a -> b) -> a -> a -> c
.> i -> i -> i
forall a. Num a => a -> a -> a
(*)

genericQuot :: forall i j. (Integral i, Integral j) => i -> i -> j
genericQuot :: i -> i -> j
genericQuot = i -> j
forall i j. (Integral i, Integral j) => i -> j
toIntegral (i -> j) -> (i -> i -> i) -> i -> i -> j
forall b c a. (b -> c) -> (a -> a -> b) -> a -> a -> c
.> i -> i -> i
forall a. Integral a => a -> a -> a
quot

genericRem :: forall i j. (Integral i, Integral j) => i -> i -> j
genericRem :: i -> i -> j
genericRem = i -> j
forall i j. (Integral i, Integral j) => i -> j
toIntegral (i -> j) -> (i -> i -> i) -> i -> i -> j
forall b c a. (b -> c) -> (a -> a -> b) -> a -> a -> c
.> i -> i -> i
forall a. Integral a => a -> a -> a
rem

genericQuotRem :: forall i j. (Integral i, Integral j) => i -> i -> (j, j)
genericQuotRem :: i -> i -> (j, j)
genericQuotRem = (i -> j) -> (i, i) -> (j, j)
forall (g :: * -> * -> *) a b.
Bifunctor g =>
(a -> b) -> g a a -> g b b
bothmap i -> j
forall i j. (Integral i, Integral j) => i -> j
toIntegral ((i, i) -> (j, j)) -> (i -> i -> (i, i)) -> i -> i -> (j, j)
forall b c a. (b -> c) -> (a -> a -> b) -> a -> a -> c
.> i -> i -> (i, i)
forall a. Integral a => a -> a -> (a, a)
quotRem

genericDiv :: forall i j. (Integral i, Integral j) => i -> i -> j
genericDiv :: i -> i -> j
genericDiv = i -> j
forall i j. (Integral i, Integral j) => i -> j
toIntegral (i -> j) -> (i -> i -> i) -> i -> i -> j
forall b c a. (b -> c) -> (a -> a -> b) -> a -> a -> c
.> i -> i -> i
forall a. Integral a => a -> a -> a
div

genericMod :: forall i j. (Integral i, Integral j) => i -> i -> j
genericMod :: i -> i -> j
genericMod = i -> j
forall i j. (Integral i, Integral j) => i -> j
toIntegral (i -> j) -> (i -> i -> i) -> i -> i -> j
forall b c a. (b -> c) -> (a -> a -> b) -> a -> a -> c
.> i -> i -> i
forall a. Integral a => a -> a -> a
mod

genericDivMod :: forall i j. (Integral i, Integral j) => i -> i -> (j, j)
genericDivMod :: i -> i -> (j, j)
genericDivMod = (i -> j) -> (i, i) -> (j, j)
forall (g :: * -> * -> *) a b.
Bifunctor g =>
(a -> b) -> g a a -> g b b
bothmap i -> j
forall i j. (Integral i, Integral j) => i -> j
toIntegral ((i, i) -> (j, j)) -> (i -> i -> (i, i)) -> i -> i -> (j, j)
forall b c a. (b -> c) -> (a -> a -> b) -> a -> a -> c
.> i -> i -> (i, i)
forall a. Integral a => a -> a -> (a, a)
divMod

safeQuot :: forall a. Integral a => a -> a -> Maybe a
safeQuot :: a -> a -> Maybe a
safeQuot a
_ a
0 = Maybe a
forall a. Maybe a
Nothing
safeQuot a
a a
b = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
\$ a -> a -> a
forall a. Integral a => a -> a -> a
quot a
a a
b

safeRem :: forall a. Integral a => a -> a -> Maybe a
safeRem :: a -> a -> Maybe a
safeRem a
_ a
0 = Maybe a
forall a. Maybe a
Nothing
safeRem a
a a
b = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
\$ a -> a -> a
forall a. Integral a => a -> a -> a
rem a
a a
b

safeQuotRem :: forall a. Integral a => a -> a -> Maybe (a, a)
safeQuotRem :: a -> a -> Maybe (a, a)
safeQuotRem a
_ a
0 = Maybe (a, a)
forall a. Maybe a
Nothing
safeQuotRem a
a a
b = (a, a) -> Maybe (a, a)
forall a. a -> Maybe a
Just ((a, a) -> Maybe (a, a)) -> (a, a) -> Maybe (a, a)
forall a b. (a -> b) -> a -> b
\$ a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
quotRem a
a a
b

safeDiv :: forall a. Integral a => a -> a -> Maybe a
safeDiv :: a -> a -> Maybe a
safeDiv a
_ a
0 = Maybe a
forall a. Maybe a
Nothing
safeDiv a
a a
b = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
\$ a -> a -> a
forall a. Integral a => a -> a -> a
div a
a a
b

safeMod :: forall a. Integral a => a -> a -> Maybe a
safeMod :: a -> a -> Maybe a
safeMod a
_ a
0 = Maybe a
forall a. Maybe a
Nothing
safeMod a
a a
b = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
\$ a -> a -> a
forall a. Integral a => a -> a -> a
mod a
a a
b

safeDivMod :: forall a. Integral a => a -> a -> Maybe (a, a)
safeDivMod :: a -> a -> Maybe (a, a)
safeDivMod a
_ a
0 = Maybe (a, a)
forall a. Maybe a
Nothing
safeDivMod a
a a
b = (a, a) -> Maybe (a, a)
forall a. a -> Maybe a
Just ((a, a) -> Maybe (a, a)) -> (a, a) -> Maybe (a, a)
forall a b. (a -> b) -> a -> b
\$ a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
divMod a
a a
b

genericSafeQuot :: forall i j. (Integral i, Integral j) => i -> i -> Maybe j
genericSafeQuot :: i -> i -> Maybe j
genericSafeQuot i
_ i
0 = Maybe j
forall a. Maybe a
Nothing
genericSafeQuot i
a i
b = j -> Maybe j
forall a. a -> Maybe a
Just (j -> Maybe j) -> j -> Maybe j
forall a b. (a -> b) -> a -> b
\$ i -> i -> j
forall i j. (Integral i, Integral j) => i -> i -> j
genericQuot i
a i
b

genericSafeRem :: forall i j. (Integral i, Integral j) => i -> i -> Maybe j
genericSafeRem :: i -> i -> Maybe j
genericSafeRem i
_ i
0 = Maybe j
forall a. Maybe a
Nothing
genericSafeRem i
a i
b = j -> Maybe j
forall a. a -> Maybe a
Just (j -> Maybe j) -> j -> Maybe j
forall a b. (a -> b) -> a -> b
\$ i -> i -> j
forall i j. (Integral i, Integral j) => i -> i -> j
genericRem i
a i
b

genericSafeQuotRem :: forall i j. (Integral i, Integral j) => i -> i -> Maybe (j, j)
genericSafeQuotRem :: i -> i -> Maybe (j, j)
genericSafeQuotRem i
_ i
0 = Maybe (j, j)
forall a. Maybe a
Nothing
genericSafeQuotRem i
a i
b = (j, j) -> Maybe (j, j)
forall a. a -> Maybe a
Just ((j, j) -> Maybe (j, j)) -> (j, j) -> Maybe (j, j)
forall a b. (a -> b) -> a -> b
\$ i -> i -> (j, j)
forall i j. (Integral i, Integral j) => i -> i -> (j, j)
genericQuotRem i
a i
b

genericSafeDiv :: forall i j. (Integral i, Integral j) => i -> i -> Maybe j
genericSafeDiv :: i -> i -> Maybe j
genericSafeDiv i
_ i
0 = Maybe j
forall a. Maybe a
Nothing
genericSafeDiv i
a i
b = j -> Maybe j
forall a. a -> Maybe a
Just (j -> Maybe j) -> j -> Maybe j
forall a b. (a -> b) -> a -> b
\$ i -> i -> j
forall i j. (Integral i, Integral j) => i -> i -> j
genericDiv i
a i
b

genericSafeMod :: forall i j. (Integral i, Integral j) => i -> i -> Maybe j
genericSafeMod :: i -> i -> Maybe j
genericSafeMod i
_ i
0 = Maybe j
forall a. Maybe a
Nothing
genericSafeMod i
a i
b = j -> Maybe j
forall a. a -> Maybe a
Just (j -> Maybe j) -> j -> Maybe j
forall a b. (a -> b) -> a -> b
\$ i -> i -> j
forall i j. (Integral i, Integral j) => i -> i -> j
genericMod i
a i
b

genericSafeDivMod :: forall i j. (Integral i, Integral j) => i -> i -> Maybe (j, j)
genericSafeDivMod :: i -> i -> Maybe (j, j)
genericSafeDivMod i
_ i
0 = Maybe (j, j)
forall a. Maybe a
Nothing
genericSafeDivMod i
a i
b = (j, j) -> Maybe (j, j)
forall a. a -> Maybe a
Just ((j, j) -> Maybe (j, j)) -> (j, j) -> Maybe (j, j)
forall a b. (a -> b) -> a -> b
\$ i -> i -> (j, j)
forall i j. (Integral i, Integral j) => i -> i -> (j, j)
genericDivMod i
a i
b```