Haskell report typo (bad law for readsPrec)

Patrik Jansson patrikj@cs.chalmers.se
Wed, 20 Jun 2001 11:04:29 +0200 (MET DST)


On Tue, 19 Jun 2001, Wolfgang Lux wrote:
> > > | Another simple alternative is to weaken the the equation by
> > > | replacing the variable r by the empty string: [Alternative B:]
> > > |
> > > | ...
> > > |    should be the case that
> > > |
> > > |      fst (head (readsPrec d (showsPrec d x ""))) == x
> > >
> > > Yes, I like this alternative too.  I'll implement it.
> >
> > Why not require
> >
> >   readsPrec d (showPrec d x "") == [(x,"")]
> >
> > in this case? At least it should be
> >
> >   head (readsPrec d x "") == (x,"")
[Typo - you mean:]
      head (readsPrec d (showsPrec d x "")) == (x,"")

Both formulations look OK to me, but neither is an essential change and
there are many other small variations possible with slight semantical
differences. One example is

      (x,"") `elem` readsPrec d (showsPrec d x "")

And already the current (proposed) formulation is most likely impossible
to satisfy if the element x contains bottoms somewhere inside. (Example:
try to show and read the string (undefined : "hello").)

I think we'll have to accept that Show and Read are to be used mainly for
presentation, and not when real uniquely invertible behaviour is desired.
I think a separate Serializable class designed for correct and portable
(and efficient?) data transmission is necessary anyway. In that design
process the laws would have to be thought through much more carefully.
(There are suggestions for a library already - see below.)

/Patrik

A quick search:

http://www.cs.york.ac.uk/fp/nhc98/libs/Binary.html

The Bits Between The Lambdas: Binary Data in a Lazy Functional Language (1998)
  Malcolm Wallace Colin Runciman
  http://citeseer.nj.nec.com/260182.html

Heap Compression and Binary I/O in Haskell
  Malcolm Wallace Colin Runciman
  http://citeseer.nj.nec.com/68962.html