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

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

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

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

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

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

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

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

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

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

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