The main features of the printcess pretty printing library are

In this section, a small example is presented, which pretty prints a lambda calculus expression.

First we define an abstract syntax tree for lambda calculus expressions.

data Expr = EVar String | EAbs String Expr | EApp Expr Expr

Then we make Expr an instance of the Pretty type class, which declares one method pp . This method takes an Expr and returns a PrettyM () action, which describes how to pretty print the Expr .

instance Pretty Expr where pp (EVar x) = pp x pp (EApp e1 e2) = assocL 9 $ L e1 ~> R e2 pp (EAbs x e) = assocR 0 $ "λ" +> I x +> "." ~> R e

We print

a variable EVar x by printing the identifier String x .

by printing the identifier . a function application EApp e1 e2 as a left-associative operator of fixity 9 ( assocL 9 ), where e1 is the left argument ( L ) and e2 is the right argument ( R ). The ( ~> ) combinator separates its first argument with a space from its second argument.

as a left-associative operator of fixity 9 ( ), where e1 is the left argument ( ) and is the right argument ( ). The ( ) combinator separates its first argument with a space from its second argument. a function abstraction EAbs x e as a right-associative operator of fixity 0 ( assocR 0 ), where x is an inner argument ( I ) and e is the right argument ( R ). The ( +> ) combinator behaves as ( ~> ), but without inserting a space.

Then we define a simple test expression e1 representing λx. λy. x y (x y)

e1 :: Expr e1 = EAbs "x" $ EAbs "y" $ EApp (EApp (EVar "x") (EVar "y")) (EApp (EVar "x") (EVar "y"))

and pretty print it to String using the pretty function

s1, s2 :: String s1 = pretty defConfig e1 -- evaluates to "λx. λy. x y (x y)" s2 = pretty (cMaxLineWidth .= Just 12) e1 -- evaluates to "λx. λy. x y -- (x y)"