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

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

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

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

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

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

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

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

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

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

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