A DSL for formatting
We’ll start with a relatively simple programmer-oriented DSL, one for pretty-printing source code. Pretty-printing means generating a textual layout that is nicely formatted.
Overview
The Haskell DSL we’ll study is available in the Text.PrettyPrint
module in the pretty
package. It combines three elements that distinguish this module from arbitrary other libraries and characterizes it as an embedded DSL:
- The DSL revolves around an abstract data type (or multiple ones). An abstract data type is a data type whose representation is hidden from the user. The only way it can be manipulated is through the API of the library. The user can’t perform any pattern matching on it or invoke constructors directly.
- In our DSL, the abstract data type is
Doc
; this is the type of the (formatted) document. Such a document combines textual content with layout directives. - To create values of the abstract data type, the DSL provides several...