I’m writing a pretty-printer for a simple white-space sensitive language.
I like the Leijen pretty-printer library more than I like the Wadler library, but the Leijen library has one problem in my domain: any line break I insert may be overridden by the group construct, which may compress any line, which might change the semantics of the output.
I don’t think I can implement an ungroupable line in the wl-pprint (although I’d love to be wrong).
Looking a bit at the wl-pprint-extras package, I don’t think that even the exposed internal interface allows me to create a line which will not be squashed by group.
Do I just have to rely on the fact that I never use group, or do I have some better option?
Given that you want to be able to group and you also need to be able to ensure some lines aren’t uninserted,
why don’t we use the fact that the library designers encoded the semantics in the data type,
instead of in code. This fabulous decision makes it eminently re-engineerable.
The
Docdata type encodes a line break using the constructorLine :: Bool -> Doc.The Bool represents whether to omit a space when removing a line. (Lines indent when they’re there.)
Let’s replace the Bool:
The beautiful thing about the semantics-as-data design is that if we replace
this
Booldata withLineBehaviourdata, functions that didn’t use it butpassed it on unchanged don’t need editing. Functions that look inside at what
the Bool is break with the change – we’ll rewrite exactly the parts of the code
that need changing to support the new semantics by changing the data type where
the old semantics resided. The program won’t compile until we’ve made all the
changes we should, while we won’t need to touch a line of code that doesn’t
depend on line break semantics. Hooray!
For example,
renderPrettyuses theLineconstructor, but in the patternLine _,so we can leave that alone.
First, we need to replace
Line TruewithLine OmitSpace, andLine FalsewithLine AddSpace,but perhaps we should add our own
and we could perhaps do with a binary operator that uses it
and the equvalent of the vertical seperator, which I can’t think of a better name than very vertical separator:
The actual removing of lines happens in the
groupfunction. Everything can stay the same except:should be changed to
That’s it: I can’t find anything else to change!