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