I’ve recently been trying to “learn me a Haskell,” and I’d like to create a new type to represent an integer state, without just using a raw Integer (for type safety and code clarity). Specifically, the following code compiles:
newtype AuxState = AuxState Integer
deriving (Eq, Ord, Num, Integral, Real, Enum)
However, since there are an infinite number of states in my application, I have no interest in converting this state into an Enum. However, if I try to remove the deriving (Enum) statement so it’s just deriving (Eq, Ord, Num, Integral, Real), the compiler complains:
No instance for (Enum AuxState)
arising from the 'deriving' clause of a data type declaration
Possible fix:
add an instance declaration for (Enum AuxState)
or use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
When deriving the instance for (Integral AuxState)
I find it hard to believe that Haskell forces a type in the Integral class to also be in the Enum class; shouldn’t it just be the other way around? Is there a reason for this, or am I doing/understanding something wrong?
All
Integralare necessarilyEnumbecause the foundations ofIntegralmath are thesuccandpredoperations. (TechnicallyEnumis standing in for a proper type hierarchy where anIntegraltype is a mathematical semigroup, I think.) The other way around seems even more wrong: you mean that everyEnumshould beIntegral? Does this include random ADTs like?
(Every
Enumshould be isomorphic to a subset ofIntegral, surely, but that actually suggests it going the other direction:Integralcan represent anyEnumbut not vice versa, soIntegralis kind of the ur-Enum.)