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

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

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

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

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

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

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

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

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

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

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