Safe Haskell | Safe |
---|---|
Language | Haskell98 |
- data Doc ann :: * -> *
- class Pretty a where
- render :: Doc ann -> Text
- renderWide :: Doc ann -> SimpleDocStream ann
- renderCompact :: Doc ann -> SimpleDocStream ann
- 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
- module Data.Monoid
- 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
Functor Doc | Alter the document’s annotations. This instance makes |
Show (Doc ann) |
|
IsString (Doc ann) |
This instance uses the |
Generic (Doc ann) | |
Semigroup (Doc ann) | x
|
Monoid (Doc ann) |
|
type Rep (Doc ann) | |
>>>
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]
Pretty Bool |
|
Pretty Char | Instead of
|
Pretty Double |
|
Pretty Float |
|
Pretty Int |
|
Pretty Int8 | |
Pretty Int16 | |
Pretty Int32 | |
Pretty Int64 | |
Pretty Integer |
|
Pretty Natural | |
Pretty Word | |
Pretty Word8 | |
Pretty Word16 | |
Pretty Word32 | |
Pretty Word64 | |
Pretty () |
The argument is not used,
|
Pretty Text | Automatically converts all newlines to
Note that
Manually use |
Pretty Text | (lazy |
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.
|
Pretty Attributes # | |
Pretty CertificationProblemInput # | |
Pretty a => Pretty [a] |
|
Pretty a => Pretty (Maybe a) | Ignore
|
Pretty a => Pretty (NonEmpty a) | |
Pretty a => Pretty (Marked a) # | |
(Pretty a1, Pretty a2) => Pretty (a1, a2) |
|
(Pretty a1, Pretty a2, Pretty a3) => Pretty (a1, a2, a3) |
|
renderWide :: Doc ann -> SimpleDocStream ann Source #
renderCompact :: Doc ann -> SimpleDocStream ann Source #
renderPretty :: Doc ann -> SimpleDocStream ann Source #
:: 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
.
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 )
module Data.Monoid
(
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)