{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_HADDOCK not-home #-}
module I.Autogen.CUShort () 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 CUShort = MinT CUShort
type instance MaxR CUShort = MaxT CUShort
instance forall l r.
( IntervalCtx CUShort l r
) => Interval CUShort l r where
type IntervalCtx CUShort l r =
( L.KnownNat l
, L.KnownNat r
, MinT CUShort <= l
, l <= r
, r <= MaxT CUShort )
type MinI CUShort l r = l
type MaxI CUShort l r = r
inhabitant :: I CUShort l r
inhabitant = I CUShort l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min
from :: CUShort -> Maybe (I CUShort l r)
from = \CUShort
x -> CUShort -> I CUShort l r
forall x (l :: L x) (r :: R x). x -> I x l r
unsafest CUShort
x I CUShort l r -> Maybe () -> Maybe (I CUShort 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 (CUShort
l CUShort -> CUShort -> Bool
forall a. Ord a => a -> a -> Bool
<= CUShort
x Bool -> Bool -> Bool
&& CUShort
x CUShort -> CUShort -> Bool
forall a. Ord a => a -> a -> Bool
<= CUShort
r)
where l :: CUShort
l = Integer -> CUShort
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)) :: CUShort
r :: CUShort
r = Integer -> CUShort
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)) :: CUShort
(I CUShort l r -> CUShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUShort
a) plus' :: I CUShort l r -> I CUShort l r -> Maybe (I CUShort l r)
`plus'` (I CUShort l r -> CUShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUShort
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUShort
b CUShort -> CUShort -> Bool
forall a. Ord a => a -> a -> Bool
<= CUShort
forall a. Bounded a => a
maxBound CUShort -> CUShort -> CUShort
forall a. Num a => a -> a -> a
- CUShort
a)
CUShort -> Maybe (I CUShort l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUShort
a CUShort -> CUShort -> CUShort
forall a. Num a => a -> a -> a
+ CUShort
b)
(I CUShort l r -> CUShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUShort
a) mult' :: I CUShort l r -> I CUShort l r -> Maybe (I CUShort l r)
`mult'` (I CUShort l r -> CUShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUShort
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUShort
b CUShort -> CUShort -> Bool
forall a. Eq a => a -> a -> Bool
== CUShort
0 Bool -> Bool -> Bool
|| CUShort
a CUShort -> CUShort -> Bool
forall a. Ord a => a -> a -> Bool
<= CUShort
forall a. Bounded a => a
maxBound CUShort -> CUShort -> CUShort
forall a. Integral a => a -> a -> a
`quot` CUShort
b)
CUShort -> Maybe (I CUShort l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUShort
a CUShort -> CUShort -> CUShort
forall a. Num a => a -> a -> a
* CUShort
b)
(I CUShort l r -> CUShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUShort
a) minus' :: I CUShort l r -> I CUShort l r -> Maybe (I CUShort l r)
`minus'` (I CUShort l r -> CUShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUShort
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUShort
b CUShort -> CUShort -> Bool
forall a. Ord a => a -> a -> Bool
<= CUShort
a)
CUShort -> Maybe (I CUShort l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUShort
a CUShort -> CUShort -> CUShort
forall a. Num a => a -> a -> a
- CUShort
b)
(I CUShort l r -> CUShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUShort
a) div' :: I CUShort l r -> I CUShort l r -> Maybe (I CUShort l r)
`div'` (I CUShort l r -> CUShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUShort
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUShort
b CUShort -> CUShort -> Bool
forall a. Eq a => a -> a -> Bool
/= CUShort
0)
let (CUShort
q, CUShort
m) = CUShort -> CUShort -> (CUShort, CUShort)
forall a. Integral a => a -> a -> (a, a)
divMod CUShort
a CUShort
b
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUShort
m CUShort -> CUShort -> Bool
forall a. Eq a => a -> a -> Bool
== CUShort
0)
CUShort -> Maybe (I CUShort l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from CUShort
q
instance (Interval CUShort l r) => Clamp CUShort l r
instance (Interval CUShort ld rd, Interval CUShort lu ru, lu <= ld, rd <= ru)
=> Up CUShort ld rd lu ru
instance forall l r t.
( Interval CUShort l r, KnownCtx CUShort l r t
) => Known CUShort l r t where
type KnownCtx CUShort l r t = (L.KnownNat t, l <= t, t <= r)
known' :: Proxy t -> I CUShort l r
known' = CUShort -> I CUShort l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (CUShort -> I CUShort l r)
-> (Proxy t -> CUShort) -> Proxy t -> I CUShort l r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> CUShort
forall a. Num a => Integer -> a
fromInteger (Integer -> CUShort) -> (Proxy t -> Integer) -> Proxy t -> CUShort
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 CUShort l r) => With CUShort l r where
with :: forall b.
I CUShort l r
-> (forall (t :: T CUShort). Known CUShort l r t => Proxy t -> b)
-> b
with I CUShort l r
x forall (t :: T CUShort). Known CUShort 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 (CUShort -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUShort l r -> CUShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CUShort 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 CUShort). Known CUShort l r t => Proxy t -> b
g Proxy n
Proxy n
pt)
instance (Interval CUShort l r, l /= r) => Discrete CUShort l r where
pred' :: I CUShort l r -> Maybe (I CUShort l r)
pred' I CUShort l r
i = CUShort -> I CUShort l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I CUShort l r -> CUShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CUShort l r
i CUShort -> CUShort -> CUShort
forall a. Num a => a -> a -> a
- CUShort
1) I CUShort l r -> Maybe () -> Maybe (I CUShort 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 CUShort l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min I CUShort l r -> I CUShort l r -> Bool
forall a. Ord a => a -> a -> Bool
< I CUShort l r
i)
succ' :: I CUShort l r -> Maybe (I CUShort l r)
succ' I CUShort l r
i = CUShort -> I CUShort l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I CUShort l r -> CUShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CUShort l r
i CUShort -> CUShort -> CUShort
forall a. Num a => a -> a -> a
+ CUShort
1) I CUShort l r -> Maybe () -> Maybe (I CUShort 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 CUShort l r
i I CUShort l r -> I CUShort l r -> Bool
forall a. Ord a => a -> a -> Bool
< I CUShort 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 CUShort 0 r) => Zero CUShort 0 r where
zero :: I CUShort 0 r
zero = CUShort -> I CUShort 0 r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe CUShort
0
instance (Interval CUShort l r, l <= 1, 1 <= r) => One CUShort l r where
one :: I CUShort l r
one = CUShort -> I CUShort l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe CUShort
1
instance forall l r. (Interval CUShort l r) => Shove CUShort l r where
shove :: CUShort -> I CUShort l r
shove = \CUShort
x -> CUShort -> I CUShort l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (CUShort -> I CUShort l r) -> CUShort -> I CUShort l r
forall a b. (a -> b) -> a -> b
$ Integer -> CUShort
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
mod (CUShort -> Integer
forall a. Integral a => a -> Integer
toInteger CUShort
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 = CUShort -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUShort l r -> CUShort
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 @CUShort @l @r))
r :: Integer
r = CUShort -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUShort l r -> CUShort
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 @CUShort @l @r))