[m-dev.] Contribution: Pretty Printing Library

Ralph Becket rbeck at microsoft.com
Wed Apr 12 22:59:02 AEST 2000


> From: Fergus Henderson [mailto:fjh at cs.mu.OZ.AU]
> 
> > nest("my ", nest("label: ", Doc)) = nest("my label: ", Doc)
> > 
> > or
> > 
> > nest("my ", nest("label: ", Doc)) = nest("label: ", Doc)
> > 
> > ?
> 
> Probably the former, I think.

That's what I thought - it's all working.

> > What should we do for `nest(2, nest("my label: ", Doc))' ?
> 
> That should definitely indent by two spaces followed by
> "my label:".

Yes, the new constructor (label/2) is just a special form of
nest/2.

> BTW, I suggest spelling the one that indents by an
> int as `indent' rather than `nest'.

Agreed - see above.

> > pprint__write(N,
> >   nest(
> >     "my label: ",
> >     line `<>` group(text("this is ") `</>` text("some text"))
> >   )
> > )
> > 
> > would produce the following, depending upon N:
> > 
> > my label: this is some text		{ N > 24 }
> > 
> > or
> > 
> > my label: this is
> > my label: some text			{ N < 24 }
> 
> I notice that you have defined the string version of
> nest to always insert the label at the start,
> whereas the int version only inserts the spaces
> after newlines.

No, I think you missed the line/0 preceding the group/1
in the nest (er, label).

> Note that given the version which does not insert at
> the start, you can easily define the version which does,
> e.g.
> 
> 	:- func prefix(string, doc) = doc.
> 	prefix(Prefix, Doc) = nest(Prefix, Prefix `<>` Doc).
> 
> but you can't do it the other way around.

We're in violent agreement.

> Something else that would be nice would be providing a way
> to insert something at the end of every line.  This would
> be useful for formatting code in languages such as `sh',
> `sed', or `make' that use line continuation characters
> (e.g. trailing backslash).

Agh!  It was nice and simple.  It's growing hairy!  But you're
right...

> Another nice feature would be a function which handles word-wrap.
> This can be programmed easily enough using the functions in
> your pretty-print module, but I think it would be useful
> enough to include in that module.
> 
> 		% Splits the string at spaces, and joins the resulting
> 		% pieces using `separated(text, space_line, Pieces)'.
> 	:- func word_wrap(string) = doc.
> 	word_wrap(String) = separated(text, space_line, Words) :-
> 		Words = split_string(String, ' ').
> 
> See the attached files for a definition of split_string.

Another capital idea.  I'll put it in this afternoon.

Ralph
--------------------------------------------------------------------------
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