The Dutch mathematician Nicolaas Govert de Bruijn invented these indexes for representing terms of lambda calculus without naming the bound variables. Lets take this lambda expression:
K = λx.λy.x
With de Bruijn indexes it reads, when we use the convention, as here, that the zero de Bruijn index refers to the first enclosing lambda binder:
K = λλ1
The de Bruijn index 1 refers to the second lambda binder enclosing the de Bruijn index. How would one write Prolog code for a substitution, so that we can compute beta reduction such as:
Kab = a
I came up with another take, which is tailored towards linear logic. That means it avoids calling funny_arrow/4 inside substitute/4 every time a binder is visited. Here is a test case:
The source code is as follows. Also correcting the typo in the PDF, but capitalizing that funny_arrow/4 can not only take -1 and 1, but also other values:
Warning the below code is not steadfast!