#| (include-book "m1-lemmas") |# (in-package "M1") ; Here is a function to compute the square a natural number ; without using multiplication: (defun square (n) (if (zp n) 0 (+ n n -1 (square (- n 1))))) ; Problem 7-1: Fill in the blank below with an M1 program that ; computes the square of a natural number by iterative addition ; and halts with (square n) on the stack, provided n is local ; 0. You may not use the JVM IMUL instruction. You may assume n ; is a natural number. (defconst *square-program* ...) ; Problem 7-2: Prove *square-program* correct. ; Here is a function to compute the cube of a natural number: (defun cube (n) (if (zp n) 0 (+ (* 3 n (- n 1)) 1 (cube (- n 1))))) ; Problem 7-3: Fill in the blank below with an M1 program that ; computes the cube of a natural number by iterative addition and ; multiplication and halts with (cube n) on the stack, provided ; than n is local 0. You may assume n is a natural number. (defconst *cube-program* ...) ; Problem 7-4: Prove *cube-program* correct. ; Here is the definition of the Fibonacci function: ; fib(n) = 1 if n = 0 or n = 1 ; fib(n-1) + fib(n-2) if n is a natural number > 1. ; Problem 7-5: Fill in the blank below with an M1 program that ; halts with the nth Fibonacci number on the stack, provided n is ; local 0. You may assume n is a natural number. (defconst *fib-program* ...) ; Problem 7-6: Prove *fib-program* correct. Note: this is a hard ; problem! Pay close attention to your scheduler function and ; carefully state the theorem about the loop. Proving the ; equivalence of the recursive and iterative Fibonacci functions ; also requires some thought. You may need to use the updated ; version of the M1 compiler which supports if expressions and ; testing with =. This version is contained in ; problem-set-4-answers.lisp.