From 40b7072437e3fe1c9fecdfec65063aecfbf85cd4 Mon Sep 17 00:00:00 2001
From: Jim Pryor
Date: Tue, 26 Oct 2010 10:26:04 -0400
Subject: [PATCH] Revert "Revert "homework formatting/some de-utf8ing""
This reverts commit a25c09109cd0ae26500eae8d133ef7f82f0f8821.
---
assignment5.mdwn | 243 ++++++++++++++++++++++++++++---------------------------
1 file changed, 122 insertions(+), 121 deletions(-)
diff --git a/assignment5.mdwn b/assignment5.mdwn
index 12ac950a..61096c4e 100644
--- a/assignment5.mdwn
+++ b/assignment5.mdwn
@@ -3,144 +3,144 @@ Assignment 5
Types and OCaml
---------------
-0. Recall that the S combinator is given by \x y z. x z (y z).
- Give two different typings for this function in OCaml.
- To get you started, here's one typing for K:
+0. Recall that the S combinator is given by \x y z. x z (y z).
+ Give two different typings for this function in OCaml.
+ To get you started, here's one typing for K:
- # let k (y:'a) (n:'b) = y;;
- val k : 'a -> 'b -> 'a = [fun]
- # k 1 true;;
- - : int = 1
+ # let k (y:'a) (n:'b) = y;;
+ val k : 'a -> 'b -> 'a = [fun]
+ # k 1 true;;
+ - : int = 1
-1. Which of the following expressions is well-typed in OCaml?
- For those that are, give the type of the expression as a whole.
- For those that are not, why not?
+1. Which of the following expressions is well-typed in OCaml?
+ For those that are, give the type of the expression as a whole.
+ For those that are not, why not?
- let rec f x = f x;;
+ let rec f x = f x;;
- let rec f x = f f;;
+ let rec f x = f f;;
- let rec f x = f x in f f;;
+ let rec f x = f x in f f;;
- let rec f x = f x in f ();;
+ let rec f x = f x in f ();;
- let rec f () = f f;;
+ let rec f () = f f;;
- let rec f () = f ();;
+ let rec f () = f ();;
- let rec f () = f () in f f;;
+ let rec f () = f () in f f;;
- let rec f () = f () in f ();;
+ let rec f () = f () in f ();;
-2. Throughout this problem, assume that we have
+2. Throughout this problem, assume that we have
- let rec omega x = omega x;;
+ let rec omega x = omega x;;
- All of the following are well-typed.
- Which ones terminate? What are the generalizations?
+ All of the following are well-typed.
+ Which ones terminate? What are the generalizations?
- omega;;
+ omega;;
- omega ();;
+ omega ();;
- fun () -> omega ();;
+ fun () -> omega ();;
- (fun () -> omega ()) ();;
+ (fun () -> omega ()) ();;
- if true then omega else omega;;
+ if true then omega else omega;;
- if false then omega else omega;;
+ if false then omega else omega;;
- if true then omega else omega ();;
+ if true then omega else omega ();;
- if false then omega else omega ();;
+ if false then omega else omega ();;
- if true then omega () else omega;;
+ if true then omega () else omega;;
- if false then omega () else omega;;
+ if false then omega () else omega;;
- if true then omega () else omega ();;
+ if true then omega () else omega ();;
- if false then omega () else omega ();;
+ if false then omega () else omega ();;
- let _ = omega in 2;;
+ let _ = omega in 2;;
- let _ = omega () in 2;;
+ let _ = omega () in 2;;
-3. This problem is to begin thinking about controlling order of evaluation.
+3. This problem is to begin thinking about controlling order of evaluation.
The following expression is an attempt to make explicit the
behavior of `if`-`then`-`else` explored in the previous question.
-The idea is to define an `if`-`then`-`else` expression using
+The idea is to define an `if`-`then`-`else` expression using
other expression types. So assume that "yes" is any OCaml expression,
and "no" is any other OCaml expression (of the same type as "yes"!),
and that "bool" is any boolean. Then we can try the following:
"if bool then yes else no" should be equivalent to
- let b = bool in
- let y = yes in
- let n = no in
- match b with true -> y | false -> n
+ let b = bool in
+ let y = yes in
+ let n = no in
+ match b with true -> y | false -> n
-This almost works. For instance,
+ This almost works. For instance,
- if true then 1 else 2;;
+ if true then 1 else 2;;
-evaluates to 1, and
+ evaluates to 1, and
- let b = true in let y = 1 in let n = 2 in
- match b with true -> y | false -> n;;
+ let b = true in let y = 1 in let n = 2 in
+ match b with true -> y | false -> n;;
-also evaluates to 1. Likewise,
+ also evaluates to 1. Likewise,
- if false then 1 else 2;;
+ if false then 1 else 2;;
-and
+ and
- let b = false in let y = 1 in let n = 2 in
- match b with true -> y | false -> n;;
+ let b = false in let y = 1 in let n = 2 in
+ match b with true -> y | false -> n;;
-both evaluate to 2.
+ both evaluate to 2.
-However,
+ However,
- let rec omega x = omega x in
- if true then omega else omega ();;
+ let rec omega x = omega x in
+ if true then omega else omega ();;
-terminates, but
+ terminates, but
- let rec omega x = omega x in
- let b = true in
- let y = omega in
- let n = omega () in
- match b with true -> y | false -> n;;
+ let rec omega x = omega x in
+ let b = true in
+ let y = omega in
+ let n = omega () in
+ match b with true -> y | false -> n;;
-does not terminate. Incidentally, `match bool with true -> yes |
-false -> no;;` works as desired, but your assignment is to solve it
-without using the magical evaluation order properties of either `if`
-or of `match`. That is, you must keep the `let` statements, though
-you're allowed to adjust what `b`, `y`, and `n` get assigned to.
+ does not terminate. Incidentally, `match bool with true -> yes |
+ false -> no;;` works as desired, but your assignment is to solve it
+ without using the magical evaluation order properties of either `if`
+ or of `match`. That is, you must keep the `let` statements, though
+ you're allowed to adjust what `b`, `y`, and `n` get assigned to.
-[[Hint assignment 5 problem 3]]
+ [[Hint assignment 5 problem 3]]
Baby monads
-----------
- Read the lecture notes for week 6, then write a
- function `lift` that generalized the correspondence between + and
- `add`: that is, `lift` takes any two-place operation on integers
- and returns a version that takes arguments of type `int option`
- instead, returning a result of `int option`. In other words,
- `lift` will have type
+Read the lecture notes for week 6, then write a
+function `lift` that generalized the correspondence between + and
+`add`: that is, `lift` takes any two-place operation on integers
+and returns a version that takes arguments of type `int option`
+instead, returning a result of `int option`. In other words,
+`lift` will have type
- (int -> int -> int) -> (int option) -> (int option) -> (int option)
+ (int -> int -> int) -> (int option) -> (int option) -> (int option)
- so that `lift (+) (Some 3) (Some 4)` will evalute to `Some 7`.
- Don't worry about why you need to put `+` inside of parentheses.
- You should make use of `bind` in your definition of `lift`:
+so that `lift (+) (Some 3) (Some 4)` will evalute to `Some 7`.
+Don't worry about why you need to put `+` inside of parentheses.
+You should make use of `bind` in your definition of `lift`:
- let bind (x: int option) (f: int -> (int option)) =
- match x with None -> None | Some n -> f n;;
+ let bind (x: int option) (f: int -> (int option)) =
+ match x with None -> None | Some n -> f n;;
Booleans, Church numbers, and Church lists in OCaml
@@ -151,66 +151,67 @@ Booleans, Church numbers, and Church lists in OCaml
The idea is to get booleans, Church numbers, "Church" lists, and
binary trees working in OCaml.
- Recall from class System F, or the polymorphic Î»-calculus.
+Recall from class System F, or the polymorphic Î»-calculus.
- ÏÂ ::=Â Î±Â |Â Ï1Â âÂ Ï2Â |Â âÎ±.Â Ï
- eÂ ::=Â xÂ |Â Î»x:Ï.Â eÂ |Â e1Â e2Â |Â ÎÎ±.Â eÂ |Â eÂ [ÏÂ ]
+ Ï ::= Î± | Ï1 â Ï2 | âÎ±. Ï
+ e ::= x | Î»x:Ï. e | e1 e2 | ÎÎ±. e | e [Ï ]
- RecallÂ thatÂ boolÂ mayÂ beÂ encodedÂ asÂ follows:
+Recall that bool may be encoded as follows:
- boolÂ :=Â âÎ±.Â Î±Â âÂ Î±Â âÂ Î±
- trueÂ :=Â ÎÎ±.Â Î»t:Î±.Â Î»fÂ :Î±.Â t
- falseÂ :=Â ÎÎ±.Â Î»t:Î±.Â Î»fÂ :Î±.Â f
+ bool := âÎ±. Î± â Î± â Î±
+ true := ÎÎ±. Î»t:Î±. Î»f :Î±. t
+ false := ÎÎ±. Î»t:Î±. Î»f :Î±. f
- (whereÂ ÏÂ indicatesÂ theÂ typeÂ ofÂ e1Â andÂ e2)
+(where Ï indicates the type of e1 and e2)
- NoteÂ thatÂ eachÂ ofÂ the followingÂ terms,Â whenÂ appliedÂ toÂ the
- appropriateÂ arguments,Â returnÂ aÂ resultÂ ofÂ typeÂ bool.
+Note that each of the following terms, when applied to the
+appropriate arguments, return a result of type bool.
- (a)Â theÂ termÂ notÂ thatÂ takesÂ anÂ argumentÂ ofÂ typeÂ boolÂ andÂ computesÂ itsÂ negation;
- (b)Â theÂ termÂ andÂ thatÂ takesÂ twoÂ argumentsÂ ofÂ typeÂ boolÂ andÂ computesÂ theirÂ conjunction;
- (c)Â theÂ termÂ orÂ thatÂ takesÂ twoÂ argumentsÂ ofÂ typeÂ boolÂ andÂ computesÂ theirÂ disjunction.
+(a) the term not that takes an argument of type bool and computes its negation;
+(b) the term and that takes two arguments of type bool and computes their conjunction;
+(c) the term or that takes two arguments of type bool and computes their disjunction.
- TheÂ typeÂ nat (for "natural number") mayÂ beÂ encodedÂ asÂ follows:
+The type nat (for "natural number") may be encoded as follows:
- natÂ :=Â âÎ±.Â Î±Â âÂ (Î±Â âÂ Î±)Â âÂ Î±
- zeroÂ :=Â ÎÎ±.Â Î»z:Î±.Â Î»s:Î±Â âÂ Î±.Â z
- succÂ :=Â Î»n:nat.Â ÎÎ±.Â Î»z:Î±.Â Î»s:Î±Â âÂ Î±.Â sÂ (nÂ [Î±]Â zÂ s)
+ nat := âÎ±. Î± â (Î± â Î±) â Î±
+ zero := ÎÎ±. Î»z:Î±. Î»s:Î± â Î±. z
+ succ := Î»n:nat. ÎÎ±. Î»z:Î±. Î»s:Î± â Î±. s (n [Î±] z s)
- AÂ natÂ nÂ isÂ deï¬nedÂ byÂ whatÂ itÂ canÂ do,Â whichÂ isÂ toÂ computeÂ aÂ functionÂ iteratedÂ nÂ times.Â InÂ theÂ polymorphic
- encodingÂ above,Â theÂ resultÂ ofÂ thatÂ iterationÂ canÂ beÂ anyÂ typeÂ Î±,Â asÂ longÂ asÂ youÂ haveÂ aÂ baseÂ elementÂ zÂ :Â Î±Â and
- aÂ functionÂ sÂ :Â Î±Â âÂ Î±.
+A nat n is deï¬ned by what it can do, which is to compute a function iterated n times. In the polymorphic
+encoding above, the result of that iteration can be any type Î±, as long as you have a base element z : Î± and
+a function s : Î± â Î±.
- **Excercise**: get booleans and Church numbers working in OCaml,
- including OCaml versions of bool, true, false, zero, succ, add.
+**Excercise**: get booleans and Church numbers working in OCaml,
+including OCaml versions of bool, true, false, zero, succ, add.
- ConsiderÂ theÂ followingÂ listÂ type:
+Consider the following list type:
- typeÂ âaÂ listÂ = Nil |Â ConsÂ ofÂ âaÂ *Â âaÂ list
+ type âa list = Nil | Cons of âa * âa list
- WeÂ canÂ encodeÂ ÏÂ lists,Â listsÂ ofÂ elementsÂ ofÂ typeÂ ÏÂ asÂ follows:
+We can encode Ï lists, lists of elements of type Ï as follows:
- ÏÂ listÂ :=Â âÎ±.Â Î±Â âÂ (ÏÂ âÂ Î±Â âÂ Î±)Â âÂ Î±
- nilÏÂ :=Â ÎÎ±.Â Î»n:Î±.Â Î»c:ÏÂ âÂ Î±Â âÂ Î±.Â n
- makeListÏÂ :=Â Î»h:Ï.Â Î»t:ÏÂ list.Â ÎÎ±.Â Î»n:Î±.Â Î»c:ÏÂ âÂ Î±Â âÂ Î±.Â cÂ hÂ (tÂ [Î±]Â nÂ c)
+ Ï list := âÎ±. Î± â (Ï â Î± â Î±) â Î±
+ nilÏ := ÎÎ±. Î»n:Î±. Î»c:Ï â Î± â Î±. n
+ makeListÏ := Î»h:Ï. Î»t:Ï list. ÎÎ±. Î»n:Î±. Î»c:Ï â Î± â Î±. c h (t [Î±] n c)
- AsÂ withÂ nats,Â recursion is built into the datatype.
+As with nats, recursion is built into the datatype.
- WeÂ canÂ writeÂ functions likeÂ map:
+We can write functions like map:
- mapÂ :Â (ÏÂ âÂ ÏÂ )Â âÂ ÏÂ listÂ âÂ ÏÂ list
- :=Â Î»fÂ :ÏÂ âÂ Ï.Â Î»l:ÏÂ list.Â lÂ [ÏÂ list]Â nilÏÂ (Î»x:Ï.Â Î»y:ÏÂ list.Â consÏÂ (fÂ x)Â y
+ map : (Ï â Ï ) â Ï list â Ï list
+ = Î»f :Ï â Ï. Î»l:Ï list. l [Ï list] nilÏ (Î»x:Ï. Î»y:Ï list. consÏ (f x) y
- **Excercise** convert this function to OCaml. Also write an `append` function.
- Test with simple lists.
+**Excercise** convert this function to OCaml. Also write an `append` function.
+Test with simple lists.
- ConsiderÂ theÂ followingÂ simpleÂ binaryÂ treeÂ type:
+Consider the following simple binary tree type:
- typeÂ âaÂ treeÂ = Leaf |Â NodeÂ ofÂ âaÂ treeÂ *Â âaÂ *Â âaÂ tree
+ type âa tree = Leaf | Node of âa tree * âa * âa tree
- **Excercise**
- Write a function `sumLeaves` that computes the sum of all the
- leaves in an int tree.
+**Excercise**
+Write a function `sumLeaves` that computes the sum of all the
+leaves in an int tree.
+
+Write a function `inOrder` : Ï tree â Ï list that computes the in-order traversal of a binary tree. You
+may assume the above encoding of lists; deï¬ne any auxiliary functions you need.
- WriteÂ aÂ functionÂ `inOrder`Â :Â ÏÂ treeÂ âÂ ÏÂ listÂ thatÂ computesÂ theÂ in-orderÂ traversalÂ ofÂ aÂ binaryÂ tree.Â You
- mayÂ assumeÂ theÂ aboveÂ encodingÂ ofÂ lists;Â deï¬neÂ anyÂ auxiliaryÂ functionsÂ youÂ need.
--
2.11.0