[m-rev.] For review: user-configurable pretty printer

Ralph Becket rafe at csse.unimelb.edu.au
Wed Jun 6 10:45:01 AEST 2007


Peter Ross, Tuesday,  5 June 2007:
> On 6/5/07, Ralph Becket <rafe at csse.unimelb.edu.au> wrote:
> >    % The type and inst of pretty-printer converters.
> >    %
> >:- type pp == ( pred(univ, docs) ).
> >:- inst pp == ( pred(in, out) is semidet ).
> >
> I did a similar thing once to add user defined pretty printing, except
> I used the type_desc to see if a particular type had a pretty printing
> alternative.
> 
> :- pred register_pretty_printer(type_desc::in, (func(univ) = doc)::in,
> io::di, io::uo) is det.
> 
> Then the pretty printing code simply checked a map to see if the
> current type was handled specially by the pretty printer.

You wouldn't want to use type_descs because they pertain to ground types
when you really want your pretty printer to handle, say, list(T) for all
T or map(K, V) for all K and V.  However, using type_ctor_descs might work
(all instances of list(T) and map(K, V) have type_ctor_descs list/1 and
map/2 respectively).

> This maybe more efficient then trying a potentially very long list of
> predicates to find one which succeeds.

That's a point; I'm not sure whether it would be an issue in practice:
how many types in the library need a specialised pretty printer?  I can
think of just a few: the maps, list, the arrays, the sets, tuple, int,
string, float, char.

The drawback is that it makes the interface more complicated:
- adding a new pretty printer also involves specifying the corresponding
  type_ctor_desc, e.g.:

    add_user_defined_pp(type_ctor(type_desc(_ : map(unit, unit))),
        pp_map, !IO)

  (we need to supply types for map in order to avoid a warning; I've
  used unit, but it's all rather ugly) rather than just

    add_user_defined_pp(pp_map, !IO)

- I think this approach limits us to having one pretty printer per type
  constructor, so you couldn't add a specialised pretty printer for,
  say, map(foo, bar) on top of the generic map(K, V) pretty printer.
  Would this be an issue?

-- Ralph
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list