Lambda hackulus: machinery for Bohm's theorem

I've been distracting myself recently by doing some untyped-lambda-calculus hackery, so I may as well write it up, because I don't think I've seen this particular kind of hackery described elsewhere, and I'm trying to get better at day-to-day documentation. It's kind of cute and probably not very profound, but if anyone sees connections to other work, please point it out.

The context is here is that I was attempting to understand Bohm's Theorem (a la [1] or [2]), and Twelf is my usual tool for understanding things about lambda calculi. So I started with the bog-standard Twelf encoding of untyped lambda calculus:

exp : type.
lam : (exp -> exp) -> exp.
app : exp -> exp -> exp.


...with the goal in mind of eventually formalizing Bohm's Theorem. (I do still have that goal in mind, by the way, and will hopefully post about it when completed.)

The puzzle is how to write programs over such terms that implement the various meta-level functions in [2], i.e. REPLACE(k, S), SELECT(k, i), and ROTATE(k). Their definitions are given using the convenience of paper notation:

REPLACE(k, S) = λx_0.λx_1...λx_k.S
SELECT(k, i) = λx_0.λx_1...λx_k.x_i
ROTATE(k) = λx_0.λx_1...λx_k.x_k x_0 x_1 ... x_k-1

Looks straightforward, but given our standard binary tree structure of lambda terms, we need to uncover the inductive structure of these n-ary term-formers in order to write the corresponding elf programs. These programs are declared as follows:

replace : nat -> exp -> exp -> type.
select : nat -> nat -> exp -> type.
rotate : nat -> exp -> type.



Replace and select turn out to be pretty straightforward once we define

klam : nat -> (exp -> exp) -> type.
klam/0 : klam 0 ([x] x).
klam/s : klam (s K) ([x] lam [y] (M x))
          <- klam K ([x] M x).



An E for which there is a derivation of klam K ([x] E x) is expression beginning with K lambda bindings and ending in x; that is: klam(k) = [x] lam[x_1]...lam[x_k] x

Now we can write replace and select:


replace : nat -> exp -> exp -> type.
%mode replace +N +E -E'.
replace/i : replace K S (E S)
             <- klam K ([x] E x).


select : nat -> nat -> exp -> type.

select/0 : select (s K) 0 (lam [x0] E x0)

            <- klam K ([x] E x).

select/s : select (s K) (s I) (lam [x0] E)

            <- select K I E.

Rotation is significantly trickier. We can't use our klam trick because the body of the expression actually needs to mention every variable we bind. The inductive structure is also less apparent and, once discovered, annoyingly inside-out.

The first thing we can notice is that the inner-most variable is treated in a distinct way from every other part of the term, so let's try to separate concerns by leaving a hole for the function and building up the application to arguments inductively.

First, a slight digression:

Spine Form

Spine form refers to a way of writing down lambda terms to look more like C functions. Spines are simply lists of expressions, and application instead of consisting of two expressions consists of a head expression and a spine consisting of its arguments. (Usually there are some additional constraints like that the head is a variable or constant and the spine drives the head down to base type, but here we're only concerned with the syntactic structure.)

So in fact if we had encoded the lambda calculus as
sp : type.

exp : type.

nil : sp.

cons : exp -> sp -> sp.

lam : (exp -> exp) -> exp.

app : exp -> sp -> exp.


I expect ROTATE would be easier to write. The consequent code winds up looking a lot like the standard translation from spine form to traditional application.

Okay. So, I said that we're going to construct a term with a hole in it for a function to go eventually. A term with a hole in it is represented by the LF type (exp -> exp), and inhabited by things like ([x:exp] app x x).

I found it helpful to write down the inductive step in isolation. Suppose we have a context M containing a hole for the function, which is that inner variable x_k applied to some of its arguments, and now we want to apply it to one more argument all the way on the inside. Because we're working inside-out, from the last argument to the first one. So we need to go from \x_i...\x_k-1 (... ([HOLE] x_i) ...) x_k-1 to \x_i-1.\x_i...\x_k-1 ( ... (([HOLE] x_i-1) x_i ...) x_k-1, or in Twelf,

extend : (exp -> exp) -> (exp -> exp) -> type.

extend/i : extend ([f] M f) ([f] lam [x] M (app f x)).


(Shortly before this point, I showed the puzzle to William Lovas, who pointed out that my initial version of extend applied things in the wrong order. Thanks to him for that and for working on the remainder of the construction with me.)

Now let's implement the function that does this K times for a given K.

spine : nat -> (exp -> exp) -> type.

spine/0 : spine 0 ([f] f). 

spine/s : spine (s K) C'

           <- spine K C 
           <- extend C C'.


Finally we're ready to deal with creating the innermost lambda binding and applying the function to the spine. Effectively we have [f] \xs f xs, and we need \xs\f.f xs.

jam_in : (exp -> exp) -> exp -> type.

jam_in/lam : jam_in ([f] lam [x] M f x) (lam [x] M' x)

              <- ({x} jam_in ([f] M f x) (M' x)).

jam_in/at  : jam_in ([f] R f) (lam [f] R f).


Putting these pieces together at last lets us define rotation.

rot : nat -> exp -> type.


rot/i : rot K M 

         <- spine K C 

         <- jam_in C M.



[1] http://www.cosc.brocku.ca/~mwinter/Courses/5P05/lambda.pdf
[2] http://www.di.unito.it/~dezani/papers/dgp.pdf

Comments

  1. Hi thank you for the information. i was searching for the same thing your post is very useful to me.
    regards
    Tank Car Loading

    ReplyDelete

Post a Comment

Popular posts from this blog

Reading academic papers while having ADHD

Using Twine for Games Research (Part II)

Using Twine for Games Research (Part III)