Headline

Unparsing in Language:Haskell with a pretty printing combinator library

Characteristics

This contribution demonstrates unparsing, i.e., rendering a term-based representation as according to a some specific text-based concrete syntax. The unparser is described at a relatively high level of abstraction by means of an appropriate library in Language:Haskell.

Illustration

Input of unparsing is a term-based representation like this; "..." indicates an elision:

sampleCompany = Company
  "Acme Corporation"
  [ Department "Research"
      (Employee "Craig" "Redmond" 123456)
      []
      [ Employee "Erik" "Utrecht" 12345,
        Employee "Ralf" "Koblenz" 1234
      ],
    Department "Development"
      ...
  ]

Output of unparsing is a test-based representation like this:

company "Acme Corporation" {
  department "Research" {
    manager "Craig" {
      address "Redmond"
      salary 123456.0
    }
    employee "Erik" {
      address "Utrecht"
      salary 12345.0
    }
    employee "Ralf" {
      address "Koblenz"
      salary 1234.0
    }
  }
  department "Development" {
    ...
  }
}

Here is the function for unparsing:

unparse :: Company -> Doc
unparse (Company n ds) =
  bracy "company" n (vcat (map unparseD ds))
  where
    bracy :: String -> String -> Doc -> Doc
    bracy k n d =
         text k <+> doubleQuotes (text n) <+> text "{"
      $$ nest 2 d
      $$ text "}"
    unparseD :: Department -> Doc
    unparseD (Department n m ds es) =
      bracy "department" n (vcat (   [unparseE "manager" m]
                                ++ map unparseD ds
                                ++ map (unparseE "employee") es))
      where
        unparseE :: String -> Employee -> Doc
        unparseE k (Employee n a s) = bracy k n (a' $$ s')
          where
            a' = text "address" <+> doubleQuotes (text a)
            s' = text "salary" <+> text (show s)

It uses various combinators of Technology:HughesPJ all over the place. For instance, it uses vcat to vertically compose departments; it uses nest to achieve indentation for constituents of companies, departments, and employees. Overall, all the subexpressions render the company terms to a library-specific type of Doc, which is essentially an abstraction over text.

Relationships

Architecture

  • module Company.Data where
-- | A company consists of name and top-level departments data Company = Company Name [Department] deriving (Eq, Read, Show) -- | A department consists of name, manager, sub-departments, and employees data Department = Department Name Manager [Department] [Employee] deriving (Eq, Read, Show) -- | An employee consists of name, address, and salary data Employee = Employee Name Address Salary deriving (Eq, Show, Read) -- | Managers as employees type Manager = Employee -- | Names of companies, departments, and employees type Name = String -- | Addresses as strings type Address = String -- | Salaries as floats type Salary = Float
: a data model based on algebraic data types
  • module Company.Sample where
import Company.Data -- | A sample company useful for basic tests sampleCompany :: Company sampleCompany = Company "Acme Corporation" [ Department "Research" (Employee "Craig" "Redmond" 123456) [] [ (Employee "Erik" "Utrecht" 12345), (Employee "Ralf" "Koblenz" 1234) ], Department "Development" (Employee "Ray" "Redmond" 234567) [ Department "Dev1" (Employee "Klaus" "Boston" 23456) [ Department "Dev1.1" (Employee "Karl" "Riga" 2345) [] [(Employee "Joe" "Wifi City" 2344)] ] [] ] [] ]
: a sample company using term-based representation
  • module Company.Unparser where
import Company.Data import Text.PrettyPrint.HughesPJ unparse :: Company -> Doc unparse (Company n ds) = bracy "company" n (vcat (map unparseD ds)) where bracy :: String -> String -> Doc -> Doc bracy k n d = text k <+> doubleQuotes (text n) <+> text "{" $$ nest 2 d $$ text "}" unparseD :: Department -> Doc unparseD (Department n m ds es) = bracy "department" n (vcat ( [unparseE "manager" m] ++ map unparseD ds ++ map (unparseE "employee") es)) where unparseE :: String -> Employee -> Doc unparseE k (Employee n a s) = bracy k n (a' $$ s') where a' = text "address" <+> doubleQuotes (text a) s' = text "salary" <+> text (show s)
: the actual unparser, rendering terms as text

Usage

See Contribution:haskellComposition.

Metadata


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.