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

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

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

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

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

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

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

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

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

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