{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_HADDOCK not-home #-}
module I.Autogen.Word32 () 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
_ignore :: (CSize, Word)
_ignore :: (CSize, Word)
_ignore = (CSize
0, Word
0)
type instance MinL Word32 = MinT Word32
type instance MaxR Word32 = MaxT Word32
instance forall l r.
( IntervalCtx Word32 l r
) => Interval Word32 l r where
type IntervalCtx Word32 l r =
( L.KnownNat l
, L.KnownNat r
, MinT Word32 <= l
, l <= r
, r <= MaxT Word32 )
type MinI Word32 l r = l
type MaxI Word32 l r = r
inhabitant :: I Word32 l r
inhabitant = I Word32 l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min
from :: Word32 -> Maybe (I Word32 l r)
from = \Word32
x -> Word32 -> I Word32 l r
forall x (l :: L x) (r :: R x). x -> I x l r
unsafest Word32
x I Word32 l r -> Maybe () -> Maybe (I Word32 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 (Word32
l Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32
x Bool -> Bool -> Bool
&& Word32
x Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32
r)
where l :: Word32
l = Integer -> Word32
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)) :: Word32
r :: Word32
r = Integer -> Word32
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)) :: Word32
(I Word32 l r -> Word32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Word32
a) plus' :: I Word32 l r -> I Word32 l r -> Maybe (I Word32 l r)
`plus'` (I Word32 l r -> Word32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Word32
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Word32
b Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32
forall a. Bounded a => a
maxBound Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
a)
Word32 -> Maybe (I Word32 l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (Word32
a Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Word32
b)
(I Word32 l r -> Word32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Word32
a) mult' :: I Word32 l r -> I Word32 l r -> Maybe (I Word32 l r)
`mult'` (I Word32 l r -> Word32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Word32
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Word32
b Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
0 Bool -> Bool -> Bool
|| Word32
a Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32
forall a. Bounded a => a
maxBound Word32 -> Word32 -> Word32
forall a. Integral a => a -> a -> a
`quot` Word32
b)
Word32 -> Maybe (I Word32 l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (Word32
a Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
* Word32
b)
(I Word32 l r -> Word32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Word32
a) minus' :: I Word32 l r -> I Word32 l r -> Maybe (I Word32 l r)
`minus'` (I Word32 l r -> Word32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Word32
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Word32
b Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32
a)
Word32 -> Maybe (I Word32 l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (Word32
a Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
b)
(I Word32 l r -> Word32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Word32
a) div' :: I Word32 l r -> I Word32 l r -> Maybe (I Word32 l r)
`div'` (I Word32 l r -> Word32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Word32
b) = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Word32
b Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word32
0)
let (Word32
q, Word32
m) = Word32 -> Word32 -> (Word32, Word32)
forall a. Integral a => a -> a -> (a, a)
divMod Word32
a Word32
b
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Word32
m Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
0)
Word32 -> Maybe (I Word32 l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from Word32
q
instance (Interval Word32 l r) => Clamp Word32 l r
instance (Interval Word32 ld rd, Interval Word32 lu ru, lu <= ld, rd <= ru)
=> Up Word32 ld rd lu ru
instance forall l r t.
( Interval Word32 l r, KnownCtx Word32 l r t
) => Known Word32 l r t where
type KnownCtx Word32 l r t = (L.KnownNat t, l <= t, t <= r)
known' :: Proxy t -> I Word32 l r
known' = Word32 -> I Word32 l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (Word32 -> I Word32 l r)
-> (Proxy t -> Word32) -> Proxy t -> I Word32 l r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Word32
forall a. Num a => Integer -> a
fromInteger (Integer -> Word32) -> (Proxy t -> Integer) -> Proxy t -> Word32
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 Word32 l r) => With Word32 l r where
with :: forall b.
I Word32 l r
-> (forall (t :: T Word32). Known Word32 l r t => Proxy t -> b)
-> b
with I Word32 l r
x forall (t :: T Word32). Known Word32 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 (Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger (I Word32 l r -> Word32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I Word32 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 Word32). Known Word32 l r t => Proxy t -> b
g Proxy n
Proxy n
pt)
instance (Interval Word32 l r, l /= r) => Discrete Word32 l r where
pred' :: I Word32 l r -> Maybe (I Word32 l r)
pred' I Word32 l r
i = Word32 -> I Word32 l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I Word32 l r -> Word32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I Word32 l r
i Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
1) I Word32 l r -> Maybe () -> Maybe (I Word32 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 Word32 l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min I Word32 l r -> I Word32 l r -> Bool
forall a. Ord a => a -> a -> Bool
< I Word32 l r
i)
succ' :: I Word32 l r -> Maybe (I Word32 l r)
succ' I Word32 l r
i = Word32 -> I Word32 l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (I Word32 l r -> Word32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I Word32 l r
i Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Word32
1) I Word32 l r -> Maybe () -> Maybe (I Word32 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 Word32 l r
i I Word32 l r -> I Word32 l r -> Bool
forall a. Ord a => a -> a -> Bool
< I Word32 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 Word32 0 r) => Zero Word32 0 r where
zero :: I Word32 0 r
zero = Word32 -> I Word32 0 r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe Word32
0
instance (Interval Word32 l r, l <= 1, 1 <= r) => One Word32 l r where
one :: I Word32 l r
one = Word32 -> I Word32 l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe Word32
1
instance forall l r. (Interval Word32 l r) => Shove Word32 l r where
shove :: Word32 -> I Word32 l r
shove = \Word32
x -> Word32 -> I Word32 l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (Word32 -> I Word32 l r) -> Word32 -> I Word32 l r
forall a b. (a -> b) -> a -> b
$ Integer -> Word32
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
mod (Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger Word32
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 = Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger (I Word32 l r -> Word32
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 @Word32 @l @r))
r :: Integer
r = Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger (I Word32 l r -> Word32
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 @Word32 @l @r))