HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleansi-wl-pprint-1.0.2Haskell2010

Text.PrettyPrint.ANSI.Leijen

Deprecated. Compatibility module for users of ansi-wl-pprint - use Prettyprinter instead

This module is an extended implementation of the functional pretty printer given by Philip Wadler (1997):

     "A prettier printer"
     Draft paper, April 1997, revised March 1998.
     https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf

In their bare essence, the combinators given by Wadler are not expressive enough to describe some commonly occurring layouts. This library adds new primitives to describe these layouts and works well in practice.

The library is based on a single way to concatenate documents, which is associative and has both a left and right unit. This simple design leads to an efficient and short implementation. The simplicity is reflected in the predictable behaviour of the combinators which make them easy to use in practice.

A thorough description of the primitive combinators and their implementation can be found in Philip Wadler's paper. The main differences with his original paper are:

  • The nil document is called empty.

  • The above combinator is called <$>. The operator </> is used for soft line breaks.

  • There are three new primitives: align, fill and fillBreak. These are very useful in practice.

  • There are many additional useful combinators, like fillSep and list.

  • There are two renderers: renderPretty for pretty printing, and renderCompact for quickly rendered, compact output more suitable for generating input to other programs.

  • The pretty printing algorithm used by renderPretty extends the algorithm given by Wadler to take into account a "ribbon width", i.e., a desired maximum number of non-indentation characters to output on any one line.

  • There are two displayers, displayS for strings and displayIO for file-based output.

  • There is a Pretty class.

  • The implementation uses optimised representations and strictness annotations.

  • The library has been extended to allow formatting text for output to ANSI style consoles. New combinators allow control of foreground and background color and the ability to make parts of the text bold or underlined.

  • 2 types
  • 1 class
  • 113 values

The algebra of pretty-printing

0 declarations

The combinators in this library satisfy many algebraic laws.

The concatenation operator <> is associative and has empty as a left and right unit:

x <> (y <> z)           = (x <> y) <> z
x <> empty              = x
empty <> x              = x

The text combinator is a homomorphism from string concatenation to document concatenation:

text (s ++ t)           = text s <> text t
text ""                 = empty

The char combinator behaves like one-element text:

char c                  = text [c]

The nest combinator is a homomorphism from addition to document composition. nest also distributes through document concatenation and is absorbed by text and align:

nest (i + j) x          = nest i (nest j x)
nest 0 x                = x
nest i (x <> y)         = nest i x <> nest i y
nest i empty            = empty
nest i (text s)         = text s
nest i (align x)        = align x

The group combinator is absorbed by empty. group is commutative with nest and align:

group empty             = empty
group (text s <> x)     = text s <> group x
group (nest i x)        = nest i (group x)
group (align x)         = align (group x)

The align combinator is absorbed by empty and text. align is idempotent:

align empty             = empty
align (text s)          = text s
align (align x)         = align x

From the laws of the primitive combinators, we can derive many other laws for the derived combinators. For example, the above operator <$> is defined as:

x <$> y                 = x <> line <> y

It follows that <$> is associative and that <$> and <> associate with each other:

x <$> (y <$> z)         = (x <$> y) <$> z
x <> (y <$> z)          = (x <> y) <$> z
x <$> (y <> z)          = (x <$> y) <> z

Similar laws also hold for the other line break operators </>, <$$>, and <//>.

Documents

1 declaration

Basic combinators

19 declarations
method(<>) :: a -> a -> a
#

An associative operation.

Examples
Example1 expression
[1,2,3] <> [4,5,6][1,2,3,4,5,6]
Example1 expression
Just [1, 2, 3] <> Just [4, 5, 6]Just [1,2,3,4,5,6]
Example1 expression
putStr "Hello, " <> putStrLn "World!"Hello, World!

Alignment combinators

7 declarations

The combinators in this section cannot be described by Wadler's original combinators. They align their output relative to the current output position — in contrast to nest which always aligns to the current nesting level. This deprives these combinators from being `optimal'. In practice however they prove to be very useful. The combinators in this section should be used with care, since they are more expensive than the other combinators. For example, align shouldn't be used to pretty print all top-level declarations of a language, but using hang for let expressions is fine.

Operators

5 declarations

List combinators

9 declarations

Filler combinators

2 declarations

Bracketing combinators

7 declarations

Named character combinators

17 declarations

ANSI formatting combinators

0 declarations

