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