Module Text.Pretty

This library provides pretty printing combinators. The interface is that of Daan Leijen's library linear-time, bounded implementation by Olaf Chitil. Note that the implementation of fill and fillBreak is not linear-time bounded Support of ANSI escape codes for formatting and colorisation of documents in text terminals (see https://en.wikipedia.org/wiki/ANSIescapecode)

This library corresponds to the library provided by the PAKCS and KiCS2 compilers. But it was partially rewritten and reorganized to make use of type classes and provide pretty printing combinators in a package.

Author: Sebastian Fischer, Bjoern Peemoeller, Jan Tikovsky

Version: October 2017

Summary of exported operations:

pPrint :: Doc -> String  Deterministic 
Standard printing with a column length of 80.
empty :: Doc  Deterministic 
The empty document
isEmpty :: Doc -> Bool  Deterministic 
Is the document empty?
text :: String -> Doc  Deterministic 
The document (text s) contains the literal string s.
linesep :: String -> Doc  Deterministic 
The document (linesep s) advances to the next line and indents to the current nesting level.
hardline :: Doc  Deterministic 
The document hardline advances to the next line and indents to the current nesting level.
line :: Doc  Deterministic 
The document line advances to the next line and indents to the current nesting level.
linebreak :: Doc  Deterministic 
The document linebreak advances to the next line and indents to the current nesting level.
softline :: Doc  Deterministic 
The document softline behaves like space if the resulting output fits the page, otherwise it behaves like line.
softbreak :: Doc  Deterministic 
The document softbreak behaves like (text "") if the resulting output fits the page, otherwise it behaves like line.
group :: Doc -> Doc  Deterministic 
The combinator group is used to specify alternative layouts.
nest :: Int -> Doc -> Doc  Deterministic 
The document (nest i d) renders document d with the current indentation level increased by i (See also hang, align and indent).
hang :: Int -> Doc -> Doc  Deterministic 
The combinator hang implements hanging indentation.
align :: Doc -> Doc  Deterministic 
The document (align d) renders document `d with the nesting level set to the current column.
indent :: Int -> Doc -> Doc  Deterministic 
The document (indent i d) indents document d with i spaces.
combine :: Doc -> Doc -> Doc -> Doc  Deterministic 
The document (combine c d1 d2) combines document d1 and d2 with document c in between using (<>) with identity empty.
(<>) :: Doc -> Doc -> Doc  Deterministic 
The document (x <> y) concatenates document x and document y.
(<+>) :: Doc -> Doc -> Doc  Deterministic 
The document (x <+> y) concatenates document x and y with a space in between with identity empty.
($$) :: Doc -> Doc -> Doc  Deterministic 
The document (x $$ y) concatenates document x and y with a line in between with identity empty.
(<$+$>) :: Doc -> Doc -> Doc  Deterministic 
The document (x <$+$> y) concatenates document x and y with a blank line in between with identity empty.
(</>) :: Doc -> Doc -> Doc  Deterministic 
The document (x </> y) concatenates document x and y with a softline in between with identity empty.
(<$$>) :: Doc -> Doc -> Doc  Deterministic 
The document (x <$$> y) concatenates document x and y with a linebreak in between with identity empty.
(<//>) :: Doc -> Doc -> Doc  Deterministic 
The document (x <//> y) concatenates document x and y with a softbreak in between with identity empty.
(<$!$>) :: Doc -> Doc -> Doc  Deterministic 
The document (x <$!$> y) concatenates document x and y with a hardline in between with identity empty.
compose :: (Doc -> Doc -> Doc) -> [Doc] -> Doc  Deterministic 
The document (compose f xs) concatenates all documents xs with function f.
hsep :: [Doc] -> Doc  Deterministic 
The document (hsep xs) concatenates all documents xs horizontally with (<+>).
vsep :: [Doc] -> Doc  Deterministic 
The document (vsep xs) concatenates all documents xs vertically with ($$).
vsepBlank :: [Doc] -> Doc  Deterministic 
The document vsep xs concatenates all documents xs vertically with (<$+$>).
fillSep :: [Doc] -> Doc  Deterministic 
The document (fillSep xs) concatenates documents xs horizontally with (</>) as long as its fits the page, than inserts a line and continues doing that for all documents in xs.
sep :: [Doc] -> Doc  Deterministic 
The document (sep xs) concatenates all documents xs either horizontally with (<+>), if it fits the page, or vertically with ($$).
hcat :: [Doc] -> Doc  Deterministic 
The document (hcat xs) concatenates all documents xs horizontally with (<>).
vcat :: [Doc] -> Doc  Deterministic 
The document (vcat xs) concatenates all documents xs vertically with (<$$>).
fillCat :: [Doc] -> Doc  Deterministic 
The document (fillCat xs) concatenates documents xs horizontally with (<//>) as long as its fits the page, than inserts a linebreak and continues doing that for all documents in xs.
cat :: [Doc] -> Doc  Deterministic 
The document (cat xs) concatenates all documents xs either horizontally with (<>), if it fits the page, or vertically with (<$$>).
punctuate :: Doc -> [Doc] -> [Doc]  Deterministic 
(punctuate p xs) concatenates all documents xs with document p except for the last document.
encloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc  Deterministic 
The document (encloseSep l r s xs) concatenates the documents xs seperated by s and encloses the resulting document by l and r.
encloseSepSpaced :: Doc -> Doc -> Doc -> [Doc] -> Doc  Deterministic 
The document (encloseSepSpaced l r s xs) concatenates the documents xs seperated by s and encloses the resulting document by l and r.
hEncloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc  Deterministic 
The document (hEncloseSep l r s xs) concatenates the documents xs seperated by s and encloses the resulting document by l and r.
fillEncloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc  Deterministic 
The document (fillEncloseSep l r s xs) concatenates the documents xs seperated by s and encloses the resulting document by l and r.
fillEncloseSepSpaced :: Doc -> Doc -> Doc -> [Doc] -> Doc  Deterministic 
The document (fillEncloseSepSpaced l r s xs) concatenates the documents xs seperated by s and encloses the resulting document by l and r.
list :: [Doc] -> Doc  Deterministic 
The document (list xs) comma seperates the documents xs and encloses them in square brackets.
listSpaced :: [Doc] -> Doc  Deterministic 
Spaced version of list
set :: [Doc] -> Doc  Deterministic 
The document (set xs) comma seperates the documents xs and encloses them in braces.
setSpaced :: [Doc] -> Doc  Deterministic 
Spaced version of set
tupled :: [Doc] -> Doc  Deterministic 
The document (tupled xs) comma seperates the documents xs and encloses them in parenthesis.
tupledSpaced :: [Doc] -> Doc  Deterministic 
Spaced version of tupled
semiBraces :: [Doc] -> Doc  Deterministic 
The document (semiBraces xs) seperates the documents xs with semi colons and encloses them in braces.
semiBracesSpaced :: [Doc] -> Doc  Deterministic 
Spaced version of semiBraces
enclose :: Doc -> Doc -> Doc -> Doc  Deterministic 
The document (enclose l r x) encloses document x between documents l and r using (<>).
squotes :: Doc -> Doc  Deterministic 
Document (squotes x) encloses document x with single quotes "'".
dquotes :: Doc -> Doc  Deterministic 
Document (dquotes x) encloses document x with double quotes.
bquotes :: Doc -> Doc  Deterministic 
Document (bquotes x) encloses document x with back quotes "\`".
parens :: Doc -> Doc  Deterministic 
Document (parens x) encloses document x in parenthesis, "(" and ")".
parensIf :: Bool -> Doc -> Doc  Deterministic 
Document (parensIf x) encloses document x in parenthesis,"(" and ")", iff the condition is true.
angles :: Doc -> Doc  Deterministic 
Document (angles x) encloses document x in angles, "<" and ">".
braces :: Doc -> Doc  Deterministic 
Document (braces x) encloses document x in braces, "{" and "}".
brackets :: Doc -> Doc  Deterministic 
Document (brackets x) encloses document x in square brackets, "[" and "]".
bool :: Bool -> Doc  Deterministic 
The document (bool b) shows the boolean b using text.
char :: Char -> Doc  Deterministic 
The document (char c) contains the literal character c.
string :: String -> Doc  Deterministic 
The document (string s) concatenates all characters in s using line for newline characters and char for all other characters.
int :: Int -> Doc  Deterministic 
The document (int i) shows the literal integer i using text.
float :: Float -> Doc  Deterministic 
The document (float f) shows the literal float f using text.
lparen :: Doc  Deterministic 
The document lparen contains a left parenthesis, "(".
rparen :: Doc  Deterministic 
The document rparen contains a right parenthesis, ")".
langle :: Doc  Deterministic 
The document langle contains a left angle, "<".
rangle :: Doc  Deterministic 
The document rangle contains a right angle, ">".
lbrace :: Doc  Deterministic 
The document lbrace contains a left brace, "{".
rbrace :: Doc  Deterministic 
The document rbrace contains a right brace, "}".
lbracket :: Doc  Deterministic 
The document lbracket contains a left square bracket, "[".
rbracket :: Doc  Deterministic 
The document rbracket contains a right square bracket, "]".
squote :: Doc  Deterministic 
The document squote contains a single quote, "'".
dquote :: Doc  Deterministic 
The document dquote contains a double quote.
semi :: Doc  Deterministic 
The document semi contains a semi colon, ";".
colon :: Doc  Deterministic 
The document colon contains a colon, ":".
comma :: Doc  Deterministic 
The document comma contains a comma, ",".
space :: Doc  Deterministic 
The document space contains a single space, " ".
dot :: Doc  Deterministic 
The document dot contains a single dot, ".".
backslash :: Doc  Deterministic 
The document backslash contains a back slash, "\\".
equals :: Doc  Deterministic 
The document equals contains an equal sign, "=".
larrow :: Doc  Deterministic 
The document larrow contains a left arrow sign, "<-".
rarrow :: Doc  Deterministic 
The document rarrow contains a right arrow sign, "->".
doubleArrow :: Doc  Deterministic 
The document doubleArrow contains an double arrow sign, "=>".
doubleColon :: Doc  Deterministic 
The document doubleColon contains a double colon sign, "::".
bar :: Doc  Deterministic 
The document bar contains a vertical bar sign, "|".
at :: Doc  Deterministic 
The document at contains an at sign, "@".
tilde :: Doc  Deterministic 
The document tilde contains a tilde sign, "~".
fill :: Int -> Doc -> Doc  Deterministic 
The document (fill i d) renders document d.
fillBreak :: Int -> Doc -> Doc  Deterministic 
The document (fillBreak i d) first renders document d.
bold :: Doc -> Doc  Deterministic 
The document (bold d) displays document d with bold text
faint :: Doc -> Doc  Deterministic 
The document (faint d) displays document d with faint text
blinkSlow :: Doc -> Doc  Deterministic 
The document (blinkSlow d) displays document d with slowly blinking text (rarely supported)
blinkRapid :: Doc -> Doc  Deterministic 
The document (blinkRapid d) displays document d with rapidly blinking text (rarely supported)
italic :: Doc -> Doc  Deterministic 
The document (italic d) displays document d with italicized text (rarely supported)
underline :: Doc -> Doc  Deterministic 
The document (underline d) displays document d with underlined text
crossout :: Doc -> Doc  Deterministic 
The document (crossout d) displays document d with crossed out text
inverse :: Doc -> Doc  Deterministic 
The document (inverse d) displays document d with inversed coloring, i.e.
black :: Doc -> Doc  Deterministic 
The document (black d) displays document d with black text color
red :: Doc -> Doc  Deterministic 
The document (red d) displays document d with red text color
green :: Doc -> Doc  Deterministic 
The document (green d) displays document d with green text color
yellow :: Doc -> Doc  Deterministic 
The document (yellow d) displays document d with yellow text color
blue :: Doc -> Doc  Deterministic 
The document (blue d) displays document d with blue text color
magenta :: Doc -> Doc  Deterministic 
The document (magenta d) displays document d with magenta text color
cyan :: Doc -> Doc  Deterministic 
The document (cyan d) displays document d with cyan text color
white :: Doc -> Doc  Deterministic 
The document (white d) displays document d with white text color
bgBlack :: Doc -> Doc  Deterministic 
The document (bgBlack d) displays document d with black background color
bgRed :: Doc -> Doc  Deterministic 
The document (bgRed d) displays document d with red background color
bgGreen :: Doc -> Doc  Deterministic 
The document (bgGreen d) displays document d with green background color
bgYellow :: Doc -> Doc  Deterministic 
The document (bgYellow d) displays document d with yellow background color
bgBlue :: Doc -> Doc  Deterministic 
The document (bgBlue d) displays document d with blue background color
bgMagenta :: Doc -> Doc  Deterministic 
The document (bgMagenta d) displays document d with magenta background color
bgCyan :: Doc -> Doc  Deterministic 
The document (bgCyan d) displays document d with cyan background color
bgWhite :: Doc -> Doc  Deterministic 
The document (bgWhite d) displays document d with white background color

Exported operations:

pPrint :: Doc -> String  Deterministic 

Standard printing with a column length of 80.

empty :: Doc  Deterministic 

The empty document

Returns:
an empty document
Further infos:
  • solution complete, i.e., able to compute all solutions

isEmpty :: Doc -> Bool  Deterministic 

Is the document empty?

text :: String -> Doc  Deterministic 

The document (text s) contains the literal string s. The string shouldn't contain any newline (\n) characters. If the string contains newline characters, the function string should be used.

Example call:
(text s)
Parameters:
  • s : a string without newline ([\n](#\n)) characters
Returns:
a document which contains the literal string
Further infos:
  • solution complete, i.e., able to compute all solutions

linesep :: String -> Doc  Deterministic 

The document (linesep s) advances to the next line and indents to the current nesting level. Document (linesep s) behaves like (text s) if the line break is undone by group.

Example call:
(linesep s)
Parameters:
  • s : a string
Returns:
a document which advances to the next line or behaves like (text s)

hardline :: Doc  Deterministic 

The document hardline advances to the next line and indents to the current nesting level. hardline cannot be undone by group.

Returns:
a document which advances to the next line
Further infos:
  • solution complete, i.e., able to compute all solutions

line :: Doc  Deterministic 

The document line advances to the next line and indents to the current nesting level. Document line behaves like (text " ") if the line break is undone by group.

Returns:
a document which advances to the next line or behaves like (text " ")

linebreak :: Doc  Deterministic 

The document linebreak advances to the next line and indents to the current nesting level. Document linebreak behaves like (text "") if the line break is undone by group.

Returns:
a document which advances to the next line or behaves like (text "")

softline :: Doc  Deterministic 

The document softline behaves like space if the resulting output fits the page, otherwise it behaves like line. softline = group line

Returns:
a document which behaves like space or line

softbreak :: Doc  Deterministic 

The document softbreak behaves like (text "") if the resulting output fits the page, otherwise it behaves like line. softbreak = group linebreak

Returns:
a document which behaves like (text "") or line

group :: Doc -> Doc  Deterministic 

The combinator group is used to specify alternative layouts. The document (group x) undoes all line breaks in document x. The resulting line is added to the current line if that fits the page. Otherwise, the document x is rendered without any changes.

Example call:
(group d)
Parameters:
  • d : a document
Returns:
document d without line breaks if that fits the page.

nest :: Int -> Doc -> Doc  Deterministic 

The document (nest i d) renders document d with the current indentation level increased by i (See also hang, align and indent).

nest 2 (text "hello" $$ text "world") $$ text "!"

outputs as:

hello
  world
!
Example call:
(nest i d)
Parameters:
  • i : an integer which increases the indentation level
  • d : a document
Returns:
document d with an indentation level increased by i

hang :: Int -> Doc -> Doc  Deterministic 

The combinator hang implements hanging indentation. The document (hang i d) renders document d with a nesting level set to the current column plus i. The following example uses hanging indentation for some text:

test = hang 4
         (fillSep
            (map text
                 (words "the hang combinator indents these words !")))

Which lays out on a page with a width of 20 characters as:

the hang combinator
    indents these
    words !

The hang combinator is implemented as:

hang i x  = align (nest i x)
Example call:
(hang i d)
Parameters:
  • i : an integer which increases the indentation level
  • d : a document
Returns:
document d with an indentation level set to the current column plus i

align :: Doc -> Doc  Deterministic 

The document (align d) renders document d with the nesting level set to the current column. It is used for example to implement hang`.

