[Haskell-cafe] Meaning of "ribbonsPerLine" at Text.PrettyPrint.HughesPJ ?

Thorkil Naur naur at post11.tele.dk
Thu Jun 19 03:14:10 EDT 2008


Hello,

On Wednesday 18 June 2008 22:13, Alfonso Acosta wrote:
> Hi,
> 
> Can anyone give a good explanation of what ribbonsPerLine means?
> 
> Maybe it would be better to simply ask for the meaning of ribbon in
> this context. The documentation is totally meaningless to me:
> "reibbonsPerLine: Ratio of ribbon length to line length".

In the paper "The Design of a Pretty-printing Library" by John Hughes 
(http://www.cs.chalmers.se/~rjmh/Papers/pretty.ps) that 
introduced this pretty printing library, the ribbon concept is introduced 
like this (apologies for the layout, please use the original .ps file for 
accuracy):

> 7.4 Choosing a Pretty Layout
>
> Now that we have designed combinators for constructing documents with many 
possible layouts, it is time to discuss choosing among those alternatives. 
Many prettyprinters aim simply to avoid exceeding a given page width. 
However, we found that using this criterion alone tends to produce layouts 
such as
>
> for i = 1 to 100 do for j = 1 to 100 do for k = 1 to 100 do a[i,j,k] := 0 
>
> which fits on a page, but cannot be described as pretty. We therefore impose 
an additional constraint limiting the number of characters on each line 
(excluding indentation) to a smaller number. The idea is to avoid placing too 
much information on a line -- even a line that begins at the left margin. 
Under this constraint the example above might instead be laid out as
>
> for i = 1 to 100 do
>   for j = 1 to 100 do
>     for k = 1 to 100 do a[i,j,k] := 0
>
> In general a pretty layout will consist of a ribbon of text snaking across 
the page. To see that this is reasonable, ask yourself: `is the prettiest 
layout on an infinitely wide page really to place everything on one line?'

So pretty printing is guided by two lengths: The line length and the (smaller) 
ribbon length.

The ribbons per line ratio that you can specify is simply a way of specifying 
the ribbon length relative to the line length. So, for example, if the line 
length is 80 and the ratio is 1.5, the ribbon length would be 80/1.5 which is 
rounded to 53.

> ...

Best regards
Thorkil


More information about the Haskell-Cafe mailing list