[Haskell-cafe] type/class question: toString

Luke Palmer lrpalmer at gmail.com
Tue Nov 6 12:21:07 EST 2007


> I'm assuming you're not fond of the way the print function handles
> Strings?
>
> With GHC you can do this:
>
> > {-# OPTIONS -fallow-overlapping-instances #-}
> > {-# OPTIONS -fallow-undecidable-instances #-}
> >
> > class Show a => MyShow a where show_ :: a -> String
> > instance MyShow String where show_ s = s
> > instance (Show a) => MyShow a where show_ s = show s

I'm curious why this works.  How does GHC know to pick the MyShow String
instance instead of the one coming from Show String?

I expect there's no way to do this without undecidable instances, is there?
I try to stay away from that flag nowadays, since I've seen some strange
unpredictable behavior from it in the past  (the unpredictability of the
behavior may come from the fact that I don't know how the inference algorithm
works).

Luke


More information about the Haskell-Cafe mailing list