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:
# 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?
let rec f x = f x;;
let rec f x = f f;;
let rec f x = f x in f f;;
let rec f x = f x in f ();;
let rec f () = f f;;
let rec f () = f ();;
let rec f () = f () in f f;;
let rec f () = f () in f ();;
2. Throughout this problem, assume that we have
let rec blackhole x = blackhole x;;
All of the following are well-typed.
Which ones terminate? What are the generalizations?
blackhole;;
blackhole ();;
fun () -> blackhole ();;
(fun () -> blackhole ()) ();;
if true then blackhole else blackhole;;
if false then blackhole else blackhole;;
if true then blackhole else blackhole ();;
if false then blackhole else blackhole ();;
if true then blackhole () else blackhole;;
if false then blackhole () else blackhole;;
if true then blackhole () else blackhole ();;
if false then blackhole () else blackhole ();;
let _ = blackhole 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
behavior of `if`-`then`-`else` explored in the previous question.
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
This almost works. For instance,
if true then 1 else 2;;
evaluates to 1, and
let b = true in let y = 1 in let n = 2 in
match b with true -> y | false -> n;;
also evaluates to 1. Likewise,
if false then 1 else 2;;
and
let b = false in let y = 1 in let n = 2 in
match b with true -> y | false -> n;;
both evaluate to 2.
However,
let rec blackhole x = blackhole x in
if true then blackhole else blackhole ();;
terminates, but
let rec blackhole x = blackhole x in
let b = true 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 |
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]]
Booleans, Church numerals, and v3 lists in OCaml
------------------------------------------------
(These questions adapted from web materials by Umut Acar. See .)
Let's think about the encodings of booleans, numerals and lists in System F, and get datastructures with the same explicit form working in OCaml. (The point... so we won't rely on OCaml's native booleans, integers, or lists.)
Recall from class System F, or the polymorphic λ-calculus.
types τ ::= c | 'a | τ1 → τ2 | ∀'a. τ
expressions e ::= x | λx:τ. e | e1 e2 | Λ'a. e | e [τ]
The boolean type, and its two values, may be encoded as follows:
bool := ∀'a. 'a → 'a → 'a
true := Λ'a. λt:'a. λf :'a. t
false := Λ'a. λt:'a. λf :'a. f
It's used like this:
b [τ] e1 e2
where b is a boolean value, and τ is the shared type of e1 and e2.
**Exercise**. How should we implement the following terms. Note that the result of applying them to the appropriate arguments should also give us a term 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.
The type nat (for "natural number") may be encoded as follows:
nat := ∀'a. 'a → ('a → 'a) → 'a
zero := Λ'a. λz:'a. λs:'a → 'a. z
succ := λn:nat. Λ'a. λz:'a. λs:'a → 'a. s (n ['a] 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 'a, as long as you have a base element z : 'a and a function s : 'a → 'a.
**Excercise**: get booleans and Church numbers working in OCaml,
including OCaml versions of bool, true, false, zero, iszero, succ, and **pred**.
Consider the following list type:
type 'a list = Nil | Cons of 'a * 'a list
We can encode τ lists, lists of elements of type τ as follows:
τ list := ∀'a. 'a → (τ → 'a → 'a) → 'a
nil τ := Λ'a. λn:'a. λc:τ → 'a → 'a. n
make_list τ := λh:τ. λt:τ list. Λ'a. λn:'a. λc:τ → 'a → 'a. c h (t ['a] n c)
As with nats, recursion is built into the datatype.
We can write functions like map:
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.
Also write a **head** function. Also write nil??? Test with simple lists.
Consider the following simple binary tree type:
type 'a tree = Leaf | Node of 'a tree * 'a * 'a tree
**Excercise**
Write a function `sum_leaves` that computes the sum of all the
leaves in an int tree.
Write a function `in_order` : τ 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.
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;;