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` and `drop_while` work as described in [[Week 1's homework|assignment1]].
Using those resources, define a `set_cons` and a `set_equal?` function. The first should take a number argument `x` and a set argument `xs` (implemented as a list of numbers assumed to have no repeating elements), and return a (possibly new) set argument which contains `x`. (But make sure `x` doesn't appear in the result twice!) The `set_equal?` function should take two set arguments `xs` and `ys` 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 the `list_equal?` function you defined in last week's homework.)
Here are some tips for getting started. Use `drop_while`, `num_equal?`, and `empty?` to define a `mem?` function that returns `true` if number `x` is a member of a list of numbers `xs`, else returns `false`. Also use `take_while`, `drop_while`, `num_equal?`, `tail` and `append` to define a `without` function that returns a copy of a list of numbers `xs` that omits the first occurrence of a number `x`, if there be such. You may find these functions `mem?` and `without` useful in defining `set_cons` and `set_equal?`. Also, for `set_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 like `eqset?` in Chapter 7 of *The Little Schemer*, and `set_cons x xs` would be like `(makeset (cons x xs))`, from that same chapter.
* `mem?` and `without` are like the `member?` and `rember` 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 defines `multirember`, which removes all occurrences of a match rather than just the first; and `member*` and `rember*` 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 is `memv`, though that is defined for more than just numbers, and when that `memv` finds a match it returns a *list* starting with the match, rather than `#t`.
ANSWER:
let not_equal? = \n m. neg (num_equal? n m) in
let mem? = \n xs. neg (empty? (drop_while (not_equal? n) xs)) in
let without = \n xs. append (take_while (not_equal? n) xs) (tail (drop_while (not_equal? n) xs)) in
let set_cons = \x xs. (mem? x xs) xs (cons x xs) in
let set_equal? = Y (\set_equal?. \xs ys. (empty? xs)
(empty? ys)
; else when xs aren't empty
((mem? (head xs) ys)
(set_equal? (tail xs) (without (head xs) ys))
; else when head xs not in ys
false))
7. 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`, namely `6`. You can use any Lambda Calculus implementation of lists you like.
ANSWER:
The tricky thing about defining these functions is that it's easy to unwittingly violate the conventions about how we're encoding trees. Thus `Leaf1 ≡ [1]` is a tree, and `[Leaf1, Leaf1, Leaf1]` is also a tree, but `[Leaf1]` is not a tree. (It's a singleton whose content is not a number, but rather a leaf, a list containing a number.) Thus when we are recursively processing `[Leaf1, Leaf1, Leaf1]`, we have to be careful not to just blindly call our function with the tail of our input, since when we get to the end the tail `[Leaf1]` is not itself a tree, on the conventions we've adopted. And our function is likely to rely on those conventions in such a way that it chokes when they are violated.
That understood, here is a recursive implementation of `sum_leaves`:
let singleton? = \xs. num_equal? one (length xs) in
let singleton = \x. cons x empty in
let doubleton = \x y. cons x (singleton y) in
let second = \xs. head (tail xs) in
let sum_leaves = Y (\sum_leaves. \t.
(singleton? t)
(head t)
; else if t is a tree, it contains two or more subtrees
(add
(sum_leaves (head t))
; don't recurse if (tail t) is a singleton
((singleton? (tail t))
(sum_leaves (second t))
; else it's ok to recurse
(sum_leaves (tail t))))) in
...
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.)
ANSWER using right-fold lists:
; are xs strictly longer than ys?
let longer? = \xs ys. neg (leq? (length xs) (length ys)) in
; uncons xs f ~~> f (head xs) (tail xs)
let uncons = \xs f. f (head xs) (tail xs) in
let check = \x p. p (\bool ys. uncons ys (\y ys. pair (and (num_equal? x y) bool) ys)) in
let finish = \bool ys. (empty? ys) bool false in
let list_equal? = \xs ys. (longer? xs ys) false (xs check (pair true (rev ys)) finish) in
let get_fringe = Y (\get_fringe. \t.
; this uses a similar pattern to previous problem
(singleton? t)
t
; else if t is a tree, it contains two or more subtrees
(append
(get_fringe (head t))
; don't recurse if (tail t) is a singleton
((singleton? (tail t))
(get_fringe (second t))
; else it's ok to recurse
(get_fringe (tail t))))) in
Here is some test data:
let leaf1 = singleton 1 in
let leaf2 = singleton 2 in
let leaf3 = singleton 3 in
let t12 = doubleton leaf1 leaf2 in
let t23 = doubleton leaf2 leaf3 in
let alpha = cons leaf1 t23 in
let beta = doubleton t12 leaf3 in
let gamma = doubleton leaf1 t23 in
list_equal? (get_fringe gamma) (get_fringe alpha)
And here are some cleverer implementations of some of the functions used above:
let box = \a. \v. v a in
let singleton? = \xs. xs (\x b. box (b (K true))) (K false) not in
; this function works by first converting [x1,x2,x3] into (true,(true,(true,(K false))))
; then each element of ys unpacks that stack by applying its fst to its snd and itself
; so long as we've not gotten to the end, this will have the result of selecting the snd each time
; when we get to the end of the stack, ((K false) fst) ((K false) snd) (K false) ~~> K false
; after ys are done iterating, we apply the result to fst, which will give us either true or ((K false) fst) ~~> false
let longer? = \xs ys. ys (\y p. (p fst) (p snd) p) (xs (\x. pair true) (K false)) fst in
let shift = \x t. t (\a b c. triple (cons x a) a (pair x))) in
let uncons = \xs. xs shift (triple empty empty (K err_head)) (\a b c. c b) in
...
## 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.
9. Find a fixed point `ξ` for the successor function. Prove it's a fixed
point, i.e., demonstrate that `succ ξ <~~> ξ`.
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 number `n`:
n + inf == inf
n * inf == inf
n ^ inf == inf
leq n inf == true
(Note, though, that with *some* notions of infinite numbers, like [[!wikipedia ordinal numbers]], operations like `+` are defined in such a way that `inf + n` is different from `n + inf`, and does exceed `inf`; similarly for `*` and `^`. With other notions of infinite numbers, like the [[!wikipedia cardinal numbers]], even less familiar arithmetic operations are employed.)
ANSWER: Let `H ≡ \u. succ (u u)`, and `X ≡ H H ≡ (\u. succ (u u)) H`. Note that `X ≡ H H ~~> succ (H H)`. Hence `X` is a fixed point for `succ`.
10. Prove that `add ξ 1 <~~> ξ`, where `ξ` is the fixed
point you found in (1). What about `add ξ 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.
ANSWER: Prove that `add X 1 <~~> X`:
add X 1 == (\m n. n succ m) X 1
~~> 1 succ X
== (\s z. s z) succ X
~~> succ X
Which by the previous problem is convertible with `X`. (In particular, `X ~~> succ X`.) What about `add X 2`?
add X 2 == (\m n. n succ m) X 2
~~> 2 succ X
== (\s z. s (s z)) succ X
~~> succ (succ X)
And we know the inner term will be convertible with `X`, and hence we get that the whole result is convertible with `succ X`. Which we already said is convertible with `X`. We can readily see that `add X n <~~> X` for all (finite) natural numbers `n`.
## Mutually-recursive functions ##
11. (Challenging.) One way to define the function `even?` is to have it hand off
part of the work to another function `odd?`:
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 to `even?`:
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?` and `odd?` 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` and `Y2`, that operated on
a *pair* of functions `h` and `g`, 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` and `Y2`, how would you implement the above
definitions of `even?` and `odd?`?
ANSWER:
let proto_even = \even odd. \n. (zero? n) true (odd (pred n)) in
let proto_odd = \even odd. \n. (zero? n) false (even (pred n)) in
let even = Y1 proto_even proto_odd in
let odd = Y2 proto_even proto_odd in
...

By the definitions of `Y1` and `Y2`, we know that `even <~~> proto_even even odd`, and `odd <~~> proto_odd even odd`. Hence the bound variables `even` and `odd` inside the `proto_...` functions have the values we want. `even` will be bound to (something convertible with) the underlined portion of `proto_even`, and `odd` will be bound to (something convertible with) the underlined portion of `proto_odd`.
12. (More challenging.) Using our derivation of `Y` from [[this week's notes|topics/week4_fixed_point_combinators#deriving-y]] as a model, construct a pair `Y1` and `Y2` 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 into `H` and using `H H` as `h`'s fixed point. We suggested the thought exercise, how might you instead evolve `h` into some `T` and then use `T T T` as `h`'s fixed point. Try solving this problem first. It may help give you the insights you need to define a `Y1` and `Y2`. [[Here are some hints|assignment4_hint]].
ANSWER: One solution is given in the hint. Here is another:
let Y1 = \f g . (\u v . f (u u v)(v v u))
(\u v . f (u u v)(v v u))
(\v u . g (v v u)(u u v)) in
let Y2 = \f g . Y1 g f in
...