Tue 12 Jul 2005

DSLs in Lisp

Bill Clementson posted about Rainer Joswig's example of DSL development in Lisp. This was all prompted by Martin Fowler's article on DSLs.

As well as the interesting discussion of Lisp development environments, and the video (that's a torrent link for a 125MB movie), there is Rainer's observation of the development style that he employed. He termed this “putting parentheses around the specification and make it run”.

There must be something to it, because this is similar to how I approached the development of twinql (though Rainer is discussing a network protocol, of course, and using a lot more mojo!). I wrote an s-expression representation of the queries — SPARQL is a DSL, so this was just a syntactic change — and made them run through decomposition into implementation functions. In the case of SPARQL, the execution involves resolving all of the graph patterns, building up bindings, then doing the ordering/filtering/&c. work, before finally presenting the results.

To accept the actual SPARQL text, I needed a parser, so I employed CL-YACC. After parsing, the results are in the s-expr DSL, and can be run. I found this two-stage process useful, as problems in the parser could be solved by inspection of the s-expr output, rather than execution, and I could develop the query engine before finishing the parser.

It wouldn't be much effort to put an incremental parser on the front end (a SPARQL reader, really) and make the Semantic Web version of a SQL console.

What would I have done in, say, Java? Rainer discusses Fowler's Java DSL example — “better not look at it… it's not really pretty”! Well, the parsing would have been much the same*, but I'd have had less fun throwing lists together (after all, the output couldn't be an s-expression without doing a Greenspun). Actually doing the querying would have required walking over some objects, rather than writing the right functions. I certainly don't think the Java solution would be as clear, and I suspect it would have taken much longer to write.

As Rainer said, it wouldn't have been pretty. I tend to conflate prettiness with elegance — I consider it a good heuristic — so I'd term the Lispy solution more elegant.

* In fact, it would have taken much less time to do the parser in Java, as the SPARQL presentation grammar is automatically derived from a JavaCC grammar! That's a clear demonstration of Java's advantages (whatever you're trying to do, someone's already written it), but also quite promising — even without that enormous advantage, I still would rate the Lisp development process as faster and more satisfying than doing the same in Java.

Give me a REPL any day!


Posted at 2005-07-12 10:20:35 by RichardLink to DSLs in Lisp