{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_HADDOCK not-home #-}
module I.Autogen.CUIntPtr () where
import Control.Monad
import Data.Constraint
import Data.Maybe
import Data.Proxy
import Data.Word
import Data.Type.Ord
import Foreign.C.Types
import GHC.TypeLits qualified as L
import KindInteger (type (/=))
import Prelude hiding (min, max, div)
import I.Internal
_ignore :: (CSize, Word)
_ignore :: (CSize, Word)
_ignore = (CSize
0, Word
0)
type instance MinL CUIntPtr = MinT CUIntPtr
type instance MaxR CUIntPtr = MaxT CUIntPtr
instance forall l r.
( IntervalCtx CUIntPtr l r
) => Interval CUIntPtr l r where
type IntervalCtx CUIntPtr l r =
( L.KnownNat l
, L.KnownNat r
, MinT CUIntPtr <= l
, l <= r
, r <= MaxT CUIntPtr )
type MinI CUIntPtr l r = l
type MaxI CUIntPtr l r = r
inhabitant :: I CUIntPtr l r
inhabitant = I CUIntPtr l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min
from :: CUIntPtr -> Maybe (I CUIntPtr l r)
from = \CUIntPtr
x -> CUIntPtr -> I CUIntPtr l r
forall x (l :: L x) (r :: R x). x -> I x l r
unsafest CUIntPtr
x I CUIntPtr l r -> Maybe () -> Maybe (I CUIntPtr 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 (CUIntPtr
l CUIntPtr -> CUIntPtr -> Bool
forall a. Ord a => a -> a -> Bool
<= CUIntPtr
x Bool -> Bool -> Bool
&& CUIntPtr
x CUIntPtr -> CUIntPtr -> Bool
forall a. Ord a => a -> a -> Bool
<= CUIntPtr
r)
where l :: CUIntPtr
l = Integer -> CUIntPtr
forall a. Num a => Integer -> a
fromInteger (Proxy l -> Integer
forall (n :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
L.natVal (forall (t :: Natural). Proxy t
forall {k} (t :: k). Proxy t
Proxy @l)) :: CUIntPtr
r :: CUIntPtr
r = Integer -> CUIntPtr
forall a. Num a => Integer -> a
fromInteger (Proxy r -> Integer
forall (n :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
L.natVal (forall (t :: Natural). Proxy t
forall {k} (t :: k). Proxy t
Proxy @r)) :: CUIntPtr
(I CUIntPtr l r -> CUIntPtr
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntPtr
a) plus' :: I CUIntPtr l r -> I CUIntPtr l r -> Maybe (I CUIntPtr l r)
`plus'` (I CUIntPtr l r -> CUIntPtr
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntPtr
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUIntPtr
b CUIntPtr -> CUIntPtr -> Bool
forall a. Ord a => a -> a -> Bool
<= CUIntPtr
forall a. Bounded a => a
maxBound CUIntPtr -> CUIntPtr -> CUIntPtr
forall a. Num a => a -> a -> a
- CUIntPtr
a)
CUIntPtr -> Maybe (I CUIntPtr l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUIntPtr
a CUIntPtr -> CUIntPtr -> CUIntPtr
forall a. Num a => a -> a -> a
+ CUIntPtr
b)
(I CUIntPtr l r -> CUIntPtr
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntPtr
a) mult' :: I CUIntPtr l r -> I CUIntPtr l r -> Maybe (I CUIntPtr l r)
`mult'` (I CUIntPtr l r -> CUIntPtr
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntPtr
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUIntPtr
b CUIntPtr -> CUIntPtr -> Bool
forall a. Eq a => a -> a -> Bool
== CUIntPtr
0 Bool -> Bool -> Bool
|| CUIntPtr
a CUIntPtr -> CUIntPtr -> Bool
forall a. Ord a => a -> a -> Bool
<= CUIntPtr
forall a. Bounded a => a
maxBound CUIntPtr -> CUIntPtr -> CUIntPtr
forall a. Integral a => a -> a -> a
`quot` CUIntPtr
b)
CUIntPtr -> Maybe (I CUIntPtr l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUIntPtr
a CUIntPtr -> CUIntPtr -> CUIntPtr
forall a. Num a => a -> a -> a
* CUIntPtr
b)
(I CUIntPtr l r -> CUIntPtr
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntPtr
a) minus' :: I CUIntPtr l r -> I CUIntPtr l r -> Maybe (I CUIntPtr l r)
`minus'` (I CUIntPtr l r -> CUIntPtr
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntPtr
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUIntPtr
b CUIntPtr -> CUIntPtr -> Bool
forall a. Ord a => a -> a -> Bool
<= CUIntPtr
a)
CUIntPtr -> Maybe (I CUIntPtr l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (CUIntPtr
a CUIntPtr -> CUIntPtr -> CUIntPtr
forall a. Num a => a -> a -> a
- CUIntPtr
b)
(I CUIntPtr l r -> CUIntPtr
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntPtr
a) div' :: I CUIntPtr l r -> I CUIntPtr l r -> Maybe (I CUIntPtr l r)
`div'` (I CUIntPtr l r -> CUIntPtr
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> CUIntPtr
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUIntPtr
b CUIntPtr -> CUIntPtr -> Bool
forall a. Eq a => a -> a -> Bool
/= CUIntPtr
0)
let (CUIntPtr
q, CUIntPtr
m) = CUIntPtr -> CUIntPtr -> (CUIntPtr, CUIntPtr)
forall a. Integral a => a -> a -> (a, a)
divMod CUIntPtr
a CUIntPtr
b
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (CUIntPtr
m CUIntPtr -> CUIntPtr -> Bool
forall a. Eq a => a -> a -> Bool
== CUIntPtr
0)
CUIntPtr -> Maybe (I CUIntPtr l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from CUIntPtr
q
instance (Interval CUIntPtr l r) => Clamp CUIntPtr l r
instance (Interval CUIntPtr ld rd, Interval CUIntPtr lu ru, lu <= ld, rd <= ru)
=> Up CUIntPtr ld rd lu ru
instance forall l r t.
( Interval CUIntPtr l r, KnownCtx CUIntPtr l r t
) => Known CUIntPtr l r t where
type KnownCtx CUIntPtr l r t = (L.KnownNat t, l <= t, t <= r)
known' :: Proxy t -> I CUIntPtr l r
known' = CUIntPtr -> I CUIntPtr l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (CUIntPtr -> I CUIntPtr l r)
-> (Proxy t -> CUIntPtr) -> Proxy t -> I CUIntPtr l r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> CUIntPtr
forall a. Num a => Integer -> a
fromInteger (Integer -> CUIntPtr)
-> (Proxy t -> Integer) -> Proxy t -> CUIntPtr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy t -> Integer
forall (n :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
L.natVal
instance forall l r. (Interval CUIntPtr l r) => With CUIntPtr l r where
with :: forall b.
I CUIntPtr l r
-> (forall (t :: T CUIntPtr). Known CUIntPtr l r t => Proxy t -> b)
-> b
with I CUIntPtr l r
x forall (t :: T CUIntPtr). Known CUIntPtr l r t => Proxy t -> b
g = 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
L.SomeNat (Proxy n
pt :: Proxy t) <- Integer -> Maybe SomeNat
L.someNatVal (CUIntPtr -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUIntPtr l r -> CUIntPtr
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CUIntPtr l r
x))
Dict
(Assert (OrdCond (CmpNat l n) 'True 'True 'False) (TypeError ...))
Dict <- forall (a :: Natural) (b :: Natural).
(KnownNat a, KnownNat b) =>
Maybe (Dict (a <= b))
leNatural @l @t
Dict
(Assert (OrdCond (CmpNat n r) 'True 'True 'False) (TypeError ...))
Dict <- forall (a :: Natural) (b :: Natural).
(KnownNat a, KnownNat b) =>
Maybe (Dict (a <= b))
leNatural @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 CUIntPtr). Known CUIntPtr l r t => Proxy t -> b
g Proxy n
Proxy n
pt)
instance (Interval CUIntPtr l r, l /= r) => Discrete CUIntPtr l r where
pred' :: I CUIntPtr l r -> Maybe (I CUIntPtr l r)
pred' I CUIntPtr l r
i = CUIntPtr -> I CUIntPtr l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I CUIntPtr l r -> CUIntPtr
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CUIntPtr l r
i CUIntPtr -> CUIntPtr -> CUIntPtr
forall a. Num a => a -> a -> a
- CUIntPtr
1) I CUIntPtr l r -> Maybe () -> Maybe (I CUIntPtr 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 CUIntPtr l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min I CUIntPtr l r -> I CUIntPtr l r -> Bool
forall a. Ord a => a -> a -> Bool
< I CUIntPtr l r
i)
succ' :: I CUIntPtr l r -> Maybe (I CUIntPtr l r)
succ' I CUIntPtr l r
i = CUIntPtr -> I CUIntPtr l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I CUIntPtr l r -> CUIntPtr
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CUIntPtr l r
i CUIntPtr -> CUIntPtr -> CUIntPtr
forall a. Num a => a -> a -> a
+ CUIntPtr
1) I CUIntPtr l r -> Maybe () -> Maybe (I CUIntPtr 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 CUIntPtr l r
i I CUIntPtr l r -> I CUIntPtr l r -> Bool
forall a. Ord a => a -> a -> Bool
< I CUIntPtr l r
forall x (l :: L x) (r :: R x). Known x l r (MaxI x l r) => I x l r
max)
instance (Interval CUIntPtr 0 r) => Zero CUIntPtr 0 r where
zero :: I CUIntPtr 0 r
zero = CUIntPtr -> I CUIntPtr 0 r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe CUIntPtr
0
instance (Interval CUIntPtr l r, l <= 1, 1 <= r) => One CUIntPtr l r where
one :: I CUIntPtr l r
one = CUIntPtr -> I CUIntPtr l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe CUIntPtr
1
instance forall l r. (Interval CUIntPtr l r) => Shove CUIntPtr l r where
shove :: CUIntPtr -> I CUIntPtr l r
shove = \CUIntPtr
x -> CUIntPtr -> I CUIntPtr l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (CUIntPtr -> I CUIntPtr l r) -> CUIntPtr -> I CUIntPtr l r
forall a b. (a -> b) -> a -> b
$ Integer -> CUIntPtr
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
mod (CUIntPtr -> Integer
forall a. Integral a => a -> Integer
toInteger CUIntPtr
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 = CUIntPtr -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUIntPtr l r -> CUIntPtr
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 @CUIntPtr @l @r))
r :: Integer
r = CUIntPtr -> Integer
forall a. Integral a => a -> Integer
toInteger (I CUIntPtr l r -> CUIntPtr
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 @CUIntPtr @l @r))