concept dsl in category clojure

appears as: DSLs, DSL, The DSL
Clojure in Action, Second Edition

This is an excerpt from Manning's book Clojure in Action, Second Edition.

Figure 11.2. The typical layers in a DSL-driven system. Such systems benefit from a bottom-up design where the lowest levels are the primitive concepts of the domain modeled on top of the basic Clojure language. Higher layers are compositions of these primitives into more complex domain concepts. Finally, a runtime layer sits on top of these, which can execute code specified in a DSL. This final layer often represents the core solution of the problem that the software was meant to solve.

You can add more functions such as this one, and they can be as complex as needed. The user of the DSL doesn’t need to know how they’re implemented, because they’ll be described as the primitives of the DSL.

Now, let’s go ahead and define the remainder of the execution engine. The first piece is a function to load up with the DSL program. Typically, this will be some text either written by a user or generated by another program such as a graphical rules editor. Given that ultimately the DSL is Clojure code, you can use load-string to load it. Consider the following code:

The Joy of Clojure, Second Edition

This is an excerpt from Manning's book The Joy of Clojure, Second Edition.

This almost looks like the units map from chapter 7, except for a few minor details. We’ll eventually zero in on a specification that matches the form expected by the convert function from chapter 7, but there is a more pressing matter at hand. That is, although you can use the convert function directly, it assumes a well-formed unit conversion map. This is problematic for use in a DSL because the details of covert will leak out to users in the form of nasty exceptions or errors. Instead, let’s create a driver function that checks input values and reports more sensible errors.

Notice that the data processors have a similar form. If you really wanted to be fancy, you could build a DSL to help build the SQL-like DSL; we’ll leave that as an exercise for you. Instead, observe how the use of SELECT works to compose with the existing data processors:

(process-select-clause shuffle-expr
  '[a b c]
  (process-from-clause shuffle-expr 'X
    (process-left-join-clause shuffle-expr 'Y :ON '(= X.a Y.b)))
  (process-where-clause shuffle-expr '(AND (< a 5) (< b ~max))))

;;=> "SELECT a, b, c FROM X
;;    LEFT JOIN Y ON (X.a = Y.b)
;;    WHERE ((a < 5) AND (b < ?))"

Now that you have the data processors in place and operating as you expect, you can start to think about cleaning up the DSL so your users don’t need to explicitly quote the clause elements but can instead write queries in a more natural way. This is where macros come into play: now that you’ve built your DSL infrastructure on data and processors, you can use macros to provide some syntactic sugar. First, it’s not a terribly compelling DSL if it requires you to use long clauses with names of the form process-*-clause. Instead, it would be fun to define a mapping from more relevant names like SELECT and FROM. You can describe this name mapping directly to the functions that provide the data processing:

(declare apply-syntax)

(def ^:dynamic *clause-map*
  {'SELECT    (partial process-select-clause apply-syntax)
   'FROM      (partial process-from-clause apply-syntax)
   'LEFT-JOIN (partial process-left-join-clause shuffle-expr)
   'WHERE     (partial process-where-clause shuffle-expr)})

Adding a level of indirection in the way a DSL syntax maps to the underlying processors is a powerful technique. By making such a split, we’re viewing DSLs as a way to define a language to decouple the problem from the strategies for solving it. That is, you can swap out the data-processing functions for other, more powerful and robust functions at any time to enhance the effect of the DSL while maintaining a consistent SQL-like syntax.

DSLs are an important part of a Clojure programmer’s tool-set and stem from a long Lisp tradition. When Paul Graham talks about “bottom-up programming” in his perennial work On Lisp (Prentice Hall, 1993), this is what he’s referring to. In Clojure, it’s common practice to start by defining and implementing a low-level language specifically for the levels above. Creating complex software systems is hard, but using this approach, you can build the complicated parts out of smaller, simpler pieces.