[Haskell-cafe] Can this be improved?

Chris Kuklewicz haskell at list.mightyreason.com
Fri Dec 30 09:26:29 EST 2005

Udo Stenzel wrote:
> This is your only special case, a single list of a sigle type that is
> different from other lists?  To avoid undecidable, incoherent and other
> nasty instances, just do what the Prelude does:
> class MyShow t where
>     swim     :: t -> String
>     swimList :: [t] -> String
>     swimList [] = "[]"
>     swimList xs = '[' : foldr1 (\x -> (swim x ++) . (',' :)) "]" xs
> instance MyShow Char where
>     swim     = (:[])
>     swimList = id
> Untested, but you get the idea.  It's pure Haskell 98.  Heck, it's
> almost Prelude.Show.  It won't scale to more type constructors easily,
> but you didn't ask for that ability, either ;-)
> Udo.

You example treats Char specially.  I want (swim 'a') to be ("'a'") not

I want every type but String to eventually use the builtin "show"
function.  This is not what your example does unless I make it longer.
For example, if I wrote

instance MyShow Int where swim = show
instance MyShow Double where swim = show
... many many instances ...

then it would work.

I need for the compiler to understand that MyShow String has swim = id
and MyShow (any other Show instance but String) has swim = show.

GHC can do this, the working examples with -fallow* prove it.  I was
curious if there is some type level programming knowledge in the Haskell
community which would allow me to express (any other Show instance but
String) in a non-overlapping way.

I can read the HList and OOHaskell work, and see the advanced type level
programming, but I am not yet clever enough to derive a solution for MyShow.


More information about the Haskell-Cafe mailing list