A Tour of Morfa

Example I: Matrix DSL

Providing a natural syntax for matrix operations possibly the most frequently mentioned example of a task where a Domain Specific Language is necessary. Numerous such DSL exist, MATLAB (R) being one.

To be able to embed such Matrix DSL inside a fully-featured general-purpose programming language like Morfa, without having all the "Matrix stuff" built-in, was the main motivation for having Morfa.

In the next sections we will give a test-drive of Morfa DSL creation capabilities explained on this example.

Laying the groundwork: Matrix class

The back-bone of the matrix DSL is, not surprisingly, a Matrix class. We will not plunge into the inner workings of this class, as this is a matter of implementation, and the DSL itself is rather a matter of interface. One might say that the matrix DSL should work with any implementation of Matrix.

Another important thing to point out is that Matrix is always an opt-in concept. Unless specifically asked to be a Matrix, a first-choice collection of numbers (e.g. floating point numbers) will always be an array. So:

var A = [1.2, 3.4];

is an array not a Matrix. As already mentioned in the introduction to the Tour of DSL creation, one of the principle of Morfa's DSL-machinery is protecting its basic language features to always work as expected.

Luckily, the "opting-in" is easy and readable enough to justify this, as you will see in one of the next sections.