X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=assignment5.mdwn;h=85ac9a180a6d29cba48e9200aa68d507cd476df5;hp=61096c4e7a2b34a344e02d6755fe1e53f8bd30b1;hb=ae46ef3c2cb5de4b994fc7df4a95055d03c6b867;hpb=17eb4f0a0146d06ef52c2532405f6805cbaef0ec diff --git a/assignment5.mdwn b/assignment5.mdwn index 61096c4e..85ac9a18 100644 --- a/assignment5.mdwn +++ b/assignment5.mdwn @@ -35,38 +35,38 @@ Types and OCaml 2. Throughout this problem, assume that we have - let rec omega x = omega x;; + let rec blackhole x = blackhole x;; All of the following are well-typed. Which ones terminate? What are the generalizations? - omega;; + blackhole;; - omega ();; + blackhole ();; - fun () -> omega ();; + fun () -> blackhole ();; - (fun () -> omega ()) ();; + (fun () -> blackhole ()) ();; - if true then omega else omega;; + if true then blackhole else blackhole;; - if false then omega else omega;; + if false then blackhole else blackhole;; - if true then omega else omega ();; + if true then blackhole else blackhole ();; - if false then omega else omega ();; + if false then blackhole else blackhole ();; - if true then omega () else omega;; + if true then blackhole () else blackhole;; - if false then omega () else omega;; + if false then blackhole () else blackhole;; - if true then omega () else omega ();; + if true then blackhole () else blackhole ();; - if false then omega () else omega ();; + if false then blackhole () else blackhole ();; - let _ = omega in 2;; + let _ = blackhole in 2;; - let _ = omega () in 2;; + let _ = blackhole () in 2;; 3. This problem is to begin thinking about controlling order of evaluation. The following expression is an attempt to make explicit the @@ -104,15 +104,15 @@ and that "bool" is any boolean. Then we can try the following: However, - let rec omega x = omega x in - if true then omega else omega ();; + let rec blackhole x = blackhole x in + if true then blackhole else blackhole ();; terminates, but - let rec omega x = omega x in + let rec blackhole x = blackhole x in let b = true in - let y = omega in - let n = omega () in + let y = blackhole in + let n = blackhole () in match b with true -> y | false -> n;; does not terminate. Incidentally, `match bool with true -> yes | @@ -123,32 +123,12 @@ and that "bool" is any boolean. Then we can try the following: [[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 - - (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`: - - 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 --------------------------------------------------- (These questions adapted from web materials by Umut Acar. See .) -The idea is to get booleans, Church numbers, "Church" lists, and +The idea is to get booleans, Church numbers, v3 lists, and binary trees working in OCaml. Recall from class System F, or the polymorphic λ-calculus. @@ -215,3 +195,23 @@ 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; define any auxiliary functions you need. +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 + + (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'`: + + let bind' (x: int option) (f: int -> (int option)) = + match x with None -> None | Some n -> f n;; + +