Encoding intermediate representations as S expressions
Over dinner @rae and I discussed various things that GHC and OCaml respectively do well. One of the items he reported in the latter category is OCaml's use of S expressions to represent the compiler's intermediate representations, e.g., for use in testing.
As I understand it, using a generic grammar like S expressions to represent syntax trees carries a number of advantages:
- Much of complexity of writing/maintaing a textual parser can be pushed into a generic, reusable S expression parser; one merely needs to write an isomorphism between the S expression syntax and the various intermediate representations
- crude automated formatting is fairly easy, meaning that the syntax can be strongly normalizing (which is important for test output)
- automated refactoring of S expressions is quite straightforward, meaning that it becomes much less painful to adapt tests when the underlying IR inevitably changes
- it is easy to write traversals of S expressions; one can imagine having a small DSL for inspecting and asserting properties about an sexpr which could be used by tests instead of the current
awk
/sed
/grep
usage
One could imagine applying this idea in Core, STG, and Cmm, opening new avenues to targetted testing of GHC's various pipeline stages.