In: Computer Science
Given, op: (λ(n) (λ(f) (λ(x) (((n (λ(g) (λ(h) (h (g f))))) (λ(u) x)) (λ(u) u)))))
zero: (λ(f) (λ(x) x))
one: (λ(f) (λ(x) (f x)))
two: (λ(f) (λ(x) (f (f x))))
three: (λ(f) (λ(x) (f (f (f x)))))
i. (4 pt) What is the result of (op one)?
ii. (4 pt) What is the result of (op two)?
iii. (4 pt)What is the result of (op three)?
iv. (3 pt) What computation does op perform?
Understand Before proceed further:
One way of thinking about the Church numeral n, which is often useful when analysing programs, is as an instruction 'repeat n times'. For example, using the PAIR and NIL functions defined below, one can define a function that constructs a (linked) list of n elements all equal to x by repeating 'prepend another x element' n times, starting from an empty list. The lambda term is
λn.λx.n (PAIR x) NIL
By varying what is being repeated, and varying what argument that function being repeated is applied to, a great many different effects can be achieved.
Solution op(i)
Given (λ(f) (λ(x) (f x)))
Because the m-th composition of f composed with the n-th composition of f gives the m+n-th composition of f, addition can be defined as follows:
λm.λn.λf.λx.m f (n f x)
Solution op(ii)
Given (λ(f) (λ(x) (f (f x))))
Again m-th composition of f composed with the n-th composition of f gives the m+n-th composition of f.
λm.λn.λf.λx.m f(f (n f x))
Solution op(iii)
Given (λ(f) (λ(x) (f (f (f x)))))
Here also λm.λn.λf.λx.m f(f (f(n f x)))
Solution op(iv)
op : λn.λf.λx.n (λg.λh.h (g f)) (λu.x) (λu.u)
By convention, the following operation (known as Church booleans) are used for the boolean values TRUE and FALSE:
TRUE := λx.λy.x
FALSE := λx.λy.y
(Note that FALSE is equivalent to the Church numeral zero defined above).
Then, with these two lambda terms, we can define some logic operators (these are just possible formulations; other expressions are equally correct):
AND := λp.λq.p q p
OR := λp.λq.p p q
NOT := λp.p FALSE TRUE
IFTHENELSE := λp.λa.λb.p a b
We are now able to compute some logic functions, for example:
AND TRUE FALSE
≡ (λp.λq.p q p) TRUE FALSE → TRUE FALSE TRUE
≡ (λx.λy.x) FALSE TRUE → FALSE