
I'm a little confused. Why is this allowed:
data Blah = Blah
instance Eq Blah where x == y = True
But not this:
class Stringable a where toString :: a -> String
instance Stringable [Char] where toString = id
(Resulting in:)
Illegal instance declaration for `Stringable [Char]' (All instance types must be of the form (T a1 ... an) where a1 ... an are distinct type *variables* Use -XFlexibleInstances if you want to disable this.) In the instance declaration for `Stringable [Char]'
'Blah' isn't a type variable, is it? Is my brain just not working right today?

On Tue, 2008-10-14 at 19:20 -0700, George Pollard wrote:
I'm a little confused. Why is this allowed:
data Blah = Blah
instance Eq Blah where x == y = True
But not this:
class Stringable a where toString :: a -> String
instance Stringable [Char] where toString = id
(Resulting in:)
Illegal instance declaration for `Stringable [Char]' (All instance types must be of the form (T a1 ... an) where a1 ... an are distinct type *variables* Use -XFlexibleInstances if you want to disable this.) In the instance declaration for `Stringable [Char]'
'Blah' isn't a type variable, is it? Is my brain just not working right today?
Blah = T for [Char], T = [] and a1 = Char where it should be a variable. Why this is an error is basically because the Report says so.

Hi,
There is some discussion about the different design choices relevant
for Haskell's class system in the following paper:
"Type classes: exploring the design space"
Simon Peyton Jones, Mark Jones, Erik Meijer
Presented at the 1997 Haskell Workshop.
Section 4.5 discusses options related to the restrictions on the instance heads.
-Iavor
On Tue, Oct 14, 2008 at 7:32 PM, Derek Elkins
On Tue, 2008-10-14 at 19:20 -0700, George Pollard wrote:
I'm a little confused. Why is this allowed:
data Blah = Blah
instance Eq Blah where x == y = True
But not this:
class Stringable a where toString :: a -> String
instance Stringable [Char] where toString = id
(Resulting in:)
Illegal instance declaration for `Stringable [Char]' (All instance types must be of the form (T a1 ... an) where a1 ... an are distinct type *variables* Use -XFlexibleInstances if you want to disable this.) In the instance declaration for `Stringable [Char]'
'Blah' isn't a type variable, is it? Is my brain just not working right today?
Blah = T
for [Char], T = [] and a1 = Char where it should be a variable.
Why this is an error is basically because the Report says so.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Tue, Oct 14, 2008 at 8:20 PM, George Pollard
Illegal instance declaration for `Stringable [Char]' (All instance types must be of the form (T a1 ... an) where a1 ... an are distinct type *variables* Use -XFlexibleInstances if you want to disable this.) In the instance declaration for `Stringable [Char]'
'Blah' isn't a type variable, is it? Is my brain just not working right today?
If you're stuck, this is a fairly arbitrary limitation and is easily generalized. GHC, at least, implements the FlexibleInstances extension. Put {-# LANGUAGE FlexibleInstances #-} At the top of the file and that will be an acceptable instance. Luke

George Pollard wrote:
I'm a little confused. Why is this allowed:
instance Eq Blah where x == y = True
But not this:
Illegal instance declaration for `Stringable [Char]' (All instance types must be of the form (T a1 ... an) where a1 ... an are distinct type *variables* Use -XFlexibleInstances if you want to disable this.) In the instance declaration for `Stringable [Char]'
'Blah' isn't a type variable, is it? Is my brain just not working right today?
Just in case: n=0 for "instance Eq Blah", i.e., "T a1 ... an" becomes "T".

Hello Albert, Wednesday, October 15, 2008, 7:51:06 AM, you wrote:
Illegal instance declaration for `Stringable [Char]' (All instance types must be of the form (T a1 ... an) where a1 ... an are distinct type *variables*
Just in case: n=0 for "instance Eq Blah", i.e., "T a1 ... an" becomes "T".
and [Char] = [] Char, where Char isn't type variable but constant -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

So, the Haskell98 solution to this is:
class StringableList a where
listToString :: [a] -> String
-- now [a] is of the proper form; T = [], a is a type variable
instance StringableList a => Stringable [a] where
toString = listToString
-- now to make an instance for Stringable [Char]
-- we just make an instance for StringableList Char
instance StringableList Char where
listToString = id
I think "FlexibleInstances" just makes the compiler jump through these
hoops instead of you.
-- ryan
On Wed, Oct 15, 2008 at 3:20 AM, George Pollard
I'm a little confused. Why is this allowed:
data Blah = Blah
instance Eq Blah where x == y = True
But not this:
class Stringable a where toString :: a -> String
instance Stringable [Char] where toString = id
(Resulting in:)
Illegal instance declaration for `Stringable [Char]' (All instance types must be of the form (T a1 ... an) where a1 ... an are distinct type *variables* Use -XFlexibleInstances if you want to disable this.) In the instance declaration for `Stringable [Char]'
'Blah' isn't a type variable, is it? Is my brain just not working right today?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks for the portable solution.
I'd also like to know how is the following different from
-XFlexibleInstances with [Char]? Stronger, weaker, same thing?
{-# OPTIONS -XTypeSynonymInstances #-}
class Stringable a where
toString :: a -> String
instance Stringable String where
toString = id
--A
On Wed, Oct 15, 2008 at 10:16 AM, Ryan Ingram
So, the Haskell98 solution to this is:
class StringableList a where listToString :: [a] -> String
-- now [a] is of the proper form; T = [], a is a type variable instance StringableList a => Stringable [a] where toString = listToString
-- now to make an instance for Stringable [Char] -- we just make an instance for StringableList Char instance StringableList Char where listToString = id
I think "FlexibleInstances" just makes the compiler jump through these hoops instead of you.
-- ryan
On Wed, Oct 15, 2008 at 3:20 AM, George Pollard
wrote: I'm a little confused. Why is this allowed:
data Blah = Blah
instance Eq Blah where x == y = True
But not this:
class Stringable a where toString :: a -> String
instance Stringable [Char] where toString = id
(Resulting in:)
Illegal instance declaration for `Stringable [Char]' (All instance types must be of the form (T a1 ... an) where a1 ... an are distinct type *variables* Use -XFlexibleInstances if you want to disable this.) In the instance declaration for `Stringable [Char]'
'Blah' isn't a type variable, is it? Is my brain just not working right today?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks to all that replied (Derek & Ryan for the major understanding, Albert & Luke also)*. I guess I was getting confused with the error message:
(All instance types must be of the form (T a1 ... an)
I was interpreting this with Stringable/Enum as T and [Char]/Blah as a1. Now I have clarity! I shall have to follow up Iavor's lead on why this *is*. Thanks! *[Silly email reply system; discussions on the internet should form an acyclic directed graph, not a tree! This, incidentally, is why I'm replying to myself.]

George Pollard schrieb:
I'm a little confused. Why is this allowed:
data Blah = Blah
instance Eq Blah where x == y = True
But not this:
class Stringable a where toString :: a -> String
instance Stringable [Char] where toString = id
(Resulting in:)
Illegal instance declaration for `Stringable [Char]' (All instance types must be of the form (T a1 ... an) where a1 ... an are distinct type *variables* Use -XFlexibleInstances if you want to disable this.) In the instance declaration for `Stringable [Char]'
'Blah' isn't a type variable, is it? Is my brain just not working right today?
participants (9)
-
Albert Y. C. Lai
-
Anton Tayanovskyy
-
Bulat Ziganshin
-
Derek Elkins
-
George Pollard
-
Henning Thielemann
-
Iavor Diatchki
-
Luke Palmer
-
Ryan Ingram