Docsity

Homework 1 Solutions - Evaluation and Environments | CS 421, Assignments of Computer Science

Toggle sidebar
bg1
HW 1 – Evaluation and Environments
CS 421 – Fall 2008
Revision 1.1
Assigned September 16, 2008
Due September 23, 2008, 2:00 pm, in class
Extension 48 hours (20% penalty)
1 Change Log
1.1 Changed strange to weird in the last problem.
1.0 Initial Release.
2 Turn-In Procedure
Your answers to the following questions are to be hand-written, or printed, neatly on one or more sheets of paper, each
with your name in the upper right corner. The homework is to be turned in in class at the start of class. Alternately,
you may hand it to Prof. Elsa Gunter in person before the deadline.
3 Objectives and Background
The purpose of this HW is to test your understanding of
•the order of evaluation of expressions in OCaml;
•the scope of variables, and the state of environments used during evaluation
Another purpose of HWs is to provide you with experience answering non-programming written questions of the
kind you may experience on the midterms and final.
4 Problems
1. (15 pts) Below is a fragment of OCaml code, with various program points indicated by numbers with comments.
let x = "a";;
let y = 8;;
(*1*)
letfxz=x+y+z;;
(*2*)
let z =
let y = "b" in
(*3*)
x ˆ y;;
(*4*)
letgw=fww;;
let y = g y;;
(*5*)
1
pf3
pf4
pf5

Partial preview of the text

Download Homework 1 Solutions - Evaluation and Environments | CS 421 and more Assignments Computer Science in PDF only on Docsity!

HW 1 – Evaluation and Environments

CS 421 – Fall 2008

Revision 1.

Assigned September 16, 2008

Due September 23, 2008, 2:00 pm, in class

Extension 48 hours (20% penalty)

1 Change Log

1.1 Changed strange to weird in the last problem.

1.0 Initial Release.

2 Turn-In Procedure

Your answers to the following questions are to be hand-written, or printed, neatly on one or more sheets of paper, each with your name in the upper right corner. The homework is to be turned in in class at the start of class. Alternately, you may hand it to Prof. Elsa Gunter in person before the deadline.

3 Objectives and Background

The purpose of this HW is to test your understanding of

  • the order of evaluation of expressions in OCaml;
  • the scope of variables, and the state of environments used during evaluation Another purpose of HWs is to provide you with experience answering non-programming written questions of the kind you may experience on the midterms and final.

4 Problems

  1. (15 pts) Below is a fragment of OCaml code, with various program points indicated by numbers with comments.

let x = "a";; let y = 8;; (* 1 ) let f x z = x + y + z;; ( 2 ) let z = let y = "b" in ( 3 ) x ˆ y;; ( 4 ) let g w = f w w;; let y = g y;; ( 5 *)

For each of program points 1, 2, 3, 4 and 5, please describe the environment in effect after evaluation has reached that point. You may assume that the evaluation begins in an empty environment, and that the environment is cumu- lative thereafter. The program points are supposed to indicate points at which all complete preceding declarations have been fully evaluated. In describing the environments 1 through 4, you may use set notation, as done in class, or you may use the update operator +. If you use set notation, no duplicate bindings should occur. The answer for program point 5 should be written out fully in set notation. Solution:

  1. ρ 1 = {y → 8 , x → "a"}
  2. ρ 2 = {f → 〈x 7 → fun z -> x + y + z, ρ 1 〉} + ρ 1 = {f → 〈x 7 → fun z -> x + y + z, {y → 8 , x → "a"}〉, y → 8 , x → "a"}
  3. ρ 3 = {y → "b"} + ρ 2 = {y → "b", f → 〈x 7 → fun z -> x + y + z, {y → 8 , x → "a"}〉, x → "a"}
  4. ρ 4 = {z → "ab"} + ρ 2 = {z → "ab", f → 〈x 7 → fun z -> x + y + z, {y → 8 , x → "a"}〉, y → 8 , x → "a"}
  5. { y → 24 , g → 〈w 7 → f w w, {z → "ab", f → 〈x 7 → fun z -> x + y + z, {y → 8 , x → "a"}〉, y → 8 , x → "a"}〉, z → "ab", f → 〈x 7 → fun z -> x + y + z, {y → 8 , x → "a"}〉, x → "a"}
  6. (20 pts) Below is a fragment of Ocaml code. Describe everything that is displayed on the screen (its observable behavior) after this code has been cut-and-pasted into an interactive Ocaml session, and explain why this behavior is observed. This should include both the type information that the compiler gives back for each declaration, and any other things printed to the screen. For the type information, no explanation is required (but it should be correct). Give explanation for all other things printed, and the order in which they occur.

let f g x = (let r = if (((print_string "a"; x > 2) && (print_string "b"; x > 10)) && (let y = (print_string "c"; 14) in (print_string "d"; x > y) || (print_string "e"; x < 5))) then let z = (print_string "f"; 7) in (print_string "g"; x - z) else let z = (print_string "h"; 15) in (print_string "i"; z) in (g(); r));; let u = (f (fun () -> print_string "j\n") (f (fun () -> print_string "k\n") 3));;

