[Haskell-cafe] type/class question: toString
Graham Fawcett
graham.fawcett at gmail.com
Tue Nov 6 11:21:40 EST 2007
On Nov 6, 2007 10:30 AM, Bayley, Alistair
<Alistair_Bayley at invescoperpetual.co.uk> wrote:
> > From: haskell-cafe-bounces at haskell.org
> > [mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Graham Fawcett
> > Is there a way to declare a 'toString' function, such that
> > toString x | <x is a String> = x
> > toString x | <x's type is an instance of Show> = show x
> I'm assuming you're not fond of the way the print function handles
> Strings?
More a curiosity about the flexibility of the type system -- but yes,
I could see cases where such a thing could be useful.
> 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
This doesn't want to compile for me:
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.6.1
$ ghc ToString.hs # your code, verbatim
ToString.hs:5:0:
Illegal instance declaration for `MyShow String'
(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 `MyShow String'
ToString.hs:6:0:
Illegal instance declaration for `MyShow 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 `MyShow a'
I'll read up on those two GHC options, and try to figure it out myself
(but hints are welcome).
Thanks,
Graham
More information about the Haskell-Cafe
mailing list