This is the first of a multi-part post about -Calculus. I’m going to start with a description of the Untyped
-Calculus. Future articles will include Church Numerals and Typed
-Calculus.
Why?
-Calculus is important because it has a close tie with functional programming. In fact most functional languages compile to some form of
-Calculus. You could say that
-Calculus is to functional programming what the Turing machine is to imperative programming.
The very basics
Everything is a function in -Calculus but for the purposes of this post we will extend the core calculus with integers and basic integer operations. In a future post we will go into how integers can be represented as functions (for those unwilling to wait, check out Church Numerals).
Lets start with a sample expression:
This is called an abstraction. In this expression we call the x variable bound and the y variable free. There is one other operation called application. Application takes the form of:
-Calculus evaluates expressions by simple substitution so the above expression simplifies to
. One thing to note about this is that in the term
where
is another term, only free occurences of
get replaced.
therefore simplifies to
.
All functions are functions of one variable
-Calculus does not allow abstractions that take multiple arguments. Instead you write functions that return functions which take the next variable.
simplifying this with we get:
For conciseness, can be written as
but it is important to remember that this is a function that takes the value for x and returns a new function.
The Y combinator
It may have occured to you that recursion cannot be implemented with a simple substitution model and there are no looping constructs. How can this be Turing Complete? Recursion can be simulated by using a function such that
(ie. a fixed-point function), assume for the moment that such a function
exists.
If you wanted to represent the factorial function with recursion you could do it as:
Since terms are not named, and recursion is impossible, we have to write a function that takes (Yf) as a parameter.
we’ll call this expression for brevity.
Since:
and f is bound to
giving us recursion!
Now to show the (not-so-magical) fixed point function :
Proof
October 25, 2008 at 7:19 pm
[...] Quick and dirty intro to Lambda Calculus – Post #2 This is a continuatin of my previous post. [...]