Lambda calculus expression evaluation.

108 Views Asked by At

I ran across the following lambda calculus example problem

plusTwo = n.successor(successor n)
4 plusTwo 2 = 10

I'm having trouble understanding how the answer is 10 and not 8.

Would it not break down as follows:

4 n.successor(successor n)2 
4 successor(successor 2) 
4 successor(3) 
4 4
8

Is the answer I'm given incorrect? Am I missing something obvious?

Maybe, the answer comes from calculating with the entire 4 plusTwo 2 expression

4 plusTwo 2 = 8 

n.successor(successor n)8
successor(successor 8)
successor(9)
10
1

There are 1 best solutions below

4
On BEST ANSWER

In the $\lambda$-calculus, application is left-associative, which means that a term of the form $MNL$ must be read as $(MN)L$.

Your first reduction sequence (the one ending in $\underline{8}$) is wrong because at the beginning you read the term $\underline{4} \, \mathrm{plusTwo} \, \underline{2}$ as the term $\underline{4} \, (\mathrm{plusTwo} \, \underline{2})$ (instead of $(\underline{4} \, \mathrm{plusTwo}) \, \underline{2}$), breaking the aforementioned convention.

A correct reduction sequence is the following.

\begin{align} \underline{4} \, \mathrm{plusTwo} \, \underline{2} &= \big(\lambda f.\lambda x.f(f(f(fx)) \big) \mathrm{plusTwo} \, \underline{2} \\ & \to_\beta \big(\lambda x. \mathrm{plusTwo} \, (\mathrm{plusTwo} \,(\mathrm{plusTwo} \, (\mathrm{plusTwo} \, x))) \big) \underline{2} \\ & \to_\beta \mathrm{plusTwo} \, (\mathrm{plusTwo} \, (\mathrm{plusTwo} \, (\mathrm{plusTwo} \, \underline{2}))) \\ &\to_\beta^* \mathrm{plusTwo} \, (\mathrm{plusTwo} \, \underline{6}) \\ &\to_\beta^* \mathrm{plusTwo} \, \underline{8} \\ &\to_\beta^* \underline{10} \end{align} where I used that fact that for every natural number $m$, \begin{align} \mathrm{plusTwo} \, \underline{m} &= (\lambda n . \mathrm{succ} \, (\mathrm{succ} \, n)) \underline{m} \\ &\to_\beta \mathrm{succ} \, (\mathrm{succ} \, \underline{m}) \\ &\to_\beta \mathrm{succ} \, \underline{m+1} \\ &\to_\beta \underline{m+2}. \end{align}


Your second reduction sequence is meaningless, I don't understand the relation between its first line and its second line.