FYI (I'm replying shortly)

-----Original Message-----
From: Philip Wadler [mailto:[email protected]] 
Sent: 22 August 2011 11:39
To: Simon Peyton-Jones
Cc: José Pedro Magalhães; Razvan Ranca
Subject: Re: Support for pretty printing in GHC

Hi Simon,

I wrote to you in May to say that I would have a student
writing a generic pretty printer for Haskell, and to ask on
your advice for which generic mechanism to use.  The
student, Razvan Ranca, has now completed his work, which is
available here:

  http://hackage.haskell.org/package/GenericPretty

It is based on the GHC Generics features in GHC 7.2 that
you recommended.

My motivation for generic pretty printing is practical.  In
my first year class, I often demonstrate code using GHCi,
with (alas!) unreadable output such as this:

  > fsm0
  FSM {states = [0,1,2], start = 0, final = [2], arcs = [Arc
{from = 0, by = 'a', to = 1},Arc {from = 1, by = 'b', to =
0},Arc {from = 1, by = 'c', to = 2}]}

It would be better if it was prettyprinted by default:

  > fsm0
  FSM {
    states = [0,1,2],
    start = 0,
    final = [2],
    arcs = [
      Arc {from = 0, by = 'a', to = 1},
      Arc {from = 1, by = 'b', to = 0},
      Arc {from = 1, by = 'c', to = 2}
    ]
  }

As well as a pedagogic aid in class, this would aid
me in testing and debugging.

Razvan has completed a generic pretty printer that creates
the output above.  It uses a class Out, similar in structure
to Outputable.  We could not use Outputable itself, because
in order to get parentheses right, we need a precedence
parameter corresponding to that used by showsPrec, which
Outputable does not support.

To use Razvan's library with first-year students, some extra
support in GHC is required.

  * The ability to specify how the read-eval-print loop
  converts data values to strings.

  * The ability to specify new class names that can
  appear in a deriving clause.

  * To make things easy for first years, it would also
  help to be able to create a custom version of ghci,
  which automatically imports the necessary modules
  and turns on the necessary pragmas.  This would be
  easy if GHCi had flags to specify the necessary
  information.

Is there any chance of getting the above support
added to GHCi?

Another alternative, which you and I discussed at one point,
is to build in the appropriate concepts directly.  I suspect
that everyone will have slightly different notions of the
best pretty printer to use, so I suspect the above route is
preferable as it supports customisation and experimentation.

Also, a question about the current pretty printing library.
Razvan built the generic derivation of pretty printers on
top of the current library,

  http://www.haskell.org/ghc/docs/7.0.4/html/libraries/ghc-7.0.4/Pretty.html

In the course of this, we realised that the library defaults
to a line length of 120, which is not suitable for our
purposes.  For 120 to be convenient, one must use a small
font by default---no good for display on an overhead, and no
good for those of us with deteriorating eyesight.  A line
length of 72 or 80 seems to work better with most systems,
as well as being a historical default widely used.  Is there
any way to change the library?  It's rather sad that the
default is hardwired and not easy to customise---this is one
of the few places where I envy OO languages, which are
optimised for this sort of customisation.

Output via strings alone is obsolete.  Haskell should lead
the way by providing customisable support for output via
documents.  I'm interested in documents for pretty printing,
but it is easy to imagine other applications as well, such
as HTML.

What do you think?  Many thanks for your help.  Yours,  -- P

-- 
.\ Philip Wadler, Professor of Theoretical Computer Science
./\ School of Informatics, University of Edinburgh
/  \ http://homepages.inf.ed.ac.uk/wadler/

The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.



_______________________________________________
Cvs-ghc mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/cvs-ghc

Reply via email to