As an example, we will put a document right above another one, regardless of the current nesting level:

x $$ y  = align (x $$ y)
test    = text "hi" <+> (text "nice" $$ text "world")

which will be layed out as:

hi nice
   world
Example call:
(align d)
Parameters:
  • d : a document
Returns:
document d with the nesting level set to the current column

indent :: Int -> Doc -> Doc  Deterministic 

The document (indent i d) indents document d with i spaces.

test  = indent 4 (fillSep (map text
        (words "the indent combinator indents these words !")))

Which lays out with a page width of 20 as:

the indent
combinator
indents these
words !
Example call:
(indent i d)
Parameters:
  • i : an integer which increases the indentation level
  • d : a document
Returns:
document d with an indentation level set to the current column plus i

combine :: Doc -> Doc -> Doc -> Doc  Deterministic 

The document (combine c d1 d2) combines document d1 and d2 with document c in between using (<>) with identity empty. Thus, the following equations hold.

combine c d1    empty == d1
combine c empty d2    == d2
combine c d1    d2    == d1 <> c <> d2 if neither d1 nor d2 are empty
Example call:
(combine c d1 d2)
Parameters:
  • c : the middle document
  • d1 : the left document
  • d2 : the right document
Returns:
concatenation of d1 and d2 with c in between unless one of the documents is empty

