Fixed points
Recall that
ω ≡ \x. x x
, andΩ ≡ ω ω
. IsΩ
a fixed point forω
? Find a fixed point forω
, and prove that it is a fixed point.Consider
Ω ξ
for an arbitrary termξ
.Ω
is so busy reducing itself (the eternal narcissist) that it never gets around to noticing whether it has an argument, let alone doing anything with that argument. If so, how couldΩ
have a fixed point? That is, how could there be anξ
such thatΩ ξ <~~> ξ
? To answer this question, begin by constructingY Ω
. Prove thatY Ω
is a fixed point forΩ
.Find two different terms that have the same fixed point. That is, find terms
F
,G
, andξ
such thatF ξ <~~> ξ
andG ξ <~~> ξ
. (If you need a hint, reread the notes on fixed points.)Assume that
Ψ
is some fixed point combinator; we're not telling you which one. (You can just writePsi
in your homework if you don't know how to generate the symbolΨ
.) Prove thatΨ Ψ
is a fixed point of itself, that is, thatΨ Ψ <~~> Ψ Ψ (Ψ Ψ)
.
Writing recursive functions
Helping yourself to the functions given below, write a recursive function called
fact
that computes the factorial. The factorialn! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1
. For instance,fact 0 ~~> 1
,fact 1 ~~> 1
,fact 2 ~~> 2
,fact 3 ~~> 6
, andfact 4 ~~> 24
.let true = \y n. y in let false = \y n. n in let pair = \a b. \v. v a b in let fst = \a b. a in ; aka true let snd = \a b. b in ; aka false let zero = \s z. z in let succ = \n s z. s (n s z) in let zero? = \n. n (\p. false) true in let pred = \n. n (\p. p (\a b. pair (succ a) a)) (pair zero zero) snd in let add = \l r. r succ l in let mult = \l r. r (add l) 0 in let Y = \h. (\u. h (u u)) (\u. h (u u)) in let fact = ... in fact 4
For this question, we want to implement sets of numbers in terms of lists of numbers, where we make sure as we construct those lists that they never contain a single number more than once. (It would be even more efficient if we made sure that the lists were always sorted, but we won't try to implement that refinement here.) To enforce the idea of modularity, let's suppose you don't know the details of how the lists are implemented. You just are given the functions defined below for them (but pretend you don't see the actual definitions). These define lists in terms of one of the new encodings discussed last week.
; all functions from the previous question, plus ; `num_cmp x y lt eq gt` returns lt when x<y, eq when x==y, gt when x>y let num_cmp = (\base build consume. \l r. r consume (l build base) fst) ; where base is (pair (\a b c. b) (K (\a b c. a))) ; and build is (\p. pair (\a b c. c) p) ; and consume is (\p. p fst p (p snd) (p snd)) in let num_equal? = \x y. num_cmp x y false true false in let neg = \b y n. b n y in let empty = \f n. n in let cons = \x xs. \f n. f x xs in let empty? = \xs. xs (\y ys. false) true in let head = \xs. xs (\y ys. y) err in let tail = \xs. xs (\y ys. ys) empty in let append = Y (\append. \xs zs. xs (\y ys. (cons y (append ys zs))) zs) in let take_while = Y (\take_while. \p xs. xs (\y ys. (p y) (cons y (take_while p ys)) empty) empty) in let drop_while = Y (\drop_while. \p xs. xs (\y ys. (p y) (drop_while p ys) xs) empty) in ...
The functions
take_while
anddrop_while
work as described in Week 1's homework.Using those resources, define a
set_cons
and aset_equal?
function. The first should take a number argumentx
and a set argumentxs
(implemented as a list of numbers assumed to have no repeating elements), and return a (possibly new) set argument which containsx
. (But make surex
doesn't appear in the result twice!) Theset_equal?
function should take two set argumentsxs
andys
and say whether they represent the same set. (Be careful, the lists[1, 2]
and[2, 1]
are different lists but do represent the same set. Hence, you can't just use thelist_equal?
function you defined in last week's homework.)Here are some tips for getting started. Use
drop_while
,num_equal?
, andempty?
to define amem?
function that returnstrue
if numberx
is a member of a list of numbersxs
, else returnsfalse
. Also usetake_while
,drop_while
,num_equal?
,tail
andappend
to define awithout
function that returns a copy of a list of numbersxs
that omits the first occurrence of a numberx
, if there be such. You may find these functionsmem?
andwithout
useful in definingset_cons
andset_equal?
. Also, forset_equal?
, you are probably going to want to define the function recursively... as now you know how to do.Some comments comparing this exercise to The Little Schemer, and Scheme more generally:
- The
set_equal?
you're trying to define here is likeeqset?
in Chapter 7 of The Little Schemer, andset_cons x xs
would be like(makeset (cons x xs))
, from that same chapter. mem?
andwithout
are like themember?
andrember
functions defined in Chapter 2 and 3 of The Little Schemer, though those functions are defined for lists of symbolic atoms, and here you are instead defining them for lists of numbers. The Little Schemer also definesmultirember
, which removes all occurrences of a match rather than just the first; andmember*
andrember*
in Chapter 5, that operate on lists that may contain other, embedded lists.- The native Scheme function that most resembles the
mem?
you're defining ismemv
, though that is defined for more than just numbers, and when thatmemv
finds a match it returns a list starting with the match, rather than#t
.
- The
Linguists often analyze natural language expressions into trees. We'll need trees in future weeks, and tree structures provide good opportunities for learning how to write recursive functions. Making use of our current resources, we might approximate trees as follows. Instead of words or syntactic categories, we'll have the nodes of the tree labeled with Church numbers. We'll think of a tree as a list in which each element is itself a tree. For simplicity, we'll adopt the convention that a tree of length 1 must contain a number as its only element.
Then we have the following representations:
. /|\ / | \ 1 2 3 [[1], [2], [3]] . / \ /\ 3 1 2 [[[1], [2]], [3]] . / \ 1 /\ 2 3 [[1], [[2], [3]]]
Some limitations of this scheme: there is no easy way to label an inner, branching node (for example with a syntactic category like VP), and there is no way to represent a tree in which a mother node has a single daughter.
When processing a tree, you can test for whether the tree is a leaf node (that is, contains only a single number), by testing whether the length of the list is 1. This will be your base case for your recursive definitions that work on these trees. (You'll probably want to write a function
leaf?
that encapsulates this check.)Your assignment is to write a Lambda Calculus function that expects a tree, encoded in the way just described, as an argument, and returns the sum of its leaves as a result. So for all of the trees listed above, it should return
1 + 2 + 3
, namely6
. You can use any Lambda Calculus implementation of lists you like.
8. The fringe of a leaf-labeled tree is the list of values at its leaves, ordered from left-to-right. For example, the fringe of all three trees displayed above is the same list, [1, 2, 3]
. We are going to return to the question of how to tell whether trees have the same fringe several times this course. We'll discover more interesting and more efficient ways to do it as our conceptual toolboxes get fuller. For now, we're going to explore the straightforward strategy. Write a function that expects a tree as an argument, and returns the list which is its fringe. Next write a function that expects two trees as arguments, converts each of them into their fringes, and then determines whether the two lists so produced are equal. (Convert your list_equal?
function from last week's homework into the Lambda Calculus for this last step.)
Arithmetic infinity?
The next few questions involve reasoning about Church arithmetic and infinity. Let's choose some arithmetic functions:
succ = \n s z. s (n s z)
add = \l r. r succ l in
mult = \l r. r (add l) 0 in
exp = \base r. r (mult base) 1 in
There is a pleasing pattern here: addition is defined in terms of the successor function, multiplication is defined in terms of addition, and exponentiation is defined in terms of multiplication.
Find a fixed point
ξ
for the successor function. Prove it's a fixed point, i.e., demonstrate thatsucc ξ <~~> ξ
.We've had surprising success embedding normal arithmetic in the Lambda Calculus, modeling the natural numbers, addition, multiplication, and so on. But one thing that some versions of arithmetic supply is a notion of infinity, which we'll write as
inf
. This object sometimes satisfies the following constraints, for any finite natural numbern
:n + inf == inf n * inf == inf n ^ inf == inf leq n inf == true
(Note, though, that with some notions of infinite numbers, like ordinal numbers, operations like
+
are defined in such a way thatinf + n
is different fromn + inf
, and does exceedinf
; similarly for*
and^
. With other notions of infinite numbers, like the cardinal numbers, even less familiar arithmetic operations are employed.)Prove that
add ξ 1 <~~> ξ
, whereξ
is the fixed point you found in (1). What aboutadd ξ 2 <~~> ξ
?
Comment: a fixed point for the successor function is an object such that it
is unchanged after adding 1 to it. It makes a certain amount of sense
to use this object to model arithmetic infinity. For instance,
depending on implementation details, it might happen that leq n ξ
is
true for all (finite) natural numbers n
. However, the fixed point
you found for succ
and (+n)
(recall this is shorthand for \x. add x n
) may not be a fixed point for (*n)
, for example.
Mutually-recursive functions
(Challenging.) One way to define the function
even?
is to have it hand off part of the work to another functionodd?
:let even? = \x. (zero? x) ; if x == 0 then result is true ; else result turns on whether x-1 is odd (odd? (pred x))
At the same tme, though, it's natural to define
odd?
in such a way that it hands off part of the work toeven?
:let odd? = \x. (zero? x) ; if x == 0 then result is false ; else result turns on whether x-1 is even (even? (pred x))
Such a definition of
even?
andodd?
is called mutually recursive. If you trace through the evaluation of some sample numerical arguments, you can see that eventually we'll always reach a base step. So the recursion should be perfectly well-grounded:even? 3 ~~> (zero? 3) true (odd? (pred 3)) ~~> odd? 2 ~~> (zero? 2) false (even? (pred 2)) ~~> even? 1 ~~> (zero? 1) true (odd? (pred 1)) ~~> odd? 0 ~~> (zero? 0) false (even? (pred 0)) ~~> false
But we don't yet know how to implement this kind of recursion in the Lambda Calculus.
The fixed point operators we've been working with so far worked like this:
let ξ = Y h in ξ <~~> h ξ
Suppose we had a pair of fixed point operators,
Y1
andY2
, that operated on a pair of functionsh
andg
, as follows:let ξ1 = Y1 h g in let ξ2 = Y2 h g in ξ1 <~~> h ξ1 ξ2 and ξ2 <~~> g ξ1 ξ2
If we gave you such a
Y1
andY2
, how would you implement the above definitions ofeven?
andodd?
?(More challenging.) Using our derivation of
Y
from this week's notes as a model, construct a pairY1
andY2
that behave in the way described above.Here is one hint to get you started: remember that in the notes, we constructed a fixed point for
h
by evolving it intoH
and usingH H
ash
's fixed point. We suggested the thought exercise, how might you instead evolveh
into someT
and then useT T T
ash
's fixed point. Try solving this problem first. It may help give you the insights you need to define aY1
andY2
. Here are some hints.