papa-0.7.0: src/Papa/Base/Data/Integral.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Total integral division and exponentiation.
--
-- "Prelude"'s @div@, @mod@, @divMod@, @quot@, @rem@ and @quotRem@ throw for a
-- divisor of zero, and for @'Prelude.minBound' \`div\` (-1)@ on a bounded
-- type; @(^)@ throws for a negative exponent. They are in "Papa.Unsafe.Numeric".
-- The functions here are total, under names of their own:
--
-- * the division functions take a 'NonZero' divisor, so they cannot divide by
-- zero, and return the result itself; build the divisor once with
-- @preview nonZero@;
-- * 'pow' takes a 'Word' exponent, so it cannot be negative.
--
-- A divisor of @-1@ is handled without dividing: the quotient is
-- @'Prelude.negate' n@ and the remainder is @0@. On a signed bounded type,
-- @'Prelude.negate' 'Prelude.minBound'@ is 'Prelude.minBound' (two's
-- complement wrap-around), which is what every other arithmetic operation of
-- that type does on overflow. The divisor is @-1@ when it is negative and one
-- more than it is zero, which is never true at an unsigned type (whose
-- @'Prelude.negate' 1@ is 'Prelude.maxBound') or at 'Numeric.Natural.Natural' (whose
-- @'Prelude.negate' 1@ throws), so these are divided as "Prelude" divides
-- them. The test needs no 'Prelude.Integer', so at 'Data.Int.Int' and
-- 'Data.Word.Word' each function is "Prelude"'s plus two comparisons.
--
-- 'toIntegralSized' (of "Data.Bits") is the total @fromIntegral@ between
-- integral types: 'Data.Maybe.Nothing' where the value does not fit in the target type,
-- where 'Prelude.fromIntegral' wraps around (@'Prelude.fromIntegral' (256 ::
-- Int) :: Word8@ is @0@) or throws (to @Natural@ from a negative number).
--
-- @since 0.6.0
module Papa.Base.Data.Integral (
divNonZero,
modNonZero,
divModNonZero,
quotNonZero,
remNonZero,
quotRemNonZero,
pow,
Bits,
toIntegralSized,
) where
import Data.Bits (Bits, toIntegralSized)
import Data.Bool (bool, (&&))
import Data.Eq (Eq ((==)))
import Data.Int (Int)
import Data.Word (Word)
import Papa.Base.Data.Divisor (DivisorLike (divisor))
import Papa.Base.Data.NonZero (NonZero)
import Papa.Lens.Optic (the)
import qualified Prelude as P
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Papa.Unsafe.Numeric as U
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genInt = Gen.int (Range.linearFrom 0 (-1000) 1000)
-- >>> let genNonZero = Gen.just (preview nonZero <$> Gen.int (Range.linearFrom 0 (-1000) 1000))
-- >>> let genWord8 = Gen.word8 Range.linearBounded
-- >>> let genNonZeroWord8 = Gen.just (preview nonZero <$> Gen.word8 Range.linearBounded)
-- >>> let genNatural = Gen.integral (Range.linear 0 1000) :: Gen U.Natural
-- >>> let genNonZeroNatural = Gen.just (preview nonZero <$> (Gen.integral (Range.linear 0 1000) :: Gen U.Natural))
-- | Whether a divisor is @-1@: negative, and one more than it is zero. It is
-- never @-1@ at an unsigned type or at 'Numeric.Natural.Natural', where nothing is negative,
-- and @d + 1@ is not computed there.
minusOne :: (P.Integral a) => a -> P.Bool
minusOne d =
d P.< 0 && d P.+ 1 == 0
{-# INLINE minusOne #-}
-- | Integer division by a 'NonZero', truncated toward negative infinity.
-- "Prelude"'s @div@ is in "Papa.Unsafe.Numeric".
--
-- ==== __Examples__
--
-- >>> fmap (divNonZero 7) (preview nonZero (2 :: Int))
-- Just 3
--
-- >>> fmap (divNonZero (-7)) (preview nonZero (2 :: Int))
-- Just (-4)
--
-- >>> fmap (divNonZero minBound) (preview nonZero (-1 :: Int)) == Just minBound
-- True
--
-- >>> fmap (divNonZero 7) (preview nonZero (2 :: U.Natural))
-- Just 3
--
-- >>> check $ withTests 200 $ property $ do n <- forAll genInt; d <- forAll genNonZero; divNonZero n d * the divisor d + modNonZero n d === n
-- ...passed 200 tests.
-- True
--
-- @since 0.7.0
divNonZero :: (P.Integral a) => a -> NonZero a -> a
divNonZero n nz =
let d = the divisor nz
in bool (P.div n d) (P.negate n) (minusOne d)
{-# INLINE divNonZero #-}
-- | Integer modulus by a 'NonZero', with the sign of the divisor.
-- "Prelude"'s @mod@ is in "Papa.Unsafe.Numeric".
--
-- ==== __Examples__
--
-- >>> fmap (modNonZero 7) (preview nonZero (2 :: Int))
-- Just 1
--
-- >>> fmap (modNonZero (-7)) (preview nonZero (2 :: Int))
-- Just 1
--
-- >>> check $ withTests 200 $ property $ do n <- forAll genInt; d <- forAll genNonZero; modNonZero n d === P.snd (divModNonZero n d)
-- ...passed 200 tests.
-- True
--
-- @since 0.7.0
modNonZero :: (P.Integral a) => a -> NonZero a -> a
modNonZero n nz =
let d = the divisor nz
in bool (P.mod n d) 0 (minusOne d)
{-# INLINE modNonZero #-}
-- | Simultaneous 'divNonZero' and 'modNonZero'. "Prelude"'s @divMod@ is in
-- "Papa.Unsafe.Numeric".
--
-- ==== __Examples__
--
-- >>> fmap (divModNonZero 7) (preview nonZero (2 :: Int))
-- Just (3,1)
--
-- >>> fmap (divModNonZero 7) (preview nonZero (255 :: Word8))
-- Just (0,7)
--
-- >>> check $ withTests 200 $ property $ do n <- forAll genInt; d <- forAll genNonZero; (\(q, m) -> q * the divisor d + m) (divModNonZero n d) === n
-- ...passed 200 tests.
-- True
--
-- At an unsigned type and at 'Numeric.Natural.Natural', it is "Prelude"'s:
--
-- >>> check $ withTests 200 $ property $ do n <- forAll genWord8; d <- forAll genNonZeroWord8; divModNonZero n d === U.divMod n (the divisor d)
-- ...passed 200 tests.
-- True
--
-- >>> check $ withTests 200 $ property $ do n <- forAll genNatural; d <- forAll genNonZeroNatural; divModNonZero n d === U.divMod n (the divisor d)
-- ...passed 200 tests.
-- True
--
-- @since 0.7.0
divModNonZero :: (P.Integral a) => a -> NonZero a -> (a, a)
divModNonZero n nz =
let d = the divisor nz
in bool (P.divMod n d) (P.negate n, 0) (minusOne d)
{-# INLINE divModNonZero #-}
-- | Integer division by a 'NonZero', truncated toward zero. "Prelude"'s
-- @quot@ is in "Papa.Unsafe.Numeric".
--
-- ==== __Examples__
--
-- >>> fmap (quotNonZero (-7)) (preview nonZero (2 :: Int))
-- Just (-3)
--
-- >>> check $ withTests 200 $ property $ do n <- forAll genInt; d <- forAll genNonZero; quotNonZero n d === P.fst (quotRemNonZero n d)
-- ...passed 200 tests.
-- True
--
-- @since 0.7.0
quotNonZero :: (P.Integral a) => a -> NonZero a -> a
quotNonZero n nz =
let d = the divisor nz
in bool (P.quot n d) (P.negate n) (minusOne d)
{-# INLINE quotNonZero #-}
-- | Integer remainder by a 'NonZero', with the sign of the dividend.
-- "Prelude"'s @rem@ is in "Papa.Unsafe.Numeric".
--
-- ==== __Examples__
--
-- >>> fmap (remNonZero (-7)) (preview nonZero (2 :: Int))
-- Just (-1)
--
-- >>> check $ withTests 200 $ property $ do n <- forAll genInt; d <- forAll genNonZero; remNonZero n d === P.snd (quotRemNonZero n d)
-- ...passed 200 tests.
-- True
--
-- @since 0.7.0
remNonZero :: (P.Integral a) => a -> NonZero a -> a
remNonZero n nz =
let d = the divisor nz
in bool (P.rem n d) 0 (minusOne d)
{-# INLINE remNonZero #-}
-- | Simultaneous 'quotNonZero' and 'remNonZero'. "Prelude"'s @quotRem@ is in
-- "Papa.Unsafe.Numeric".
--
-- ==== __Examples__
--
-- >>> fmap (quotRemNonZero (-7)) (preview nonZero (2 :: Int))
-- Just (-3,-1)
--
-- >>> fmap (quotRemNonZero minBound) (preview nonZero (-1 :: Int)) == Just (minBound, 0)
-- True
--
-- >>> check $ withTests 200 $ property $ do n <- forAll genInt; d <- forAll genNonZero; (\(q, r) -> q * the divisor d + r) (quotRemNonZero n d) === n
-- ...passed 200 tests.
-- True
--
-- >>> check $ withTests 200 $ property $ do n <- forAll genWord8; d <- forAll genNonZeroWord8; quotRemNonZero n d === U.quotRem n (the divisor d)
-- ...passed 200 tests.
-- True
--
-- >>> check $ withTests 200 $ property $ do n <- forAll genNatural; d <- forAll genNonZeroNatural; quotRemNonZero n d === U.quotRem n (the divisor d)
-- ...passed 200 tests.
-- True
--
-- @since 0.7.0
quotRemNonZero :: (P.Integral a) => a -> NonZero a -> (a, a)
quotRemNonZero n nz =
let d = the divisor nz
in bool (P.quotRem n d) (P.negate n, 0) (minusOne d)
{-# INLINE quotRemNonZero #-}
-- | Exponentiation by a 'Word' power, which cannot be negative.
-- "Prelude"'s @(^)@, which throws for a negative exponent, is in
-- "Papa.Unsafe.Numeric".
--
-- ==== __Examples__
--
-- >>> pow (2 :: Int) 10
-- 1024
--
-- >>> pow (2 :: Double) 0
-- 1.0
--
-- >>> check $ withTests 100 $ property $ do x <- forAll genInt; e <- forAll (Gen.integral (Range.linear 0 6)); pow x (e + 1) === pow x e * x
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
pow :: (P.Num a) => a -> Word -> a
pow =
(P.^)
{-# INLINEABLE pow #-}
{-# SPECIALIZE pow :: Int -> Word -> Int #-}
{-# SPECIALIZE pow :: Word -> Word -> Word #-}
{-# SPECIALIZE pow :: P.Integer -> Word -> P.Integer #-}
{-# SPECIALIZE pow :: P.Double -> Word -> P.Double #-}