Docsity

CS 405/505 Exam 2 Sample Questions: Denotational Semantics and Lisp Programming, Exams of Programming Languages

Toggle sidebar
bg1
CS 405/505 EXAM #2 SAMPLE QUESTIONS (SOLUTION)
1. The mathematical model of computation that denotational semantics is based upon is called
lambda calculus.
2. A(n) loop invariant is a condition which is true upon entry to a loop every time the loop
is entered, and is still true at the termination of the loop.
3. True or False. Denotational semantics is a method for defining the semantics of a programming
language by giving an interpreter for that language.
False. Denotational semantics defines a mapping from syntax domains to
functions over semantic domains. These functions may be executed to effect
an interpreter.
4. In pure λ-calculus, integers, Boolean values, lists, etc., must all be represented by λ-expressions.
For example, 0 = λx.λy.y, 1 = λx.λy.x y, 2 = λx.λy.x (x y), etc., and succ =λz .λx.λy.x ((z x)y).
succ is a function which adds 1 to its argument. Evaluate succ 1. Did you get 2? Why or
why not?
succ 1 = (\\z.\\x.\\y.x ((z x) y)) (\\x.\\y.x y) => \\x.\\y.x (((\\x.\\y.x y) x) y)
Technically, the reduction stops here as there are no actual parameters
corresponding to x. However, by inspecting the inside expression, there is
an internal redex which may be reduced further as follows (including
further reductions of internal redexes).
\\x.\\y.x (((\\x.\\y.x y) x) y) => \\x.\\y.x ((\\y.x y) y)
=> \\x.\\y.x ((\\y.x y) y) => \\x.\\y.x (x y) = 2
5. Consider the following Lisp function:
(defun update (store V n)
(prog (currentenv newbinding newstore)
(setq currentenv (cdaddr store))
(setq newbinding (cons (list ’eq ’V (list ’quote V)) (list n)))
(setq newstore (list ’lambda (list ’V) (cons ’cond (cons newbinding currentenv))))
(return newstore)
)
)
Note that the (prog (list of local variables) (exp-1) (exp-2) ... (exp-n) (return
exp)) construction declares a list of local variables used in the expressions with exp being
the value returned. Let S represent the Lisp expression (lambda (V) (cond ((eq V (quote
x)) 4) (t (quote bottom)))). What is the internal list representation that Lisp uses for
S? What is (cdaddr S)?
(((EQ V ’X) 4) (T ’BOTTOM))
pf3
pf4

Partial preview of the text

Download CS 405/505 Exam 2 Sample Questions: Denotational Semantics and Lisp Programming and more Exams Programming Languages in PDF only on Docsity!

CS 405/505 EXAM #2 SAMPLE QUESTIONS (SOLUTION)

  1. The mathematical model of computation that denotational semantics is based upon is called lambda calculus.
  2. A(n) loop invariant is a condition which is true upon entry to a loop every time the loop is entered, and is still true at the termination of the loop.
  3. True or False. Denotational semantics is a method for defining the semantics of a programming language by giving an interpreter for that language.

False. Denotational semantics defines a mapping from syntax domains to functions over semantic domains. These functions may be executed to effect an interpreter.

  1. In pure λ-calculus, integers, Boolean values, lists, etc., must all be represented by λ-expressions. For example, 0 = λx.λy.y, 1 = λx.λy.x y, 2 = λx.λy.x (x y), etc., and succ = λz.λx.λy.x ((z x) y). succ is a function which adds 1 to its argument. Evaluate succ 1. Did you get 2? Why or why not?

succ 1 = (\z.\x.\y.x ((z x) y)) (\x.\y.x y) => \x.\y.x (((\x.\y.x y) x) y)

Technically, the reduction stops here as there are no actual parameters corresponding to x. However, by inspecting the inside expression, there is an internal redex which may be reduced further as follows (including further reductions of internal redexes).

\x.\y.x (((\x.\y.x y) x) y) => \x.\y.x ((\y.x y) y) => \x.\y.x ((\y.x y) y) => \x.\y.x (x y) = 2

  1. Consider the following Lisp function:

