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

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

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

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

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

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

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

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

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

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