{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE UndecidableInstances #-}

{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_HADDOCK not-home #-}

module I.Autogen.CShort () where

import Control.Monad
import Data.Constraint
import Data.Int
import Data.Maybe
import Data.Proxy
import Data.Type.Ord
import Foreign.C.Types
import KindInteger (type (/=), type (==))
import KindInteger qualified as K
import Prelude hiding (min, max, div)
import Prelude qualified as P

import I.Internal

--------------------------------------------------------------------------------

-- | This is so that GHC doesn't complain about the unused modules,
-- which we import here so that `genmodules.sh` doesn't have to add it
-- to the generated modules.
_ignore :: (CSize, Int)
_ignore :: (CSize, Int)
_ignore = (CSize
0, Int
0)

--------------------------------------------------------------------------------

type instance MinL CShort = MinT CShort
type instance MaxR CShort = MaxT CShort

instance forall (l :: K.Integer) (r :: K.Integer).
  ( IntervalCtx CShort l r
  ) => Interval CShort l r where
  type IntervalCtx CShort l r =
    ( K.KnownInteger l
    , K.KnownInteger r
    , MinT CShort <= l
    , l <= r
    , r <= MaxT CShort )
  type MinI CShort l r = l
  type MaxI CShort l r = r
  inhabitant :: I CShort l r
inhabitant = I CShort l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min
  from :: CShort -> Maybe (I CShort l r)
from = \CShort
x -> CShort -> I CShort l r
forall x (l :: L x) (r :: R x). x -> I x l r
unsafest CShort
x I CShort l r -> Maybe () -> Maybe (I CShort 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 (CShort
l CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
<= CShort
x Bool -> Bool -> Bool
&& CShort
x CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
<= CShort
r)
    where l :: CShort
l = Integer -> CShort
forall a. Num a => Integer -> a
fromInteger (Proxy l -> Integer
forall (i :: Integer) (proxy :: Integer -> *).
KnownInteger i =>
proxy i -> Integer
K.integerVal (forall {k} (t :: k). Proxy t
forall (t :: Integer). Proxy t
Proxy @l)) :: CShort
          r :: CShort
r = Integer -> CShort
forall a. Num a => Integer -> a
fromInteger (Proxy r -> Integer
forall (i :: Integer) (proxy :: Integer -> *).
KnownInteger i =>
proxy i -> Integer
K.integerVal (forall {k} (t :: k). Proxy t
forall (t :: Integer). Proxy t
Proxy @r)) :: CShort
  negate' :: I CShort l r -> Maybe (I CShort l r)
negate' (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CShort
x) = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CShort
x CShort -> CShort -> Bool
forall a. Eq a => a -> a -> Bool
/= CShort
forall a. Bounded a => a
minBound)
    CShort -> Maybe (I CShort l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CShort -> CShort
forall a. Num a => a -> a
P.negate CShort
x)
  (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CShort
a) plus' :: I CShort l r -> I CShort l r -> Maybe (I CShort l r)
`plus'` (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CShort
b)
    | CShort
b CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
> CShort
0 Bool -> Bool -> Bool
&& CShort
a CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
> CShort
forall a. Bounded a => a
maxBound CShort -> CShort -> CShort
forall a. Num a => a -> a -> a
- CShort
b = Maybe (I CShort l r)
forall a. Maybe a
Nothing
    | CShort
b CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
< CShort
0 Bool -> Bool -> Bool
&& CShort
a CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
< CShort
forall a. Bounded a => a
minBound CShort -> CShort -> CShort
forall a. Num a => a -> a -> a
- CShort
b = Maybe (I CShort l r)
forall a. Maybe a
Nothing
    | Bool
otherwise                 = CShort -> Maybe (I CShort l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CShort
a CShort -> CShort -> CShort
forall a. Num a => a -> a -> a
+ CShort
b)
  (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CShort
a) mult' :: I CShort l r -> I CShort l r -> Maybe (I CShort l r)
`mult'` (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CShort
b) = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> Bool -> Maybe ()
forall a b. (a -> b) -> a -> b
$ case CShort
a CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
<= CShort
0 of
      Bool
True  | CShort
b CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
<= CShort
0    -> CShort
a CShort -> CShort -> Bool
forall a. Eq a => a -> a -> Bool
== CShort
0 Bool -> Bool -> Bool
|| CShort
b CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
>= (CShort
forall a. Bounded a => a
maxBound CShort -> CShort -> CShort
forall a. Integral a => a -> a -> a
`quot` CShort
a)
            | Bool
otherwise -> CShort
a CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
>= (CShort
forall a. Bounded a => a
minBound CShort -> CShort -> CShort
forall a. Integral a => a -> a -> a
`quot` CShort
b)
      Bool
False | CShort
b CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
<= CShort
0    -> CShort
b CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
>= (CShort
forall a. Bounded a => a
minBound CShort -> CShort -> CShort
forall a. Integral a => a -> a -> a
`quot` CShort
a)
            | Bool
otherwise -> CShort
a CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
<= (CShort
forall a. Bounded a => a
maxBound CShort -> CShort -> CShort
forall a. Integral a => a -> a -> a
`quot` CShort
b)
    CShort -> Maybe (I CShort l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CShort
a CShort -> CShort -> CShort
forall a. Num a => a -> a -> a
* CShort
b)
  (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CShort
a) minus' :: I CShort l r -> I CShort l r -> Maybe (I CShort l r)
`minus'` (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CShort
b)
    | CShort
b CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
> CShort
0 Bool -> Bool -> Bool
&& CShort
a CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
< CShort
forall a. Bounded a => a
minBound CShort -> CShort -> CShort
forall a. Num a => a -> a -> a
+ CShort
b = Maybe (I CShort l r)
forall a. Maybe a
Nothing
    | CShort
b CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
< CShort
0 Bool -> Bool -> Bool
&& CShort
a CShort -> CShort -> Bool
forall a. Ord a => a -> a -> Bool
> CShort
forall a. Bounded a => a
maxBound CShort -> CShort -> CShort
forall a. Num a => a -> a -> a
+ CShort
b = Maybe (I CShort l r)
forall a. Maybe a
Nothing
    | Bool
otherwise                 = CShort -> Maybe (I CShort l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CShort
a CShort -> CShort -> CShort
forall a. Num a => a -> a -> a
- CShort
b)
  (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CShort
a) div' :: I CShort l r -> I CShort l r -> Maybe (I CShort l r)
`div'` (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CShort
b) = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CShort
b CShort -> CShort -> Bool
forall a. Eq a => a -> a -> Bool
/= CShort
0 Bool -> Bool -> Bool
&& (CShort
b CShort -> CShort -> Bool
forall a. Eq a => a -> a -> Bool
/= -CShort
1 Bool -> Bool -> Bool
|| CShort
a CShort -> CShort -> Bool
forall a. Eq a => a -> a -> Bool
/= CShort
forall a. Bounded a => a
minBound))
    let (CShort
q, CShort
m) = CShort -> CShort -> (CShort, CShort)
forall a. Integral a => a -> a -> (a, a)
divMod CShort
a CShort
b
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CShort
m CShort -> CShort -> Bool
forall a. Eq a => a -> a -> Bool
== CShort
0)
    CShort -> Maybe (I CShort l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from CShort
q

instance (Interval CShort l r) => Clamp CShort l r

instance (Interval CShort ld rd, Interval CShort lu ru, lu <= ld, rd <= ru)
  => Up CShort ld rd lu ru

instance forall l r t.
  ( Interval CShort l r, KnownCtx CShort l r t
  ) => Known CShort l r t where
  type KnownCtx CShort l r t = (K.KnownInteger t, l <= t, t <= r)
  known' :: Proxy t -> I CShort l r
known' = CShort -> I CShort l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (CShort -> I CShort l r)
-> (Proxy t -> CShort) -> Proxy t -> I CShort l r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> CShort
forall a. Num a => Integer -> a
fromInteger (Integer -> CShort) -> (Proxy t -> Integer) -> Proxy t -> CShort
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy t -> Integer
forall (i :: Integer) (proxy :: Integer -> *).
KnownInteger i =>
proxy i -> Integer
K.integerVal

instance forall l r. (Interval CShort l r) => With CShort l r where
  with :: forall b.
I CShort l r
-> (forall (t :: T CShort). Known CShort l r t => Proxy t -> b)
-> b
with I CShort l r
x forall (t :: T CShort). Known CShort l r t => Proxy t -> b
g = case Integer -> SomeInteger
K.someIntegerVal (CShort -> Integer
forall a. Integral a => a -> Integer
toInteger (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CShort l r
x)) of
    K.SomeInteger (Proxy n
pt :: Proxy t) ->
      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
        Dict
  (Assert
     (OrdCond
        (CmpInteger_ (Normalize l) (Normalize n)) 'True 'True 'False)
     (TypeError ...))
Dict <- forall (a :: Integer) (b :: Integer).
(KnownInteger a, KnownInteger b) =>
Maybe (Dict (a <= b))
leInteger @l @t
        Dict
  (Assert
     (OrdCond
        (CmpInteger_ (Normalize n) (Normalize r)) 'True 'True 'False)
     (TypeError ...))
Dict <- forall (a :: Integer) (b :: Integer).
(KnownInteger a, KnownInteger b) =>
Maybe (Dict (a <= b))
leInteger @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 CShort). Known CShort l r t => Proxy t -> b
g Proxy n
Proxy n
pt)

instance (Interval CShort l r, l /= r) => Discrete CShort l r where
  pred' :: I CShort l r -> Maybe (I CShort l r)
pred' I CShort l r
i = CShort -> I CShort l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CShort l r
i CShort -> CShort -> CShort
forall a. Num a => a -> a -> a
- CShort
1) I CShort l r -> Maybe () -> Maybe (I CShort 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 CShort l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min I CShort l r -> I CShort l r -> Bool
forall a. Ord a => a -> a -> Bool
< I CShort l r
i)
  succ' :: I CShort l r -> Maybe (I CShort l r)
