For a library I'm writing, I would like to be able to retrieve the size of any type with Bounded
and Enum
constraints, as a type-level Nat
. The purpose is to define typeclass instances such as:
instance ( Enum a, Bounded a, n ~ BoundedEnumSize a ) => Action ( CyclicGroup n ) ( CyclicEnum a ) where
...
Is there perhaps a way to achieve this using Template Haskell, e.g.
class ( Enum a, Bounded a ) => BoundedEnum a where
type FiniteEnumSize a :: Nat
instance ( Enum a, Bounded a ) => BoundedEnum a where
type BoundedEnumSize a = ... Template Haskell ... 1 + fromEnum maxBound - fromEnum minBound
The only other "solution" I can think of would be to manually define BoundedEnum
instances for all types that have both Enum
and Bounded
instances, but this would lead to many orphan instances for users of the library (as I wouldn't be able to define all the necessary instances without importing the entire universe).
The finitary library is exactly what I was looking for, as it gives access at the type-level to the cardinality of any finite type, which works for types which contain fields.