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

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

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

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

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

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

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

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

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

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

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