[Haskell-cafe] Class

Bulat Ziganshin bulat.ziganshin at gmail.com
Thu Nov 2 09:42:32 EST 2006


Hello Slavomir,

Thursday, November 2, 2006, 4:42:21 PM, you wrote:

> instance Show a => Visible a where
>     toString = show
>     size = length . show

> instance Visible a => Visible [a] where
>    toString = concat . map toString
>    size = foldl (+) 0 . map size


>     Illegal instance declaration for `Visible a'
>         (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 `Visible a'
> Failed, modules loaded: none.

> at instance Show a => Visible a where.

> Probably I should reconsider my expectations? How should something
> like this designed?

1) i'm still highly recommend you wiki i've pointed before. i'd a lot
of similar problems until i realized that haskell classes are somethat
different form c++ ones

2) there are some proposals that will allow one to specify
anti-conditions or priority of declarations. but currently compiler
has no way to distinguish that instance should be used, for example,
for [Char] - both are good enough.

you may use -fglasgow-exts to supress this error message and and then
-fallow-incoherent-instances to allow compiler select random
definition from these two. you should look into ghc documentation for
more details about it 

in practice, i just define required function bodies and then declare
all specific instances i need:

toString1 = concat . map toString
size1 = foldl (+) 0 . map size

instance Visible [Char] where
    toString = toString1
    size = size1

instance Visible [Int] where
    toString = toString1
    size = size1



-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list