[Haskell-cafe] Class
Slavomir Kaslev
slavomir.kaslev at gmail.com
Thu Nov 2 09:51:17 EST 2006
On 11/2/06, Bulat Ziganshin <bulat.ziganshin at gmail.com> wrote:
> 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
I am on it. Thanks for pointing it to me. =-)
>
> 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
>
I wouldn't like to manually define instances of Visible for all types
that have Show instances. I think I need something like that:
class Visible a where
toString :: a -> String
size :: a -> Int
instance Show a => Visible a where
toString = show
size = length . show
Which still doesn't work. Any ideas?
--
Slavomir Kaslev
More information about the Haskell-Cafe
mailing list