HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleghc-9.10.3GHC2021

GHC.Utils.Ppr

John Hughes's and Simon Peyton Jones's Pretty Printer Combinators

Based on The Design of a Pretty-printing Library in Advanced Functional Programming, Johan Jeuring and Erik Meijer (eds), LNCS 925 http://www.cse.chalmers.se/~rjmh/Papers/pretty.ps

  • 4 types
  • 59 values
  • Packageghc-9.10.3
  • Exports63
  • LanguageGHC2021
  • LicenceBSD-3-Clause
  • SourcePpr.hs

The document type

2 declarations
datadata Doc
#

The abstract type of documents. A Doc represents a *set* of layouts. A Doc with no occurrences of Union or NoDoc represents just one layout.

Instances1Show
  • Show DocDefined in ghc-9.10.3 · GHC.Utils.Ppr

Constructing documents

0 declarations

Converting values into documents

valuechar :: Char -> Doc
#

A document of height and width 1, containing a literal character.

valuetext :: String -> Doc
#

A document of height 1 containing a literal string. text satisfies the following laws:

The side condition on the last law is necessary because text "" has height 1, while empty has no height.

valuezeroWidthText :: String -> Doc
#

Some text, but without any width. Use for non-printing text such as a HTML or Latex tags

valueemptyText :: Doc
#

Empty text (one line high but no width). (emptyText = text "")

Simple derived documents

Wrapping documents in delimiters

Combining documents

value(<+>) :: Doc -> Doc -> Doc
#

Beside, separated by space, unless one of the arguments is empty. <+> is associative, with identity empty.

value($$) :: Doc -> Doc -> Doc
#

Above, except that if the last line of the first argument stops at least one position before the first line of the second begins, these two lines are overlapped. For example:

   text "hi" $$ nest 5 (text "there")

lays out as

   hi   there

rather than

   hi
        there

$$ is associative, with identity empty, and also satisfies

  • (x $$ y) <> z = x $$ (y <> z), if y non-empty.

value($+$) :: Doc -> Doc -> Doc
#

Above, with no overlapping. $+$ is associative, with identity empty.

valuepunctuate :: Doc -> [Doc] -> [Doc]
#
punctuate p [d1, ... dn] = [d1 <> p, d2 <> p, ... dn-1 <> p, dn]

Predicates on documents

2 declarations
valuedocHead :: Doc -> (Maybe Char, Doc)
#

Get the first character of a document. We also return a new document, equivalent to the original one but faster to render. Use it to avoid work duplication.

Rendering documents

0 declarations

Rendering with a particular style

valuestyle :: Style
#

The default style (mode=PageMode False, lineLength=100, ribbonsPerLine=1.5).

General rendering

valuefullRender
  1. :: Mode

    Rendering mode

  2. -> Int

    Line length

  3. -> Float

    Ribbons per line

  4. -> (TextDetails -> a -> a)

    What to do with text

  5. -> a

    What to do at the end

  6. -> Doc

    The document

  7. -> a

    Result

#

The general rendering interface.

GHC-specific rendering