Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
TPDB.Pretty
Contents
Synopsis
- data Doc ann
- class Pretty a where
- pretty :: a -> Doc ann
- prettyList :: [a] -> Doc ann
- render :: Doc ann -> Text
- renderWide :: Doc ann -> SimpleDocStream ann
- renderCompact :: Doc ann1 -> SimpleDocStream ann2
- renderPretty :: Doc ann -> SimpleDocStream ann
- displayIO :: Handle -> SimpleDocStream ann -> IO ()
- fsep :: [Doc ann] -> Doc ann
- sep :: [Doc ann] -> Doc ann
- hsep :: [Doc ann] -> Doc ann
- vsep :: [Doc ann] -> Doc ann
- vcat :: [Doc ann] -> Doc ann
- hcat :: [Doc ann] -> Doc ann
- parens :: Doc ann -> Doc ann
- brackets :: Doc ann -> Doc ann
- angles :: Doc ann -> Doc ann
- braces :: Doc ann -> Doc ann
- enclose :: Doc ann -> Doc ann -> Doc ann -> Doc ann
- encloseSep :: Doc ann -> Doc ann -> Doc ann -> [Doc ann] -> Doc ann
- punctuate :: Doc ann -> [Doc ann] -> [Doc ann]
- comma :: Doc ann
- nest :: Int -> Doc ann -> Doc ann
- list :: [Doc ann] -> Doc ann
- tupled :: [Doc ann] -> Doc ann
- mempty :: Monoid a => a
- (<>) :: Semigroup a => a -> a -> a
- empty :: Doc ann
- text :: String -> Doc ann
- (<+>) :: Doc ann -> Doc ann -> Doc ann
- ($$) :: Doc ann -> Doc ann -> Doc ann
- indent :: Int -> Doc ann -> Doc ann
- nest :: Int -> Doc ann -> Doc ann
- hang :: Int -> Doc ann -> Doc ann
Documentation
The abstract data type
represents pretty documents that have
been annotated with data of type Doc
annann
.
More specifically, a value of type
represents a non-empty set of
possible layouts of a document. The layout functions select one of these
possibilities, taking into account things like the width of the output
document.Doc
The annotation is an arbitrary piece of data associated with (part of) a document. Annotations may be used by the rendering backends in order to display output differently, such as
- color information (e.g. when rendering to the terminal)
- mouseover text (e.g. when rendering to rich HTML)
- whether to show something or not (to allow simple or detailed versions)
The simplest way to display a Doc
is via the Show
class.
>>>
putStrLn (show (vsep ["hello", "world"]))
hello world
Instances
Minimal complete definition
Methods
>>>
pretty 1 <+> pretty "hello" <+> pretty 1.234
1 hello 1.234
prettyList :: [a] -> Doc ann #
is only used to define the prettyList
instance
. In normal circumstances only the Pretty
a => Pretty
[a]
function is used.pretty
>>>
prettyList [1, 23, 456]
[1, 23, 456]
Instances
Pretty Void | 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.
|
Defined in Prettyprinter.Internal | |
Pretty Int16 | |
Defined in Prettyprinter.Internal | |
Pretty Int32 | |
Defined in Prettyprinter.Internal | |
Pretty Int64 | |
Defined in Prettyprinter.Internal | |
Pretty Int8 | |
Defined in Prettyprinter.Internal | |
Pretty Word16 | |
Defined in Prettyprinter.Internal | |
Pretty Word32 | |
Defined in Prettyprinter.Internal | |
Pretty Word64 | |
Defined in Prettyprinter.Internal | |
Pretty Word8 | |
Defined in Prettyprinter.Internal | |
Pretty Text | Automatically converts all newlines to
Note that
Manually use |
Defined in Prettyprinter.Internal | |
Pretty Text | (lazy |
Defined in Prettyprinter.Internal | |
Pretty CertificationProblemInput Source # | |
Defined in TPDB.CPF.Proof.Type Methods pretty :: CertificationProblemInput -> Doc ann # prettyList :: [CertificationProblemInput] -> Doc ann # | |
Pretty Label Source # | |
Defined in TPDB.CPF.Proof.Type | |
Pretty Symbol Source # | |
Defined in TPDB.CPF.Proof.Type | |
Pretty Attributes Source # | |
Defined in TPDB.Data.Attributes | |
Pretty Identifier Source # | |
Defined in TPDB.Plain.Write | |
Pretty Integer |
|
Defined in Prettyprinter.Internal | |
Pretty Natural | |
Defined in Prettyprinter.Internal | |
Pretty () |
The argument is not used:
|
Defined in Prettyprinter.Internal | |
Pretty Bool |
|
Defined in Prettyprinter.Internal | |
Pretty Char | Instead of
|
Defined in Prettyprinter.Internal | |
Pretty Double |
|
Defined in Prettyprinter.Internal | |
Pretty Float |
|
Defined in Prettyprinter.Internal | |
Pretty Int |
|
Defined in Prettyprinter.Internal | |
Pretty Word | |
Defined in Prettyprinter.Internal | |
Pretty a => Pretty (Identity a) |
|
Defined in Prettyprinter.Internal | |
Pretty a => Pretty (NonEmpty a) | |
Defined in Prettyprinter.Internal | |
Pretty a => Pretty (Marked a) Source # | |
Defined in TPDB.DP.Transform | |
PrettyTerm a => Pretty (Rule a) Source # | |
Defined in TPDB.Plain.Write | |
Pretty a => Pretty (Maybe a) | Ignore
|
Defined in Prettyprinter.Internal | |
Pretty a => Pretty [a] |
|
Defined in Prettyprinter.Internal | |
(Pretty a, Pretty b) => Pretty (Either a b) Source # | WARNING: there is instance Pretty a => Pretty (Maybe a) in the back-end but its spec is "Ignore Nothings, print Just contents" |
Defined in TPDB.Pretty | |
(TermC s r, Pretty s, Pretty r, Variables (Term s r)) => Pretty (Problem s r) Source # | |
Defined in TPDB.Plain.Write | |
(Pretty s, PrettyTerm r, Variables (RS s r), Pretty (Var (RS s r))) => Pretty (RS s r) Source # | |
Defined in TPDB.Plain.Write | |
(TermC v s, Pretty v, Pretty s) => Pretty (Term v s) Source # | |
Defined in TPDB.Plain.Write | |
(Pretty a1, Pretty a2) => Pretty (a1, a2) |
|
Defined in Prettyprinter.Internal | |
Pretty a => Pretty (Const a b) | |
Defined in Prettyprinter.Internal | |
(Pretty a1, Pretty a2, Pretty a3) => Pretty (a1, a2, a3) |
|
Defined in Prettyprinter.Internal | |
(Pretty a, Pretty b, Pretty c, Pretty d) => Pretty (a, b, c, d) Source # | |
Defined in TPDB.Pretty |
renderWide :: Doc ann -> SimpleDocStream ann Source #
renderCompact :: Doc ann1 -> SimpleDocStream ann2 Source #
renderPretty :: Doc ann -> SimpleDocStream ann Source #
Arguments
:: Doc ann | left delimiter |
-> Doc ann | right delimiter |
-> Doc ann | separator |
-> [Doc ann] | input documents |
-> Doc ann |
(
concatenates the documents encloseSep
l r sep xs)xs
separated by
sep
, and encloses the resulting document by l
and r
.
The documents are laid out horizontally if that fits the page:
>>>
let doc = "list" <+> align (encloseSep lbracket rbracket comma (map pretty [1,20,300,4000]))
>>>
putDocW 80 doc
list [1,20,300,4000]
If there is not enough space, then the input is split into lines entry-wise therwise they are laid out vertically, with separators put in the front:
>>>
putDocW 10 doc
list [1 ,20 ,300 ,4000]
Note that doc
contains an explicit call to align
so that the list items
are aligned vertically.
For putting separators at the end of entries instead, have a look at
punctuate
.
(
appends punctuate
p xs)p
to all but the last document in xs
.
>>>
let docs = punctuate comma (Util.words "lorem ipsum dolor sit amet")
>>>
putDocW 80 (hsep docs)
lorem, ipsum, dolor, sit, amet
The separators are put at the end of the entries, which we can see if we position the result vertically:
>>>
putDocW 20 (vsep docs)
lorem, ipsum, dolor, sit, amet
If you want put the commas in front of their elements instead of at the end,
you should use tupled
or, in general, encloseSep
.
(
lays out the document nest
i x)x
with the current nesting level
(indentation of the following lines) increased by i
. Negative values are
allowed, and decrease the nesting level accordingly.
>>>
vsep [nest 4 (vsep ["lorem", "ipsum", "dolor"]), "sit", "amet"]
lorem ipsum dolor sit amet
See also
list :: [Doc ann] -> Doc ann #
Haskell-inspired variant of encloseSep
with braces and comma as
separator.
>>>
let doc = list (map pretty [1,20,300,4000])
>>>
putDocW 80 doc
[1, 20, 300, 4000]
>>>
putDocW 10 doc
[ 1 , 20 , 300 , 4000 ]
tupled :: [Doc ann] -> Doc ann #
Haskell-inspired variant of encloseSep
with parentheses and comma as
separator.
>>>
let doc = tupled (map pretty [1,20,300,4000])
>>>
putDocW 80 doc
(1, 20, 300, 4000)
>>>
putDocW 10 doc
( 1 , 20 , 300 , 4000 )
(<>) :: Semigroup a => a -> a -> a infixr 6 #
An associative operation.
>>>
[1,2,3] <> [4,5,6]
[1,2,3,4,5,6]
(
lays out the document nest
i x)x
with the current nesting level
(indentation of the following lines) increased by i
. Negative values are
allowed, and decrease the nesting level accordingly.
>>>
vsep [nest 4 (vsep ["lorem", "ipsum", "dolor"]), "sit", "amet"]
lorem ipsum dolor sit amet
See also
Arguments
:: Int | Change of nesting level, relative to the start of the first line |
-> Doc ann | |
-> Doc ann |
(
lays out the document hang
i x)x
with a nesting level set to the
current column plus i
. Negative values are allowed, and decrease the
nesting level accordingly.
>>>
let doc = reflow "Indenting these words with hang"
>>>
putDocW 24 ("prefix" <+> hang 4 doc)
prefix Indenting these words with hang
This differs from nest
, which is based on the current nesting level plus
i
. When you're not sure, try the more efficient nest
first. In our
example, this would yield
>>>
let doc = reflow "Indenting these words with nest"
>>>
putDocW 24 ("prefix" <+> nest 4 doc)
prefix Indenting these words with nest
hang
i doc =align
(nest
i doc)