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

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

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

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

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

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

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

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

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

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

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