HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Moduleghc-10.0.0.20260917GHC2024

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

Contents
  1. The document type
  2. Constructing documents
  3. Converting values into documents
  4. Simple derived documents
  5. Wrapping documents in delimiters
  6. Combining documents
  7. Predicates on documents
  8. Rendering documents
  9. Rendering with a particular style
  10. General rendering
  11. GHC-specific rendering
Synopsis

The document type

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-10.0.0.20260917 · GHC.Utils.Ppr

Constructing documents

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.

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

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.

Predicates on documents

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

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