Solution: Printed on the screen is:

val f : (unit -> ’a) -> int -> int =

abhik

abcdfgj val u : int = 8

First, the evaluation of the declaration of the f generates no observable behavior, beside the final printing of its type information, because it immediately evaluates to a closure (〈g 7 → fun x -> (let r =... ), ρinit〉 where ρinit is the environment with which we start. The evaluation of u proceeds as follows:

(fun () -> fun x -> x (^) * 2));; (* 2 ) let double2 = fun () -> (print_string "Calling double2 ... \n"; (fun x -> x (^) * 2));; ( 3 ) let double3 = fun () -> fun x -> (print_string "Calling double3 ... \n"; x (^) * 2);; ( 4 ) let result1 = let s = double1 () in List.map s [1;2;3];; ( 5 ) let result2 = let s = double2 () in List.map s [1;2;3];; ( 6 ) let result3 = let s = double3 () in List.map s [1;2;3];; ( 7 ) let result4 = List.map (double1 ()) [1;2;3];; ( 8 ) let result5 = List.map (double2 ()) [1;2;3];; ( 9 ) let result6 = List.map (double3 ()) [1;2;3];; ( 10 (^) ) let result7 = List.map (fun x -> double1 () x) [1;2;3];; ( 11 (^) ) let result8 = List.map (fun x -> double2 () x) [1;2;3];; ( 12 (^) *) let result9 = List.map (fun x -> double3 () x) [1;2;3];;

Indicate what observable behavior (as described in the previous problem) will occur with the evaluation of each declaration. Explain why the evaluation of each declaration generates the described behavior. You may omit explanations of type information.

Solution: In evaluating the declaration of double1, first printing Calling double1 ..., and then binding double1 to the closure corresponding to (fun () -> fun x -> x (^) * 2), the second expression in the sequence in the body of double1. The other two procedures, double2, and double3 evaluate to closures immediately.

Calling double ... val double1 : unit -> int -> int = val double2 : unit -> int -> int = val double3 : unit -> int -> int =

Evaluating double1 does no printing because there are no calls to print string in its closure (corresponding to (fun () -> fun x -> x * 2)). Therefore, when evaluating the declarations for result1, result4, and result7, mapping double1 () over the list [1; 2; 3], just the resultant list is returned. Thus the observable behavior for result1, result4, and result7 is just the following:

val result1 : int list = [2; 4; 6]

val result4 : int list = [2; 4; 6]

val result7 : int list = [2; 4; 6]

The application of the procedure double2 to its first argument, texttt(), causes it to print "Calling double ... \n" and then to return the closure corresponding to (fun x -> x (^) * 2). In the evaluation of result2, double2 () is evaluated once in the let...in clause and the resulting function is mapped over [1;2;3], the phrase "Calling double2 ...\n" will only be printed once. Thus we have

Calling double ... val result2 : int list = [2; 4; 6]

In the evaluation of result5, the argument (double2 ()) must be evaluated before the application List.map (double1 ()) may be evaluated. The evaluation of (double2 ()) happens only this one time and so, again, the phrase "Calling double2 ...\n" will only be printed once.

Calling double ... val result5 : int list = [2; 4; 6]

The situation changes when we evaluate result8. Here, by lambda lifting the application of result2, List.map is not applied to the result of the application of double2 to (), but rather to the closure corre- sponding to (fun x -> double2 () x). We only execute the print statement after we haev applied this to an integer, which happens once per element in the list. Thus we get:

Calling double ... Calling double ... Calling double ... val result8 : int list = [2; 4; 6]

In the case of double3, the print statement is in the body of the innermost funciton, and hence will not be executed until it has been applied to both its arguments. Thus it will print "Calling double3 ... \n" exactly when it is applied to both () and to an integer, This happens exaclty once for each of the elements in the list [1;2;3] over which double3 () is being mapped. Thus we have:

Calling double ... Calling double ... Calling double ... val result3 : int list = [2; 4; 6]

Calling double ... Calling double ... Calling double ... val result6 : int list = [2; 4; 6]

Calling double ... Calling double ... Calling double ... val result9 : int list = [2; 4; 6]

  1. (Extra Credit) (5 pts) What is the running time of the following Ocaml function, in proportion to the size of the integers input? How can you rewrite this code so that its running time is linear in the size of the input?

let rec weird n = if n <= 0 then 0 else if weird(n - 1) mod 2 = 0 then weird (n - 1) / 2 else weird (n - 1) - 1;;

Solution: The original code was (O(2n) because every step required two recursive calls on input size n. Since these calls were in fact the exact same computations, we can convert this code to linear (O(n)) by moving the common subexpression outside the inner if with a local declaration.

let rec weird n = if n <= 0 then 0 else let r = weird (n - 1) in if r mod 2 = 0 then r / 2 else r - 1;;