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

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

module I.Autogen.CULong () 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

--------------------------------------------------------------------------------

-- | 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, Word)
_ignore :: (CSize, Word)
_ignore = (CSize
0, Word
0)

--------------------------------------------------------------------------------


type instance MinL CULong = MinT CULong
type instance MaxR CULong = MaxT CULong

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

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

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

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

instance (Interval CULong l r, l /= r) => Discrete CULong l r where
  pred' :: I CULong l r -> Maybe (I CULong l r)
pred' I CULong l r
i = CULong -> I CULong l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I CULong l r -> CULong
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CULong l r
i CULong -> CULong -> CULong
forall a. Num a => a -> a -> a
- CULong
1) I CULong l r -> Maybe () -> Maybe (I CULong 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 CULong l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min I CULong l r -> I CULong l r -> Bool
forall a. Ord a => a -> a -> Bool
< I CULong l r
i)
  succ' :: I CULong l r -> Maybe (I CULong l r)
succ' I CULong l r
i = CULong -> I CULong l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I CULong l r -> CULong
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I CULong l r
i CULong -> CULong -> CULong
forall a. Num a => a -> a -> a
+ CULong
1) I CULong l r -> Maybe () -> Maybe (I CULong 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 CULong l r
i I CULong l r -> I CULong l r -> Bool
forall a. Ord a => a -> a -> Bool
< I CULong 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 CULong 0 r) => Zero CULong 0 r where
  zero :: I CULong 0 r
zero = CULong -> I CULong 0 r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe CULong
0

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

instance forall l r. (Interval CULong l r) => Shove CULong l r where
  shove :: CULong -> I CULong l r
shove = \CULong
x -> CULong -> I CULong l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (CULong -> I CULong l r) -> CULong -> I CULong l r
forall a b. (a -> b) -> a -> b
$ Integer -> CULong
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
mod (CULong -> Integer
forall a. Integral a => a -> Integer
toInteger CULong
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 = CULong -> Integer
forall a. Integral a => a -> Integer
toInteger (I CULong l r -> CULong
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 @CULong @l @r))
          r :: Integer
r = CULong -> Integer
forall a. Integral a => a -> Integer
toInteger (I CULong l r -> CULong
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 @CULong @l @r))