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

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

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

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

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

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

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

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

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

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