8. Quotations – Say “your name”

[DRAFT]

 

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.

 

 

What can you do with F# quotations ?

 

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? 

Look at the web! How many different languages do you need nowadays to code a simple web site such as a blog? Probably HTML, CSS, JavaScript on the client side, C#, Java or PHP and SQL on the server side, so at least five different languages!

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?

 

 

A taste of quotations…

 

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:

 

image

The % splicingoperator 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:

image

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 (https://msdn.microsoft.com/en-us/library/ee370577.aspx).

You could imagine transforming, interpreting or compiling this AST using pattern matching on the different constructors.

 

 

Making the web easier !

 

Although we can regret F# lacks LISP macros, it is one of those languages that are expressive enough to allow multi-paradigm programming and the creation of DSL. We could then use F# as a base language for the web. Instead of having those four languages we talked about earlier. We could just only use one! We could then use F# functions to design declarative languages to represents HTML and CSS, use F# type providers syntax modeled by monads to query data sources as a replacement of SQL and basic F# for the logic to replace JavaScript and C#, Java or PHP.

 

[Coming Soon]