From a62a6d29714711535dd7a1eb6a1d0611bf4f739b Mon Sep 17 00:00:00 2001 From: Jim Pryor Date: Tue, 26 Oct 2010 10:44:50 -0400 Subject: [PATCH] ass5: more formatting Signed-off-by: Jim Pryor --- assignment5.mdwn | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/assignment5.mdwn b/assignment5.mdwn index 85ac9a18..fab21e49 100644 --- a/assignment5.mdwn +++ b/assignment5.mdwn @@ -123,56 +123,59 @@ and that "bool" is any boolean. Then we can try the following: [[Hint assignment 5 problem 3]] -Booleans, Church numbers, and Church lists in OCaml ---------------------------------------------------- +Booleans, Church numerals, and v3 lists in OCaml +------------------------------------------------ (These questions adapted from web materials by Umut Acar. See .) -The idea is to get booleans, Church numbers, v3 lists, and -binary trees working in OCaml. +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. - Ï ::= Î± | Ï1 â Ï2 | âÎ±. Ï - e ::= x | Î»x:Ï. e | e1 e2 | ÎÎ±. e | e [Ï ] + types Ï ::= c | 'a | Ï1 â Ï2 | â'a. Ï + expressions e ::= x | Î»x:Ï. e | e1 e2 | Î'a. e | e [Ï] -Recall that bool may be encoded as follows: +The boolean type, and its two values, may be encoded as follows: - bool := âÎ±. Î± â Î± â Î± - true := ÎÎ±. Î»t:Î±. Î»f :Î±. t - false := ÎÎ±. Î»t:Î±. Î»f :Î±. f + bool := â'a. 'a â 'a â 'a + true := Î'a. Î»t:'a. Î»f :'a. t + false := Î'a. Î»t:'a. Î»f :'a. f -(where Ï indicates the type of e1 and e2) +It's used like this: -Note that each of the following terms, when applied to the -appropriate arguments, return a result of type bool. + 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 := âÎ±. Î± â (Î± â Î±) â Î± - zero := ÎÎ±. Î»z:Î±. Î»s:Î± â Î±. z - succ := Î»n:nat. ÎÎ±. Î»z:Î±. Î»s:Î± â Î±. s (n [Î±] z s) + 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 Î±, 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 '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, succ, add. 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: - Ï list := âÎ±. Î± â (Ï â Î± â Î±) â Î± - nilÏ := ÎÎ±. Î»n:Î±. Î»c:Ï â Î± â Î±. n - makeListÏ := Î»h:Ï. Î»t:Ï list. ÎÎ±. Î»n:Î±. Î»c:Ï â Î± â Î±. c h (t [Î±] n c) + Ï list := â'a. 'a â (Ï â 'a â 'a) â 'a + nilÏ := Î'a. Î»n:'a. Î»c:Ï â 'a â 'a. n + makeListÏ := Î»h:Ï. Î»t:Ï list. Î'a. Î»n:'a. Î»c:Ï â 'a â 'a. c h (t ['a] n c) As with nats, recursion is built into the datatype. @@ -186,7 +189,7 @@ Test with simple lists. 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 -- 2.11.0