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

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

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

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

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

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

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

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

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

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

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