After finishing my master’s degree, I applied to several companies I was interested in. During one of the selection processes, the interviewer asked me to do the following exercise:
“Write a stack-based interpreted language (byte code) that includes: literals, arithmetic operations, variables, and control flow primitives. As a bonus, add asynchronous primitives such as fork and await.”
Fortunately, I was already familiar with the assignment because I implemented a statically typed programming language a year ago. Consequently, I decided to take this as a chance and do something different than the rest of the candidates. Spoiler: free monads!
The concept of free monad comes from the field of category theory (CT). The formal definition requires a strong background in CT. So, to keep this pragmatic, we are going to consider the following definition:
For any functor $f$, there exists a uniquely determined monad called the free monad of the functor $f$.
Probably this definition is still too general, so let’s see how this is translated into Haskell. In particular, we are going to base our work on the encoding of free monads of the haskell package free by Edward Kmett et al. The definition is as follows:
Free f is simply a stack of layers
f on top of a value
This will allow us to compose free actions by embedding new layers on the bottom of the stack of layers, which is essential to model domain specific languages.
In practise, the user builds a free monad stacking layers of their domain specific language modelled as a functor. Later, it executes
iter (or its corresponding applicative and monadic version) to interpret and collapse each layer into a single (effectful) value.
In the next section, we will see how to put that into practise.
Solving the assignment
When modeling an embedded domain specific language (eDSL) using free monads, you usually divide the problem in two parts:
- Define your language.
- Define the interpreter for your language.
Defining our eDSL
First, we need to define our language. We are free to model any kind of language, but the type that represents our language must be a functor. Otherwise, our language will not be able to be composed using bind
Here is the sum type representing our byte code language:
Litinstruction to instantiate literals (integer and boolean values).
Writeinstructions to load and write variables, respectively.
BinaryOpinstruction to apply binary operations (+, *, <) on the top values of the stack.
Loopinstruction to execute a set of instructions iteratively based on a certain condition.
Retinstruction to stop the program and return the value on top of the stack.
Awaitinstructions to start and await an asynchronous program, respectively.
Recvinstructions to create a new asynchronous channel, send and receive a value through the channel, respectively. This channel allow to independent asynchronous programs to communicate.
The careful reader may have notice that our type
ByteCodeF has a type parameter
next :: Type. This type parameter is required for
ByteCodeF to be a functor. We can think of
next as the next instruction in our program. In fact, when we interpret the
ByteCode free monad, next will be replaced by the (evaluated) next instruction of our program.
Next, for each constructor of our
ByteCodeF, we need a function that lifts
ByteCode. For example, here is the definition of that function for
Implementing this for each constructor is tedious and error prone. This problem can be solved with metaprogramming. In particular, with template haskell (for an introduction to template haskell see my blog post). Fortunately, this is already implemented in Control.Monad.Free.TH.
Therefore, we are going to use
makeFree to automatically derive all these free monadic actions
which generates the following code
The actual signatures use MonadFree which is an mtl-style class that allow us to compose FreeT with other monad transformers. We were able to monomorphize the return value
MonadFree f m => m ~ Free ByteCodeFthanks to the instance
Functor f => MonadFree f (Free f).
Once we have the basic building blocks of our language, we can start building more complex primitives using monadic composition!
Interpreting our eDSL
Now that we have the building blocks to construct our domain specific programs, it is only remaining to implement an interpreter to evaluate this embedded DSL in our host language Haskell.
The interpreter is usually implemented using iterM:
iterM allow us to collapse our
Free f into a monadic value
m a by providing a function
f (m a) -> m a (usually called an algebra) that operates on a single layer of our free monad. If you are familiar with recursion schemes,
iterm is a specialization of cataA.
This may sound confusing at first, but it is easier than it looks like. So, let’s see how it would look like in our example. First of all, we need to choose the monadic value
m. For our interpreted language, we choose
m ~ Interpreter
Ctx is the current context of our program i.e. the state of the stack and the memory registers.
Then, we specialize
iterM to our example
The last step and usually the most difficult one is to implement the algebra of our DSL
Finally, we combine
algebra to obtain
which allow us to interpret our embedded language in our host language. The result of
interpret can be composed with other effectful programs and it can also be evaluated using
So far, we have built the following components:
- An embedded stack-based language in Haskell with primitives like:
- An interpreter for that language:
Now that we have all the ingredients to create embedded stack-based programs and interpret them, we can put this into practise.
program creates two asynchronous tasks that after a period of time, return the integer
1 through an asynchronous channel. The main program waits for these two asynchronous tasks to finish and outputs the sum of the results of the asynchronous tasks.
The source code from the examples can be found here.
In this post, we have introduced free monads and how they can be used to implement embedded domain specific languages. In particular, we have seen how to embed a stack-based language in Haskell. To see other examples of domain specific languages, we refer the reader to the examples of the free package.
This post only covered a half of the free package. In the next post of this series, we’ll explore the other half: church encoding, applicative free, cofree…