Class instance for a type without a parameter

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 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. Thanks, Peter

Hi,
On 11 March 2012 15:57, Peter Hall
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 cases. 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]' HTH, Ozgur

On Sunday 11 March 2012, 16:57:30, Peter Hall 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
It's because the language standard says that the types in the instance head must be of the form T a1 ... ak with `T' a type constructor and the ai distinct type variables. Why that is, I don't know. You can make an instance Num [Digit] where ... if you enable {-# LANGUAGE FlexibleInstances #-} That's a harmless extension allowing instances of that form.
participants (3)
-
Daniel Fischer
-
Ozgur Akgun
-
Peter Hall