I presented the shortest implementation of a normalizer for beta equality for untyped lambda calculus which is based on Normalisation by Evaluation.
We start with the definition of lambda terms using de Bruijn levels:
The reason for using de Bruijn levels is that weakening comes for free. The other side of the coin is that one has to reindex bound variables when substituting. However, this is not an issue because we never do that.
We define values by solving a negative domain equation:
It is easy to define application:
We can evaluate lambda terms in the Value domain using a list of values as the environment:
All we have to do now is to invert evaluation, i.e. to define a function
To be able to do this we have first to extend values with variables. Then to be able to extend app, we also have to represent stuck applications, or neutral values:
The code for eval remains unchanged. Now we can implement quote, though we need an extra parameter to keep track of the free variables:
Now nf just evaluates and quotes the result:
We discussed how to verify such a normalisation function – I suggested to use the partiality monad.