Pretty class as alternative for Show class

Henning Thielemann lemming at henning-thielemann.de
Wed Feb 13 01:45:27 EST 2008


 There seems to be the need for (at least) two classes for showing values
in Haskell: One class that outputs values in a way that can be copied and
pasted into Haskell programs. This is useful in GHCi. Another class (say
Pretty) that shows values in a prettily formatted way. Current libraries
use the Show class for both applications depending on the taste of the
author. A third class would be useful for outputting values with markup,
say HTML or LaTeX.
 The Show class converts to String, the Pretty class could convert to a
pretty printer data type like Doc. The Show class should output all data
that is necessary to reconstruct the value, but not necessarily in the
form of the underlying data structure, but maybe using appropriate
generating functions. Thus e.g. 'show' for Doubles does not need a
parameter for precision, it just emits all available digits. On the other
hand a Pretty class should have such parameters (maybe then requiring a
multi-parameter type class with functional dependency from value type to
formatting style type). A nice infix operator like (//) like in the GSL
wrapper could flatten the Pretty data to String, and thus is easy to use
in GHCi.

Matrix> fiboMatrix
matrix [[1,0], [1,1]]
Matrix> fiboMatrix // 3
/1.000 0.000\
\1.000 1.000/

where
  (//) :: Pretty style value => value -> style -> IO String
  x // p = putStr (Doc.toString (Pretty.toDoc p x))

  class Pretty style value | value -> style where
     toDoc :: style -> value -> Doc


 If such a separation would become consensus then libraries could better
separate the concerns of pretty output and re-usable output.


More information about the Libraries mailing list