2008 spring final - Final Examination Computer Science 312,...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Final Examination Computer Science 312, Cornell University 13 May 2008 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1. True/False [10 pts] (parts a–e) a. A computation in a thunk is evaluated zero or one times. b. In the message-passing approach to concurrency, threads exchange information by storing messages into shared memory references. c. To avoid race conditions when writing a thread-safe ADT, mutators must in general acquire locks, but observers that have no side effects need not. d. Reference counting can fail to collect some unreachable objects. e. When the lambda calculus term MYAPP = λf.λx.xf is applied to two arguments f and x , i.e., ( MYAPP f x ) , the result is the same as the result of applying f to x . 2
Background image of page 2
2. Specification and verification [19 pts] (parts a–d) The following is the interface and a partial implementation of a data abstraction. Unfortunately the implementer was a little confused about the rep invariant and abstraction function, and has gotten off to a bad start. (This code is also reproduced on page 12, which can be detached for easy reference.) signature BAG = sig (* A bag (or multiset) is a unordered collection of elements in which * a given element can appear any number of times. This sig is an * interface for a mutable bag of integers. * Examples: Ø, {0}, {-1, 10, 10} *) type bag (* create() is a new empty bag {} *) val create: unit -> bag (* empty(b) is whether b is empty *) val empty: bag -> bool (* contains(b, x) is whether x is in b *) val contains: bag * int -> bool (* Effects: add(b, x) adds one appearance of x to b. Examples: add({1}, 0) = {0, 1}, add({2}, 2) = {2,2} *) val add: bag * int -> unit (* remove(b,x) is b with one appearance of element x removed. * Examples: After remove(b, 0) with b = {0,0,1}, b = {0, 1}. * After remove(b, 0) with b = {1}, b = {1}. *) val remove: bag * int -> unit end structure Bag :> BAG = struct type bag = {elem: int, count: int} list ref fun create() = ref [] fun empty(b) = case !b of [] => true | _ => false fun contains(b, x) = List.exists (fn ({elem = x’, count = _}) => x = x’) (!b) fun add(b, x) = . .. fun remove(b, x) = if contains(b, x) then b := map (fn ({elem = x’, count = n}) => if x = x’ then {elem = x’, count = n-1} else {elem = x’, count = n}) (!b) else () end 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
(a) [5 pts] Based on the implementations of the operations other than remove , give the rep invariant and abstraction function for this implementation. (b) [7 pts] The add operation is unimplemented. Write a correct implementation using Bag.contains and List.map . (c) [2 pts] Give a test case showing that remove does not work correctly. (d) [5 pts] Suppose you wanted to consider remove correct. How would you need to change the rep invariant? Give a new implementation of empty that works for the new rep invariant. ( contains would also need to be modified, but you don’t have to show that.) 4
Background image of page 4
3. Asymptotic performance [32 pts]
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

2008 spring final - Final Examination Computer Science 312,...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online