(<>) :: Doc -> Doc -> Doc  Deterministic 

The document (x <> y) concatenates document x and document y. It is an associative operation having empty as a left and right unit.

Example call:
(x <> y)
Parameters:
  • x : the first document
  • y : the second document
Returns:
concatenation of x and y without seperator with identity empty
Further infos:
  • defined as left-associative infix operator with precedence 6

(<+>) :: Doc -> Doc -> Doc  Deterministic 

The document (x <+> y) concatenates document x and y with a space in between with identity empty.

Example call:
(x <+> y)
Parameters:
  • x : the first document
  • y : the second document
Returns:
concatenation of x and y with a space in between
Further infos:
  • defined as left-associative infix operator with precedence 6

($$) :: Doc -> Doc -> Doc  Deterministic 

The document (x $$ y) concatenates document x and y with a line in between with identity empty.

Example call:
(x $$ y)
Parameters:
  • x : the first document
  • y : the second document
Returns:
concatenation of x and y with a line in between
Further infos:
  • defined as left-associative infix operator with precedence 5

(<$+$>) :: Doc -> Doc -> Doc  Deterministic 

The document (x <$+$> y) concatenates document x and y with a blank line in between with identity empty.

Example call:
(x <$+$> y)
Parameters:
  • x : the first document
  • y : the second document
