university of aberdeen session 2008-2009

advertisement
UNIVERSITY OF ABERDEEN
SESSION 2008-2009
Examination in CS4026 (Formal Models of Computation)
19 January 2010.
(15:00-17:00)
Answer three of the following four questions. Please number all parts of your answers clearly.
1. The functional model of programming
(a) Using the conversion rules of lambda calculus, obtain a normal form for each of the following
expressions if one exists, saying which rule is used at each step; if none exists then explain why no normal
form exists.
1.
2.
3.
(λ x.λ f.(f x) + (f (f (x - 1)))) 1 (λ x.x × x )
(λ f. λ h. λ x.f (h x)) (λ y. y + 3) (λ z.z + 1) 2
(x.x)(y.y)
(6)
(b) Please explain how different conversion strategies can be applied to the following formula. (Please
justify each step. It is not necessary to name the different strategies.)
(x.y.yz) ((x.xx)(y.yy))
(4)
(c) Explain how the Haskell program below computes m [3,0,4,2,5]. What does the program do?
m :: [Int] -> [Int]
m [] = []
m (x:xs) = m[y |y <- xs,y <= x] ++ [x] ++
m[y |y <- xs ,y > x]
(6)
(d) The following two programs should compute the same top-level function t. (The first is an actual
program, the second is incomplete.)
t f g h xs v =
[(x+y)|(x,y) <- zip(map(g.f) xs)(map h xs),(x+y)>v]
t f g h xs v =
filter (>v) (1 add (zip (map 2 (map f xs)) (map h xs)))
where add (x,y) = x+y
What should 1, and 2 be to make the two programs compute the same function?
(6)
(e) What is the (polymorphic) type of the Haskell built-in function filter?
(3)
2. The Logic model
(a) Show the clausal form (in Prolog notation, so using Horn Clauses) of the following logic expressions.
(Please number your answers.)
(Y prod(zero,Y,zero)) 
(Y X Z W prod(X,Y,W)  prod(s(X),Y,Z)  sum(Y,W,Z))
(ii) Y X Z ((father(X,Y)  (mother(X,Y))  parent(X,Y) )
(i)
(4)
(3)
(b) The Prolog predicate occ(X,L,N), defined below, should hold if X occurs N times in list L. Below
follows a partial definition of occ. Fill in the blanks indicated by 1 and 2 .
occ(_,[],0).
occ(X,[X|L],N) :- occ(X,L,N1), 1.
occ(X,[Y|L],N) :- \+ X=Y, 2.
(where \+ X=Y indicates that X does not unify with Y)
(6)
(c) Suppose the predicate edge is defined by the following facts:
edge(a,b).
edge(b,c).
edge(c,d).
Explain the behaviour of following two Prolog predicates, highlighting any important differences. If
possible, construct a query that is answered differently by them:
% path1(X,Y) : there exists a path from X to Y
path1(X,Y) :- edge(X,Y).
path1(X,Y) :- path1(X,Z),edge(Z,Y).
% path2(X,Y) : there exists a path from X to Y
path2(X,Y) :- edge(X,Y).
path2(X,Y) :- edge(Z,Y),path2(X,Z).
(6)
(d) State two ways in which the order of the terms inside a Prolog clause can affect
the meaning of the program of which the clause is a part (i.e., its answers to queries)
(6)
3. The imperative Model
(a)
Explain what language the Turing Machine M below accepts/recognises.
(7)
M = (Q, , , , q0, qacc, qrej), where Q = {q0, q1, qacc, qrej},
 = {0, 1},  = {0, 1, B} (where “B” is the blank symbol) and
(q0, 0) = (q1, 0,R)
(q0, 1) = (q0, 1,R)
(q1, 0) = (qrej, 0, R)
(q0, B) = (qrej , B, R)
(q1, 1) = (q1, 1,R)
( q1, B) = (qacc, B, R)
(b) Does this TM ever loop? Explain what this implies for the question whether the TM decides the
language that it accepts/recognises.
(4)
(c) Formally define the states and the transitions of a Turing machine that recognizes the set of all bit
strings (i.e. all elements of {0,1}*) that end with a zero. (You may use formulas or a diagram, as long as
the TM is specified precisely.)
(8)
(d) Construct a Turing Machine, this time using a high-level description, to receive two strings w, w’
{0,1}* as input (separated by a “#” symbol) and accept them if and only if they have the same length.
(6)
4. Computability.
In exploring computability, we shall also draw on your knowledge of Haskell and Prolog.
(a) Consider the following Haskell function. The intended meaning of the program is that
member x xs is true if and only if x is an element of the list xs.
member _ [] = False
member x (y:es)
| x == y
= True
| otherwise
= member x es
Under what conditions does this function always terminate (i.e., halt)?
Justify your answer.
(4)
(b) Suppose you were asked to program, in Haskell, a function f of type [Int] -> Bool such that
f xs = True if and only if the list xs contains the number 1 *exactly* once. Is it possible to do this in
such a way that f will always terminate? Explain your answer, and comment on any differences with the
situation in (a) above (involving the member function). (Hint: consider infinite lists.)
(5)
(c) Consider the set S of all functions f from natural numbers to natural numbers. Please answer the
following questions (and please number your answers accordingly):
i: What is the size (i.e., the cardinality) of this set S? Please justify your answer.
(3)
ii: Consider the set H of all Haskell functions of type Int -> Int. What is the size of H? Please justify. (3)
iii: Can each of the functions in S be programmed in H? Please justify.
(3)
(d) Logical validity for full (First Order) Predicate Logic is not computable (i.e., there cannot exist an
algorithm that tells us correctly for each formula of Predicate Logic whether the formula is valid or not).
Against this background, explain Prolog’s take on proving formulas of Predicate Logic.
(A good answer does not need to be lengthy, but would contain examples of formulas that Prolog cannot
express.)
(7)
[End of exam]
Download