This is the lambda calculus representation for the AND operator:
lambda(m).lambda(n).lambda (a).lambda (b). m(n a b) b
Can anyone help me in understanding this representation?
This is the lambda calculus representation for the AND operator:
lambda(m).lambda(n).lambda (a).lambda (b). m(n a b) b
Can anyone help me in understanding this representation?
To understand how to represent Booleans in lambda calculus, it helps to think about an IF expression, "if a then b else c". This is an expression which chooses the first branch, b, if it is true, and the second, c, if it is false. Lambda expressions can do that very easily:
will give you the first of its arguments, and
gives you the second. So if a is one of those expressions, then
gives either
b
orc
, which is just what we want the IF to do. So defineand
a b c
will behave likeif a then b else c
.Looking inside your expression at
(n a b)
, that meansif n then a else b
. Thenm (n a b) b
meansThis expression evaluates to
a
if bothm
andn
aretrue
, and tob
otherwise. Sincea
is the first argument of your function andb
is the second, andtrue
has been defined as a function that gives the first of its two arguments, then ifm
andn
are bothtrue
, so is the whole expression. Otherwise it isfalse
. And that's just the definition ofand
!All this was invented by Alonzo Church, who invented the lambda calculus.
In the lambda calculus, a Boolean is represented by a function that takes two arguments, one for success and one for failure. The arguments are called continuations, because they continue with the rest of the computation; the boolean True calls the success continuation and the Boolean False calls the failure continuation. This coding is called the Church encoding, and the idea is that a Boolean is very much like an "if-then-else function".
So we can say
where
s
stands for success,f
stands for failure, and the backslash is an ASCII lambda.Now I hope you can see where this is going. How do we code
and
? Well, in C we could expand it toOnly these are functions, so
BUT, the ternary construct, when coded in the lambda calculus, is just function application, so we have
So finally we arrive at
And if we rename the success and failure continuations to
a
andb
we return to your originalAs with other computations in lambda calculus, especially when using Church encodings, it is often easier to work things out with algebraic laws and equational reasoning, then convert to lambdas at the end.
Actually it's a little more than just the AND operator. It's the lambda calculus' version of
if m and n then a else b
. Here's the explanation:In lambda calculus true is represented as a function taking two arguments* and returning the first. False is represented as function taking two arguments* and returning the second.
The function you showed above takes four arguments*. From the looks of it m and n are supposed to be booleans and a and b some other values. If m is true, it will evaluate to its first argument which is
n a b
. This in turn will evaluate to a if n is true and b if n is false. If m is false, it will evaluate to its second argument b.So basically the function returns a if both m and n are true and b otherwise.
(*) Where "taking two arguments" means "taking an argument and returning a function taking another argument".
Edit in response to your comment:
and true false
as seen on the wiki page works like this:The first step is simply to replace each identifier with its definition, i.e.
(λm.λn. m n m) (λa.λb. a) (λa.λb. b)
. Now the function(λm.λn. m n m)
is applied. This means that every occurrence of m inm n m
is replaced with the first argument ((λa.λb. a)
) and every occurrence of n is replaced with the second argument ((λa.λb. b)
). So we get(λa.λb. a) (λa.λb. b) (λa.λb. a)
. Now we simply apply the function(λa.λb. a)
. Since the body of this function is simply a, i.e. the first argument, this evaluates to(λa.λb. b)
, i.e.false
(asλx.λy. y
meansfalse
).