instance Num [Digit] where ... -- This isn't allowed
I could make Num a => [a] an instance of Num, but I can't make Digit
also an instance of Num because D8 + D5 is not a digit. So then I
could introduce a ConvertableToNum class, but I feel like I'd be
heading down a path with a lot of unnecessary extra type classes.
On 11 March 2012 15:57, Peter Hall <peter.hall at memorphic.com> wrote:
> Can someone remind me why I can't do this:
> data Digit = D0 | D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8 | D9
> deriving (Eq, Ord, Show)
> instance Num [Digit] where ... -- This isn't allowed
Why is it not allowed? Yes, it needs FlexibleInstances, but this is a
fairly common language extension so shouldn't be a big problem for most use
The error message generated by ghc should tell you about this. Following is
what I get for example:
Illegal instance declaration for `Num [Digit]'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Num [Digit]'