Writing Lisp Macros with s-cargot and uniplate

It's possible to explore and write simple Lisp macros using Haskell tools.

s-cargot is an S-Expression library for Haskell. S-Expressions are a foundation of the Lisp family of languages.

uniplate is a close-to-magic library that we'll be using to transform data easily.

Let's start with some boilerplate we need:

Lisp expressions can be thought of as a tree. When we want to apply some transformation or macro to that tree, there is usually some notion of traversal. This traversal can be tricky, and can be recursive. The magic of uniplate is that it lets us ignore it completely!

Here's an example expression with its representation in s-cargot:

We'll stick with a simple macro, that just renames "add" expressions to "+" expressions in Lisp programs:

Not to be too anticlimactic, but we are ... done. Here are a few examples and their transformations:

All without describing any traversals, or writing any recursive code.

Let's try one more macro, describing a new "unless" language construct that we we build from "if-not" statements: