A three-stage program you definitely want to write

Posted on February 14, 2019

Writing programs explicitly in stages gives you guarantees that abstraction will be removed. A guarantee that the optimiser most certainly does not give you.

After spending the majority of my early 20s inside the optimiser, I decided enough was enough and it was time to gain back control over how my programs were partially evaluated.

So in this post I’ll give an example of how I took back control and eliminated two levels of abstraction for an interpreter by writing a program which runs in three stages.

Enter: An applicative interpreter for Hutton’s razor.

Written simply at one level, there are two levels of abstraction which could be failed to be eliminated.

  1. If we statically know the expression we can eliminate Expr.
  2. If we statically know which Applicative then we can remove the indirection from the typeclass.

Using typed Template Haskell we’ll work out how to remove both of these layers.

Eliminating the Expression

First we’ll have a look at how to stage the program just to eliminate the expression without discussion the application fragment. This is a two-stage program.

The eval function takes an expression and generates code which unrolls the expression that needs to be evaluated.

Splicing in eval gives us a chain of additions which are computed at run-time.

$$(eval (Add (Val 1) (Val 2)))
=> 1 + 2

By explicitly separating the program into stages we know that there will be no mention of Expr in the resulting program.

Eliminating the Applicative Functor

That’s good. Eliminating the Expr data type was easy. We’ll have to work a bit more to eliminate the applicative.

In the first stage, we will eliminate the expression in the same manner but instead of producing an Int, we will produce a SynApplicative which is a syntactic representation of an applicative. This allows us to inspect the structure of the program in the second stage and remove that overhead as well.

WithCode is a wrapper which pairs a value with a code fragment which was used to produce that value.

If you notice in the earlier example, this wasn’t necessary when it was known that we needed to persist an Int, as there is a Lift instance for Int. However, in general, not all values can be persisted so using WithCode is more general and flexible, if a bit more verbose.

elimExpr eliminates the first layer of abstraction and returns code which generates a SynApplicative.

In the case for Add we encounter a situation where we would have liked to use nested brackets to persist the value of [|| (+) ||]. Instead you have to lift it to the top level and then persist that identifier.

Next, it’s time to provide an interpreter to remove the abstraction of the applicative. In order to do this, we need to provide a dictionary which will be used to give the interpretation of the applicative commands.

WithCode is necessary again as it will be used to generate a program so it’s necessary to know how to implement the methods.

This interpretation is very boring as it just amounts to replacing all the constructors with their implementations. However, it is exciting that we have guaranteed the removal of the overhead of the applicative abstraction.

Running the Splice

Now that we’ve written two functions independently to to eliminate the two layers, they need to be combined together. This is the birth of our three-stage program.

elim is the combination of elimApplicative and elimExpr. The nested splices indicate that the program is more than two levels.

Using -ddump-splices we can have a look at the program that gets generated.

Test.hs:10:30-59: Splicing expression
    elimExpr (Add (Val 1) (Val 2))
  ======>
    ((Return ((WithCode (+)) codePlus)
        `App` Return ((WithCode 1) (liftT 1)))
       `App` Return ((WithCode 2) (liftT 2)))
Test.hs:10:11-73: Splicing expression
    elimApplicative $$(elimExpr (Add (Val 1) (Val 2))) identityDict
  ======>
    (idAp ((idAp (Identity (+))) (Identity 1))) (Identity 2)

Both steps appear in the debug output with the code which was produced at each step. Notice that we had very precise control over what code was generated and that functions like idAp are not inlined. In this case, the compiler will certainly inline idAp and so on but in general it might be useful to generate code which contains calls to GHC.Exts.inline to force even recursive functions to be inlined once.

Conclusion

In general, splitting your program up into stages is quite difficult so mechanisms like type class specialisation will be easier to achieve. In controlled situations though, staging gives you the guarantees you need.