7

I am trying to determine what facilities Clojure and F# have for creating DSLs. What facilities does each provide in order to create and manipulate DSLs?

As F# is statically typed, does this make it more difficult for this specific task? On the Clojure part I have no real experience but all LISPs are known to be great for metaprogramming/DSLs.

My question is not intended to make a war or something of the sort between both languages. If I'm making questions about the both lately is because I do think both are great and want to know more about the specifics of the two.

After reading some days about Intentional Programming, it has made me revive my interest in DSLs and all.

Although I have some knowledge of F# it's true I haven't yet developed anything using quotations or something like that. I've seen examples of DSLs based on discriminated unions which seem interesting.

1
  • This is an open-ended and pretty subjective question. Please read the FAQ. Jun 1, 2012 at 12:20

2 Answers 2

5

You can ultimately create DSLs in any language.

What make Clojure / other Lisps particularly unique and well-suited to metaprogramming is that they are homoiconic - that is, the language itself is expressed naturally in the data structures of the same language. In Lisp, you are effectively writing code directly as an AST.

This is surprisingly powerful - it means that code generation is effectively equivalent to creating a relatively simple data structure. And the language provides facilities for you to generate arbitrary code at compile time via macros. This effectively allows you to "extend the language" to support any particular DSL you require.

As an example, I recently found myself wanting an imperative for loop in Clojure (apologies to functional programming purists, but sometimes you want one....). Adding this to the language was a 5-liner:

(defmacro for-loop [[sym init check change :as params] & steps]
  `(loop [~sym ~init value# nil]
     (if ~check
       (let [new-value# (do ~@steps)] (recur ~change new-value#))
       value#)))

So now I can do:

(for-loop [i 0 (< i 10) (inc i)]
   (println i))
=> < prints numbers from 0..9 >

This is obviously a simple example, but it should hopefully be clear that the ability to generate new language constructs by creating a set of short macros that expand to exactly the code you want makes building a DSL particularly easy.

Some reading / links you may find interesting:

6
  • Thanks a lot for the comment. I'll no doubt read all of those later (Graham's one I already did some time ago anyway; it's a good classic). One point raised by Alex below that I forgot to mention... I assume this is all to make embedded DSLs and that in order to make an external/estandalone one there's the standard way of lex & yacc. Am I right? Jun 1, 2012 at 11:32
  • 2
    Lisp is great for embedded DSLs since, as mikera mentioned in the answer above, Lisp programs are expressed by using lists which you can then manipulate at your will. If you want to write a custom lexer/parser you would have to use some parser generator for Lisp or write your own parser by hand.
    – Alex
    Jun 1, 2012 at 14:27
  • 1
    @Jacobo - well it would be easy enough to write a parser that produces a Lisp AST and then continue from there in the normal way. I doubt it is worth bringing in an external parser generator like lex / yacc if you are already using Clojure: there are a bunch of parser libraries already which should do the job (e.g. those inspired by Haskell's monadic parser combinators)
    – mikera
    Jun 1, 2012 at 15:56
  • "well-suited to metaprogramming". Only in the specific case of Lisp-like embedded domain specific languages...
    – J D
    Jun 1, 2012 at 18:02
  • 1
    @JacoboPolavieja Thanks! There is a lot to be said about this and I was biting my lip when I wrote that. Homoiconicity is completely useless and arguably more damaging than helpful (see Mathematica and OCaml). Lisp-style macros have their uses but they will not help much with the implementation of a DSL unless your DSL is an embedded Lisp dialect. I'm not saying that Clojure will be worse than F#, just that the advantages claimed here are bogus. Would be interesting to compare existing solutions, like a regex compiler written in Clojure and F#...
    – J D
    Jun 5, 2012 at 0:28
4

I can't talk about Clojure since I have't used it, but I know a bit about DSLs in F#. F# provides two main language oriented programming features (which is what Don Syme likes to call them): code quotations and computation expressions.

Code quotations are closer to what you would get with macros in a language like Lisp. They allow you to generate expressions programmatically which you can then execute. By using the ReflectedDefinition attribute on F# expressions you get access to their ASTs. See http://msdn.microsoft.com/en-us/library/dd233212.aspx for more details.

Computation expressions are similar to the do notation in Haskell. The compiler rewrites code using a special syntax into calls to a type which you define. This type should ideally form a monad. Since they are monads in disguise they should allow you to implement the custom semantics of your DSL. See http://msdn.microsoft.com/en-us/library/dd233182.aspx for more details.

IMO computation expressions are better suited for writing DSLs on top of F#, while code quotations are better for tasks like transformations or translations (e.g. F# to JavaScript).

Besides these two main features you have the rest of the language which you can use.

Of course, above I've been only talking about embedded domain specific languages. You can go the extra mile and use fslex and fsyacc for a standalone DSL.

1
  • Thanks for the comment and visions on both techniques. It seems like any good thing where F# or Clojure shines you discover the other one has it equally good. Good point about embedded or external DSLs, which I forgot to mention. I really am learning a lot by investigating each language characteristics and ways of doing things. Thanks again! Jun 1, 2012 at 11:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.