{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Generic.Data.Internal.Enum where
import GHC.Generics
import Data.Ix
gtoEnum :: (Generic a, GEnum StandardEnum (Rep a)) => Int -> a
gtoEnum :: Int -> a
gtoEnum = String -> Int -> a
forall opts a.
(Generic a, GEnum opts (Rep a)) =>
String -> Int -> a
gtoEnum' @StandardEnum String
"gtoEnum"
gfromEnum :: (Generic a, GEnum StandardEnum (Rep a)) => a -> Int
= forall a. (Generic a, GEnum StandardEnum (Rep a)) => a -> Int
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
gfromEnum' @StandardEnum
genumFrom :: (Generic a, GEnum StandardEnum (Rep a)) => a -> [a]
genumFrom :: a -> [a]
genumFrom = forall a. (Generic a, GEnum StandardEnum (Rep a)) => a -> [a]
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> [a]
genumFrom' @StandardEnum
genumFromThen :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> [a]
genumFromThen :: a -> a -> [a]
genumFromThen = forall a. (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> [a]
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> a -> [a]
genumFromThen' @StandardEnum
genumFromTo :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> [a]
genumFromTo :: a -> a -> [a]
genumFromTo = forall a. (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> [a]
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> a -> [a]
genumFromTo' @StandardEnum
genumFromThenTo :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> a -> [a]
genumFromThenTo :: a -> a -> a -> [a]
genumFromThenTo = forall a.
(Generic a, GEnum StandardEnum (Rep a)) =>
a -> a -> a -> [a]
forall opts a.
(Generic a, GEnum opts (Rep a)) =>
a -> a -> a -> [a]
genumFromThenTo' @StandardEnum
gtoFiniteEnum :: (Generic a, GEnum FiniteEnum (Rep a)) => Int -> a
gtoFiniteEnum :: Int -> a
gtoFiniteEnum = String -> Int -> a
forall opts a.
(Generic a, GEnum opts (Rep a)) =>
String -> Int -> a
gtoEnum' @FiniteEnum String
"gtoFiniteEnum"
gfromFiniteEnum :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> Int
gfromFiniteEnum :: a -> Int
gfromFiniteEnum = forall a. (Generic a, GEnum FiniteEnum (Rep a)) => a -> Int
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
gfromEnum' @FiniteEnum
gfiniteEnumFrom :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> [a]
gfiniteEnumFrom :: a -> [a]
gfiniteEnumFrom = forall a. (Generic a, GEnum FiniteEnum (Rep a)) => a -> [a]
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> [a]
genumFrom' @FiniteEnum
gfiniteEnumFromThen :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> [a]
gfiniteEnumFromThen :: a -> a -> [a]
gfiniteEnumFromThen = forall a. (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> [a]
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> a -> [a]
genumFromThen' @FiniteEnum
gfiniteEnumFromTo :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> [a]
gfiniteEnumFromTo :: a -> a -> [a]
gfiniteEnumFromTo = forall a. (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> [a]
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> a -> [a]
genumFromTo' @FiniteEnum
gfiniteEnumFromThenTo :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> a -> [a]
gfiniteEnumFromThenTo :: a -> a -> a -> [a]
gfiniteEnumFromThenTo = forall a.
(Generic a, GEnum FiniteEnum (Rep a)) =>
a -> a -> a -> [a]
forall opts a.
(Generic a, GEnum opts (Rep a)) =>
a -> a -> a -> [a]
genumFromThenTo' @FiniteEnum
gtoEnumRaw' :: forall opts a. (Generic a, GEnum opts (Rep a)) => Int -> a
gtoEnumRaw' :: Int -> a
gtoEnumRaw' = Rep a Any -> a
forall a x. Generic a => Rep a x -> a
to (Rep a Any -> a) -> (Int -> Rep a Any) -> Int -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall opts (f :: * -> *) p. GEnum opts f => Int -> f p
forall (f :: * -> *) p. GEnum opts f => Int -> f p
gToEnum @opts
gtoEnum' :: forall opts a. (Generic a, GEnum opts (Rep a)) => String -> Int -> a
gtoEnum' :: String -> Int -> a
gtoEnum' String
name Int
n
| Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
n Bool -> Bool -> Bool
&& Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
card = Int -> a
forall opts a. (Generic a, GEnum opts (Rep a)) => Int -> a
gtoEnumRaw' @opts Int
n
| Bool
otherwise = String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$
String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": out of bounds, index " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
", cardinality " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
card
where
card :: Int
card = GEnum opts (Rep a) => Int
forall opts (f :: * -> *). GEnum opts f => Int
gCardinality @opts @(Rep a)
gfromEnum' :: forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
= forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
forall (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @opts (Rep a Any -> Int) -> (a -> Rep a Any) -> a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rep a Any
forall a x. Generic a => a -> Rep a x
from
genumMin :: Int
genumMin :: Int
genumMin = Int
0
genumMax :: forall opts a. (Generic a, GEnum opts (Rep a)) => Int
genumMax :: Int
genumMax = GEnum opts (Rep a) => Int
forall opts (f :: * -> *). GEnum opts f => Int
gCardinality @opts @(Rep a) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
genumFrom' :: forall opts a. (Generic a, GEnum opts (Rep a)) => a -> [a]
genumFrom' :: a -> [a]
genumFrom' a
x = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
toE [ Int
i_x .. (Generic a, GEnum opts (Rep a)) => Int
forall opts a. (Generic a, GEnum opts (Rep a)) => Int
genumMax @opts @a ]
where
toE :: Int -> a
toE = forall a. (Generic a, GEnum opts (Rep a)) => Int -> a
forall opts a. (Generic a, GEnum opts (Rep a)) => Int -> a
gtoEnumRaw' @opts
i_x :: Int
i_x = a -> Int
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
gfromEnum' @opts a
x
genumFromThen' :: forall opts a. (Generic a, GEnum opts (Rep a)) => a -> a -> [a]
genumFromThen' :: a -> a -> [a]
genumFromThen' a
x1 a
x2 = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
toE [ Int
i_x1, Int
i_x2 .. Int
bound ]
where
toE :: Int -> a
toE = forall a. (Generic a, GEnum opts (Rep a)) => Int -> a
forall opts a. (Generic a, GEnum opts (Rep a)) => Int -> a
gtoEnumRaw' @opts
i_x1 :: Int
i_x1 = a -> Int
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
gfromEnum' @opts a
x1
i_x2 :: Int
i_x2 = a -> Int
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
gfromEnum' @opts a
x2
bound :: Int
bound | Int
i_x1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
i_x2 = Int
genumMin
| Bool
otherwise = (Generic a, GEnum opts (Rep a)) => Int
forall opts a. (Generic a, GEnum opts (Rep a)) => Int
genumMax @opts @a
genumFromTo' :: forall opts a. (Generic a, GEnum opts (Rep a)) => a -> a -> [a]
genumFromTo' :: a -> a -> [a]
genumFromTo' a
x a
y = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
toE [ Int
i_x .. Int
i_y ]
where
toE :: Int -> a
toE = forall a. (Generic a, GEnum opts (Rep a)) => Int -> a
forall opts a. (Generic a, GEnum opts (Rep a)) => Int -> a
gtoEnumRaw' @opts
i_x :: Int
i_x = a -> Int
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
gfromEnum' @opts a
x
i_y :: Int
i_y = a -> Int
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
gfromEnum' @opts a
y
genumFromThenTo' :: forall opts a. (Generic a, GEnum opts (Rep a)) => a -> a -> a -> [a]
genumFromThenTo' :: a -> a -> a -> [a]
genumFromThenTo' a
x1 a
x2 a
y = (Int -> a) -> [Int] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Int -> a
toE [ Int
i_x1, Int
i_x2 .. Int
i_y ]
where
toE :: Int -> a
toE = forall a. (Generic a, GEnum opts (Rep a)) => Int -> a
forall opts a. (Generic a, GEnum opts (Rep a)) => Int -> a
gtoEnumRaw' @opts
i_x1 :: Int
i_x1 = a -> Int
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
gfromEnum' @opts a
x1
i_x2 :: Int
i_x2 = a -> Int
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
gfromEnum' @opts a
x2
i_y :: Int
i_y = a -> Int
forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
gfromEnum' @opts a
y
gminBound :: (Generic a, GBounded (Rep a)) => a
gminBound :: a
gminBound = Rep a Any -> a
forall a x. Generic a => Rep a x -> a
to Rep a Any
forall (f :: * -> *) p. GBounded f => f p
gMinBound
gmaxBound :: (Generic a, GBounded (Rep a)) => a
gmaxBound :: a
gmaxBound = Rep a Any -> a
forall a x. Generic a => Rep a x -> a
to Rep a Any
forall (f :: * -> *) p. GBounded f => f p
gMaxBound
grange :: (Generic a, GIx (Rep a)) => (a, a) -> [a]
grange :: (a, a) -> [a]
grange (a
m, a
n) = (Rep a Any -> a) -> [Rep a Any] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Rep a Any -> a
forall a x. Generic a => Rep a x -> a
to ([Rep a Any] -> [a]) -> [Rep a Any] -> [a]
forall a b. (a -> b) -> a -> b
$ (Rep a Any, Rep a Any) -> [Rep a Any]
forall (f :: * -> *) p. GIx f => (f p, f p) -> [f p]
gRange (a -> Rep a Any
forall a x. Generic a => a -> Rep a x
from a
m, a -> Rep a Any
forall a x. Generic a => a -> Rep a x
from a
n)
gindex :: (Generic a, GIx (Rep a)) => (a, a) -> a -> Int
gindex :: (a, a) -> a -> Int
gindex (a, a)
b a
i
| (a, a) -> a -> Bool
forall a. (Generic a, GIx (Rep a)) => (a, a) -> a -> Bool
ginRange (a, a)
b a
i = (a, a) -> a -> Int
forall a. (Generic a, GIx (Rep a)) => (a, a) -> a -> Int
gunsafeIndex (a, a)
b a
i
| Bool
otherwise = String -> Int
forall a. String -> a
errorWithoutStackTrace String
"gindex: out of bounds"
gunsafeIndex :: (Generic a, GIx (Rep a)) => (a, a) -> a -> Int
gunsafeIndex :: (a, a) -> a -> Int
gunsafeIndex (a
m, a
n) a
i = (Rep a Any, Rep a Any) -> Rep a Any -> Int
forall (f :: * -> *) p. GIx f => (f p, f p) -> f p -> Int
gUnsafeIndex (a -> Rep a Any
forall a x. Generic a => a -> Rep a x
from a
m, a -> Rep a Any
forall a x. Generic a => a -> Rep a x
from a
n) (a -> Rep a Any
forall a x. Generic a => a -> Rep a x
from a
i)
ginRange :: (Generic a, GIx (Rep a)) => (a, a) -> a -> Bool
ginRange :: (a, a) -> a -> Bool
ginRange (a
m, a
n) a
i = (Rep a Any, Rep a Any) -> Rep a Any -> Bool
forall (f :: * -> *) p. GIx f => (f p, f p) -> f p -> Bool
gInRange (a -> Rep a Any
forall a x. Generic a => a -> Rep a x
from a
m, a -> Rep a Any
forall a x. Generic a => a -> Rep a x
from a
n) (a -> Rep a Any
forall a x. Generic a => a -> Rep a x
from a
i)
class GEnum opts f where
gCardinality :: Int
:: f p -> Int
gToEnum :: Int -> f p
data StandardEnum
data FiniteEnum
instance GEnum opts f => GEnum opts (M1 i c f) where
gCardinality :: Int
gCardinality = GEnum opts f => Int
forall opts (f :: * -> *). GEnum opts f => Int
gCardinality @opts @f
gFromEnum :: M1 i c f p -> Int
gFromEnum = forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
forall (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @opts (f p -> Int) -> (M1 i c f p -> f p) -> M1 i c f p -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 i c f p -> f p
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1
gToEnum :: Int -> M1 i c f p
gToEnum = f p -> M1 i c f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (f p -> M1 i c f p) -> (Int -> f p) -> Int -> M1 i c f p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall opts (f :: * -> *) p. GEnum opts f => Int -> f p
forall (f :: * -> *) p. GEnum opts f => Int -> f p
gToEnum @opts
instance (GEnum opts f, GEnum opts g) => GEnum opts (f :+: g) where
gCardinality :: Int
gCardinality = GEnum opts f => Int
forall opts (f :: * -> *). GEnum opts f => Int
gCardinality @opts @f Int -> Int -> Int
forall a. Num a => a -> a -> a
+ GEnum opts g => Int
forall opts (f :: * -> *). GEnum opts f => Int
gCardinality @opts @g
gFromEnum :: (:+:) f g p -> Int
gFromEnum (L1 f p
x) = f p -> Int
forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @opts f p
x
gFromEnum (R1 g p
y) = Int
cardF Int -> Int -> Int
forall a. Num a => a -> a -> a
+ g p -> Int
forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @opts g p
y
where
cardF :: Int
cardF = GEnum opts f => Int
forall opts (f :: * -> *). GEnum opts f => Int
gCardinality @opts @f
gToEnum :: Int -> (:+:) f g p
gToEnum Int
n
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
cardF = f p -> (:+:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (Int -> f p
forall opts (f :: * -> *) p. GEnum opts f => Int -> f p
gToEnum @opts Int
n)
| Bool
otherwise = g p -> (:+:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (Int -> g p
forall opts (f :: * -> *) p. GEnum opts f => Int -> f p
gToEnum @opts (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
cardF))
where
cardF :: Int
cardF = GEnum opts f => Int
forall opts (f :: * -> *). GEnum opts f => Int
gCardinality @opts @f
instance (GEnum FiniteEnum f, GEnum FiniteEnum g) => GEnum FiniteEnum (f :*: g) where
gCardinality :: Int
gCardinality = GEnum FiniteEnum f => Int
forall opts (f :: * -> *). GEnum opts f => Int
gCardinality @FiniteEnum @f Int -> Int -> Int
forall a. Num a => a -> a -> a
* GEnum FiniteEnum g => Int
forall opts (f :: * -> *). GEnum opts f => Int
gCardinality @FiniteEnum @g
gFromEnum :: (:*:) f g p -> Int
gFromEnum (f p
x :*: g p
y) = f p -> Int
forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @FiniteEnum f p
x Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
cardG Int -> Int -> Int
forall a. Num a => a -> a -> a
+ g p -> Int
forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @FiniteEnum g p
y
where
cardG :: Int
cardG = GEnum FiniteEnum g => Int
forall opts (f :: * -> *). GEnum opts f => Int
gCardinality @FiniteEnum @g
gToEnum :: Int -> (:*:) f g p
gToEnum Int
n = Int -> f p
forall opts (f :: * -> *) p. GEnum opts f => Int -> f p
gToEnum @FiniteEnum Int
x f p -> g p -> (:*:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: Int -> g p
forall opts (f :: * -> *) p. GEnum opts f => Int -> f p
gToEnum @FiniteEnum Int
y
where
(Int
x, Int
y) = Int
n Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Int
cardG
cardG :: Int
cardG = GEnum FiniteEnum g => Int
forall opts (f :: * -> *). GEnum opts f => Int
gCardinality @FiniteEnum @g
instance GEnum opts U1 where
gCardinality :: Int
gCardinality = Int
1
gFromEnum :: U1 p -> Int
gFromEnum U1 p
U1 = Int
0
gToEnum :: Int -> U1 p
gToEnum Int
_ = U1 p
forall k (p :: k). U1 p
U1
instance (Bounded c, Enum c) => GEnum FiniteEnum (K1 i c) where
gCardinality :: Int
gCardinality = c -> Int
forall a. Enum a => a -> Int
fromEnum (c
forall a. Bounded a => a
maxBound :: c) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
gFromEnum :: K1 i c p -> Int
gFromEnum = c -> Int
forall a. Enum a => a -> Int
fromEnum (c -> Int) -> (K1 i c p -> c) -> K1 i c p -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 i c p -> c
forall i c k (p :: k). K1 i c p -> c
unK1
gToEnum :: Int -> K1 i c p
gToEnum = c -> K1 i c p
forall k i c (p :: k). c -> K1 i c p
K1 (c -> K1 i c p) -> (Int -> c) -> Int -> K1 i c p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> c
forall a. Enum a => Int -> a
toEnum
class GBounded f where
gMinBound :: f p
gMaxBound :: f p
deriving instance GBounded f => GBounded (M1 i c f)
instance GBounded U1 where
gMinBound :: U1 p
gMinBound = U1 p
forall k (p :: k). U1 p
U1
gMaxBound :: U1 p
gMaxBound = U1 p
forall k (p :: k). U1 p
U1
instance Bounded c => GBounded (K1 i c) where
gMinBound :: K1 i c p
gMinBound = c -> K1 i c p
forall k i c (p :: k). c -> K1 i c p
K1 c
forall a. Bounded a => a
minBound
gMaxBound :: K1 i c p
gMaxBound = c -> K1 i c p
forall k i c (p :: k). c -> K1 i c p
K1 c
forall a. Bounded a => a
maxBound
instance (GBounded f, GBounded g) => GBounded (f :+: g) where
gMinBound :: (:+:) f g p
gMinBound = f p -> (:+:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 f p
forall (f :: * -> *) p. GBounded f => f p
gMinBound
gMaxBound :: (:+:) f g p
gMaxBound = g p -> (:+:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 g p
forall (f :: * -> *) p. GBounded f => f p
gMaxBound
instance (GBounded f, GBounded g) => GBounded (f :*: g) where
gMinBound :: (:*:) f g p
gMinBound = f p
forall (f :: * -> *) p. GBounded f => f p
gMinBound f p -> g p -> (:*:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g p
forall (f :: * -> *) p. GBounded f => f p
gMinBound
gMaxBound :: (:*:) f g p
gMaxBound = f p
forall (f :: * -> *) p. GBounded f => f p
gMaxBound f p -> g p -> (:*:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g p
forall (f :: * -> *) p. GBounded f => f p
gMaxBound
class GIx f where
gRange :: (f p, f p) -> [f p]
gUnsafeIndex :: (f p, f p) -> f p -> Int
gInRange :: (f p, f p) -> f p -> Bool
instance GIx f => GIx (M1 i c f) where
gRange :: (M1 i c f p, M1 i c f p) -> [M1 i c f p]
gRange (M1 f p
m, M1 f p
n) = (f p -> M1 i c f p) -> [f p] -> [M1 i c f p]
forall a b. (a -> b) -> [a] -> [b]
map f p -> M1 i c f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 ([f p] -> [M1 i c f p]) -> [f p] -> [M1 i c f p]
forall a b. (a -> b) -> a -> b
$ (f p, f p) -> [f p]
forall (f :: * -> *) p. GIx f => (f p, f p) -> [f p]
gRange (f p
m, f p
n)
gUnsafeIndex :: (M1 i c f p, M1 i c f p) -> M1 i c f p -> Int
gUnsafeIndex (M1 f p
m, M1 f p
n) (M1 f p
i) = (f p, f p) -> f p -> Int
forall (f :: * -> *) p. GIx f => (f p, f p) -> f p -> Int
gUnsafeIndex (f p
m, f p
n) f p
i
gInRange :: (M1 i c f p, M1 i c f p) -> M1 i c f p -> Bool
gInRange (M1 f p
m, M1 f p
n) (M1 f p
i) = (f p, f p) -> f p -> Bool
forall (f :: * -> *) p. GIx f => (f p, f p) -> f p -> Bool
gInRange (f p
m, f p
n) f p
i
instance (GEnum StandardEnum f, GEnum StandardEnum g) => GIx (f :+: g) where
gRange :: ((:+:) f g p, (:+:) f g p) -> [(:+:) f g p]
gRange ((:+:) f g p
x, (:+:) f g p
y) = (Int -> (:+:) f g p) -> [Int] -> [(:+:) f g p]
forall a b. (a -> b) -> [a] -> [b]
map Int -> (:+:) f g p
forall p. Int -> (:+:) f g p
toE [ Int
i_x .. Int
i_y ]
where
toE :: Int -> (:+:) f g p
toE = forall opts (f :: * -> *) p. GEnum opts f => Int -> f p
forall (f :: * -> *) p. GEnum StandardEnum f => Int -> f p
gToEnum @StandardEnum
i_x :: Int
i_x = (:+:) f g p -> Int
forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @StandardEnum (:+:) f g p
x
i_y :: Int
i_y = (:+:) f g p -> Int
forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @StandardEnum (:+:) f g p
y
gUnsafeIndex :: ((:+:) f g p, (:+:) f g p) -> (:+:) f g p -> Int
gUnsafeIndex ((:+:) f g p
m, (:+:) f g p
_) (:+:) f g p
i = Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
i_i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i_m)
where
i_m :: Int
i_m = (:+:) f g p -> Int
forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @StandardEnum (:+:) f g p
m
i_i :: Int
i_i = (:+:) f g p -> Int
forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @StandardEnum (:+:) f g p
i
gInRange :: ((:+:) f g p, (:+:) f g p) -> (:+:) f g p -> Bool
gInRange ((:+:) f g p
m, (:+:) f g p
n) (:+:) f g p
i = Int
i_m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
i_i Bool -> Bool -> Bool
&& Int
i_i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
i_n
where
i_m :: Int
i_m = (:+:) f g p -> Int
forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @StandardEnum (:+:) f g p
m
i_n :: Int
i_n = (:+:) f g p -> Int
forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @StandardEnum (:+:) f g p
n
i_i :: Int
i_i = (:+:) f g p -> Int
forall opts (f :: * -> *) p. GEnum opts f => f p -> Int
gFromEnum @StandardEnum (:+:) f g p
i
instance (GIx f, GIx g) => GIx (f :*: g) where
gRange :: ((:*:) f g p, (:*:) f g p) -> [(:*:) f g p]
gRange (f p
m1 :*: g p
m2, f p
n1 :*: g p
n2) =
[ f p
i1 f p -> g p -> (:*:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g p
i2 | f p
i1 <- (f p, f p) -> [f p]
forall (f :: * -> *) p. GIx f => (f p, f p) -> [f p]
gRange (f p
m1, f p
n1), g p
i2 <- (g p, g p) -> [g p]
forall (f :: * -> *) p. GIx f => (f p, f p) -> [f p]
gRange (g p
m2, g p
n2) ]
gUnsafeIndex :: ((:*:) f g p, (:*:) f g p) -> (:*:) f g p -> Int
gUnsafeIndex (f p
m1 :*: g p
m2, f p
n1 :*: g p
n2) (f p
i1 :*: g p
i2) = Int
int1 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
rangeSize2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
int2
where
int1 :: Int
int1 = (f p, f p) -> f p -> Int
forall (f :: * -> *) p. GIx f => (f p, f p) -> f p -> Int
gUnsafeIndex (f p
m1, f p
n1) f p
i1
int2 :: Int
int2 = (g p, g p) -> g p -> Int
forall (f :: * -> *) p. GIx f => (f p, f p) -> f p -> Int
gUnsafeIndex (g p
m2, g p
n2) g p
i2
rangeSize2 :: Int
rangeSize2 = (g p, g p) -> g p -> Int
forall (f :: * -> *) p. GIx f => (f p, f p) -> f p -> Int
gUnsafeIndex (g p
m2, g p
n2) g p
n2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
gInRange :: ((:*:) f g p, (:*:) f g p) -> (:*:) f g p -> Bool
gInRange (f p
m1 :*: g p
m2, f p
n1 :*: g p
n2) (f p
i1 :*: g p
i2) =
(f p, f p) -> f p -> Bool
forall (f :: * -> *) p. GIx f => (f p, f p) -> f p -> Bool
gInRange (f p
m1, f p
n1) f p
i1 Bool -> Bool -> Bool
&& (g p, g p) -> g p -> Bool
forall (f :: * -> *) p. GIx f => (f p, f p) -> f p -> Bool
gInRange (g p
m2, g p
n2) g p
i2
instance GIx U1 where
gRange :: (U1 p, U1 p) -> [U1 p]
gRange (U1 p
U1, U1 p
U1) = [U1 p
forall k (p :: k). U1 p
U1]
gUnsafeIndex :: (U1 p, U1 p) -> U1 p -> Int
gUnsafeIndex (U1 p
U1, U1 p
U1) U1 p
U1 = Int
0
gInRange :: (U1 p, U1 p) -> U1 p -> Bool
gInRange (U1 p
U1, U1 p
U1) U1 p
U1 = Bool
True
instance (Ix c) => GIx (K1 i c) where
gRange :: (K1 i c p, K1 i c p) -> [K1 i c p]
gRange (K1 c
m, K1 c
n) = (c -> K1 i c p) -> [c] -> [K1 i c p]
forall a b. (a -> b) -> [a] -> [b]
map c -> K1 i c p
forall k i c (p :: k). c -> K1 i c p
K1 ([c] -> [K1 i c p]) -> [c] -> [K1 i c p]
forall a b. (a -> b) -> a -> b
$ (c, c) -> [c]
forall a. Ix a => (a, a) -> [a]
range (c
m, c
n)
gUnsafeIndex :: (K1 i c p, K1 i c p) -> K1 i c p -> Int
gUnsafeIndex (K1 c
m, K1 c
n) (K1 c
i) = (c, c) -> c -> Int
forall a. Ix a => (a, a) -> a -> Int
index (c
m, c
n) c
i
gInRange :: (K1 i c p, K1 i c p) -> K1 i c p -> Bool
gInRange (K1 c
m, K1 c
n) (K1 c
i) = (c, c) -> c -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (c
m, c
n) c
i