Download Question 1.

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

History of the function concept wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Infinity wikipedia , lookup

Transcript
CSE505
Dr. Lukasz Ziarek
Midterm - 2
First Name
Last Name
Student Number
Directions and Assumptions
1) Please write your name on all pages (top of the page)
2) No notes, phones, books, or material of any kind can be used on the
exam. Failure to follow this rule will result in an automatic 0.
3) There is to be no talking during the exam. Failure to follow this rule will
result in an automatic 0.
4) Please write on both sides of the paper
5) You are given two extra sheets at the end of the exam packet
6) Each question is worth 10 points
7) All code is assumed to be correctly written and will compile
8) Be BRIEF and CONCISE in your answers, more is not always better!
Last Name:
First Name:
1
Question 1.
Consider the following two code snippets from SML programs. Please describe what the type of F
and A must be if the type of B is int. Note you may need to use type variables (i.e. ‘a ‘b ‘c etc. where
precise type constraints are not possible to determine).
Program 1
Program 2
F
F (A
A
B
B)
F: ‘a -> int -> ‘b
F: ‘a -> ‘b
A: ‘a
A: int -> ‘a
2
Last Name:
First Name:
CSE505
Dr. Lukasz Ziarek
Question 2.
Write two functions: producer and consumer that encode two servers in CML. The producer function
will take a channel as an argument and send the natural numbers starting from 0 onto the channel. The
consumer function will take a channel as an argument and receive all of the numbers sent on the
channel. (hint: you will need to create a thread inside each function, much like our reference cell server
we discussed in class. Both servers should be infinite loops.). Use the following CML primitives in your
encodings.
val
val
val
val
channel : unit ->
send : ('a chan *
recv : 'a chan ->
spawn : (unit ->
'a chan
'a) -> unit
'a
unit) -> thread_id
fun producer ch =
let fun loop x = (send(ch, x); loop x+1)
in spawn (fn () => loop 0)
end
fun receiver ch =
let fun loop () = (recv ch; loop ())
in spawn loop
end
Last Name:
First Name:
3
Question 3.
Given the helper functions below, create an infinite list that contains the triangular number
sequence. The nth triangular number is defined as: xn = n(n+1)/2. The first number in the sequence
is 1, the second number would be 3, the third number would be 6, and so on.
datatype 'a inflist = NIL
| CONS of 'a * (unit -> 'a inflist);
fun HD (CONS(a,b)) = a
| HD NIL = raise Subscript;
fun TL (CONS(a,b)) = b()
| TL NIL = raise Subscript;
fun NULL NIL = true
| NULL _ = false;
fun tri x n = CONS(x, fn () => tri ((n*(n+1)) div 2) (n+1));
val infTri = tri 1 2;
or
fun tri n = CONS(((n*(n+1)) div 2), fn () => tri (n+1));
val infTri = tri 1;
4
Last Name:
First Name:
CSE505
Dr. Lukasz Ziarek
Question 4.
Assume you have defined an infinite list of triangular numbers (see Question 3) and stored this
infinite list in a variable called: triInf. Create a new infinite list that has all odd triangular numbers
removed. You may use the helper functions from question 3 and those listed below.
fun FILTER f l = if NULL l
then NIL
else if f (HD l)
then CONS(HD l, fn () => (FILTER f (TL l)))
else FILTER f (TL l);
val evenTri = FILTER even infTri;
Last Name:
First Name:
5
Question 5.
What is the function mystery computing? What is the type of this function? Explain your answer!
You can assume the function will only be called with positive integers supplied as arguments.
(hint: calculate what this function would produce when passing in 1, 2, 3, and 4).
fun mystery 0 = (1, 0)
| mystery 1 = (1, 1)
| mystery (n) =
let val (a, b) = mystery (n-1)
in
(a+b, a)
end
val
val
val
val
val
mystery = fn
it = (1,1) :
it = (2,1) :
it = (3,2) :
it = (5,3) :
: int
int *
int *
int *
int *
-> int * int
int
int
int
int
we see that mystery is returning the nth and n-1th Fibonacci numbers
6
Last Name:
First Name:
CSE505
Dr. Lukasz Ziarek
Question 6.
Describe the difference between weak and strong atomicity.
See solution to midterm 1
Describe the difference between serializable and serial.
See solution to midterm 1
Last Name:
First Name:
7
Question 7.
Consider the code below, what are the possible values of z the future can witness in its read from
the reference z? Explain your answer.
Initially: x=0, z=0
Thread 1
let fun f() = !z
val tag = future(f)
in x := 1; touch(tag)
end
Thread 2
(if !x = 1
then z := 2
else z := 3; z := 4)
The future can see 0, 3, or 4 for z, but cannot see 2. It cannot see
two as the continuation is the only computation that sets x to 1,
which orders thread 2’s write to z after the continuation
8
Last Name:
First Name:
CSE505
Dr. Lukasz Ziarek
Question 8.
Can a future witness writes performed by a thread spawned in the future’s continuation? Explain
your answer.
No, the creation of the thread occurs logically after the future.
Can a continuation witness writes performed by a thread spawned in the continuation’s future?
Explain your answer.
Yes: the continuation is obligated to see the creation of the thread. It may or may not be able to see
the writes this thread performs, depending on scheduling constraints.
Last Name:
First Name:
9
Question 9.
What can go wrong in the following code if we are not careful in our implementation of the atomic
keyword? Explain your answer with respect to the problem of privatization.
Thread 1
atomic{
t1 = head;
if(t1)
t1->x = t1->y = 1
}
Thread 2
atomic{
t2 = head;
head = t2->next;
t2->next = NULL;
}
priv = t2->x
…
assert(priv == t2->y);
Please see tic-stm.pptx slide #40
10 Last Name:
First Name:
CSE505
Dr. Lukasz Ziarek
Extra Space For Your Answers
Last Name:
First Name:
11
Extra Space For Your Answers
12 Last Name:
First Name: