{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_HADDOCK not-home #-}
module I.Autogen.CUIntMax () where
import Control.Monad
import Data.Constraint
import Data.Maybe
import Data.Proxy
import Data.Word
import Data.Type.Ord
import Foreign.C.Types
import GHC.TypeLits qualified as L
import KindInteger (type (/=))
import Prelude hiding (min, max, div)
import I.Internal
_ignore :: (CSize, Word)
_ignore :: (CSize, Word)
_ignore = (CSize
0, Word
0)
type instance MinL CUIntMax = MinT CUIntMax
type instance MaxR CUIntMax = MaxT CUIntMax
instance forall l r.
( IntervalCtx CUIntMax l r
) => Interval CUIntMax l r where
type IntervalCtx CUIntMax l r =
( L.KnownNat l
, L.KnownNat r
, MinT CUIntMax <= l
, l <= r
, r <= MaxT CUIntMax )
type MinI CUIntMax l r = l
type MaxI CUIntMax l r = r
inhabitant :: I CUIntMax l r
inhabitant = I CUIntMax l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min
from :: CUIntMax -> Maybe (I CUIntMax l r)
from = \CUIntMax
x -> CUIntMax -> I CUIntMax l r
forall x (l :: L x) (r :: R x). x -> I x l r
unsafest CUIntMax
x I CUIntMax l r -> Maybe () -> Maybe (I CUIntMax l r)
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUIntMax
l CUIntMax -> CUIntMax -> Bool
forall a. Ord a => a -> a -> Bool
<= CUIntMax
x Bool -> Bool -> Bool
&& CUIntMax
x CUIntMax -> CUIntMax -> Bool
forall a. Ord a => a -> a -> Bool
<= CUIntMax
r)
where l :: CUIntMax
l = Integer -> CUIntMax
forall a. Num a => Integer -> a
fromInteger (Proxy l -> Integer
forall (n :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
L.natVal (forall (t :: Natural). Proxy t
forall {k} (t :: k). Proxy t
Proxy @l)) :: CUIntMax
r :: CUIntMax
r = Integer -> CUIntMax
forall a. Num a => Integer -> a
fromInteger (Proxy r -> Integer
forall (n :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
L.natVal (forall (t :: Natural). Proxy t
forall {k} (t :: k). Proxy t
Proxy @r)) :: CUIntMax
(I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntMax
a) plus' :: I CUIntMax l r -> I CUIntMax l r -> Maybe (I CUIntMax l r)
`plus'` (I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntMax
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUIntMax
b CUIntMax -> CUIntMax -> Bool
forall a. Ord a => a -> a -> Bool
<= CUIntMax
forall a. Bounded a => a
maxBound CUIntMax -> CUIntMax -> CUIntMax
forall a. Num a => a -> a -> a
- CUIntMax
a)
CUIntMax -> Maybe (I CUIntMax l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUIntMax
a CUIntMax -> CUIntMax -> CUIntMax
forall a. Num a => a -> a -> a
+ CUIntMax
b)
(I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntMax
a) mult' :: I CUIntMax l r -> I CUIntMax l r -> Maybe (I CUIntMax l r)
`mult'` (I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntMax
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUIntMax
b CUIntMax -> CUIntMax -> Bool
forall a. Eq a => a -> a -> Bool
== CUIntMax
0 Bool -> Bool -> Bool
|| CUIntMax
a CUIntMax -> CUIntMax -> Bool
forall a. Ord a => a -> a -> Bool
<= CUIntMax
forall a. Bounded a => a
maxBound CUIntMax -> CUIntMax -> CUIntMax
forall a. Integral a => a -> a -> a
`quot` CUIntMax
b)
CUIntMax -> Maybe (I CUIntMax l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUIntMax
a CUIntMax -> CUIntMax -> CUIntMax
forall a. Num a => a -> a -> a
* CUIntMax
b)
(I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntMax
a) minus' :: I CUIntMax l r -> I CUIntMax l r -> Maybe (I CUIntMax l r)
`minus'` (I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntMax
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUIntMax
b CUIntMax -> CUIntMax -> Bool
forall a. Ord a => a -> a -> Bool
<= CUIntMax
a)
CUIntMax -> Maybe (I CUIntMax l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUIntMax
a CUIntMax -> CUIntMax -> CUIntMax
forall a. Num a => a -> a -> a
- CUIntMax
b)
(I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntMax
a) div' :: I CUIntMax l r -> I CUIntMax l r -> Maybe (I CUIntMax l r)
`div'` (I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntMax
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUIntMax
b CUIntMax -> CUIntMax -> Bool
forall a. Eq a => a -> a -> Bool
/= CUIntMax
0)
let (CUIntMax
q, CUIntMax
m) = CUIntMax -> CUIntMax -> (CUIntMax, CUIntMax)
forall a. Integral a => a -> a -> (a, a)
divMod CUIntMax
a CUIntMax
b
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUIntMax
m CUIntMax -> CUIntMax -> Bool
forall a. Eq a => a -> a -> Bool
== CUIntMax
0)
CUIntMax -> Maybe (I CUIntMax l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from CUIntMax
q
instance (Interval CUIntMax l r) => Clamp CUIntMax l r
instance (Interval CUIntMax ld rd, Interval CUIntMax lu ru, lu <= ld, rd <= ru)
=> Up CUIntMax ld rd lu ru
instance forall l r t.
( Interval CUIntMax l r, KnownCtx CUIntMax l r t
) => Known CUIntMax l r t where
type KnownCtx CUIntMax l r t = (L.KnownNat t, l <= t, t <= r)
known' :: Proxy t -> I CUIntMax l r
known' = CUIntMax -> I CUIntMax l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (CUIntMax -> I CUIntMax l r)
-> (Proxy t -> CUIntMax) -> Proxy t -> I CUIntMax l r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> CUIntMax
forall a. Num a => Integer -> a
fromInteger (Integer -> CUIntMax)
-> (Proxy t -> Integer) -> Proxy t -> CUIntMax
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy t -> Integer
forall (n :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
L.natVal
instance forall l r. (Interval CUIntMax l r) => With CUIntMax l r where
with :: forall b.
I CUIntMax l r
-> (forall (t :: T CUIntMax). Known CUIntMax l r t => Proxy t -> b)
-> b
with I CUIntMax l r
x forall (t :: T CUIntMax). Known CUIntMax l r t => Proxy t -> b
g = b -> Maybe b -> b
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> b
forall a. HasCallStack => [Char] -> a
error [Char]
"I.with: impossible") (Maybe b -> b) -> Maybe b -> b
forall a b. (a -> b) -> a -> b
$ do
L.SomeNat (Proxy n
pt :: Proxy t) <- Integer -> Maybe SomeNat
L.someNatVal (CUIntMax -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CUIntMax l r
x))
Dict
(Assert (OrdCond (CmpNat l n) 'True 'True 'False) (TypeError ...))
Dict <- forall (a :: Natural) (b :: Natural).
(KnownNat a, KnownNat b) =>
Maybe (Dict (a <= b))
leNatural @l @t
Dict
(Assert (OrdCond (CmpNat n r) 'True 'True 'False) (TypeError ...))
Dict <- forall (a :: Natural) (b :: Natural).
(KnownNat a, KnownNat b) =>
Maybe (Dict (a <= b))
leNatural @t @r
b -> Maybe b
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Proxy n -> b
forall (t :: T CUIntMax). Known CUIntMax l r t => Proxy t -> b
g Proxy n
Proxy n
pt)
instance (Interval CUIntMax l r, l /= r) => Discrete CUIntMax l r where
pred' :: I CUIntMax l r -> Maybe (I CUIntMax l r)
pred' I CUIntMax l r
i = CUIntMax -> I CUIntMax l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CUIntMax l r
i CUIntMax -> CUIntMax -> CUIntMax
forall a. Num a => a -> a -> a
- CUIntMax
1) I CUIntMax l r -> Maybe () -> Maybe (I CUIntMax l r)
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (I CUIntMax l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min I CUIntMax l r -> I CUIntMax l r -> Bool
forall a. Ord a => a -> a -> Bool
< I CUIntMax l r
i)
succ' :: I CUIntMax l r -> Maybe (I CUIntMax l r)
succ' I CUIntMax l r
i = CUIntMax -> I CUIntMax l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CUIntMax l r
i CUIntMax -> CUIntMax -> CUIntMax
forall a. Num a => a -> a -> a
+ CUIntMax
1) I CUIntMax l r -> Maybe () -> Maybe (I CUIntMax l r)
forall a b. a -> Maybe b -> Maybe a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (I CUIntMax l r
i I CUIntMax l r -> I CUIntMax l r -> Bool
forall a. Ord a => a -> a -> Bool
< I CUIntMax l r
forall x (l :: L x) (r :: R x). Known x l r (MaxI x l r) => I x l r
max)
instance (Interval CUIntMax 0 r) => Zero CUIntMax 0 r where
zero :: I CUIntMax 0 r
zero = CUIntMax -> I CUIntMax 0 r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe CUIntMax
0
instance (Interval CUIntMax l r, l <= 1, 1 <= r) => One CUIntMax l r where
one :: I CUIntMax l r
one = CUIntMax -> I CUIntMax l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe CUIntMax
1
instance forall l r. (Interval CUIntMax l r) => Shove CUIntMax l r where
shove :: CUIntMax -> I CUIntMax l r
shove = \CUIntMax
x -> CUIntMax -> I CUIntMax l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (CUIntMax -> I CUIntMax l r) -> CUIntMax -> I CUIntMax l r
forall a b. (a -> b) -> a -> b
$ Integer -> CUIntMax
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
mod (CUIntMax -> Integer
forall a. Integral a => a -> Integer
toInteger CUIntMax
x) (Integer
r Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
l Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
l)
where l :: Integer
l = CUIntMax -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap (forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min @CUIntMax @l @r))
r :: Integer
r = CUIntMax -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUIntMax l r -> CUIntMax
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap (forall x (l :: L x) (r :: R x). Known x l r (MaxI x l r) => I x l r
max @CUIntMax @l @r))