Technology:HughesPJ

Headline

A Language:Haskell library pretty printing

Illustration

The library's central type is Doc for documents. Doc is abstraction over text. The idea is that one maps abstract syntax (modeled by algebraic data types) to docs with the help of combinators serving, for example, horizontal and vertical composition. A doc can then be "shown" literally as text.

Thus:

instance Show Doc 
  where
    -- type is abstract; it can be shown

Documents can be contructed from literals by these combinators:

-- Map string to document
text :: String -> Doc

-- Map int to document
int :: Int -> Doc

Here is an illustration:

> text "hello"
hello
> int 42
42

Documents can be composed in some ways, e.g.:

-- The empty docment
empty :: Doc

-- Compose horizontally
(<>) :: Doc -> Doc -> Doc

-- Compose horizontally with extra space for separation
(<+>) :: Doc -> Doc -> Doc

-- Compose vertically
($$) :: Doc -> Doc -> Doc

Here is an illustration:

> empty

> text "4" <> text "2"
42
> text "before" <+> text "after"
before after
> text "above" $$ text "below"
above
below

The combinators also satisfy some reasonable laws. For example, empty is a unit of horizontal composition -- even the form with an extra space for separation.

> empty <+> int 42
42

Metadata

Metadata

this
instanceOf

this
supports

Resources

  • hackage.haskell.org


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.