/Я language (β)/Tutorials/Lambda calculus interpreter 4/
Full source code ⋅ Previous chapter ⋅ Next chapter
Okay, let's take a look at Instruction covariant functor coupled with a specialised natural transformation:
So we have t' and tt' functors yet to be picked - good news is that once particular t' is known we don't have much choice left!
Our modified lambda Term is either an abstraction or a application - a functor composition of Along (t) and Twice (tt) under Sum in disguise:
To use this monstrously looking source lax co-Kleisli operator we also need to pick (ttt) and (tttt) functors so that these transformations do exist:
Good news is that Along (t) and Twice (tt) currently have only one transformation of this kind each:
How do these operate on values? Let's find out on elementary examples:
Along just copies a value attached to a parameter and Twice attaches an information about each parameter position - memorise it.
Finally, all functors have been chosen, let's create another alias for an extension of Term:
Coming back to this source lax co-Kleisli operator for Instruction from the beginning of this chapter with all unknown functors substituted:
Before we switch to examples, it would be more convenient to use some new patterns, you'll get meaning of each of them shortly:
To see how it works, let's take one of the simplest lambda expressions - identity abstraction (\x.x):
Demonstrating whole AST doesn't make much sense since it left untouched except variables - let's see what happened to them:
\x.x - Regarding context of variable x, it is used in abstraction body with a parameter called x (ABS'VAR).
New let's take a look at traced variables but this time in identity application ((\x.x)y):
Yep, this time we have two variables - x and y with a distinguished context:
(\x.x)y - Regarding context of variable x, it is used in application as a function (APP'FUN), in abstraction body with a parameter called x (ABS'VAR).
(\x.x)y - Regarding context of variable y, it is used in application as an argument (APP'ARG).
Using this type of information attached to every variable we also can say if it's either bound or free and if it's bound - how far binder is.
Yes, we are going to use De Bruijn indices.