{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeOperators #-}
module Basement.Bounded
( Zn64
, unZn64
, Zn
, unZn
, zn64
, zn
, zn64Nat
, znNat
) where
import GHC.TypeLits
import Data.Word
import Basement.Compat.Base
import Basement.Compat.Natural
import Basement.Numerical.Number
import Data.Proxy
import Basement.Nat
import qualified Prelude
newtype Zn64 (n :: Nat) = Zn64 { Zn64 n -> Word64
unZn64 :: Word64 }
deriving (Int -> Zn64 n -> ShowS
[Zn64 n] -> ShowS
Zn64 n -> String
(Int -> Zn64 n -> ShowS)
-> (Zn64 n -> String) -> ([Zn64 n] -> ShowS) -> Show (Zn64 n)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (n :: Nat). Int -> Zn64 n -> ShowS
forall (n :: Nat). [Zn64 n] -> ShowS
forall (n :: Nat). Zn64 n -> String
showList :: [Zn64 n] -> ShowS
$cshowList :: forall (n :: Nat). [Zn64 n] -> ShowS
show :: Zn64 n -> String
$cshow :: forall (n :: Nat). Zn64 n -> String
showsPrec :: Int -> Zn64 n -> ShowS
$cshowsPrec :: forall (n :: Nat). Int -> Zn64 n -> ShowS
Show,Zn64 n -> Zn64 n -> Bool
(Zn64 n -> Zn64 n -> Bool)
-> (Zn64 n -> Zn64 n -> Bool) -> Eq (Zn64 n)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (n :: Nat). Zn64 n -> Zn64 n -> Bool
/= :: Zn64 n -> Zn64 n -> Bool
$c/= :: forall (n :: Nat). Zn64 n -> Zn64 n -> Bool
== :: Zn64 n -> Zn64 n -> Bool
$c== :: forall (n :: Nat). Zn64 n -> Zn64 n -> Bool
Eq,Eq (Zn64 n)
Eq (Zn64 n)
-> (Zn64 n -> Zn64 n -> Ordering)
-> (Zn64 n -> Zn64 n -> Bool)
-> (Zn64 n -> Zn64 n -> Bool)
-> (Zn64 n -> Zn64 n -> Bool)
-> (Zn64 n -> Zn64 n -> Bool)
-> (Zn64 n -> Zn64 n -> Zn64 n)
-> (Zn64 n -> Zn64 n -> Zn64 n)
-> Ord (Zn64 n)
Zn64 n -> Zn64 n -> Bool
Zn64 n -> Zn64 n -> Ordering
Zn64 n -> Zn64 n -> Zn64 n
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (n :: Nat). Eq (Zn64 n)
forall (n :: Nat). Zn64 n -> Zn64 n -> Bool
forall (n :: Nat). Zn64 n -> Zn64 n -> Ordering
forall (n :: Nat). Zn64 n -> Zn64 n -> Zn64 n
min :: Zn64 n -> Zn64 n -> Zn64 n
$cmin :: forall (n :: Nat). Zn64 n -> Zn64 n -> Zn64 n
max :: Zn64 n -> Zn64 n -> Zn64 n
$cmax :: forall (n :: Nat). Zn64 n -> Zn64 n -> Zn64 n
>= :: Zn64 n -> Zn64 n -> Bool
$c>= :: forall (n :: Nat). Zn64 n -> Zn64 n -> Bool
> :: Zn64 n -> Zn64 n -> Bool
$c> :: forall (n :: Nat). Zn64 n -> Zn64 n -> Bool
<= :: Zn64 n -> Zn64 n -> Bool
$c<= :: forall (n :: Nat). Zn64 n -> Zn64 n -> Bool
< :: Zn64 n -> Zn64 n -> Bool
$c< :: forall (n :: Nat). Zn64 n -> Zn64 n -> Bool
compare :: Zn64 n -> Zn64 n -> Ordering
$ccompare :: forall (n :: Nat). Zn64 n -> Zn64 n -> Ordering
$cp1Ord :: forall (n :: Nat). Eq (Zn64 n)
Ord)
instance (KnownNat n, NatWithinBound Word64 n) => Prelude.Num (Zn64 n) where
fromInteger :: Integer -> Zn64 n
fromInteger = Word64 -> Zn64 n
forall (n :: Nat).
(KnownNat n, NatWithinBound Word64 n) =>
Word64 -> Zn64 n
zn64 (Word64 -> Zn64 n) -> (Integer -> Word64) -> Integer -> Zn64 n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Integer -> Word64
forall a. Num a => Integer -> a
Prelude.fromInteger
+ :: Zn64 n -> Zn64 n -> Zn64 n
(+) = Zn64 n -> Zn64 n -> Zn64 n
forall (n :: Nat).
(KnownNat n, NatWithinBound Word64 n) =>
Zn64 n -> Zn64 n -> Zn64 n
add64
(-) = Zn64 n -> Zn64 n -> Zn64 n
forall (n :: Nat).
(KnownNat n, NatWithinBound Word64 n) =>
Zn64 n -> Zn64 n -> Zn64 n
sub64
* :: Zn64 n -> Zn64 n -> Zn64 n
(*) = Zn64 n -> Zn64 n -> Zn64 n
forall (n :: Nat).
(KnownNat n, NatWithinBound Word64 n) =>
Zn64 n -> Zn64 n -> Zn64 n
mul64
abs :: Zn64 n -> Zn64 n
abs Zn64 n
a = Zn64 n
a
negate :: Zn64 n -> Zn64 n
negate Zn64 n
_ = String -> Zn64 n
forall a. HasCallStack => String -> a
error String
"cannot negate Zn64: use Foundation Numerical hierarchy for this function to not be exposed to Zn64"
signum :: Zn64 n -> Zn64 n
signum (Zn64 Word64
a) = Word64 -> Zn64 n
forall (n :: Nat). Word64 -> Zn64 n
Zn64 (Word64 -> Word64
forall a. Num a => a -> a
Prelude.signum Word64
a)
type instance NatNumMaxBound (Zn64 n) = n
instance (KnownNat n, NatWithinBound Word64 n) => Integral (Zn64 n) where
fromInteger :: Integer -> Zn64 n
fromInteger = Word64 -> Zn64 n
forall (n :: Nat).
(KnownNat n, NatWithinBound Word64 n) =>
Word64 -> Zn64 n
zn64 (Word64 -> Zn64 n) -> (Integer -> Word64) -> Integer -> Zn64 n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Integer -> Word64
forall a. Num a => Integer -> a
Prelude.fromInteger
instance (KnownNat n, NatWithinBound Word64 n) => IsIntegral (Zn64 n) where
toInteger :: Zn64 n -> Integer
toInteger (Zn64 Word64
n) = Word64 -> Integer
forall a. IsIntegral a => a -> Integer
toInteger Word64
n
instance (KnownNat n, NatWithinBound Word64 n) => IsNatural (Zn64 (n :: Nat)) where
toNatural :: Zn64 n -> Natural
toNatural (Zn64 Word64
n) = Word64 -> Natural
forall a. IsNatural a => a -> Natural
toNatural Word64
n
zn64 :: forall n . (KnownNat n, NatWithinBound Word64 n) => Word64 -> Zn64 n
zn64 :: Word64 -> Zn64 n
zn64 Word64
v = Word64 -> Zn64 n
forall (n :: Nat). Word64 -> Zn64 n
Zn64 (Word64
v Word64 -> Word64 -> Word64
forall a. Integral a => a -> a -> a
`Prelude.mod` Proxy n -> Word64
forall (n :: Nat) (proxy :: Nat -> *).
(KnownNat n, NatWithinBound Word64 n) =>
proxy n -> Word64
natValWord64 (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
zn64Nat :: forall m n . (KnownNat m, KnownNat n, NatWithinBound Word64 m, NatWithinBound Word64 n, CmpNat m n ~ 'LT)
=> Proxy m
-> Zn64 n
zn64Nat :: Proxy m -> Zn64 n
zn64Nat Proxy m
p = Word64 -> Zn64 n
forall (n :: Nat). Word64 -> Zn64 n
Zn64 (Proxy m -> Word64
forall (n :: Nat) (proxy :: Nat -> *).
(KnownNat n, NatWithinBound Word64 n) =>
proxy n -> Word64
natValWord64 Proxy m
p)
add64 :: forall n . (KnownNat n, NatWithinBound Word64 n) => Zn64 n -> Zn64 n -> Zn64 n
add64 :: Zn64 n -> Zn64 n -> Zn64 n
add64 (Zn64 Word64
a) (Zn64 Word64
b) = Word64 -> Zn64 n
forall (n :: Nat). Word64 -> Zn64 n
Zn64 ((Word64
a Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
Prelude.+ Word64
b) Word64 -> Word64 -> Word64
forall a. Integral a => a -> a -> a
`Prelude.mod` Proxy n -> Word64
forall (n :: Nat) (proxy :: Nat -> *).
(KnownNat n, NatWithinBound Word64 n) =>
proxy n -> Word64
natValWord64 (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
sub64 :: forall n . (KnownNat n, NatWithinBound Word64 n) => Zn64 n -> Zn64 n -> Zn64 n
sub64 :: Zn64 n -> Zn64 n -> Zn64 n
sub64 (Zn64 Word64
a) (Zn64 Word64
b) = Word64 -> Zn64 n
forall (n :: Nat). Word64 -> Zn64 n
Zn64 ((Word64
a Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
Prelude.- Word64
b) Word64 -> Word64 -> Word64
forall a. Integral a => a -> a -> a
`Prelude.mod` Proxy n -> Word64
forall (n :: Nat) (proxy :: Nat -> *).
(KnownNat n, NatWithinBound Word64 n) =>
proxy n -> Word64
natValWord64 (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
mul64 :: forall n . (KnownNat n, NatWithinBound Word64 n) => Zn64 n -> Zn64 n -> Zn64 n
mul64 :: Zn64 n -> Zn64 n -> Zn64 n
mul64 (Zn64 Word64
a) (Zn64 Word64
b) = Word64 -> Zn64 n
forall (n :: Nat). Word64 -> Zn64 n
Zn64 ((Word64
a Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
Prelude.* Word64
b) Word64 -> Word64 -> Word64
forall a. Integral a => a -> a -> a
`Prelude.mod` Proxy n -> Word64
forall (n :: Nat) (proxy :: Nat -> *).
(KnownNat n, NatWithinBound Word64 n) =>
proxy n -> Word64
natValWord64 (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
newtype Zn (n :: Nat) = Zn { Zn n -> Natural
unZn :: Natural }
deriving (Int -> Zn n -> ShowS
[Zn n] -> ShowS
Zn n -> String
(Int -> Zn n -> ShowS)
-> (Zn n -> String) -> ([Zn n] -> ShowS) -> Show (Zn n)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (n :: Nat). Int -> Zn n -> ShowS
forall (n :: Nat). [Zn n] -> ShowS
forall (n :: Nat). Zn n -> String
showList :: [Zn n] -> ShowS
$cshowList :: forall (n :: Nat). [Zn n] -> ShowS
show :: Zn n -> String
$cshow :: forall (n :: Nat). Zn n -> String
showsPrec :: Int -> Zn n -> ShowS
$cshowsPrec :: forall (n :: Nat). Int -> Zn n -> ShowS
Show,Zn n -> Zn n -> Bool
(Zn n -> Zn n -> Bool) -> (Zn n -> Zn n -> Bool) -> Eq (Zn n)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (n :: Nat). Zn n -> Zn n -> Bool
/= :: Zn n -> Zn n -> Bool
$c/= :: forall (n :: Nat). Zn n -> Zn n -> Bool
== :: Zn n -> Zn n -> Bool
$c== :: forall (n :: Nat). Zn n -> Zn n -> Bool
Eq,Eq (Zn n)
Eq (Zn n)
-> (Zn n -> Zn n -> Ordering)
-> (Zn n -> Zn n -> Bool)
-> (Zn n -> Zn n -> Bool)
-> (Zn n -> Zn n -> Bool)
-> (Zn n -> Zn n -> Bool)
-> (Zn n -> Zn n -> Zn n)
-> (Zn n -> Zn n -> Zn n)
-> Ord (Zn n)
Zn n -> Zn n -> Bool
Zn n -> Zn n -> Ordering
Zn n -> Zn n -> Zn n
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (n :: Nat). Eq (Zn n)
forall (n :: Nat). Zn n -> Zn n -> Bool
forall (n :: Nat). Zn n -> Zn n -> Ordering
forall (n :: Nat). Zn n -> Zn n -> Zn n
min :: Zn n -> Zn n -> Zn n
$cmin :: forall (n :: Nat). Zn n -> Zn n -> Zn n
max :: Zn n -> Zn n -> Zn n
$cmax :: forall (n :: Nat). Zn n -> Zn n -> Zn n
>= :: Zn n -> Zn n -> Bool
$c>= :: forall (n :: Nat). Zn n -> Zn n -> Bool
> :: Zn n -> Zn n -> Bool
$c> :: forall (n :: Nat). Zn n -> Zn n -> Bool
<= :: Zn n -> Zn n -> Bool
$c<= :: forall (n :: Nat). Zn n -> Zn n -> Bool
< :: Zn n -> Zn n -> Bool
$c< :: forall (n :: Nat). Zn n -> Zn n -> Bool
compare :: Zn n -> Zn n -> Ordering
$ccompare :: forall (n :: Nat). Zn n -> Zn n -> Ordering
$cp1Ord :: forall (n :: Nat). Eq (Zn n)
Ord)
instance KnownNat n => Prelude.Num (Zn n) where
fromInteger :: Integer -> Zn n
fromInteger = Natural -> Zn n
forall (n :: Nat). KnownNat n => Natural -> Zn n
zn (Natural -> Zn n) -> (Integer -> Natural) -> Integer -> Zn n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Integer -> Natural
forall a. Num a => Integer -> a
Prelude.fromInteger
+ :: Zn n -> Zn n -> Zn n
(+) = Zn n -> Zn n -> Zn n
forall (n :: Nat). KnownNat n => Zn n -> Zn n -> Zn n
add
(-) = Zn n -> Zn n -> Zn n
forall (n :: Nat). KnownNat n => Zn n -> Zn n -> Zn n
sub
* :: Zn n -> Zn n -> Zn n
(*) = Zn n -> Zn n -> Zn n
forall (n :: Nat). KnownNat n => Zn n -> Zn n -> Zn n
mul
abs :: Zn n -> Zn n
abs Zn n
a = Zn n
a
negate :: Zn n -> Zn n
negate Zn n
_ = String -> Zn n
forall a. HasCallStack => String -> a
error String
"cannot negate Zn: use Foundation Numerical hierarchy for this function to not be exposed to Zn"
signum :: Zn n -> Zn n
signum = Natural -> Zn n
forall (n :: Nat). Natural -> Zn n
Zn (Natural -> Zn n) -> (Zn n -> Natural) -> Zn n -> Zn n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Natural -> Natural
forall a. Num a => a -> a
Prelude.signum (Natural -> Natural) -> (Zn n -> Natural) -> Zn n -> Natural
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Zn n -> Natural
forall (n :: Nat). Zn n -> Natural
unZn
type instance NatNumMaxBound (Zn n) = n
instance KnownNat n => Integral (Zn n) where
fromInteger :: Integer -> Zn n
fromInteger = Natural -> Zn n
forall (n :: Nat). KnownNat n => Natural -> Zn n
zn (Natural -> Zn n) -> (Integer -> Natural) -> Integer -> Zn n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Integer -> Natural
forall a. Num a => Integer -> a
Prelude.fromInteger
instance KnownNat n => IsIntegral (Zn n) where
toInteger :: Zn n -> Integer
toInteger (Zn Natural
n) = Natural -> Integer
forall a. IsIntegral a => a -> Integer
toInteger Natural
n
instance KnownNat n => IsNatural (Zn n) where
toNatural :: Zn n -> Natural
toNatural Zn n
i = Zn n -> Natural
forall (n :: Nat). Zn n -> Natural
unZn Zn n
i
zn :: forall n . KnownNat n => Natural -> Zn n
zn :: Natural -> Zn n
zn Natural
v = Natural -> Zn n
forall (n :: Nat). Natural -> Zn n
Zn (Natural
v Natural -> Natural -> Natural
forall a. Integral a => a -> a -> a
`Prelude.mod` Proxy n -> Natural
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Natural
natValNatural (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
znNat :: forall m n . (KnownNat m, KnownNat n, CmpNat m n ~ 'LT) => Proxy m -> Zn n
znNat :: Proxy m -> Zn n
znNat Proxy m
m = Natural -> Zn n
forall (n :: Nat). Natural -> Zn n
Zn (Proxy m -> Natural
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Natural
natValNatural Proxy m
m)
add :: forall n . KnownNat n => Zn n -> Zn n -> Zn n
add :: Zn n -> Zn n -> Zn n
add (Zn Natural
a) (Zn Natural
b) = Natural -> Zn n
forall (n :: Nat). Natural -> Zn n
Zn ((Natural
a Natural -> Natural -> Natural
forall a. Num a => a -> a -> a
Prelude.+ Natural
b) Natural -> Natural -> Natural
forall a. Integral a => a -> a -> a
`Prelude.mod` Proxy n -> Natural
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Natural
natValNatural (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
sub :: forall n . KnownNat n => Zn n -> Zn n -> Zn n
sub :: Zn n -> Zn n -> Zn n
sub (Zn Natural
a) (Zn Natural
b) = Natural -> Zn n
forall (n :: Nat). Natural -> Zn n
Zn ((Natural
a Natural -> Natural -> Natural
forall a. Num a => a -> a -> a
Prelude.- Natural
b) Natural -> Natural -> Natural
forall a. Integral a => a -> a -> a
`Prelude.mod` Proxy n -> Natural
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Natural
natValNatural (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))
mul :: forall n . KnownNat n => Zn n -> Zn n -> Zn n
mul :: Zn n -> Zn n -> Zn n
mul (Zn Natural
a) (Zn Natural
b) = Natural -> Zn n
forall (n :: Nat). Natural -> Zn n
Zn ((Natural
a Natural -> Natural -> Natural
forall a. Num a => a -> a -> a
Prelude.* Natural
b) Natural -> Natural -> Natural
forall a. Integral a => a -> a -> a
`Prelude.mod` Proxy n -> Natural
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Natural
natValNatural (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n))