succ' I CShort l r
i = CShort -> I CShort l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CShort l r
i CShort -> CShort -> CShort
forall a. Num a => a -> a -> a
+ CShort
1) I CShort l r -> Maybe () -> Maybe (I CShort 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 CShort l r
i I CShort l r -> I CShort l r -> Bool
forall a. Ord a => a -> a -> Bool
< I CShort l r
forall x (l :: L x) (r :: R x). Known x l r (MaxI x l r) => I x l r
max)

instance (Zero CShort l r, l == K.Negate r) => Negate CShort l r where
  negate :: I CShort l r -> I CShort l r
negate = CShort -> I CShort l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (CShort -> I CShort l r)
-> (I CShort l r -> CShort) -> I CShort l r -> I CShort l r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CShort -> CShort
forall a. Num a => a -> a
P.negate (CShort -> CShort)
-> (I CShort l r -> CShort) -> I CShort l r -> CShort
forall b c a. (b -> c) -> (a -> b) -> a -> c
. I CShort l r -> CShort
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap

instance (Interval CShort l r, l <= K.P 0, K.P 0 <= r) => Zero CShort l r where
  zero :: I CShort l r
zero = CShort -> I CShort l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe CShort
0

instance (Interval CShort l r, l <= K.P 1, K.P 1 <= r) => One CShort l r where
  one :: I CShort l r
one = CShort -> I CShort l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe CShort
1

instance forall l r. (Interval CShort l r) => Shove CShort l r where
  shove :: CShort -> I CShort l r
shove = \CShort
x -> I CShort l r -> Maybe (I CShort l r) -> I CShort l r
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> I CShort l r
forall a. HasCallStack => [Char] -> a
error [Char]
"shove(CShort): impossible") (Maybe (I CShort l r) -> I CShort l r)
-> Maybe (I CShort l r) -> I CShort l r
forall a b. (a -> b) -> a -> b
$
                  CShort -> Maybe (I CShort l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CShort -> Maybe (I CShort l r)) -> CShort -> Maybe (I CShort l r)
forall a b. (a -> b) -> a -> b
$ Integer -> CShort
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
mod (CShort -> Integer
forall a. Integral a => a -> Integer
toInteger CShort
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 = CShort -> Integer
forall a. Integral a => a -> Integer
toInteger (I CShort l r -> CShort
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 @CShort @l @r))
          r :: Integer
r = CShort -> Integer
forall a. Integral a => a -> Integer
toInteger (I CShort l r -> CShort
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 @CShort @l @r))