(defun update (store V n) (prog (currentenv newbinding newstore) (setq currentenv (cdaddr store)) (setq newbinding (cons (list ’eq ’V (list ’quote V)) (list n))) (setq newstore (list ’lambda (list ’V) (cons ’cond (cons newbinding currentenv)))) (return newstore) ) )

Note that the (prog (list of local variables) (exp-1) (exp-2) ... (exp-n) (return exp)) construction declares a list of local variables used in the expressions with exp being the value returned. Let S represent the Lisp expression (lambda (V) (cond ((eq V (quote x)) 4) (t (quote bottom)))). What is the internal list representation that Lisp uses for S? What is (cdaddr S)?

(((EQ V ’X) 4) (T ’BOTTOM))

Trace the execution of (update S ’x 5), showing the values of currentenv, newbinding, and newstore.

(setq currentenv (cdaddr store)) (((EQ V ’X) 4) (T ’BOTTOM)) (setq newbinding (cons (list ’eq ’V (list ’quote ’x)) (list 5))) ((EQ V ’X) 5) (setq newstore (list ’lambda (list ’V) (cons ’cond (cons newbinding currentenv)))) (LAMBDA (V) (COND ((EQ V ’X) 5) ((EQ V ’X) 4) (T ’BOTTOM)))

  1. Consider the Prolog program which solves the Towers of Hanoi problem. This problem is to move a stack of blocks from one location to another without ever changing the order in which blocks are placed on top of each other. A third stack may be used but only one block may be moved at a time.

hanoi(N) :- move(N, left, center, right).

move(0, X, Y, Z). move(N, X, Y, Z) :- N > 0, M is N - 1, move(M, X, Z, Y), print([X,Y]), move(M, Z, Y, X).

Trace the above program using the query hanoi(3). Show all queries which are generated by the execution and all moves which are printed. You may wish to draw a tree to show this clearly.

Script started on Tue 22 Mar 2005 08:57:15 AM CST redstone% sbp SB-Prolog Version 3. | ?- consult(’hanoi.pro’). yes | ?- trace(hanoi/1). yes | ?- trace(move/4). yes | ?- hanoi(3). (1) Call: hanoi(3)? y no | ?- hanoi(3). (2) Call: hanoi(3)? (3) Call: move(3,left,center,right)? (4) Call: move(2,left,right,center)? (5) Call: move(1,left,center,right)? (6) Call: move(0,left,right,center)? (6) Exit: move(0,left,right,center) [left,center] (7) Call: move(0,right,center,left)? (7) Exit: move(0,right,center,left) (5) Exit: move(1,left,center,right)

  1. Consider the denotational semantics below:

S [[S 1 ; S 2 ]] store = S [[S 2 ]] (S [[S 1 ]] store) S [[V := E]] store = store[E [[E]] store/V] S [[while C loop S end loop ]] store = if C [[C]] store then S [[ while C loop S end loop ]] (S [[S]] store) else store C [[E 1 > E 2 ]] store = if E [[E 1 ]] store > E [[E 2 ]] store then true else false E [[E 1 + E 2 ]] store = E [[E 1 ]] store + E [[E 2 ]] store E [[E 1 − E 2 ]] store = E [[E 1 ]] store − E [[E 2 ]] store E [[I]] store = N [[I]] E [[V]] store = if store[[V]] = ⊥ then > else store[[V]] Assume that N returns the integer value of its argument. Given the initial store (λV.⊥) [1/u] [5/y] [5/z], compute the store which results from evaluating S on the program below, showing all steps used in the evaluation.

while (u > 0) loop u := u - 1; z := z + y end loop

  1. Consider the axioms of assignment, composition, and loop below:

Assignment {P [E/V]} V := E {P}

Composition {P}S 1 {Q}, {Q}S 2 {R} {P}S 1 ; S 2 {R}

Loop {P&B}S{P} {P}while B loop S end loop{P&¬B}

Prove the correctness of the following program segment.

{n = j ∗ (j + 1)/2 & i >= 0} while j! = i loop j := j + 1; n := n + j; end loop {n = j ∗ (j + 1)/2 & i >= 0 & ¬(j! = i)}

Show all steps used in the proof.