; CS378 A Formal Model of the Java Virtual Machine ; J Strother Moore ; Midterm ; 3:30 - 5:00 pm, Thursday, March 8, 2007 ; There are 5 problems, worth a total of 100 points. Be sure to write your name ; on your answers! ; Implicitly below I am operating in the following environment: (include-book "m1-lemmas") (in-package "M1") ; You may assume without proof all the lemmas we've proved in class. If you ; don't remember the names, just write down the formulas. ; Problem 1 (10 points) ; Define the function execute-DUP_X1 to extend M1 with the following ; new instruction ; Format: (DUP_X1) ; Stack: ..., v,w => ..., w,v,w ; Description: ; Duplicate the top value on the stack and insert the duplicated value two ; values down ; Note: To actually extend M1 we would have to change do-inst also, but that is ; not necessary here. By the way, DUP_X1 is a real JVM instruction. ; Answer 1: (defun execute-DUP_X1 (inst s) (declare (ignore inst)) (make-state (+ 1 (pc s)) (locals s) (push (top (stack s)) (push (top (pop (stack s))) (push (top (stack s)) (pop (pop (stack s)))))) (program s))) ; Problem 2 (30 points) ; Suppose popn1 and popn2 are defined as shown: (defun popn1 (n s) (if (zp n) s (popn1 (- n 1) (pop s)))) (defun popn2 (n s) (if (zp n) s (pop (popn2 (- n 1) s)))) ; Prove (defthm popn1-is-popn2 (equal (popn1 n s) (popn2 n s))) ; You may give an abbreviated proof, but show the abbreviated proofs of any ; lemmas you need. ; Answer 2: (defthm popn2-pop (equal (popn2 n (pop s)) (pop (popn2 n s))) :hints (("Goal" :induct (popn2 n s)))) (defthm popn1-is-popn2 (equal (popn1 n s) (popn2 n s)) :hints (("Goal" :induct (popn1 n s)))) ; Problem 3 (20 points) ; Write an M1 program, ; (defconst *prog* ; '( ; ... ; )) ;that takes a natural number (a nonnegative integer) n in its 0th local and a ;stack with at least n integers on it and halts after popping n items off the ;stack. Hint: to pop one item off the stack, think about IFLE. ; Answer 3 (defconst *prog* '((iload 0) (ifle 7) (iload 0) (iconst -1) (iadd) (istore 0) (ifle 1) (goto -7) (halt))) ; Problem 4 (20 points) ; Write the schedule function, sched, to drive your program *prog* to its HALT ; statement for an arbitrary given natural number n. ; Answer 4: (20 points) (defun sched (n) (if (zp n) (repeat 0 3) (app (repeat 0 8) (sched (- n 1))))) ; Problem 5 (20 points) ; Write the additional definitions and theorems required to give ; the abbreviated proof of ; (implies (natp n) ; (equal (stack ; (run (sched n) ; (make-state 0 (list n) stack *prog*))) ; (popn2 n stack))) ; You may assume the theorem of Problem 2 is available. ; Answer 5: (defthm main-lemma (implies (natp n) (equal (run (sched n) (make-state 0 (list n) stack *prog*)) (make-state 8 (list 0) (popn1 n stack) *prog*))) :hints (("Goal" :induct (popn1 n stack)))) (defthm main (implies (natp n) (equal (stack (run (sched n) (make-state 0 (list n) stack *prog*))) (popn2 n stack))))