; Problem Set 1 ; The bulk of this problem set asks you to define some ACL2 functions. I do ; not particularly want you to use the mechanized version of ACL2 to check your ; answers, but you are welcome to do so if you wish. If you do use the mechanized ; system you will find that some of the function symbols I ask you to define are ; already defined and the definitions are not always the ones I ask for below! ; We will eventually learn how to use Common Lisp's ``package system'' to ; avoid name conflicts; in the meanwhile, you can avoid name conflicts manually, ; by choosing different names when you test your proposed definitions. ; Answers should be typed and submitted by 3:30 pm Tuesday, Jan 23, 2007. To ; submit your answers put all your answer files in a directory, myname-asgi, ; where myname is your login name and i is the assignment number: 1, 2, .... ; (Typically all your answers will be in a single file.) ; Then execute on a CS Unix machine: ; turnin --submit alexsp cs378 myname-asgi ; To check your submissions: ; turnin --list alexsp cs378 ; In my presentation of the problems below, when I write ; expr ==> value ; I mean that the expression expr evaluates to value. For example, I might ; write: ; (+ 2 2) ==> 4. ; In defining the required functions, limit yourself to the following ; ACL2 forms and functions: ; expression meaning ; (defun fn (v1 ... vn) Define fn to take the formal arguments v1, ..., vn, ; body) and to return the value of body (evaluated in an ; environment where the vi are bound to the values ; of the actuals). Thus, if I define ; (defun sq (x) (* x x)) ; then (sq 7) ==> 49. ; (let ((v1 t1) Evaluate each of the ti. Then let the vi have those ; ... respective values. In that environment, evaluate and ; (vn tn)) return the value of body. ; body) ; (let* ((v1 t1) Evaluate t1 and let v1 have that value. Then ... ; ... then evaluate tn and let vn have that value. In ; (vn tn)) that environment, evaluate and return body. ; body) ; [Note: the difference between let and let* is that in the former the ; variables get their values ``in parallel'' and in the latter they get them ; ``in sequence.'' For example, suppose x has the value 1 in the ``global ; environment.'' Then ; (let ((x 5) (y (+ 1 x))) (* x y)) ==> 10 ; but ; (let* ((x 5) (y (+ 1 x))) (* x y)) ==> 30 ; end of note] ; (cond (p1 x1) Evaluate p1 and if non-nil, evaluate and return x1; ; (p2 x2) else, evaluate p2 and if non-nil, evaluate and return x2; ; ... ... ; (t xk)) else, evaluate and return xk. ; [Note: The cond above is just an abbreviation for ; (if p1 x1 (if p2 x2 ... xk)).] ; (case x Evaluate x and and if the value is literally ; (sym1 y1) sym1, evaluate and return y1; else if the value is ; (sym2 y2) sym2, evaluate and return y2; else if the value is ; ... ... ; (otherwise yk)) else, evaluate and return yk. ; [Note: The symi MUST be symbols. Note that they are not evaluated! ; The case above is just an abbreviation for: ; (cond ((equal x 'sym1) y1) ; ((equal x 'sym2) y2) ; ... ; (t yk)).] ; (and p1 ... pn) t or nil depending on whether all the pi are non-nil. ; (or p1 ... pn) t or nil depending on whether at least one pi is non-nil. ; (not p1) t if p1 is nil and nil if p1 is t. ; (implies p1 p2) t if either p1 is nil or p2 is t. ; [Note: The descriptions above are right only if the pi are Boolean valued. ; If the pi can return values other than t or nil, the descriptions are more ; complicated. I recommend not testing non-Boolean expressions!] ; (cons x y) the ordered pair whose left component (car) is x and ; whose right component (cdr) is y ; (consp x) t or nil indicating whether x is an ordered pair ; (car x) the left component of the ordered pair x; or ; nil if x is not an ordered pair. ; (cdr x) the right component of the ordered pair x; or ; nil if x is not an ordered pair. ; (list x1 ... xn) same as (cons x1 (cons x2 ... (cons xn nil))) ; (list* x1 ... xn) same as (cons x1 (cons x2 ... (cons xn-1 xn))) ; (endp x) same as (not (consp x)) ; (atom x) same as (not (consp x)) ; (symbolp x) t or nil indicating whether x is a symbol ; (keywordp x) t or nil indicating whether x is a keyword symbol ; (natp x) t or nil indicating whether x is a natural number ; (nonnegative integer) ; (if x y z) if x is t, then y, else z; ; [Note: The description is more complicated if x is not Boolean.] ; (equal x y) t or nil indicating whether x and y are the same ; (zp x) t or nil indicating whether x is 0 or not a natural ; number. Thus, (zp -3) ==> t. ; (+ x1 ... xn) sum of the xi ; (* x1 ... xn) product of the xi ; (- x1 x2) difference of x1 take away x2 ; (/ x1 x2) rational quotient of x1 divided by x2 ; (mod x1 x2) x1 mod x2, e.g., (mod 23 3) ==> 2 ; (expt x1 x2) x1 raised to the power x2 ; (< x1 x2) t or nil indicating whether x1 is less than x2 ; (<= x1 x2) t or nil indicating whether x1 is less than or ; equal to x2 ; (> x1 x2) t or nil indicating whether x1 is greater than x2 ; (>= x1 x2) t or nil indicating whether x1 is greater than or ; equal to x2 ; [Note: In the arithmetic functions, if an xi is not a number then it ; is defaulted to 0. Thus (+ T 5) ==> 5.] ; The way we will eventually implement this restriction to certain symbols is ; to define the \ptt{"M1"} package to import the symbols above but not to ; import certain other symbols -- namely the ones I'm asking you to define. ; Recall from the lecture: (defun push (x y) (cons x y)) ; | | ; | cons in the ACL2 package ; | ; push in the M1 package ; Thus, (push 3 nil) ==> (3) ; (push 2 (push 3 nil)) ==> (2 3) ; (push 1 (push 2 (push 3 nil))) ==> (1 2 3) ; If we define top and pop like this: (defun top (stack) (car stack)) (defun pop (stack) (cdr stack)) ; then (top (push 1 (push 2 (push 3 nil)))) ==> 1 ; (pop (push 1 (push 2 (push 3 nil)))) ==> (2 3) ; A recursive definition: (defun nth (n list) (if (zp n) (car list) (nth (- n 1) (cdr list)))) ; Here is another handy definition: (defun make-state (pc locals stack program) (cons pc (cons locals (cons stack (cons program nil))))) ; Note: The cons nest above is the same thing as ; (list pc locals stack program). (defun pc (s) (nth 0 s)) (defun locals (s) (nth 1 s)) (defun stack (s) (nth 2 s)) (defun program (s) (nth 3 s)) ; ----------------------------------------------------------------- ; Problem 1-1. ACL2 has various predicates for recognizing different kinds of Lisp ; objects. Among them are: ; a. NATP ; b. STRINGP ; c. CONSP ; d. SYMBOLP ; e. ENDP ; f. KEYWORDP ; Beside each object, indicate which of the above predicates it satisfies. ; 1. 123 ; 2. ILOAD ; 3. INVOKEVIRTUAL ; 4. "Hello World!" ; 5. NIL ; 6. :NAME ; 7. (ILOAD 0) ; 8. ("Beta" "position") ; 9. ((ILOAD 0) (IFNE 237)) ; 10. ILOAD_0 ; --- ; Problem 1-2. For each list x below, write down (CAR x), (CAR (CDR x)), and (CDR (CDR x)). ; 1. (ILOAD 0) ; 2. (GETFIELD ("Beta" "position")) ; 3. (IINC 3 -1) ; 4. (IADD) ; 5. ((ILOAD 0) (IFNE 237) (ICONST_1)) ; 6. (THREAD (:ID 3) (:CT NIL) (:STAT ACTIVE) (:REF (REF 27))) ; 7. (:CT NIL) ; --- ; Problem 1-3. For each object below, write the CONS expression that constructs ; it. Each answer will be a call of CONS, applied to numbers, quoted symbols, ; strings, and/or NIL. ; 0. (ILOAD 2) Answer: (cons 'ILOAD (cons 2 NIL)) ; 1. (IADD) ; 2. ((:ID 3) (:CT NIL)) ; 3. (NIL) ; 4. (GETFIELD ("Beta" "position")) ; --- ; Problem 1-4: An ``instruction'' is a list of the form (op a1 ... ak), where ; k=0, 1, 2, or 3. ; Define opcode to take an instruction and return the op. Define arg1, arg2, ; and arg3 to take an instruction and return the corresponding operand of the ; instruction, or nil if it is not present. ; --- ; Problem 1-5: Fill in the blank: ; (top (pop (push x (push y (push z nil))))) = ______________ ; --- ; Problem 1-6: Can you PROVE your answer to Problem 1-5 is correct? ; --- ; Problem 1-7: (stack (make-state a b c d)) = _____________ ; --- ; Problem 1-8: Prove Problem 1-7. ; --- ; Problem 1-9: ; Define (len x) to return the number of elements of list x. ; Thus, (len '(a b c)) ==> 3. (``==>'' means ``evaluates to'') ; --- ; Problem 1-10: ; Define (app x y) to concatenate two lists. ; Thus, (app '(a b c) '(d e f)) ==> (A B C D E F). ; --- ; Problem 1-11: ; Define (rev x) to reverse a list x. ; Thus, (rev '(a b c)) ==> (C B A). ; --- ; Problem 1-12: ; Define (repeat x n) to return a list consisting of n ; copies of x. Thus, (repeat 'a 5) ==> (A A A A A). ; --- ; Problem 1-13: ; Define (popn n stk) to return the result of popping stk ; n times. Thus, ; (popn 2 (push 1 (push 2 (push 3 nil)))) ==> (3). ; --- ; Problem 1-14: ; Define (update-nth n v x) so that the following is a ; theorem ; (implies (and (natp i) ; i is an integer and 0 <= i ; (natp j)) ; (equal (nth i (update-nth j v x)) ; (if (equal i j) ; v ; (nth i x)))) ; --- ; Problem 1-15: ; Define (member e lst) to return t or nil indicating ; whether e is an element of list lst. ; (member 3 '(1 2 3 4)) ==> T ; (member 6 '(1 2 3 4)) ==> NIL ; --- ; Problem 1-16: ; Define (index e x) so that the following is a theorem: ; (implies (member e x) ; (equal (nth (index e x) x) e)) ; --- ; Problem 1-17: ; A ``keyword argument list'' is a list of even length whose even elements ; (i.e., elements in the even positions, 0, 2, 4, ...) are keywords. Thus, ; (:A 1 :B 2 :C 3) is a keyword argument list. So is (:A :B :B 2 :C 3). ; We say that a value for keyword key is ``supplied'' by x if key appears ; as an even element of x. Define (suppliedp key x) to return t or nil ; to indicate whether key is supplied by x. ; --- ; Problem 1-18: ; If key is supplied by x, then its ``value'' is the element of x appearing ; immediately after the first even occurrence of key in x. Define ; (actual key x) to return the value of key in x, assuming key is supplied. ; --- ; Problem 1-19: ; A ``binding environment'' is a list of lists of the form ; ((x1 y1) (x2 y2) ...). The xi are said to be the ``variables'' ; and the yi are said to be their ``values.'' We say that a binding ; environment ``binds'' x if x occurs as one of the xi. In that case, ; x is said to ``bound'' and it is bound to the ``value'' y, where y is ; (x y) is the first element in the environment binding x. ; Define (boundp x env) to return T or NIL indicating whether x is bound ; by binding environment env. ; --- ; Problem 1-20: ; Define (binding x env) to return the value to which x is bound in binding ; environment env, assuming x is bound. ; --- ; Problem 1-21: ; Define (bind x y env) so that the following is a theorem ; (equal (binding v1 (bind v2 y env)) ; (if (equal v1 v2) ; y ; (binding v1 env))) ; --- ; Problem 1-22: The ``unsigned n-bit interpretation'' of a natural number x is ; the natural number denoted by the low order n bits of the binary ; representation of k. Define (u-fix x n) to return the unsigned n-bit ; interpretation of x. (The name ``u-fix'' stands for ``unsigned fix'' and ; suggests the idea of converting (or ``fixing'') an arbitrary natural into a ; particular format.) ; --- ; Problem 1-23: The ``signed n-bit interpretation'' of a natural number x is the ; integer twos complement intepretation of the low order n bits of the binary ; representation of k. The table below illustrates the signed 3-bit ; interpretation of a range of naturals numbers x. ; nat binary twos-complement ; x integer ; 0 000 0 ; 1 001 1 ; 2 010 2 ; 3 011 3 ; 4 100 -4 ; 5 101 -3 ; 6 110 -2 ; 7 111 -1 ; 8 1000 0 ; 9 1001 1 ;10 1010 2 ;11 1011 3 ;12 1100 -4 ;13 1101 -3 ;... ... ... ; Define (s-fix x n) to return the signed n-bit interpretation of the natural ; number x. ; --- ; Problem 1-24: A ``byte'' is an integer b that can be represented in 8-bits, ; e.g., (and (natp b) (< b 256)). A list of bytes can be thought of as an ; unsighed base 256 ``big number'' in conventional ``big endian'' notation, ; e.g., where the car of the list is the most significant ``digit.'' Thus, ; (192 168 0 123) denotes the natural 3232235643, which is ; (+ (* 192 (expt 256 3)) ; (* 168 (expt 256 2)) ; (* 0 (expt 256 1)) ; (* 123 (expt 256 0))) ; Define (u-big lst) to return the unsigned base 256 big endian interpretation ; of the list of bytes lst. ; --- ; Problem 1-25: The ``signed base 256 big endian interpretation'' of a list of k ; bytes is the 8*k twos-complement interpretation of the unsigned base 256 big ; endian interpretation. Define (s-big lst) to return the signed base 256 big ; endian interpretation of the list of bytes lst. ; --- ; Problem 1-26: Define (nextn n lst) to return the first n elements of the list ; lst. You may assume that lst has at least n elements. Thus ; (nextn 3 '(a b c d e)) ==> (A B C) ; --- ; Problem 1-27: Define (skipn n lst) to return the result of removing the first n ; elements from lst. You may assume that lst has at least n elements. Thus, ; (skipn 3 '(a b c d e)) ==> (D E). ; --- the end ---