[m-dev.] diff: use pretty printer in mdb

Mark Anthony BROWN dougl at cs.mu.OZ.AU
Wed May 24 01:26:41 AEST 2000


Ralph Becket writes:
> Unfortunately, this solution isn't quite correct (I'm about to post the
> correct code).
> 
> The pretty printer works by 
> 1. turning a document tree into a list of non-compound docs (including
> tracking nesting and labels etc.),
> 2. making formatting decisions (i.e. whether or not to remove `line'
> docs from a `group' doc), and
> 3. turning the resulting stream of primitive docs into a form ready for 
> output (i.e. text and newlines).
> 
> The subtle bit is that the formatting decision for a `group' doc depends
> upon whether or not the flattened version of the group doc *followed by*
> succeeding docs, up to the next `line' doc, can fit on the remainder of
> the current line.  Just considering the former may lead to strange results.
> 

That's true, but notice that the condition of the if-then-else below
still contains the call to fits/2, which considers the succeeding docs
as well as the flattened group.  So my solution would handle this
situation correctly.

However, this solution means that the width of the flattened group can be
checked a second time in fits/2, and sometimes there is an unnecessary
recursive call to be/3.  My solution is not as efficient as it could be.
The solution you recently posted eliminates these efficiency problems,
so we should use your solution in preference to mine.

Fergus pointed out that my solution overflows the det stack when formatting
very large terms (eg. dumping the MLDS).  I believe this is caused by
the way be/3 traverses the tree---there is one level of recursion for
every node in the document, due to the way 'SEQ' is handled in the
third clause.  Unfortunately, both our solutions have this problem,
and I can't see an easy way of dealing with it.

> >  be(_, _, [])                      = nil.
> > @@ -364,8 +357,8 @@
> >  be(W, _, [I - 'LINE'        | Z]) = I `line` be(W, 
> > string__length(I), Z).
> >  be(W, K, [I - 'GROUP'(X)    | Z]) =
> >      ( if
> > -        K =< W,                         % Really want an 
> > ordered conjunction...
> > -        Flattened = be(W, K, [I - flatten(X) | Z]),
> > +        try_flatten(X, FlatX, W - K, _),
> > +        Flattened = be(W, K, [I - FlatX | Z]),
> >          fits(W - K, Flattened)
> >        then
> >          Flattened

Cheers,
Mark.

--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list