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