
On Nov 6, 2007 10:30 AM, Bayley, Alistair
From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@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 |
= 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