Fan, A langugage to implement languages (I)

This will be a series of blogs introducing a new programming language Fan.

Fan is OCamlPlus, it provides all features what OCaml provides and a language to manipulate programs. I am also seeking collaboration if you are interested in such a fascinating project.

It aims to provide the OCaml + A Compiler Domain Specific Language. The compiler domain is a bit special, it’s the compiler domain which can be used by users to create their own domain specific languages, e.g, database query, financial modelling. Our purpose is to make you write a practical compiler in one day, yes, this is not a joke, with the right tools and nice abstraction, it’s very promising to help average programmers to create their own languages to fit their domains in a short term.

The compiler domain is a rather large domain, it consists of several sub-domains, so the compiler of Fan itself also benefits from the Domain specific language(DSL). Unlike other bootstrapping model, all features of the previous version of Fan compiler is usable for the next release. Yes, Fan is written using itself, it’s really Fun 🙂

Fan evolved from the Camlp4, but with a more ambitious goal and different underlying engines, I will compare them later.

Ok, let’s talk business.

Why a new programming language? Because I don’t find a programming language make me happy (yet).

Thinking about how you solve a problem.

It’s mainly divided into two steps.

  • The first step is to think of an algorithm to tackle the problem, without ambiguity. This is what we call inherent complexity, however fancy the programming language it is, you still have to think of a way to solve it.
  • The second step is to map your algorithm into your favourite language, i.e, Haskell. Ideally, it should be straightforward, but in reality, it will bring a lot of trouble, and we call it accidental complexity.

What we can do to enhance a programmer’s productivity lies in how to avoid the accidental complexity, the second step.

The problem lies that your favourite language was not designed for your specific domain, it’s a general purpose programming language. When you transfer your ideas into your language, you have to do a lot of dirty work. With the help of modern IDE, people may be alleviated a bit, but programs are not just written to execute, its more functional goal is to help exchange ideas. When you want to understand how a piece of program work, you have to do the reverse-engineering to map your programs back into your ideas. Because when you do the translation from your ideas into your programs, you will lose the big picture, the initial brief ideas are mixed with a lot of noises.

This is a sad fact that how programmers do the work nowadays. 😦

“When you have a hammer, everything is a nail”.

One difference between human being and animals is the fact that man can use tools, the fact that man can not only use tools but also create tools makes human-beings so intelligent. It’s a sad fact that most programmers still live in the cave-age, they can only accept what tools provided. Smart programmers should create a tool which is best fit for their domain.

So, what’s the right way to solve a problem?

When you find some similar problems appear once and again, try to design your language which makes you can express your ideas as isomorphic as possible to the problem’s descriptions, then write a compiler to compile the language. Then it’s done. People who read your program will understand it straight-forward, you write your programs quickly, everything seems to be perfect, everyone is happy.

Wait, you may find that I am cheating, writing a toy-language is not hard, writing a medium language is painful, creating a general purpose language is too hard, and communicating your legacy library with your new language will drive you crazy. So you may say:”let’s forget about it” and shy away.

Yes, that’s true, and that’s why I design a new programming language to address such an issue, remember that creating a language itself is a domain, this domain shares some similar abstractions which should be factored out. And to make life happier, you are extending a general purpose programming language to fit your domain instead of creating a brand new language, and they are compiled into the same intermediate representation, like C# and VB, you never have an inter-operation problem.

Once you finished the language for one domain, your productivity will be boosted exponentially in such a domain.

Fan is created to help you achieve such a goal!

There are different abstraction and DSL solutions, next post I will compare them and talk about the solution Fan chooses and its good and bad effects.