{-# 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
gtoEnum :: forall a. (Generic a, GEnum StandardEnum (Rep a)) => Int -> a
gtoEnum = gtoEnum' @StandardEnum "gtoEnum"
gfromEnum :: (Generic a, GEnum StandardEnum (Rep a)) => a -> Int
gfromEnum = gfromEnum' @StandardEnum
genumFrom :: (Generic a, GEnum StandardEnum (Rep a)) => a -> [a]
genumFrom = genumFrom' @StandardEnum
genumFromThen :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> [a]
genumFromThen = genumFromThen' @StandardEnum
genumFromTo :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> [a]
genumFromTo = genumFromTo' @StandardEnum
genumFromThenTo :: (Generic a, GEnum StandardEnum (Rep a)) => a -> a -> a -> [a]
genumFromThenTo = genumFromThenTo' @StandardEnum
gtoFiniteEnum :: forall a. (Generic a, GEnum FiniteEnum (Rep a)) => Int -> a
gtoFiniteEnum = gtoEnum' @FiniteEnum "gtoFiniteEnum"
gfromFiniteEnum :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> Int
gfromFiniteEnum = gfromEnum' @FiniteEnum
gfiniteEnumFrom :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> [a]
gfiniteEnumFrom = genumFrom' @FiniteEnum
gfiniteEnumFromThen :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> [a]
gfiniteEnumFromThen = genumFromThen' @FiniteEnum
gfiniteEnumFromTo :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> [a]
gfiniteEnumFromTo = genumFromTo' @FiniteEnum
gfiniteEnumFromThenTo :: (Generic a, GEnum FiniteEnum (Rep a)) => a -> a -> a -> [a]
gfiniteEnumFromThenTo = genumFromThenTo' @FiniteEnum
gtoEnumRaw' :: forall opts a. (Generic a, GEnum opts (Rep a)) => Int -> a
gtoEnumRaw' = to . gToEnum @opts
gtoEnum' :: forall opts a. (Generic a, GEnum opts (Rep a)) => String -> Int -> a
gtoEnum' name n
| 0 <= n && n < card = gtoEnumRaw' @opts n
| otherwise = error $
name ++ ": out of bounds, index " ++ show n ++ ", cardinality " ++ show card
where
card = gCardinality @opts @(Rep a)
gfromEnum' :: forall opts a. (Generic a, GEnum opts (Rep a)) => a -> Int
gfromEnum' = gFromEnum @opts . from
genumMin :: Int
genumMin = 0
genumMax :: forall opts a. (Generic a, GEnum opts (Rep a)) => Int
genumMax = gCardinality @opts @(Rep a) - 1
genumFrom' :: forall opts a. (Generic a, GEnum opts (Rep a)) => a -> [a]
genumFrom' x = map toE [ i_x .. genumMax @opts @a ]
where
toE = gtoEnumRaw' @opts
i_x = gfromEnum' @opts x
genumFromThen' :: forall opts a. (Generic a, GEnum opts (Rep a)) => a -> a -> [a]
genumFromThen' x1 x2 = map toE [ i_x1, i_x2 .. bound ]
where
toE = gtoEnumRaw' @opts
i_x1 = gfromEnum' @opts x1
i_x2 = gfromEnum' @opts x2
bound | i_x1 >= i_x2 = genumMin
| otherwise = genumMax @opts @a
genumFromTo' :: forall opts a. (Generic a, GEnum opts (Rep a)) => a -> a -> [a]
genumFromTo' x y = map toE [ i_x .. i_y ]
where
toE = gtoEnumRaw' @opts
i_x = gfromEnum' @opts x
i_y = gfromEnum' @opts y
genumFromThenTo' :: forall opts a. (Generic a, GEnum opts (Rep a)) => a -> a -> a -> [a]
genumFromThenTo' x1 x2 y = map toE [ i_x1, i_x2 .. i_y ]
where
toE = gtoEnumRaw' @opts
i_x1 = gfromEnum' @opts x1
i_x2 = gfromEnum' @opts x2
i_y = gfromEnum' @opts y
gminBound :: (Generic a, GBounded (Rep a)) => a
gminBound = to gMinBound
gmaxBound :: (Generic a, GBounded (Rep a)) => a
gmaxBound = to gMaxBound
class GEnum opts f where
gCardinality :: Int
gFromEnum :: f p -> Int
gToEnum :: Int -> f p
data StandardEnum
data FiniteEnum
instance GEnum opts f => GEnum opts (M1 i c f) where
gCardinality = gCardinality @opts @f
gFromEnum = gFromEnum @opts . unM1
gToEnum = M1 . gToEnum @opts
instance (GEnum opts f, GEnum opts g) => GEnum opts (f :+: g) where
gCardinality = gCardinality @opts @f + gCardinality @opts @g
gFromEnum (L1 x) = gFromEnum @opts x
gFromEnum (R1 y) = cardF + gFromEnum @opts y
where
cardF = gCardinality @opts @f
gToEnum n
| n < cardF = L1 (gToEnum @opts n)
| otherwise = R1 (gToEnum @opts (n - cardF))
where
cardF = gCardinality @opts @f
instance (GEnum FiniteEnum f, GEnum FiniteEnum g) => GEnum FiniteEnum (f :*: g) where
gCardinality = gCardinality @FiniteEnum @f * gCardinality @FiniteEnum @g
gFromEnum (x :*: y) = gFromEnum @FiniteEnum x * cardG + gFromEnum @FiniteEnum y
where
cardG = gCardinality @FiniteEnum @g
gToEnum n = gToEnum @FiniteEnum x :*: gToEnum @FiniteEnum y
where
(x, y) = n `quotRem` cardG
cardG = gCardinality @FiniteEnum @g
instance GEnum opts U1 where
gCardinality = 1
gFromEnum U1 = 0
gToEnum _ = U1
instance (Bounded c, Enum c) => GEnum FiniteEnum (K1 i c) where
gCardinality = fromEnum (maxBound :: c) + 1
gFromEnum = fromEnum . unK1
gToEnum = K1 . 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
gMaxBound = U1
instance Bounded c => GBounded (K1 i c) where
gMinBound = K1 minBound
gMaxBound = K1 maxBound
instance (GBounded f, GBounded g) => GBounded (f :+: g) where
gMinBound = L1 gMinBound
gMaxBound = R1 gMaxBound
instance (GBounded f, GBounded g) => GBounded (f :*: g) where
gMinBound = gMinBound :*: gMinBound
gMaxBound = gMaxBound :*: gMaxBound