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

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

module I.Autogen.CChar () 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 CChar = MinT CChar
type instance MaxR CChar = MaxT CChar

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

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

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

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

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

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

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

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