1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
|
#|
(defpkg "M1"
'(defun declare ignore xargs defthm mutual-recursion
include-book in-theory disable defconst
defmacro &rest
intern-in-package-of-symbol
coerce symbol-name
string
otherwise
concatenate progn strip-cars syntaxp quotep assoc pairlis$ pairlis-x2 e/d
o-p o< acl2-count
let let*
cond case
and or not implies t nil quote
cons consp car cdr endp
list list*
atom symbolp natp
if equal
zp + - * / mod expt < <= > >=))
(begin-book t :ttags :all)
|#
(in-package "M1")
; 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,
; (+ 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 ((A - NATP) (E - ENDP))
; 2. ILOAD ((D - SYMBOLP) (E - ENDP))
; 3. INVOKEVIRTUAL ((D - SYMBOLP) (E - ENDP))
; 4. "Hello World!" ((B - STRINGP) (E - ENDP))
; 5. NIL ((D - SYMBOLP) (E - ENDP))
; 6. :NAME ((D - SYMBOLP) (E - ENDP) (F - KEYWORDP))
; 7. (ILOAD 0) ((C - CONSP))
; 8. ("Beta" "position") ((C - CONSP))
; 9. ((ILOAD 0) (IFNE 237)) ((C - CONSP))
; 10. ILOAD_0 ((D - SYMBOLP) (E - ENDP))
; ---
; Problem 1-2. For each list x below, write down (CAR x), (CAR (CDR x)), and (CDR (CDR x)).
; 1. (ILOAD 0)
; Answers: ILOAD 0 NIL
; 2. (GETFIELD ("Beta" "position"))
; Answers: GETFIELD ("Beta" "position") NIL
; 3. (IINC 3 -1)
; Answers: IINC 3 (-1)
; 4. (IADD)
; Answers: IADD NIL NIL
; 5. ((ILOAD 0) (IFNE 237) (ICONST_1))
; Answers: (ILOAD 0) (IFNE 237) ((ICONST_1))
; 6. (THREAD (:ID 3) (:CT NIL) (:STAT ACTIVE) (:REF (REF 27)))
; Answers: THREAD (:ID 3) ((:CT NIL) (:STAT ACTIVE) (:REF (REF 27)))
; 7. (:CT NIL)
; Answers: :CT NIL 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) Answer: (cons 'IADD NIL)
; 2. ((:ID 3) (:CT NIL))
; Answer: (cons (cons :ID (cons 3 nil))
; (cons (cons :CT (cons NIL nil))
; nil))
; 3. (NIL) Answer: (cons NIL nil)
; 4. (GETFIELD ("Beta" "position"))
; Answer: (cons 'GETFIELD
; (cons (cons "Beta" (cons "position" nil))
; nil))
; ---
; 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.
; Answer 1-4:
(defun op-code (inst) (car inst))
(defun arg1 (inst) (nth 1 inst))
(defun arg2 (inst) (nth 2 inst))
(defun arg3 (inst) (nth 3 inst))
; ---
; Problem 1-5: Fill in the blank:
; (top (pop (push x (push y (push z nil))))) = ______________
; Answer 1-5:
; (top (pop (push x (push y (push z nil))))) = y
; ---
; Problem 1-6: Can you PROVE your answer to Problem 1-5 is correct?
; Answer 1-6: One way to prove this is to replace top, pop, and push by
; their definitions and get
; (car (cdr (cons x (cons y (cons z nil))))) = y
; and then use the axioms for car-cons and cdr-cons.
; A ``better'' way to prove it is to prove two lemmas (by following the scheme above
; of replacing the stack functions by their definitions):
; Lemma 1: top-push
; (top (push x y)) = x
; Lemma 2: pop-push
; (pop (push x y)) = y
; and then to apply those lemmas as follows:
; Proof:
; (top (pop (push x (push y (push z nil)))))
; = {by Lemma 2 and replacement of equals by equals}
; (top (push y (push z nil)))
; = {by Lemma 1 and replacement of equals by equals}
; y
; Q.E.D.
; ---
; Problem 1-7: (stack (make-state a b c d)) = _____________
; Answer 1-7: (stack (make-state a b c d)) = c
; ---
; Problem 1-8: Prove Problem 1-7.
; Answer 1-8:
; Proof that (stack (make-state a b c d)) = c
; (stack (make-state a b c d))
; = {def of stack}
; (nth 2 (make-state a b c d))
; = {def nth and the facts (zp 2) = nil and (- 2 1) = 1 and basic axioms}
; (nth 1 (cdr (make-state a b c d)))
; = {def nth and the facts (zp 1) = nil and (- 1 1) = 0 and basic axioms}
; (nth 0 (cdr (cdr (make-state a b c d))))
; = {defn nth and (zp 0) = t and basic axioms}
; (car (cdr (cdr (make-state a b c d))))
; = {def make-state}
; (car (cdr (cdr (cons a (cons b (cons c (cons d nil)))))))
; = {basic axiom about cdr-cons}
; (car (cdr (cons b (cons c (cons d nil)))))
; = {basic axiom about cdr-cons}
; (car (cons c (cons d nil)))
; = {basic axiom about car-cons}
; c
; Q.E.D.
; ---
; Problem 1-9:
; Define (len x) to return the number of elements of list x.
; Thus, (len '(a b c)) ==> 3. (``==>'' means ``evaluates to'')
; Answer 1-9:
(defun len (x)
(if (endp x)
0
(+ 1 (len (cdr x)))))
; ---
; 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).
; Answer 1-10:
(defun app (x y)
(if (endp x)
y
(cons (car x)
(app (cdr x) y))))
; ---
; Problem 1-11:
; Define (rev x) to reverse a list x.
; Thus, (rev '(a b c)) ==> (C B A).
; Answer 1-11:
(defun rev (x)
(if (endp x)
nil
(app (rev (cdr x))
(cons (car x) nil))))
; An alternative answer is the definition of frev below.
(defun rev1 (x a)
(if (endp x)
a
(rev1 (cdr x)
(cons (car x) a))))
(defun frev (x) (rev1 x nil))
; ---
; 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).
; Answer 1-12:
(defun repeat (th n)
(if (zp n)
nil
(cons th (repeat th (- n 1)))))
; ---
; 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).
; Answer 1-13:
(defun popn (n stk)
(if (zp n)
stk
(popn (- n 1) (pop stk))))
; ---
; 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))))
; Answer 1-14:
(defun update-nth (n v list)
(if (zp n)
(cons v (cdr list))
(cons (car list)
(update-nth (- n 1) v (cdr list)))))
; ---
; 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
; Answer 1-15:
(defun member (e list)
(if (endp list)
nil
(if (equal e (car list))
t
(member e (cdr list)))))
; ---
; 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))
; Answer 1-16:
(defun index (e lst)
(if (endp lst)
0
(if (equal e (car lst))
0
(+ 1 (index e (cdr lst))))))
; ---
; 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.
; Answer 1-17:
(defun suppliedp (key args)
(if (endp args)
nil
(if (equal key (car args))
t
(suppliedp key (cdr (cdr args))))))
; ---
; 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.
; Answer 1-18:
(defun actual (key args)
(if (endp args)
nil
(if (equal key (car args))
(car (cdr args))
(actual key (cdr (cdr args))))))
; ---
; 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.
; Answer 1-19:
(defun boundp (var alist)
(if (endp alist)
nil
(if (equal var (car (car alist)))
t
(boundp var (cdr alist)))))
; ---
; Problem 1-20:
; Define (binding x env) to return the value to which x is bound in binding
; environment env, assuming x is bound.
; Answer 1-20:
(defun binding (var alist)
(if (endp alist)
nil
(if (equal var (car (car alist)))
(car (cdr (car alist)))
(binding var (cdr alist)))))
; ---
; 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)))
; Answer 1-21:
(defun bind (var val alist)
(if (endp alist)
(cons (cons var (cons val nil)) nil)
(if (equal var (car (car alist)))
(cons (cons var (cons val nil)) (cdr alist))
(cons (car alist)
(bind var val (cdr alist))))))
; Note: Here's another definition that works perfectly well.
; (defun bind (var val alist)
; (cons (cons var (cons val nil)) alist))
; ---
; 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.)
; Answer 1-22:
(defun u-fix (x n)
(mod x (expt 2 n)))
; ---
; 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.
; Answer 1-23:
(defun s-fix (x n)
(let ((temp (mod x (expt 2 n))))
(if (< temp (expt 2 (- n 1)))
temp
(- temp (expt 2 n)))))
; ---
; 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.
; Answer 1-24:
(defun u-big1 (lst acc)
(if (endp lst)
acc
(u-big1 (cdr lst)
(+ (u-fix (car lst) 8)
(* (expt 2 8) acc)))))
(defun u-big (lst) (u-big1 lst 0))
; ---
; 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.
; Answer 1-25:
(defun s-big (lst)
(s-fix (u-big lst) (* 8 (len 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)
; Answer 1-26:
(defun nextn (n lst)
(if (zp n)
nil
(cons (car lst)
(nextn (- n 1) (cdr lst)))))
; ---
; 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).
; Answer 1-27:
(defun skipn (n lst)
(if (zp n)
lst
(skipn (- n 1) (cdr lst))))
; --- the end ---
|