


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



False. Denotational semantics defines a mapping from syntax domains to functions over semantic domains. These functions may be executed to effect an interpreter.
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
(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)?
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)))
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)
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
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.