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
- Packageansi-wl-pprint-1.0.2
- Exports117
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceLeijen.hs
The algebra of pretty-printing
0 declarationsThe 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 = xThe text combinator is a homomorphism from string concatenation to document concatenation:
text (s ++ t) = text s <> text t
text "" = emptyThe 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 xThe 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 xFrom 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 <> yIt 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) <> zSimilar laws also hold for the other line break operators </>, <$$>, and <//>.
Documents
1 declarationBasic combinators
19 declarationsAn associative operation.
Examples
[1,2,3] <> [4,5,6][1,2,3,4,5,6]
Just [1, 2, 3] <> Just [4, 5, 6]Just [1,2,3,4,5,6]
putStr "Hello, " <> putStrLn "World!"Hello, World!
Alignment combinators
7 declarationsThe 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 declarationsList combinators
9 declarationsFiller combinators
2 declarationsBracketing combinators
7 declarationsNamed character combinators
17 declarationsANSI formatting combinators
0 declarationsThis 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 declarationMethods
pretty :: a -> Doc annExample1 expression pretty 1 <+> pretty "hello" <+> pretty 1.2341 hello 1.234
prettyList :: [a] -> Doc annprettyListis only used to define theinstance Pretty a => Pretty [a]. In normal circumstances only theprettyfunction is used.Example1 expression prettyList [1, 23, 456][1, 23, 456]
Instances27Pretty, …
Pretty IntegerDefined in prettyprinter-1.7.1 · Prettyprinter.InternalExample1 expression pretty (2^123 :: Integer)10633823966279326983230456482242756608
Pretty NaturalDefined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty VoidDefined in prettyprinter-1.7.1 · Prettyprinter.InternalFinding 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.InternalPretty Int32Defined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty Int64Defined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty Int8Defined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty Word16Defined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty Word32Defined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty Word64Defined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty Word8Defined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty BoolDefined in prettyprinter-1.7.1 · Prettyprinter.InternalExample1 expression pretty TrueTrue
Pretty CharDefined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty DoubleDefined in prettyprinter-1.7.1 · Prettyprinter.InternalExample1 expression pretty (exp 1 :: Double)2.71828182845904...
Pretty FloatDefined in prettyprinter-1.7.1 · Prettyprinter.InternalExample1 expression pretty (pi :: Float)3.1415927
Pretty IntDefined in prettyprinter-1.7.1 · Prettyprinter.InternalExample1 expression pretty (123 :: Int)123
Pretty WordDefined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty TextDefined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty TextDefined in prettyprinter-1.7.1 · Prettyprinter.Internal(lazy Text instance, identical to the strict version)
Pretty ()Defined in prettyprinter-1.7.1 · Prettyprinter.InternalExample1 expression pretty ()()
The argument is not used:
Example1 expression pretty (error "Strict?" :: ())()
Pretty a => Pretty (NonEmpty a)Defined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty a => Pretty (Identity a)Defined in prettyprinter-1.7.1 · Prettyprinter.InternalExample1 expression pretty (Identity 1)1
Pretty a => Pretty (Maybe a)Defined in prettyprinter-1.7.1 · Prettyprinter.InternalPretty a => Pretty [a]Defined in prettyprinter-1.7.1 · Prettyprinter.InternalExample1 expression pretty [1,2,3][1, 2, 3]
(Pretty a1, Pretty a2) => Pretty (a1, a2)Defined in prettyprinter-1.7.1 · Prettyprinter.InternalExample1 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.InternalExample1 expression pretty (123, "hello", False)(123, hello, False)