This terminal formatting functionality is, as far as possible, portable across platforms with their varying terminals. However, note that to display ANSI colors and formatting will only be displayed on Windows consoles if the Doc value is output using the putDoc function or one of its friends. Rendering the Doc to a String and then outputing that will only work on Unix-style operating systems.

Forecolor combinators

Backcolor combinators

Emboldening combinators

Underlining combinators

Formatting elimination combinators

Pretty class

1 declaration
classclass Pretty a where
#

Overloaded conversion to Doc.

Laws:

  1. output should be pretty. :-)

Methods

  • pretty :: a -> Doc ann
    Example1 expression
    pretty 1 <+> pretty "hello" <+> pretty 1.2341 hello 1.234
  • prettyList :: [a] -> Doc ann

    prettyList is only used to define the instance Pretty a => Pretty [a]. In normal circumstances only the pretty function is used.

    Example1 expression
    prettyList [1, 23, 456][1, 23, 456]
Instances27Pretty, …
  • Pretty IntegerDefined in prettyprinter-1.7.1 · Prettyprinter.Internal
    Example1 expression
    pretty (2^123 :: Integer)10633823966279326983230456482242756608
  • Pretty NaturalDefined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • Pretty VoidDefined in prettyprinter-1.7.1 · Prettyprinter.Internal

    Finding a good example for printing something that does not exist is hard, so here is an example of printing a list full of nothing.

    Example1 expression
    pretty ([] :: [Void])[]
  • Pretty Int16Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • Pretty Int32Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • Pretty Int64Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • Pretty Int8Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • Pretty Word16Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • Pretty Word32Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • Pretty Word64Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • Pretty Word8Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • Pretty BoolDefined in prettyprinter-1.7.1 · Prettyprinter.Internal
    Example1 expression
    pretty TrueTrue
  • Pretty CharDefined in prettyprinter-1.7.1 · Prettyprinter.Internal

    Instead of (pretty 'n'), consider using line as a more readable alternative.

    Example2 expressions
    pretty 'f' <> pretty 'o' <> pretty 'o'foopretty ("string" :: String)string
  • Pretty DoubleDefined in prettyprinter-1.7.1 · Prettyprinter.Internal
    Example1 expression
    pretty (exp 1 :: Double)2.71828182845904...
  • Pretty FloatDefined in prettyprinter-1.7.1 · Prettyprinter.Internal
    Example1 expression
    pretty (pi :: Float)3.1415927
  • Pretty IntDefined in prettyprinter-1.7.1 · Prettyprinter.Internal
    Example1 expression
    pretty (123 :: Int)123
  • Pretty WordDefined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • Pretty TextDefined in prettyprinter-1.7.1 · Prettyprinter.Internal

    Automatically converts all newlines to line.

    Example1 expression
    pretty ("hello\nworld" :: Text)helloworld

    Note that line can be undone by group:

    Example1 expression
    group (pretty ("hello\nworld" :: Text))hello world

    Manually use hardline if you definitely want newlines.

  • Pretty TextDefined in prettyprinter-1.7.1 · Prettyprinter.Internal

    (lazy Text instance, identical to the strict version)

  • Pretty ()Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
    Example1 expression
    pretty ()()

    The argument is not used:

    Example1 expression
    pretty (error "Strict?" :: ())()
  • Pretty a => Pretty (NonEmpty a)Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • Pretty a => Pretty (Identity a)Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
    Example1 expression
    pretty (Identity 1)1
  • Pretty a => Pretty (Maybe a)Defined in prettyprinter-1.7.1 · Prettyprinter.Internal

    Ignore Nothings, print Just contents.

    Example2 expressions
    pretty (Just True)Truebraces (pretty (Nothing :: Maybe Bool)){}
    Example1 expression
    pretty [Just 1, Nothing, Just 3, Nothing][1, 3]
  • Pretty a => Pretty [a]Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
    Example1 expression
    pretty [1,2,3][1, 2, 3]
  • (Pretty a1, Pretty a2) => Pretty (a1, a2)Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
    Example1 expression
    pretty (123, "hello")(123, hello)
  • Pretty a => Pretty (Const a b)Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
  • (Pretty a1, Pretty a2, Pretty a3) => Pretty (a1, a2, a3)Defined in prettyprinter-1.7.1 · Prettyprinter.Internal
    Example1 expression
    pretty (123, "hello", False)(123, hello, False)

Rendering and displaying documents

0 declarations

Simple (i.e., rendered) documents

Simultaneous rendering and displaying of documents

Undocumented

4 declarations