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

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

module I.Autogen.CUChar () 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

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

-- | 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, Word)
_ignore :: (CSize, Word)
_ignore = (CSize
0, Word
0)

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


type instance MinL CUChar = MinT CUChar
type instance MaxR CUChar = MaxT CUChar

instance forall l r.
  ( IntervalCtx CUChar l r
  ) => Interval CUChar l r where
  type IntervalCtx CUChar l r =
    ( L.KnownNat l
    , L.KnownNat r
    , MinT CUChar <= l
    , l <= r
    , r <= MaxT CUChar )
  type MinI CUChar l r = l
  type MaxI CUChar l r = r
  inhabitant :: I CUChar l r
inhabitant = I CUChar l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min
  from :: CUChar -> Maybe (I CUChar l r)
from = \CUChar
x -> CUChar -> I CUChar l r
forall x (l :: L x) (r :: R x). x -> I x l r
unsafest CUChar
x I CUChar l r -> Maybe () -> Maybe (I CUChar 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 (CUChar
l CUChar -> CUChar -> Bool
forall a. Ord a => a -> a -> Bool
<= CUChar
x Bool -> Bool -> Bool
&& CUChar
x CUChar -> CUChar -> Bool
forall a. Ord a => a -> a -> Bool
<= CUChar
r)
    where l :: CUChar
l = Integer -> CUChar
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)) :: CUChar
          r :: CUChar
r = Integer -> CUChar
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)) :: CUChar
  (I CUChar l r -> CUChar
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUChar
a) plus' :: I CUChar l r -> I CUChar l r -> Maybe (I CUChar l r)
`plus'` (I CUChar l r -> CUChar
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUChar
b) = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUChar
b CUChar -> CUChar -> Bool
forall a. Ord a => a -> a -> Bool
<= CUChar
forall a. Bounded a => a
maxBound CUChar -> CUChar -> CUChar
forall a. Num a => a -> a -> a
- CUChar
a)
    CUChar -> Maybe (I CUChar l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUChar
a CUChar -> CUChar -> CUChar
forall a. Num a => a -> a -> a
+ CUChar
b)
  (I CUChar l r -> CUChar
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUChar
a) mult' :: I CUChar l r -> I CUChar l r -> Maybe (I CUChar l r)
`mult'` (I CUChar l r -> CUChar
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUChar
b) = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUChar
b CUChar -> CUChar -> Bool
forall a. Eq a => a -> a -> Bool
== CUChar
0 Bool -> Bool -> Bool
|| CUChar
a CUChar -> CUChar -> Bool
forall a. Ord a => a -> a -> Bool
<= CUChar
forall a. Bounded a => a
maxBound CUChar -> CUChar -> CUChar
forall a. Integral a => a -> a -> a
`quot` CUChar
b)
    CUChar -> Maybe (I CUChar l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUChar
a CUChar -> CUChar -> CUChar
forall a. Num a => a -> a -> a
* CUChar
b)
  (I CUChar l r -> CUChar
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUChar
a) minus' :: I CUChar l r -> I CUChar l r -> Maybe (I CUChar l r)
`minus'` (I CUChar l r -> CUChar
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUChar
b) = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUChar
b CUChar -> CUChar -> Bool
forall a. Ord a => a -> a -> Bool
<= CUChar
a)
    CUChar -> Maybe (I CUChar l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUChar
a CUChar -> CUChar -> CUChar
forall a. Num a => a -> a -> a
- CUChar
b)
  (I CUChar l r -> CUChar
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUChar
a) div' :: I CUChar l r -> I CUChar l r -> Maybe (I CUChar l r)
`div'` (I CUChar l r -> CUChar
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUChar
b) = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUChar
b CUChar -> CUChar -> Bool
forall a. Eq a => a -> a -> Bool
/= CUChar
0)
    let (CUChar
q, CUChar
m) = CUChar -> CUChar -> (CUChar, CUChar)
forall a. Integral a => a -> a -> (a, a)
divMod CUChar
a CUChar
b
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUChar
m CUChar -> CUChar -> Bool
forall a. Eq a => a -> a -> Bool
== CUChar
0)
    CUChar -> Maybe (I CUChar l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from CUChar
q

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

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

instance forall l r t.
  ( Interval CUChar l r, KnownCtx CUChar l r t
  ) => Known CUChar l r t where
  type KnownCtx CUChar l r t = (L.KnownNat t, l <= t, t <= r)
  known' :: Proxy t -> I CUChar l r
known' = CUChar -> I CUChar l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (CUChar -> I CUChar l r)
-> (Proxy t -> CUChar) -> Proxy t -> I CUChar l r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> CUChar
forall a. Num a => Integer -> a
fromInteger (Integer -> CUChar) -> (Proxy t -> Integer) -> Proxy t -> CUChar
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 CUChar l r) => With CUChar l r where
  with :: forall b.
I CUChar l r
-> (forall (t :: T CUChar). Known CUChar l r t => Proxy t -> b)
-> b
with I CUChar l r
x forall (t :: T CUChar). Known CUChar 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 (CUChar -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUChar l r -> CUChar
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CUChar 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 CUChar). Known CUChar l r t => Proxy t -> b
g Proxy n
Proxy n
pt)

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

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

instance forall l r. (Interval CUChar l r) => Shove CUChar l r where
  shove :: CUChar -> I CUChar l r
shove = \CUChar
x -> CUChar -> I CUChar l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (CUChar -> I CUChar l r) -> CUChar -> I CUChar l r
forall a b. (a -> b) -> a -> b
$ Integer -> CUChar
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
mod (CUChar -> Integer
forall a. Integral a => a -> Integer
toInteger CUChar
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 = CUChar -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUChar l r -> CUChar
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 @CUChar @l @r))
          r :: Integer
r = CUChar -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUChar l r -> CUChar
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 @CUChar @l @r))