[Haskell-cafe] Flexible instances

Ryan Ingram ryani.spam at gmail.com
Wed Oct 15 03:16:39 EDT 2008


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 <porges at porg.es> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list