Docsity

CS 401/501 Exam 2: Denotational Semantics and Sample Questions, Exams of Programming Languages

Toggle sidebar
bg1
CS 401/501 EXAM #2 SAMPLE QUESTIONS (SOLUTION)
1. The mathematical model of computation that denotational semantics is based upon is called
lambda calculus.
2. 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.
3. 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)))
pf3
pf4
pf5

Partial preview of the text

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

CS 401/501 EXAM #2 SAMPLE QUESTIONS (SOLUTION)

  1. The mathematical model of computation that denotational semantics is based upon is called lambda calculus.
  2. 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. 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), display([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 Sat 11 Mar 2006 10:07:30 AM CST vulcan6% xsb [xsb_configuration loaded] [sysinitrc loaded]

XSB Version 2.7.1 (Kinryo) of March 5, 2005 [i686-pc-linux-gnu; mode: optimal; engine: slg-wam; gc: indirection; scheduling: local]

| ?- [hanoi]. [Compiling ./hanoi] ++Warning[XSB]: [Compiler] ./hanoi: Singleton variable X in a clause of move/ ++Warning[XSB]: [Compiler] ./hanoi: Singleton variable Y in a clause of move/ ++Warning[XSB]: [Compiler] ./hanoi: Singleton variable Z in a clause of move/ [hanoi compiled, cpu time used: 0.0140 seconds] [hanoi loaded]

yes | ?- trace.

yes [trace] | ?- hanoi(3). (0) Call: hanoi(3)? (1) Call: move(3,left,center,right)? (2) Call: move(2,left,right,center)? (3) Call: move(1,left,center,right)? (4) Call: move(0,left,right,center)? (4) Exit: move(0,left,right,center)? (5) Call: display([left,center])? [left,center] (5) Exit: display([left,center])? (6) Call: move(0,right,center,left)? (6) Exit: move(0,right,center,left)?

  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

Let store 0 = (λV.⊥) [1/u] [5/y] [5/z]. The steps of execution using the denotational semantics are:

(a) S [[while u > 0 loop u := u − 1 ; z := z + y end loop]] store 0 (b) if C [[u > 0 ]] store 0 then S [[while u > 0 loop u := u − 1 ; z := z + y end loop]] (S[[u := u − 1 ; z := z + y]] store 0 ) else store 0 The following steps elaborate C [[u > 0 ]] store 0. i. if E [[u]] store 0 > E [[ 0 ]] store 0 then true else false The following steps elaborate E [[u]] store 0. A. if store 0 [[u]] = ⊥ then ⊤ else store 0 [[V]] B. 1 The following steps elaborate E [[ 0 ]] store 0. A. N [[ 0 ]] B. 0 ii. true

(c) The following steps elaborate S [[u := u − 1 ; z := z + y]] store 0. i. S [[z := z + y]] (S[[u := u − 1 ]] store 0 ) ii. S [[z := z + y]] (store 0 [E [[u − 1 ]] store 0 /u]) iii. S [[z := z + y]] (store 0 [E [[u]] store 0 − E [[ 1 ]] store 0 /u]) The evaluation of these 2 terms is identical to that shown in step b-i. iv. S [[z := z + y]] (store 0 [0/u]) Let store 1 = store 0 [0/u] = (λV.⊥) [1/u] [5/y] [5/z] [0/u]. v. S [[z := z + y]] store 1 vi. store 1 [E [[z]] store 1 + E [[y]] store 1 /z] The evaluation of these 2 terms is identical to that shown in step b-i. vii. Let store 2 = store 1 [10/z] = (λV.⊥) [1/u] [5/y] [5/z] [0/u] [10/z].

(d) S [[while u > 0 loop u := u − 1 ; z := z + y end loop]] store 2

(e) if C [[u > 0 ]] store 2 then S [[while u > 0 loop u := u − 1 ; z := z + y end loop]] (S[[u := u − 1 ; z := z + y]] store 2 ) else store 2 (f) store 2 = (λV.⊥) [1/u] [5/y] [5/z] [0/u] [10/z].