[Haskell-cafe] Class

Slavomir Kaslev slavomir.kaslev at gmail.com
Thu Nov 2 13:59:38 EST 2006


On 11/2/06, Sebastian Sylvan <sylvan at student.chalmers.se> wrote:
> On 11/2/06, Slavomir Kaslev <slavomir.kaslev at gmail.com> wrote:
> > Little by little, I think I am getting closer.
> >
> > class Show a => Visible a where
> >     toString :: a -> String
> >     toString = show
> >     size :: a -> Int
> >     size = length . show
> >
> > Is just declaration, not definition. It doesn't define anything, even
> > though it has default implementations for toString and size one still
> > needs to define instance of it. Right?
> >
> > As Sebastian Sylvan proposed, I probably need something like this:
> >
> > class Visible a where
> >     toString :: a -> String
> >     size :: a -> Int
> >
> > instance Show a => Visible a where
> >     toString = show
> >     size = length . show
> >
> > But it seems ghc doesn't like instance definitions like 'instance Show
> > a => Visible a where ...'. Why?
>
> This is not Haskell98 since instance declarations of this form could
> cause the type checker to go into an infinite loop. This particular
> one is okay, though, but you have to start ghc with
> -fallow-undecidable-instances and -fglasgow-exts I'm afraid.
>
>
> --
> Sebastian Sylvan
> +46(0)736-818655
> UIN: 44640862
>

Thanks, Sebastian. That was helpful. Are there any papers on the subject?

-- 
Slavomir Kaslev


More information about the Haskell-Cafe mailing list