Reputation: 141
I just created a class with Haskell but I have a problem with one of my instances. I've created this class:
class Symbol a where
nameSym :: a -> String
and these instances:
instance Symbol Double where
nameSym db = show db
instance Symbol String where
nameSym str = str
but when compiling, I get the following error message:
Illegal instance declaration for `Symbol String'
(All instance types must be of the form (T t1 ... tn)
where T is not a synonym.
In the instance declaration for `Symbol String'
Do you know what is the problem?
Upvotes: 3
Views: 1283
Reputation: 38217
The problem is that String
is a type alias for [Char]
and the Haskell 98 specification does not allow for instances to be defined on type aliases. That can be solved by adding this in the header of the file:
{-# LANGUAGE TypeSynonymInstances #-}
However, that still won't allow you to compile the program as Haskell 98 also doesn't allow instances for [SomeConcreteType]
(only instances for [a]
are allowed — thanks to Ørjan for pointing that one out), and whereas there exists a workaround for that in Haskell 98 without the use of language pragmas, the easiest way to solve this one (and it's also completely safe and idiomatic) is to add the following pragma:
{-# LANGUAGE FlexibleInstances #-}
— this also enables TypeSynonymInstances
so you don't need to keep both pragmas.
Upvotes: 6
Reputation: 2721
You can use the pragma :
{-# LANGUAGE FlexibleInstances #-}
at the head of your source file.
Upvotes: 3