Am I right to conclude that there’s no way to compute maxBound - minBound in Haskell for an arbitrary Enum and Bounded type? Or am I missing some trick/hack? This is what I have, which clearly can’t work:
difference :: (Enum a, Bounded a) => Int
difference = fromEnum maxBound - fromEnum minBound
Error:
Foo.hs:37:1:
Ambiguous constraint `Enum a'
At least one of the forall'd type variables mentioned by the constraint
must be reachable from the type after the '=>'
In the type signature for `difference': difference :: (Enum a, Bounded a) => Int
Foo.hs:37:1:
Ambiguous constraint `Bounded a'
At least one of the forall'd type variables mentioned by the constraint
must be reachable from the type after the '=>'
In the type signature for `difference': difference :: (Enum a, Bounded a) => Int
I understand why I’m getting that error—there’s no actual term in there with type a, so it can’t figure out what a is. The question is whether there’s a way to get around this.
Use a
Proxyto specify which type you want, and useScopedTypeVariablesto bring that type into scope in your function definition.Edit: Using Daniel’s suggestion: