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

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

module I.Autogen.Int32 () where

import Control.Monad
import Data.Constraint
import Data.Int
import Data.Maybe
import Data.Proxy
import Data.Type.Ord
import Foreign.C.Types
import KindInteger (type (/=), type (==))
import KindInteger qualified as K
import Prelude hiding (min, max, div)
import Prelude qualified as P

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, Int)
_ignore :: (CSize, Int)
_ignore = (CSize
0, Int
0)

--------------------------------------------------------------------------------

type instance MinL Int32 = MinT Int32
type instance MaxR Int32 = MaxT Int32

instance forall (l :: K.Integer) (r :: K.Integer).
  ( IntervalCtx Int32 l r
  ) => Interval Int32 l r where
  type IntervalCtx Int32 l r =
    ( K.KnownInteger l
    , K.KnownInteger r
    , MinT Int32 <= l
    , l <= r
    , r <= MaxT Int32 )
  type MinI Int32 l r = l
  type MaxI Int32 l r = r
  inhabitant :: I Int32 l r
inhabitant = I Int32 l r
forall x (l :: L x) (r :: R x). Known x l r (MinI x l r) => I x l r
min
  from :: Int32 -> Maybe (I Int32 l r)
from = \Int32
x -> Int32 -> I Int32 l r
forall x (l :: L x) (r :: R x). x -> I x l r
unsafest Int32
x I Int32 l r -> Maybe () -> Maybe (I Int32 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 (Int32
l Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int32
x Bool -> Bool -> Bool
&& Int32
x Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int32
r)
    where l :: Int32
l = Integer -> Int32
forall a. Num a => Integer -> a
fromInteger (Proxy l -> Integer
forall (i :: Integer) (proxy :: Integer -> *).
KnownInteger i =>
proxy i -> Integer
K.integerVal (forall {k} (t :: k). Proxy t
forall (t :: Integer). Proxy t
Proxy @l)) :: Int32
          r :: Int32
r = Integer -> Int32
forall a. Num a => Integer -> a
fromInteger (Proxy r -> Integer
forall (i :: Integer) (proxy :: Integer -> *).
KnownInteger i =>
proxy i -> Integer
K.integerVal (forall {k} (t :: k). Proxy t
forall (t :: Integer). Proxy t
Proxy @r)) :: Int32
  negate' :: I Int32 l r -> Maybe (I Int32 l r)
negate' (I Int32 l r -> Int32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Int32
x) = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Int32
x Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
/= Int32
forall a. Bounded a => a
minBound)
    Int32 -> Maybe (I Int32 l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (Int32 -> Int32
forall a. Num a => a -> a
P.negate Int32
x)
  (I Int32 l r -> Int32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Int32
a) plus' :: I Int32 l r -> I Int32 l r -> Maybe (I Int32 l r)
`plus'` (I Int32 l r -> Int32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Int32
b)
    | Int32
b Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
> Int32
0 Bool -> Bool -> Bool
&& Int32
a Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
> Int32
forall a. Bounded a => a
maxBound Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
b = Maybe (I Int32 l r)
forall a. Maybe a
Nothing
    | Int32
b Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
< Int32
0 Bool -> Bool -> Bool
&& Int32
a Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
< Int32
forall a. Bounded a => a
minBound Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
b = Maybe (I Int32 l r)
forall a. Maybe a
Nothing
    | Bool
otherwise                 = Int32 -> Maybe (I Int32 l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (Int32
a Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
b)
  (I Int32 l r -> Int32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Int32
a) mult' :: I Int32 l r -> I Int32 l r -> Maybe (I Int32 l r)
`mult'` (I Int32 l r -> Int32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Int32
b) = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> Bool -> Maybe ()
forall a b. (a -> b) -> a -> b
$ case Int32
a Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int32
0 of
      Bool
True  | Int32
b Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int32
0    -> Int32
a Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
== Int32
0 Bool -> Bool -> Bool
|| Int32
b Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
>= (Int32
forall a. Bounded a => a
maxBound Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`quot` Int32
a)
            | Bool
otherwise -> Int32
a Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
>= (Int32
forall a. Bounded a => a
minBound Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`quot` Int32
b)
      Bool
False | Int32
b Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int32
0    -> Int32
b Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
>= (Int32
forall a. Bounded a => a
minBound Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`quot` Int32
a)
            | Bool
otherwise -> Int32
a Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
<= (Int32
forall a. Bounded a => a
maxBound Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`quot` Int32
b)
    Int32 -> Maybe (I Int32 l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (Int32
a Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* Int32
b)
  (I Int32 l r -> Int32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Int32
a) minus' :: I Int32 l r -> I Int32 l r -> Maybe (I Int32 l r)
`minus'` (I Int32 l r -> Int32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Int32
b)
    | Int32
b Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
> Int32
0 Bool -> Bool -> Bool
&& Int32
a Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
< Int32
forall a. Bounded a => a
minBound Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
b = Maybe (I Int32 l r)
forall a. Maybe a
Nothing
    | Int32
b Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
< Int32
0 Bool -> Bool -> Bool
&& Int32
a Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
> Int32
forall a. Bounded a => a
maxBound Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
b = Maybe (I Int32 l r)
forall a. Maybe a
Nothing
    | Bool
otherwise                 = Int32 -> Maybe (I Int32 l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (Int32
a Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
b)
  (I Int32 l r -> Int32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Int32
a) div' :: I Int32 l r -> I Int32 l r -> Maybe (I Int32 l r)
`div'` (I Int32 l r -> Int32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap -> Int32
b) = do
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Int32
b Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
/= Int32
0 Bool -> Bool -> Bool
&& (Int32
b Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
/= -Int32
1 Bool -> Bool -> Bool
|| Int32
a Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
/= Int32
forall a. Bounded a => a
minBound))
    let (Int32
q, Int32
m) = Int32 -> Int32 -> (Int32, Int32)
forall a. Integral a => a -> a -> (a, a)
divMod Int32
a Int32
b
    Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Int32
m Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
== Int32
0)
    Int32 -> Maybe (I Int32 l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from Int32
q

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

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

instance forall l r t.
  ( Interval Int32 l r, KnownCtx Int32 l r t
  ) => Known Int32 l r t where
  type KnownCtx Int32 l r t = (K.KnownInteger t, l <= t, t <= r)
  known' :: Proxy t -> I Int32 l r
known' = Int32 -> I Int32 l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (Int32 -> I Int32 l r)
-> (Proxy t -> Int32) -> Proxy t -> I Int32 l r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Int32
forall a. Num a => Integer -> a
fromInteger (Integer -> Int32) -> (Proxy t -> Integer) -> Proxy t -> Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy t -> Integer
forall (i :: Integer) (proxy :: Integer -> *).
KnownInteger i =>
proxy i -> Integer
K.integerVal

instance forall l r. (Interval Int32 l r) => With Int32 l r where
  with :: forall b.
I Int32 l r
-> (forall (t :: T Int32). Known Int32 l r t => Proxy t -> b) -> b
with I Int32 l r
x forall (t :: T Int32). Known Int32 l r t => Proxy t -> b
g = case Integer -> SomeInteger
K.someIntegerVal (Int32 -> Integer
forall a. Integral a => a -> Integer
toInteger (I Int32 l r -> Int32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap I Int32 l r
x)) of
    K.SomeInteger (Proxy n
pt :: Proxy t) ->
      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
        Dict
  (Assert
     (OrdCond
        (CmpInteger_ (Normalize l) (Normalize n)) 'True 'True 'False)
     (TypeError ...))
Dict <- forall (a :: Integer) (b :: Integer).
(KnownInteger a, KnownInteger b) =>
Maybe (Dict (a <= b))
leInteger @l @t
        Dict
  (Assert
     (OrdCond
        (CmpInteger_ (Normalize n) (Normalize r)) 'True 'True 'False)
     (TypeError ...))
Dict <- forall (a :: Integer) (b :: Integer).
(KnownInteger a, KnownInteger b) =>
Maybe (Dict (a <= b))
leInteger @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 Int32). Known Int32 l r t => Proxy t -> b
g Proxy n
Proxy n
pt)

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

instance (Zero Int32 l r, l == K.Negate r) => Negate Int32 l r where
  negate :: I Int32 l r -> I Int32 l r
negate = Int32 -> I Int32 l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe (Int32 -> I Int32 l r)
-> (I Int32 l r -> Int32) -> I Int32 l r -> I Int32 l r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Int32
forall a. Num a => a -> a
P.negate (Int32 -> Int32) -> (I Int32 l r -> Int32) -> I Int32 l r -> Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. I Int32 l r -> Int32
forall x (l :: L x) (r :: R x). I x l r -> x
unwrap

instance (Interval Int32 l r, l <= K.P 0, K.P 0 <= r) => Zero Int32 l r where
  zero :: I Int32 l r
zero = Int32 -> I Int32 l r
forall x (l :: L x) (r :: R x).
(HasCallStack, Interval x l r) =>
x -> I x l r
unsafe Int32
0

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

instance forall l r. (Interval Int32 l r) => Shove Int32 l r where
  shove :: Int32 -> I Int32 l r
shove = \Int32
x -> I Int32 l r -> Maybe (I Int32 l r) -> I Int32 l r
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> I Int32 l r
forall a. HasCallStack => [Char] -> a
error [Char]
"shove(Int32): impossible") (Maybe (I Int32 l r) -> I Int32 l r)
-> Maybe (I Int32 l r) -> I Int32 l r
forall a b. (a -> b) -> a -> b
$
                  Int32 -> Maybe (I Int32 l r)
forall x (l :: L x) (r :: R x).
Interval x l r =>
x -> Maybe (I x l r)
from (Int32 -> Maybe (I Int32 l r)) -> Int32 -> Maybe (I Int32 l r)
forall a b. (a -> b) -> a -> b
$ Integer -> Int32
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
mod (Int32 -> Integer
forall a. Integral a => a -> Integer
toInteger Int32
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 = Int32 -> Integer
forall a. Integral a => a -> Integer
toInteger (I Int32 l r -> Int32
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 @Int32 @l @r))
          r :: Integer
r = Int32 -> Integer
forall a. Integral a => a -> Integer
toInteger (I Int32 l r -> Int32
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 @Int32 @l @r))