Strange error in show for datatype

Olaf Chitil olaf@cs.york.ac.uk
Thu, 04 Oct 2001 10:53:38 +0100


> My claim was that
> 
>         forall a. Show a => T
> 
> could be implemented by passing a bottom dictionary for Show.  

Excuse me, but Jan-Willem Maessen has already shown that this
implementation can give unexpected results. A simple example:

instance Show MyType where
  shows _ = ("element of my type" : )

Then
show (undefined :: MyType)
yields
"element of my type"

and with the suggested implementation
show undefined
would yield
undefined

The instance may look a bit weird, but in general you cannot assume that
the functions of all instances of all classes are strict. In fact, some
Haskell systems (e.g. nhc98) extend class Show by another method
showsType :: a -> String; the function is non-strict in all instances.

Anyway, I find all these suggestions about occurrence and variance of
type variables rather complicated. Worse than the monomorphic
restriction. Admittedly, many Haskell beginners fall into the `show []'
trap. However, this is a chance for the teacher to discuss the problem
(possibly before they fall into the trap). Any type system that prevents
you from making some mistakes has to reject some perfectly sound
programs (because the absence of mistakes is not decidable). Hopefully
the type system is simple enough to enable the programmer to understand
why a program is rejected. And fortunately in this special case there is
a simple way around the problem (specify a type).

By the way, I'm sure some months (a year?) ago there was a similar
discussion about replacing dictionaries by bottom dictionaries on a
Haskell mailing list or comp.lang.functional. Unfortunately I don't find
it anymore.

Ciao,
Olaf

-- 
OLAF CHITIL, 
 Dept. of Computer Science, University of York, York YO10 5DD, UK. 
 URL: http://www.cs.york.ac.uk/~olaf/
 Tel: +44 1904 434756; Fax: +44 1904 432767