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

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

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

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

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

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

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

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

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

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