Challenge Proof Problems for
In the following problems please write
Our answers to the problems in this section are in community-books
file
LP17-1: Write a
(defun rev (x) (if (endp x) nil (append (rev (cdr x)) (list (car x)))))——————————
LP17-2: Write a
Hint: Sometimes the main theorem suggests the right induction on its own.
——————————LP17-3: Prove the following:
(defthm lp17-3-main (equal (loop$ with x = lst with ans = 0 do (cond ((endp x) (return ans)) (t (progn (setq ans (+ 1 ans)) (setq x (cdr x)))))) (len lst)))——————————
LP17-4: Write a
LP17-5: Prove the following.
(defthm lp17-5-main (implies (true-listp lst) (equal (loop$ with x = lst with ans = nil do (cond ((endp x) (return ans)) (t (progn (setq ans (append ans (list (car x)))) (setq x (cdr x)))))) lst)))——————————
LP17-6: Prove the following.
(defthm lp17-6-main (implies (and (natp m) (natp n)) (equal (loop$ with i = m with j = n do (if (zp i) (return j) (progn (setq i (- i 1)) (setq j (+ j 1))))) (+ m n))))——————————
LP17-7: Write a
(defun fact (n) (if (zp n) 1 (* n (fact (- n 1)))))——————————
LP17-8: Write a
Prove that your do loop$ is equal to
(cons (loop$ for e in lst sum e) (loop$ for e in lst sum (sq e)))
Use the following definition of
(defun sq (x) (* x x))——————————
LP17-9: Define
Hint: Since you are likely to collect the elements in reverse order, a
suitable specification for these purposes is that
(cons (rev (loop$ for e in lst when (symbolp e) collect e)) (rev (loop$ for e in lst when (not (symbolp e)) collect e)))——————————
LP17-10: Write a
Hints: Remember that in order for your “lemma1” to be applied
in the proof of your main theorem it must match the rewritten lambda objects
of the main theorem. We've focused on matching the
LP17-11: Define
The definition of
(defun make-pair (i j) (declare (xargs :guard t)) (cons i j)) (defwarrant make-pair) (defun all-pairs-helper2 (i j jmax) (declare (xargs :measure (nfix (- (+ (nfix jmax) 1) (nfix j))) :guard (and (natp i) (natp j) (natp jmax)))) (let ((j (nfix j)) (jmax (nfix jmax))) (cond ((> j jmax) nil) (t (cons (make-pair i j) (all-pairs-helper2 i (+ 1 j) jmax)))))) (defun all-pairs-helper1 (i imax jmax) (declare (xargs :measure (nfix (- (+ (nfix imax) 1) (nfix i))) :guard (and (natp i) (natp imax) (natp jmax)))) (let ((i (nfix i)) (imax (nfix imax))) (cond ((> i imax) nil) (t (append (all-pairs-helper2 i 1 jmax) (all-pairs-helper1 (+ 1 i) imax jmax)))))) (defun all-pairs (imax jmax) (declare (xargs :guard (and (natp imax) (natp jmax)))) (all-pairs-helper1 1 imax jmax))
Hints: In following our advice on proving do loop$s you will define
functions that compute the same things as your
The next step in our recipe is to prove that the
Since that second step does not involve
Now go to lp-section-18 (or return to the