Integration of external DSLs

upd. In Scala 2.10 (with the macro paradise compiler plugin) and 2.11 (as a part of standard distribution) we now have quasiquotes that showcase how this design can be put to life.

The "Why It's Nice to be Quoted: Quasiquoting for Haskell" paper by Geoffrey B. Mainland describes an astonishing concept of embedding and integrating arbitrary languages into a sufficiently capable host language.

One of the most impressive examples in the paper involves a peephole optimizer for x86 assembly language. The example exhibits such prominent features of quasiquoting as: 1) expressing ASTs in native syntax, 2) easy composition, 3) enjoyable pattern matching.

peep :: [Asm] -> [Asm]
peep [:asm | mov&s $&r1, &r2
             cmp $&r3, &r4
             je &lbl |] : rest
     | r3 == r1 && r4 == r2

   = [:asm | mov&s $&r1, &r2
             jmp &lbl |] : rest

The point here is that a language with quasiquotations can subsume and embed arbitrary external domain-specific languages, granted someone implements quasiquoting services (parsing, splicing, pattern matching) for that language.

Beauty of this concept from the pragmatic standpoint comes from the fact that these external DSLs can be processed (for example, validated) by macros, which are evaluated during the compile time. This means that format strings, regexes, XML (less relevant for Scala), HTML, JavaScript - all those can be to certain extent validated during the compilation of the application, bringing additional static safety to the program.

With the syntax introduced by (an unrelated) SIP: String interpolation and formatting, it becomes possible to write quasiquotations in Scala, and with macros it becomes possible to process these quasiquotations during the compile-time.

Another useful application of this notion is convenient notation for new kinds of literals. For example, using the syntax from the aforementioned string interpolation SIP one could write b"10001" to express an integer literal 17. The interpolation would be processed by a macro during the compile-time, which means that the programmer wouldn't pay any runtime penalty for the convenience.