25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

parensIf :: Bool -> Doc -> Doc<br />

parensIf True = parens<br />

parensIf False = id<br />

Finally, we define ppr. e p variable will indicate our depth within the current structure we’re printing<br />

and allow us to print out differently to disambiguate it from its surroundings if necessary.<br />

instance Pretty Expr where<br />

ppr p e = case e of<br />

Lit (LInt a) -> text (show a)<br />

Lit (LBool b) -> text (show b)<br />

Var x -> text x<br />

App a b -> parensIf (p>0) $ (ppr (p+1) a) (ppr p b)<br />

Lam x a -> parensIf (p>0) $<br />

char ’\\’<br />

hsep (fmap pp (viewVars e))<br />

”->”<br />

ppr (p+1) (viewBody e)<br />

ppexpr :: Expr -> String<br />

ppexpr = render . ppr 0<br />

Full Source<br />

• Untyped Lambda Calculus<br />

53

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!