Returns:
concatenation of x and y with a blank line in between
Further infos:
  • defined as left-associative infix operator with precedence 5

(</>) :: Doc -> Doc -> Doc  Deterministic 

The document (x </> y) concatenates document x and y with a softline in between with identity empty. This effectively puts x and y either next to each other (with a space in between) or underneath each other.

Example call:
(x </> y)
Parameters:
  • x : the first document
  • y : the second document
Returns:
concatenation of x and y with a softline in between
Further infos:
  • defined as left-associative infix operator with precedence 5

(<$$>) :: Doc -> Doc -> Doc  Deterministic 

The document (x <$$> y) concatenates document x and y with a linebreak in between with identity empty.

Example call:
(x <$$> y)
Parameters:
  • x : the first document
  • y : the second document
Returns:
concatenation of x and y with a linebreak in between
Further infos:
  • defined as left-associative infix operator with precedence 5

(<//>) :: Doc -> Doc -> Doc  Deterministic 

The document (x <//> y) concatenates document x and y with a softbreak in between with identity empty. This effectively puts x and y either right next to each other or underneath each other.

Example call:
(x <//> y)
Parameters:
  • x : the first document
  • y : the second document
Returns:
concatenation of x and y with a softbreak in between
Further infos:
  • defined as left-associative infix operator with precedence 5

(<$!$>) :: Doc -> Doc -> Doc  Deterministic 

The document (x <$!$> y) concatenates document x and y with a hardline in between with identity empty. This effectively puts x and y underneath each other.

Example call:
(x <$!$> y)
Parameters:
  • x : the first document
  • y : the second document
Returns:
concatenation of x and y with a hardline in between
Further infos:
  • defined as left-associative infix operator with precedence 5

compose :: (Doc -> Doc -> Doc) -> [Doc] -> Doc  Deterministic 

The document (compose f xs) concatenates all documents xs with function f. Function f should be like (<+>), ($$) and so on.

Example call:
(compose f xs)
Parameters:
  • f : a combiner function
  • xs : a list of documents
Returns:
concatenation of documents

hsep :: [Doc] -> Doc  Deterministic 

The document (hsep xs) concatenates all documents xs horizontally with (<+>).

Example call:
(hsep xs)
Parameters:
  • xs : a list of documents
Returns:
horizontal concatenation of documents

vsep :: [Doc] -> Doc  Deterministic 

The document (vsep xs) concatenates all documents xs vertically with ($$). If a group undoes the line breaks inserted by vsep, all documents are separated with a space.

someText = map text (words ("text to lay out"))
test     = text "some" <+> vsep someText

This is layed out as:

some text
to
lay
out

The align combinator can be used to align the documents under their first element:

test     = text "some" <+> align (vsep someText)

This is printed as:

some text
     to
     lay
     out
Example call:
(vsep xs)
Parameters:
  • xs : a list of documents
Returns:
vertical concatenation of documents

vsepBlank :: [Doc] -> Doc  Deterministic 

The document vsep xs concatenates all documents xs vertically with (<$+$>). If a group undoes the line breaks inserted by vsepBlank, all documents are separated with a space.

Example call:
(vsepBlank xs)
Parameters:
  • xs : a list of documents
Returns:
vertical concatenation of documents

fillSep :: [Doc] -> Doc  Deterministic 

The document (fillSep xs) concatenates documents xs horizontally with (</>) as long as its fits the page, than inserts a line and continues doing that for all documents in xs. fillSep xs = foldr (</>) empty xs

Example call:
(fillSep xs)
Parameters:
  • xs : a list of documents
Returns:
horizontal concatenation of documents

sep :: [Doc] -> Doc  Deterministic 

The document (sep xs) concatenates all documents xs either horizontally with (<+>), if it fits the page, or vertically with ($$). sep xs = group (vsep xs)

Example call:
(sep xs)
Parameters:
  • xs : a list of documents
Returns:
horizontal concatenation of documents, if it fits the page, or vertical concatenation else

hcat :: [Doc] -> Doc  Deterministic 

The document (hcat xs) concatenates all documents xs horizontally with (<>).

Example call:
(hcat xs)
Parameters:
  • xs : a list of documents
Returns:
horizontal concatenation of documents

vcat :: [Doc] -> Doc  Deterministic 

The document (vcat xs) concatenates all documents xs vertically with (<$$>). If a group undoes the line breaks inserted by vcat, all documents are directly concatenated.

Example call:
(vcat xs)
Parameters:
  • xs : a list of documents
Returns:
vertical concatenation of documents

fillCat :: [Doc] -> Doc  Deterministic 

The document (fillCat xs) concatenates documents xs horizontally with (<//>) as long as its fits the page, than inserts a linebreak and continues doing that for all documents in xs. fillCat xs = foldr (<//>) empty xs

Example call:
(fillCat xs)
Parameters:
  • xs : a list of documents
Returns:
horizontal concatenation of documents

cat :: [Doc] -> Doc  Deterministic 

The document (cat xs) concatenates all documents xs either horizontally with (<>), if it fits the page, or vertically with (<$$>). cat xs = group (vcat xs)

Example call:
(cat xs)
Parameters:
  • xs : a list of documents
Returns:
horizontal concatenation of documents

punctuate :: Doc -> [Doc] -> [Doc]  Deterministic 

(punctuate p xs) concatenates all documents xs with document p except for the last document.

someText = map text ["words","in","a","tuple"]
test     = parens (align (cat (punctuate comma someText)))

This is layed out on a page width of 20 as:

(words,in,a,tuple)

But when the page width is 15, it is layed out as:

(words,
 in,
 a,
 tuple)

(If you want put the commas in front of their elements instead of at the end, you should use tupled or, in general, encloseSep.)

Example call:
(punctuate p xs)
Parameters:
  • p : a document as seperator
  • xs : a list of documents
Returns:
concatenation of documents with p in between

encloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc  Deterministic 

The document (encloseSep l r s xs) concatenates the documents xs seperated by s and encloses the resulting document by l and r. The documents are rendered horizontally if that fits the page. Otherwise they are aligned vertically. All seperators are put in front of the elements.

For example, the combinator list can be defined with encloseSep:

list xs  = encloseSep lbracket rbracket comma xs
test     = text "list" <+> (list (map int [10,200,3000]))

Which is layed out with a page width of 20 as:

list [10,200,3000]

But when the page width is 15, it is layed out as:

list [10
     ,200
     ,3000]
Example call:
(encloseSep l r s xs)
Parameters:
  • l : left document
  • r : right document
  • s : a document as seperator
  • xs : a list of documents
Returns:
concatenation of l, xs (with s in between) and r

encloseSepSpaced :: Doc -> Doc -> Doc -> [Doc] -> Doc  Deterministic 

The document (encloseSepSpaced l r s xs) concatenates the documents xs seperated by s and encloses the resulting document by l and r. In addition, after each occurrence of s, after l, and before r, a space is inserted. The documents are rendered horizontally if that fits the page. Otherwise they are aligned vertically. All seperators are put in front of the elements.

Example call:
(encloseSepSpaced l r s xs)
Parameters:
  • l : left document
  • r : right document
  • s : a document as seperator
  • xs : a list of documents
Returns:
concatenation of l, xs (with s in between) and r

hEncloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc  Deterministic 

The document (hEncloseSep l r s xs) concatenates the documents xs seperated by s and encloses the resulting document by l and r.

The documents are rendered horizontally.

Example call:
(hEncloseSep l r s xs)
Parameters:
  • l : left document
  • r : right document
  • s : a document as seperator
  • xs : a list of documents
Returns:
concatenation of l, xs (with s in between) and r

fillEncloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc  Deterministic 

The document (fillEncloseSep l r s xs) concatenates the documents xs seperated by s and encloses the resulting document by l and r.

The documents are rendered horizontally if that fits the page. Otherwise they are aligned vertically. All seperators are put in front of the elements.

Example call:
(fillEncloseSep l r s xs)
Parameters:
  • l : left document
  • r : right document
  • s : a document as seperator
  • xs : a list of documents
Returns:
concatenation of l, xs (with s in between) and r

fillEncloseSepSpaced :: Doc -> Doc -> Doc -> [Doc] -> Doc  Deterministic 

The document (fillEncloseSepSpaced l r s xs) concatenates the documents xs seperated by s and encloses the resulting document by l and r. In addition, after each occurrence of s, after l, and before r, a space is inserted.

The documents are rendered horizontally if that fits the page. Otherwise, they are aligned vertically. All seperators are put in front of the elements.

Example call:
(fillEncloseSepSpaced l r s xs)
Parameters:
  • l : left document
  • r : right document
  • s : a document as seperator
  • xs : a list of documents
Returns:
concatenation of l, xs (with s in between) and r

list :: [Doc] -> Doc  Deterministic 

The document (list xs) comma seperates the documents xs and encloses them in square brackets. The documents are rendered horizontally if that fits the page. Otherwise they are aligned vertically. All comma seperators are put in front of the elements.

Example call:
(list xs)
Parameters:
  • xs : a list of documents
Returns:
comma seperated documents xs and enclosed in square brackets

listSpaced :: [Doc] -> Doc  Deterministic 

Spaced version of list

set :: [Doc] -> Doc  Deterministic 

The document (set xs) comma seperates the documents xs and encloses them in braces. The documents are rendered horizontally if that fits the page. Otherwise they are aligned vertically. All comma seperators are put in front of the elements.

Example call:
(set xs)
Parameters:
  • xs : a list of documents
Returns:
comma seperated documents xs and enclosed in braces

setSpaced :: [Doc] -> Doc  Deterministic 

Spaced version of set

tupled :: [Doc] -> Doc  Deterministic 

The document (tupled xs) comma seperates the documents xs and encloses them in parenthesis. The documents are rendered horizontally if that fits the page. Otherwise they are aligned vertically. All comma seperators are put in front of the elements.

Example call:
(tupled xs)
Parameters:
  • xs : a list of documents
Returns:
comma seperated documents xs and enclosed in parenthesis

tupledSpaced :: [Doc] -> Doc  Deterministic 

Spaced version of tupled

semiBraces :: [Doc] -> Doc  Deterministic 

The document (semiBraces xs) seperates the documents xs with semi colons and encloses them in braces. The documents are rendered horizontally if that fits the page. Otherwise they are aligned vertically. All semi colons are put in front of the elements.

Example call:
(semiBraces xs)
Parameters:
  • xs : a list of documents
Returns:
documents xs seperated with semi colons and enclosed in braces

semiBracesSpaced :: [Doc] -> Doc  Deterministic 

Spaced version of semiBraces

enclose :: Doc -> Doc -> Doc -> Doc  Deterministic 

The document (enclose l r x) encloses document x between documents l and r using (<>). enclose l r x = l <> x <> r

Example call:
(enclose l r x)
Parameters:
  • l : the left document
  • r : the right document
  • x : the middle document
Returns:
concatenation of l, x and r

squotes :: Doc -> Doc  Deterministic 

Document (squotes x) encloses document x with single quotes "'".

Example call:
(squotes x)
Parameters:
  • x : a document
Returns:
document x enclosed by single quotes

dquotes :: Doc -> Doc  Deterministic 

Document (dquotes x) encloses document x with double quotes.

Example call:
(dquotes x)
Parameters:
  • x : a document
Returns:
document x enclosed by double quotes

bquotes :: Doc -> Doc  Deterministic 

Document (bquotes x) encloses document x with back quotes "\`".

Example call:
(bquotes x)
Parameters:
  • x : a document
Returns:
document x enclosed by \` quotes

parens :: Doc -> Doc  Deterministic 

Document (parens x) encloses document x in parenthesis, "(" and ")".

Example call:
(parens x)
Parameters:
  • x : a document
Returns:
document x enclosed in parenthesis

parensIf :: Bool -> Doc -> Doc  Deterministic 

Document (parensIf x) encloses document x in parenthesis,"(" and ")", iff the condition is true.

Example call:
(parensIf x)
Parameters:
  • x : a document
Returns:
document x enclosed in parenthesis iff the condition is true

angles :: Doc -> Doc  Deterministic 

Document (angles x) encloses document x in angles, "<" and ">".

Example call:
(angles x)
Parameters:
  • x : a document
Returns:
document x enclosed in angles

braces :: Doc -> Doc  Deterministic 

Document (braces x) encloses document x in braces, "{" and "}".

Example call:
(braces x)
Parameters:
  • x : a document
Returns:
document x enclosed in braces

brackets :: Doc -> Doc  Deterministic 

Document (brackets x) encloses document x in square brackets, "[" and "]".

Example call:
(brackets x)
Parameters:
  • x : a document
Returns:
document x enclosed in square brackets

bool :: Bool -> Doc  Deterministic 

The document (bool b) shows the boolean b using text.

Example call:
(bool b)
Parameters:
  • b : a boolean
Returns:
a document which contains the boolean b

char :: Char -> Doc  Deterministic 

The document (char c) contains the literal character c. The character should not be a newline (\n), the function line should be used for line breaks.

Example call:
(char c)
Parameters:
  • c : a character (not \n)
Returns:
a document which contains the literal character c
Further infos:
  • solution complete, i.e., able to compute all solutions

string :: String -> Doc  Deterministic 

The document (string s) concatenates all characters in s using line for newline characters and char for all other characters. It is used instead of text whenever the text contains newline characters.

Example call:
(string s)
Parameters:
  • s : a string
Returns:
a document which contains the string s

int :: Int -> Doc  Deterministic 

The document (int i) shows the literal integer i using text.

Example call:
(int i)
Parameters:
  • i : an integer
Returns:
a document which contains the integer i

float :: Float -> Doc  Deterministic 

The document (float f) shows the literal float f using text.

Example call:
(float f)
Parameters:
  • f : a float
Returns:
a document which contains the float f

lparen :: Doc  Deterministic 

The document lparen contains a left parenthesis, "(".

Returns:
a document which contains a left parenthesis
Further infos:
  • solution complete, i.e., able to compute all solutions

rparen :: Doc  Deterministic 

The document rparen contains a right parenthesis, ")".

Returns:
a document which contains a right parenthesis
Further infos:
  • solution complete, i.e., able to compute all solutions

langle :: Doc  Deterministic 

The document langle contains a left angle, "<".

Returns:
a document which contains a left angle
Further infos:
  • solution complete, i.e., able to compute all solutions

rangle :: Doc  Deterministic 

The document rangle contains a right angle, ">".

Returns:
a document which contains a right angle
Further infos:
  • solution complete, i.e., able to compute all solutions

lbrace :: Doc  Deterministic 

The document lbrace contains a left brace, "{".

Returns:
a document which contains a left brace
Further infos:
  • solution complete, i.e., able to compute all solutions

rbrace :: Doc  Deterministic 

The document rbrace contains a right brace, "}".

Returns:
a document which contains a right brace
Further infos:
  • solution complete, i.e., able to compute all solutions

lbracket :: Doc  Deterministic 

The document lbracket contains a left square bracket, "[".

Returns:
a document which contains a left square bracket
Further infos:
  • solution complete, i.e., able to compute all solutions

rbracket :: Doc  Deterministic 

The document rbracket contains a right square bracket, "]".

Returns:
a document which contains a right square bracket
Further infos:
  • solution complete, i.e., able to compute all solutions

squote :: Doc  Deterministic 

The document squote contains a single quote, "'".

Returns:
a document which contains a single quote
Further infos:
  • solution complete, i.e., able to compute all solutions

dquote :: Doc  Deterministic 

The document dquote contains a double quote.

Returns:
a document which contains a double quote
Further infos:
  • solution complete, i.e., able to compute all solutions

semi :: Doc  Deterministic 

The document semi contains a semi colon, ";".

Returns:
a document which contains a semi colon
Further infos:
  • solution complete, i.e., able to compute all solutions

colon :: Doc  Deterministic 

The document colon contains a colon, ":".

Returns:
a document which contains a colon
Further infos:
  • solution complete, i.e., able to compute all solutions

comma :: Doc  Deterministic 

The document comma contains a comma, ",".

Returns:
a document which contains a comma
Further infos:
  • solution complete, i.e., able to compute all solutions

space :: Doc  Deterministic 

The document space contains a single space, " ".

x <+> y   = x <> space <> y
Returns:
a document which contains a single space
Further infos:
  • solution complete, i.e., able to compute all solutions

dot :: Doc  Deterministic 

The document dot contains a single dot, ".".

Returns:
a document which contains a single dot
Further infos:
  • solution complete, i.e., able to compute all solutions

backslash :: Doc  Deterministic 

The document backslash contains a back slash, "\\".

Returns:
a document which contains a back slash
Further infos:
  • solution complete, i.e., able to compute all solutions

equals :: Doc  Deterministic 

The document equals contains an equal sign, "=".

Returns:
a document which contains an equal
Further infos:
  • solution complete, i.e., able to compute all solutions

larrow :: Doc  Deterministic 

The document larrow contains a left arrow sign, "<-".

Returns:
a document which contains a left arrow sign
Further infos:
  • solution complete, i.e., able to compute all solutions

rarrow :: Doc  Deterministic 

The document rarrow contains a right arrow sign, "->".

Returns:
a document which contains a right arrow sign
Further infos:
  • solution complete, i.e., able to compute all solutions

doubleArrow :: Doc  Deterministic 

The document doubleArrow contains an double arrow sign, "=>".

Returns:
a document which contains an double arrow sign
Further infos:
  • solution complete, i.e., able to compute all solutions

doubleColon :: Doc  Deterministic 

The document doubleColon contains a double colon sign, "::".

Returns:
a document which contains a double colon sign
Further infos:
  • solution complete, i.e., able to compute all solutions

bar :: Doc  Deterministic 

The document bar contains a vertical bar sign, "|".

Returns:
a document which contains a vertical bar sign
Further infos:
  • solution complete, i.e., able to compute all solutions

at :: Doc  Deterministic 

The document at contains an at sign, "@".

Returns:
a document which contains an at sign
Further infos:
  • solution complete, i.e., able to compute all solutions

tilde :: Doc  Deterministic 

The document tilde contains a tilde sign, "~".

Returns:
a document which contains a tilde sign
Further infos:
  • solution complete, i.e., able to compute all solutions

fill :: Int -> Doc -> Doc  Deterministic 

The document (fill i d) renders document d. It than appends spaces until the width is equal to i. If the width of d is already larger, nothing is appended. This combinator is quite useful in practice to output a list of bindings. The following example demonstrates this.

types  = [("empty","Doc")
         ,("nest","Int -> Doc -> Doc")
         ,("linebreak","Doc")]
ptype (name,tp)
       = fill 6 (text name) <+> text "::" <+> text tp
test   = text "let" <+> align (vcat (map ptype types))

Which is layed out as:

let empty  :: Doc
    nest   :: Int -> Doc -> Doc
    linebreak :: Doc

Note that fill is not guaranteed to be linear-time bounded since it has to compute the width of a document before pretty printing it

fillBreak :: Int -> Doc -> Doc  Deterministic 

The document (fillBreak i d) first renders document d. It than appends spaces until the width is equal to i. If the width of d is already larger than i, the nesting level is increased by i and a line is appended. When we redefine ptype in the previous example to use fillBreak, we get a useful variation of the previous output:

ptype (name,tp)
     = fillBreak 6 (text name) <+> text "::" <+> text tp

The output will now be:

let empty  :: Doc
    nest   :: Int -> Doc -> Doc
    linebreak
           :: Doc

Note that fillBreak is not guaranteed to be linear-time bounded since it has to compute the width of a document before pretty printing it

bold :: Doc -> Doc  Deterministic 

The document (bold d) displays document d with bold text

Example call:
(bold d)
Parameters:
  • d : a document
Returns:
document d displayed with bold text

faint :: Doc -> Doc  Deterministic 

The document (faint d) displays document d with faint text

Example call:
(faint d)
Parameters:
  • d : a document
Returns:
document d displayed with faint text

blinkSlow :: Doc -> Doc  Deterministic 

The document (blinkSlow d) displays document d with slowly blinking text (rarely supported)

Example call:
(blinkSlow d)
Parameters:
  • d : a document
Returns:
document d displayed with slowly blinking text

blinkRapid :: Doc -> Doc  Deterministic 

The document (blinkRapid d) displays document d with rapidly blinking text (rarely supported)

Example call:
(blinkRapid d)
Parameters:
  • d : a document
Returns:
document d displayed with rapidly blinking text

italic :: Doc -> Doc  Deterministic 

The document (italic d) displays document d with italicized text (rarely supported)

Example call:
(italic d)
Parameters:
  • d : a document
Returns:
document d displayed with italicized text

underline :: Doc -> Doc  Deterministic 

The document (underline d) displays document d with underlined text

Example call:
(underline d)
Parameters:
  • d : a document
Returns:
document d displayed with underlined text

crossout :: Doc -> Doc  Deterministic 

The document (crossout d) displays document d with crossed out text

Example call:
(crossout d)
Parameters:
  • d : a document
Returns:
document d displayed with crossed out text

inverse :: Doc -> Doc  Deterministic 

The document (inverse d) displays document d with inversed coloring, i.e. use text color of d as background color and background color of d as text color

Example call:
(inverse d)
Parameters:
  • d : a document
Returns:
document d displayed with inversed coloring

black :: Doc -> Doc  Deterministic 

The document (black d) displays document d with black text color

Example call:
(black d)
Parameters:
  • d : a document
Returns:
document d displayed with black text color

red :: Doc -> Doc  Deterministic 

The document (red d) displays document d with red text color

Example call:
(red d)
Parameters:
  • d : a document
Returns:
document d displayed with red text color

green :: Doc -> Doc  Deterministic 

The document (green d) displays document d with green text color

Example call:
(green d)
Parameters:
  • d : a document
Returns:
document d displayed with green text color

yellow :: Doc -> Doc  Deterministic 

The document (yellow d) displays document d with yellow text color

Example call:
(yellow d)
Parameters:
  • d : a document
Returns:
document d displayed with yellow text color

blue :: Doc -> Doc  Deterministic 

The document (blue d) displays document d with blue text color

Example call:
(blue d)
Parameters:
  • d : a document
Returns:
document d displayed with blue text color

magenta :: Doc -> Doc  Deterministic 

The document (magenta d) displays document d with magenta text color

Example call:
(magenta d)
Parameters:
  • d : a document
Returns:
document d displayed with magenta text color

cyan :: Doc -> Doc  Deterministic 

The document (cyan d) displays document d with cyan text color

Example call:
(cyan d)
Parameters:
  • d : a document
Returns:
document d displayed with cyan text color

white :: Doc -> Doc  Deterministic 

The document (white d) displays document d with white text color

Example call:
(white d)
Parameters:
  • d : a document
Returns:
document d displayed with white text color

bgBlack :: Doc -> Doc  Deterministic 

The document (bgBlack d) displays document d with black background color

Example call:
(bgBlack d)
Parameters:
  • d : a document
Returns:
document d displayed with black background color

bgRed :: Doc -> Doc  Deterministic 

The document (bgRed d) displays document d with red background color

Example call:
(bgRed d)
Parameters:
  • d : a document
Returns:
document d displayed with red background color

bgGreen :: Doc -> Doc  Deterministic 

The document (bgGreen d) displays document d with green background color

Example call:
(bgGreen d)
Parameters:
  • d : a document
Returns:
document d displayed with green background color

bgYellow :: Doc -> Doc  Deterministic 

The document (bgYellow d) displays document d with yellow background color

Example call:
(bgYellow d)
Parameters:
  • d : a document
Returns:
document d displayed with yellow background color

bgBlue :: Doc -> Doc  Deterministic 

The document (bgBlue d) displays document d with blue background color

Example call:
(bgBlue d)
Parameters:
  • d : a document
Returns:
document d displayed with blue background color

bgMagenta :: Doc -> Doc  Deterministic 

The document (bgMagenta d) displays document d with magenta background color

Example call:
(bgMagenta d)
Parameters:
  • d : a document
Returns:
document d displayed with magenta background color

bgCyan :: Doc -> Doc  Deterministic 

The document (bgCyan d) displays document d with cyan background color

Example call:
(bgCyan d)
Parameters:
  • d : a document
Returns:
document d displayed with cyan background color

bgWhite :: Doc -> Doc  Deterministic 

The document (bgWhite d) displays document d with white background color

Example call:
(bgWhite d)
Parameters:
  • d : a document
Returns:
document d displayed with white background color