Data.Pretty
A Leijen-style pretty printer.
Build a layout-independent Doc from the combinators below, then render it to a string at a chosen page width.
Rendering is a pure function of the document and the width, so a document lays out identically on every backend and replays byte for byte. The strict renderer carries a Flat/Brk mode through a work list; its fits lookahead stops at the next forced break or as soon as the width is exceeded.
Import explicitly (import Data.Pretty); these names are not in Base’s unqualified scope. Widths count Unicode code points, matching str_len; they are not terminal display-cell measurements.
Types
Doc
type Doc
= DNil
| DText(String)
| DSoft(String)
| DHard
| DCat(Doc, Doc)
| DNest(Int, Doc)
| DGroup(Doc)
A layout document. Its representation is hidden so indentation and break invariants are established only by the public combinators.
Functions and Values
render
render : (Int, Data.Pretty.Doc) -> String
Render a document to a string at page width w.
import Data.Pretty (render, group, cat, text, line)
render(80, group(cat(text("hello"), cat(line(), text("world")))))
hello world
pretty
pretty : (Data.Pretty.Doc) -> String
Render at the conventional width of 80 columns.
text
text : (String) -> Data.Pretty.Doc
Literal text. The string must not contain a newline; use hardline for a forced break.
empty
empty : () -> Data.Pretty.Doc
The empty document.
line
line : () -> Data.Pretty.Doc
A break that is a space when flat and a newline when broken.
linebreak
linebreak : () -> Data.Pretty.Doc
A break that is nothing when flat and a newline when broken.
softline
softline : () -> Data.Pretty.Doc
A line inside its own group: a space if it fits, a newline otherwise.
softbreak
softbreak : () -> Data.Pretty.Doc
A linebreak inside its own group: nothing if it fits, a newline otherwise.
hardline
hardline : () -> Data.Pretty.Doc
A newline that no group can flatten away.
cat
cat : (Data.Pretty.Doc, Data.Pretty.Doc) -> Data.Pretty.Doc
One document then another.
nest
nest : (Int, Data.Pretty.Doc) -> Data.Pretty.Doc
Indent every break inside d by at least zero columns. Negative indentation is clamped to zero.
group
group : (Data.Pretty.Doc) -> Data.Pretty.Doc
Try to lay d out flat; fall back to its broken form if it does not fit.
concat_docs
concat_docs : (List(Data.Pretty.Doc)) -> Data.Pretty.Doc
Concatenate a list of documents end to end.
join_docs
join_docs : (Data.Pretty.Doc, List(Data.Pretty.Doc)) -> Data.Pretty.Doc
Put separator between each pair of documents. Unlike hsep and vsep, the caller selects the exact separator document, which is useful for small target-language emitters (commas plus soft breaks, operators, or hard lines).
hsep
hsep : (List(Data.Pretty.Doc)) -> Data.Pretty.Doc
Documents separated by a single space, all on one line.
vsep
vsep : (List(Data.Pretty.Doc)) -> Data.Pretty.Doc
Documents separated by line breaks (spaces or newlines under a group).
sep
sep : (List(Data.Pretty.Doc)) -> Data.Pretty.Doc
vsep wrapped in a group: one line if it fits, one per element otherwise.
enclose
enclose : (Data.Pretty.Doc, Data.Pretty.Doc, Data.Pretty.Doc) -> Data.Pretty.Doc
d between the opening l and closing r.
parens
parens : (Data.Pretty.Doc) -> Data.Pretty.Doc
d in parentheses.
brackets
brackets : (Data.Pretty.Doc) -> Data.Pretty.Doc
d in square brackets.
braces
braces : (Data.Pretty.Doc) -> Data.Pretty.Doc
d in curly braces.