3 1. Which of the following expressions is well-typed in OCAML?
4 For those that are, give the type of the expression as a whole.
5 For those that are not, why not?
11 let rec f x = f x in f f;;
13 let rec f x = f x in f ();;
19 let rec f () = f () in f f;;
21 let rec f () = f () in f ();;
23 2. Throughout this problem, assume that we have
25 let rec omega x = omega x;;
27 All of the following are well-typed.
28 Which ones terminate? What are the generalizations?
36 (fun () -> omega ()) ();;
38 if true then omega else omega;;
40 if false then omega else omega;;
42 if true then omega else omega ();;
44 if false then omega else omega ();;
46 if true then omega () else omega;;
48 if false then omega () else omega;;
50 if true then omega () else omega ();;
52 if false then omega () else omega ();;
56 let _ = omega () in 2;;
58 3. The following expression is an attempt to make explicit the
59 behavior of `if`-`then`-`else` explored in the previous question.
60 The idea is to define an `if`-`then`-`else` expression using
61 other expression types. So assume that "yes" is any OCAML expression,
62 and "no" is any other OCAML expression (of the same type as "yes"!),
63 and that "bool" is any boolean. Then we can try the following:
64 "if bool then yes else no" should be equivalent to
69 match b with true -> y | false -> n
71 This almost works. For instance,
73 if true then 1 else 2;;
77 let b = true in let y = 1 in let n = 2 in
78 match b with true -> y | false -> n;;
80 also evaluates to 1. Likewise,
82 if false then 1 else 2;;
86 let b = false in let y = 1 in let n = 2 in
87 match b with true -> y | false -> n;;
93 let rec omega x = omega x in
94 if true then omega else omega ();;
98 let rec omega x = omega x in
102 match b with true -> y | false -> n;;
104 does not terminate. Incidentally, `match bool with true -> yes |
105 false -> no;;` works as desired, but your assignment is to solve it
106 without using the magical evaluation order properties of either `if`
107 or of `match`. That is, you must keep the `let` statements, though
108 you're allowed to adjust what `b`, `y`, and `n` get assigned to.
110 [[Hint assignment 5 problem 3]]