A Tour of Morfa

Example II: Symbolic DSL

This example handles another important example of DSL use-case. Making things very simple, symbolic operations are used when we want to manipulate expressions like xy(z - 5), differentiate them wrt. to x, y, z, evaluate and substitute other expressions for the symbols x, y, z.

symbol and expr classes

Again, the basis of the DSL are specific classes the implementations of which we are going to skip. The first is class symbol that denotes a single "unit" of an expression, like an x, the other is class expr which may hold any symbolic expression ranging from values like 1 or 0.5, through symbols x to complex expressions like xy(z - 5).

The main characteristic of the symbol class is that its instances are uniquely identifiable by their names. It may be thought of as a string with some special features.