Say your name ! ; Say “your name”! Do you understand the difference between those two sentences?
The first uses the meaning of the words “your name”, while the other refers the words themselves. How do you know that? They are quoted.
Sometimes you would like to be able to do the same thing with programming languages. Being able to manipulate the code from the code help us make programs who generate programs, and as crazy at it may sound, it is really powerful. If you are a C# developer you probably know about .NET reflection, F# allows you to go a little further, by manipulating code expressions in a concise safe and statically typed way.
Most programmers use language constructs such as functions as a mean of abstraction, however most of them don’t use the power of language abstraction. Quotations are a really interesting to create interpreters and compilers using F# code. But why creating a new F# compiler or interpreter?
Some people think programming problems have their own specific solution better expressed in a specific language, I think it is totally true, so the use of so many languages to create a web site may be justified.
But what if a language is expressive enough to allow to write more specific language within it syntax? I mean a host language embedding an other. This may sound weird but it is actually something which appears really common to functional programmers, especially LISP programmers. Indeed LISP syntax and core primitives are by design really limited, but powerful enough to create all the others. So big LISP programs consist usually in creating and using different embedded languages specifics to some part of the application, for example a language for the UI, one allowing Object Oriented Programming, one SQL like to query data, and one declarative data language to store data in files. Those specifics embedded languages are called Domain Specific Languages (DSL).
By the way minimalistic doesn’t mean powerless, look the matter in this world is composed of no more than a hundred different types of atoms, and actually all the diversity of living creatures have their DNA encoded with four basic building block molecules known as ATGC. In computer sciences, what about the dilemma RISC vs. CISC? Didn’t we “recently” realize that simple RISC architecture such as ARM, easier to build and minimize, thus consuming less energy have a lot of advantages over complexes ones?
It is really easy to define a quotation in F#, you just have to surround code with <@ and @> for typed quotations and <@@ and @@> for untyped quotations. Look at the example below:
The % splicing operator in front of a variable allows the integration of a quotation expression into an other, there is also an untyped %% splicing operator. The evaluation of this code, shows:
Both favoritePlace and code are bindings whose value are expressions, represented by the generic type Quotations.Expr<’a>. You clearly see that the Abstract Syntax Tree (AST) is directly accessible. Refers to the Quotations.Expr<’a> for more information about the expression type constructors available (http://msdn.microsoft.com/en-us/library/ee370577.aspx).
You could imagine transforming, interpreting or compiling this AST using pattern matching on the different constructors.