Is there any commonly used method or even a library for pretty printing (and parsing) a syntax tree with (binary) operators that have an associativity and a precedence level assigned, so that the result uses as few brackets as possible?
Take the formulas of propositional calculus as an example:
data Formula
= Atom String
| Not (Formula)
| And (Formula) (Formula)
| Or (Formula) (Formula)
| Imp (Formula) (Formula)
Assume that the precedence is Imp
< Or
< And
< Not
(so Not
binds the most) and that And
, Or
and Imp
should associate to the right; so for example Imp (And (Imp (Atom "A") (Atom "B")) (Atom "A")) (Atom "B")
should print something like (A -> B) /\ A -> B
.
Of course this could be achieved by pattern matching but this is tedious and very unpleasant; I'm looking for something similarly simple to this notation from the Coq proof assistant:
Notation "A /\ B" := (and A B) (at level 80, right associativity).
which generates a parser and a pretty printer.