update links
[lambda.git] / exercises / _assignment4.mdwn
index 4dfd5db..1ef8917 100644 (file)
@@ -16,10 +16,14 @@ find terms `F`, `G`, and `ξ` such that `F ξ <~~> ξ` and `G ξ
 <~~> ξ`.  (If you need a hint, reread the notes on fixed
 points.)
 
+4.  Assume that `Ψ` is some fixed point combinator; we're not telling you which one. (You can just write `Psi` in your homework if you don't know how to generate the symbol `Ψ`.) Prove that `Ψ Ψ` is a fixed point of itself, that is, that `Ψ Ψ <~~> Ψ Ψ (Ψ Ψ)`.
+
+<!-- Proof: YY ~~> Y(YY) ~~> YY(Y(YY)); YY(YY) ~~> YY(Y(YY)) -->
+
 
 ## Writing recursive functions ##
 
-4.  Helping yourself to the functions given below,
+5.  Helping yourself to the functions given below,
 write a recursive function called `fact` that computes the factorial.
 The factorial `n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1`.
 For instance, `fact 0 ~~> 1`, `fact 1 ~~> 1`, `fact 2 ~~> 2`, `fact 3 ~~>
@@ -27,9 +31,13 @@ For instance, `fact 0 ~~> 1`, `fact 1 ~~> 1`, `fact 2 ~~> 2`, `fact 3 ~~>
 
         let true = \y n. y in
         let false = \y n. n in
-        let zero? = \n. n (\p. false) true in
-        let pred = \n f z. n (\u v. v (u f)) (K z) I 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
@@ -38,7 +46,7 @@ For instance, `fact 0 ~~> 1`, `fact 1 ~~> 1`, `fact 2 ~~> 2`, `fact 3 ~~>
 
         fac 4
 
-5.  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|/topics/week3_more_lists_]].
+6.  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|/topics/week3_lists#v5-lists]].
     <!--
     let empty? = \xs. xs (\y ys. false) true in
     let head = \xs. xs (\y ys. y) err in
@@ -46,7 +54,15 @@ For instance, `fact 0 ~~> 1`, `fact 1 ~~> 1`, `fact 2 ~~> 2`, `fact 3 ~~>
     -->
 
         ; all functions from the previous question, plus
-        let num_equal? = ??? in
+        ; `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
@@ -61,7 +77,7 @@ For instance, `fact 0 ~~> 1`, `fact 1 ~~> 1`, `fact 2 ~~> 2`, `fact 3 ~~>
     Here are some tips for getting started. Use `drop_while` and `num_equal?` 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` and `drop_while` 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.
 
 
-6.  Questions about trees.
+7.  Questions about trees.
 
 
 ## Arithmetic infinity? ##
@@ -79,7 +95,7 @@ the successor function, multiplication is defined in terms of
 addition, and exponentiation is defined in terms of multiplication.
 
 
-1.  Find a fixed point `ξ` for the successor function.  Prove it's a fixed
+8.  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
@@ -95,7 +111,7 @@ satisfies the following constraints, for any finite natural number `n`:
 
     (Note, though, that with some notions of infinite numbers, operations like `+` and `*` are defined in such a way that `inf + n` is different from `n + inf`, and does exceed `inf`.)
 
-2. Prove that `add 1 ξ <~~> ξ`, where `ξ` is the fixed
+9. 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
@@ -162,6 +178,6 @@ a *pair* of functions `h` and `g`, as follows:
 definitions of `even?` and `odd?`?
 
                                        
-11. (More challenging.) Using our derivation of `Y` from [[this week's notes|topics/week4_fixed_point_combinators_]] as a model, construct a pair `Y1` and `Y2` that behave in the way described above.
+11. (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`.