CS / CE 4337 Sample Final Exam

Name:
Section:
CS / CE 4337 Sample Final Exam
Instructor: Brian W. DeVries
May 2, 2013
Instructions: Read the following instructions before beginning the exam.
• Before beginning, write your name at the top of each page of the exam. Also
write your section number on this page. This will be worth points — if you cannot
read a specification, how can you expect to implement it?
• Answer each question in the space provided. Additional paper is available upon request.
Please write your name at the top of these pages as well, and clearly indicate
what question you are answering.
• Please write legibly and clearly! No credit can be given for unreadable answers.
• Don’t panic! You have more than enough time to complete the exam. If you feel yourself
becoming overwhelmed, sit back for a minute and relax. Trying to throw down an answer
in a hurry is an easy way to make mistakes and lose points.
• By the same token, if you finish early, don’t rush out the door. Relax for a minute, then
go back through and check your answers. In particular, be on the lookout for careless
mistakes.
• Unless explicitly stated, you should not need to make any assumptions in order to answer
the questions. If you feel that a question is unclear, then please raise your hand.
Name:
CS / CE 4337
Midterm Exam
March 6, 2013
Prolog and Logic Programming
1. Consider the following logic program:
/**
* edge( Source, Weight, Destination )
*
* A collection of facts documenting the edge relation of a graph. Note that
* the set of vertices is implicit.
*/
edge( a, 5, b ).
edge( b, 3, a ).
/**
* path( Source, Weight, Destination )
*
* The path relation for the above graph (i.e., the transitive closure of the
* edge relation).
*/
path( X, W, Y ) :edge( X, W, Y ).
path( X, W, Y ) :edge( X, W1, Z ),
path( Z, W2, Y ),
W #= W1 + W2.
Recall our discussion of the semantics of logic programs: we take the set of things we know to
be true (call it Sk ), substitute them in for the bodies of the clauses in our programs, and see
what we can prove true at that point (call it Sk+1 ) based on whether the body of the clause is
satisfied. We continue doing this until we reach a fixed point.
With that in mind, answer the following questions:
(a) Given that all of these predicates are inductive, where do we begin this process. In other
words, what is S0 ?
(b) What is S1 ?
Page 1 of 8
Name:
CS / CE 4337
Midterm Exam
March 6, 2013
(c) What is S2 ?
(d) What is S3 ?
(e) By repeating this process ad infinitum, will we arrive at a least fixed point or a greatest
fixed point? In other words, is induction a least fixed point or a greatest fixed point
semantics?
Page 2 of 8
Name:
CS / CE 4337
Midterm Exam
March 6, 2013
(f) Consider the query
?- path( a, W, Y ), W = 10.
What would happen if this query were entered at the prompt? Why?
(g) What effect would tabling path / 3 have on this behavior?
Page 3 of 8
Name:
CS / CE 4337
Midterm Exam
March 6, 2013
2. Consider the following logic program:
p :- q.
q :- r.
r :- p.
s :- t.
t.
Answer the following questions:
(a) What propositions hold under an inductive interpretation?
(b) What propositions hold under a coinductive interpretation?
Page 4 of 8
Name:
CS / CE 4337
Midterm Exam
March 6, 2013
Haskell Questions
3. Recall the following higher-order functions in Haskell:
-- Function composition
(.) :: ( b -> c ) -> ( a -> b ) -> a -> c
f . g = \ x -> f ( g x )
map :: ( a -> b ) -> [ a ] -> [ b ]
map fn []
= []
map fn ( x : xs ) = ( fn x ) : ( map fn xs )
foldl :: ( a -> b -> a ) -> a -> [ b ] -> a
foldl fn x []
= x
foldl fn x ( y : ys ) = foldl fn ( fn x y ) ys
flip :: ( a -> b -> c) -> b -> a -> c
flip fn x y = fn y x
Suppose we have the following functions:
foo :: Integer -> Integer
foo x = 2 * x + 1
bar = ( foldl (+) 0 ) . ( map foo )
(a) What is the type of bar?
(b) Give two example inputs to bar and their corresponding outputs.
(c) What does this function do?
Page 5 of 8
Name:
CS / CE 4337
Midterm Exam
March 6, 2013
4. Take the following exponent function:
(^) :: ( Num a, Integral b ) => a -> b -> a
-- Examples:
4 ^ 2
-- = 16
5.3 ^ 4 -- = 789.0481
4 ^ 5.3 -- Type error
Write a single-line Haskell function prodSq that uses the higher-order functions in the previous
problem to compute the product of the squares of a list of numbers. In other words, you should
not write a recursive function to do this.
Provide the type signature of sumSq. Your type signature should be as general as possible; in
particular, it does not contain the type Integer, Int, etc.
Hint: Be careful of the ordering of the arguments of (^)!
Page 6 of 8
Name:
CS / CE 4337
Midterm Exam
March 6, 2013
T-Diagrams
5. Describe the following diagrams:
(a)
(b)
(“Py” is short for Python)
(c)
Page 7 of 8
Name:
CS / CE 4337
Midterm Exam
March 6, 2013
6. Suppose I have the following compiler. (NOTE: You should know the name for this sort
of compiler!)
Suppose I have the following tools available:
(Here, “Hs” is short for Haskell.)
(a) What program could I write that would bootstrap this compiler on the x86 architecture?
(There are two possible answers.) Draw its T-diagram.
(b) Draw the T-diagram for bootstrapping our Imp compiler using your choice above.
Page 8 of 8