
I think this must almost be a FAQ, or at least a PAQ (Previously AQ)... If I have a type class for conversion to a type X: class XType a where toX :: a -> X I can define instances for instance XType Int where toX = ... instance XType Double where toX = ... instance XType Tuple where toX = ... but not for Strings, given that they are a synonym for [Char]. Hence: instance XType String where toX = ... results in: Illegal instance declaration for `XType String' (The instance type must be of form (T a b c) where T is not a synonym, and a,b,c are distinct type variables) In the instance declaration for `XType String' Is there some type class cleverness that can make this work in haskell 98? I can create a new wrapper type for strings: newtype StringWrap = StringWrap String and write an instance for that, but then I'll have to litter my code with calls to this constructor. I'm aware of the approach taken by class Show in the prelude, which adds a extra method to the class: class XType a where toX :: a -> X listToX :: [a] -> X but I believe this says that whenever we can convert a to an X we can also convert [a] to an X, whereas I only want [Char] to be acceptable. Thanks for any pointers. Tim

Tim Docker wrote:
I think this must almost be a FAQ, or at least a PAQ (Previously AQ)...
If I have a type class for conversion to a type X:
class XType a where toX :: a -> X
I can define instances for
instance XType Int where toX = ... instance XType Double where toX = ... instance XType Tuple where toX = ...
but not for Strings, given that they are a synonym for [Char]. Hence:
instance XType String where toX = ...
results in:
Illegal instance declaration for `XType String' (The instance type must be of form (T a b c) where T is not a synonym, and a,b,c are distinct type variables) In the instance declaration for `XType String'
Is there some type class cleverness that can make this work in haskell 98? I can create a new wrapper type for strings:
newtype StringWrap = StringWrap String
and write an instance for that, but then I'll have to litter my code with calls to this constructor.
I'm aware of the approach taken by class Show in the prelude, which adds a extra method to the class:
class XType a where toX :: a -> X listToX :: [a] -> X
but I believe this says that whenever we can convert a to an X we can also convert [a] to an X, whereas I only want [Char] to be acceptable.
I believe there is a trick where essentially you end up with, instance IsChar a => XType [a] where ...

Tim,
If I have a type class for conversion to a type X:
class XType a where toX :: a -> X
[...]
instance XType String where toX = ...
results in:
Illegal instance declaration for `XType String' (The instance type must be of form (T a b c) where T is not a synonym, and a,b,c are distinct type variables) In the instance declaration for `XType String'
In addition to Derek's pointer, you could also consider extending the class definition: class XType a where toX :: a -> X listToX :: [a] -> X listToX = ... -- some default definition for listToX Of course, it depends on your type X whether a suitable default definition for listToX can be given. Assuming that it can, you can now, as before, have instance XType Int where toX = ... instance XType Double where toX = ... instance XType Tuple where toX = ... but also instance XType Char where toX c = ... -- your toX implementation for Char listToX s = ... -- your toX implementation for String This 'trick' is used in the standard libraries to accommodate a Show instance for String, for instance. Cheers, Stefan

On Tue, 22 May 2007, Tim Docker wrote:
I think this must almost be a FAQ, or at least a PAQ (Previously AQ)...
I think it too, thus I added your case to the Wiki: http://www.haskell.org/haskellwiki/List_instance

Thanks for this - I only wonder if the page title "List Instance" would have suggested that this was a solution to me problem - I can't think of a better name however: "Lists as type class instances" perhaps? -----Original Message----- From: Henning Thielemann [mailto:lemming@henning-thielemann.de] Sent: Tuesday, 22 May 2007 10:11 PM To: Tim Docker Cc: haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] type class question On Tue, 22 May 2007, Tim Docker wrote:
I think this must almost be a FAQ, or at least a PAQ (Previously AQ)...
I think it too, thus I added your case to the Wiki: http://www.haskell.org/haskellwiki/List_instance
participants (4)
-
Derek Elkins
-
Henning Thielemann
-
Stefan Holdermans
-
Tim Docker