/usr/share/acl2-8.0dfsg/books/misc/hanoi.lisp is in acl2-books-source 8.0dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ; Copyright (C) 2013, Regents of the University of Texas
; Written by Matt Kaufmann and J Strother Moore, April 2, 2003
; License: A 3-clause BSD license. See the LICENSE file distributed with ACL2.
; A Proof of the Correctness of a Towers of Hanoi Program
; Abstract
; In this book we prove the correctness of a function that
; purportedly generates moves to play the Towers of Hanoi game
; for an arbitrary number of disks.
; This is Moore's translation into ACL2 of Matt Kaufmann's
; Pc-Nqthm solution of this problem. Matt devised his solution
; as an illustration of PC-Nqthm. This proof is entirely
; rule-based. But the elegance of Matt's solution was in his
; statement of the problem and of the main lemma, h-lemma.
; To certify this book, execute:
#|
(defpkg "HANOI"
(set-difference-equal
(union-eq *acl2-exports*
*common-lisp-symbols-from-main-lisp-package*)
'(PUSH POP GET)))
(certify-book "hanoi" 1)
JSM
August, 2004
|#
(in-package "HANOI")
(defun mem (e x)
(if (consp x)
(if (equal e (car x))
x
(mem e (cdr x)))
nil))
(defun app (x y)
(if (consp x)
(cons (car x)
(app (cdr x) y))
y))
(defun del (e x)
(if (consp x)
(if (equal e (car x))
(cdr x)
(cons (car x) (del e (cdr x))))
nil))
(defun perm (x y)
(if (consp x)
(and (mem (car x) y)
(perm (cdr x)
(del (car x) y)))
(not (consp y))))
; Note: I will use the perm expression (perm (list a b c) '(0 1 2)) to
; characterize the peg numbers, a, b, and c. But I will then use the
; lemma to rewrite the perm into a conjunction of facts that give us
; all we need without "revealing" the identities of a, b, and c. If
; we allow the perm to expand, the proof of h-lemma takes 40 times
; longer.
(defthm perm-opener
(equal (perm (list a b c) '(0 1 2))
(and (integerp a) (<= 0 a) (<= a 2)
(integerp b) (<= 0 b) (<= b 2)
(integerp c) (<= 0 c) (<= c 2)
(not (equal a b))
(not (equal a c))
(not (equal b c)))))
(defthm app-assoc
(equal (app (app a b) c) (app a (app b c))))
(defthm app-right-id
(implies (true-listp x)
(equal (app x nil) x)))
(defun get (n x)
(if (zp n) (car x) (get (- n 1) (cdr x))))
(defun put (v n x)
(if (zp n) (cons v (cdr x)) (cons (car x) (put v (- n 1) (cdr x)))))
(defthm get-put
(implies (and (natp i)
(natp j))
(equal (get i (put v j x))
(if (equal i j) v (get i x)))))
(defthm put-get
(implies (and (equal x (get n s))
(natp n)
(< n (len s)))
(equal (put x n s) s)))
(defthm put-put-1
(implies (and (natp i)
(natp j)
(not (equal i j)))
(equal (put v i (put u j s))
(put u j (put v i s))))
:rule-classes ((:rewrite :loop-stopper ((i j)))))
(defthm put-put-2
(equal (put v i (put w i s))
(put v i s)))
(defthm true-listp-put
(implies (and (natp n)
(< n (len x)))
(equal (true-listp (put v n x))
(true-listp x))))
(defthm len-put
(implies (and (natp n)
(< n (len x)))
(equal (len (put v n x))
(len x))))
(defun push (e x) (cons e x))
(defun pop (x) (cdr x))
(defun top (x) (car x))
(defun h (i j k n)
(if (zp n)
nil
(app (h i k j (- n 1))
(cons (list 'MOVE i k)
(h j i k (- n 1))))))
(defun Hanoi (n) (h 0 1 2 n))
(defun a (m) (get 1 m))
(defun b (m) (get 2 m))
; Is m a syntactically well-formed move?
(defun legal-syntaxp (m)
(and (true-listp m)
(equal (car m) 'MOVE)
(equal (len m) 3)
(mem (a m) '(0 1 2))
(mem (b m) '(0 1 2))
(not (equal (a m) (b m)))))
; Is m a legal move in state s?
(defun legal-movep (m s)
(and (legal-syntaxp m)
(consp (get (a m) s))
(if (consp (get (b m) s))
(< (car (get (a m) s))
(car (get (b m) s)))
t)))
; Let's assume that s is a good state, that a and b are distinct
; pegs, and that it is legal to move the top element of tower a
; to tower b. This function returns the state produced by making
; that move.
(defun do-move (m s)
(let ((stacka (get (a m) s))
(stackb (get (b m) s)))
(put (pop stacka) (a m)
(put (push (top stacka) stackb) (b m)
s))))
; The Hanoi game generates a list of moves, where each move is of
; the form (MOVE a b), where a and b are distinct pegs.
(defun play (moves s)
; Moves is a list of moves. Each move is of the form (MOVE a b),
; where a and b are distinct pegs. Play returns the state
; produced by doing each of the moves, in turn, to s. If an
; illegal move occurs, NIL is returned. Otherwise, the final
; state (which is never nil) is returned.
(if (consp moves)
(if (legal-movep (car moves) s)
(play (cdr moves)
(do-move (car moves)
s))
nil)
s))
(defun tower (n)
(if (zp n)
nil
(app (tower (- n 1)) (list n))))
(defthm examples
(and (equal (play (Hanoi 7) (list (tower 7) nil nil))
(list nil nil (tower 7)))
(equal (play (Hanoi 2) (list (tower 3) nil '(4)))
'((3) nil (1 2 4)))
(equal (play (Hanoi 3) '((1 2 3) (0) nil))
nil)))
; ---------------------------------------------------------------
; Lemmas
(defthm true-listp-tower
(true-listp (tower n)))
(defthm play-app
(equal (play (app moves1 moves2) s)
(play moves2 (play moves1 s))))
; ----------------------------------------------------------------
; The General Lemma
; Here is the generalized form of the correctness theorem.
(defun big-tops (a b c n s)
(and (or (endp (get a s))
(< n (car (get a s))))
(or (endp (get b s))
(< n (car (get b s))))
(or (endp (get c s))
(< n (car (get c s))))))
; Note: I could have used 0, 1, and 2 for a, b, and c. But instead I
; passed them in. The reason is that in my proof of h-lemma below I
; explicitly avoid revealing that a, b, and c are those particular
; numbers.
; The key lemma is called h-lemma, below, and the next function
; definition describes the induction hint needed to prove it.
; Look at h-lemma first.
; The conclusion is
; (equal (play (h a b c n) ;[lhs]
; (put (app (tower n) (get a s)) a s))
; (put (app (tower n) (get c s)) c s)) ;[rhs]
; Consider the lhs
; (play (h a b c n)
; (put (app (tower n) (get a s)) a s))
; (h a b c n) will open to
; (app (h a c b (- n 1))
; (cons (list 'MOVE a c)
; (h b a c (- n 1))))
; and because of play-app and the definition of play,
; lhs
; =
; (play (h b a c (- n 1))
; (do-move (list 'MOVE a c)
; (play (h a c b (- n 1))
; (put a
; (app (tower n) (get a s))
; s))))
; Expand (tower n) to (app (tower (- n 1)) (list n)) and associate
; and you get
; lhs
; =
; (play (h b a c (- n 1)) ; [outer play]
; (do-move (list 'MOVE a c)
; (play (h a c b (- n 1)) ; [inner play]
; (put (app (tower (- n 1))
; (cons n (get a s)))
; a
; s))))
; Consider the [inner play] term. We can provide an induction
; hyp that tells us about this term! Here is the lhs of our
; theorem again.
; (play (h a b c n)
; (put (app (tower n)
; (get a s))
; a
; s))
; Instantiate it by a := a, b := c, c := b, n := (- n 1), and
; s := (put (cons n (get a s)) a s)
; and you get:
; (play (h a c b (- n 1))
; (put (app (tower (- n 1))
; (get a
; (put (cons n (get a s))
; a
; s)))
; a
; (put (cons n (get a s))
; a
; s)))
;
; Now that is not quite what we want. But simplify it, first by
; simplifying the (get a (put ... a ...)):
; (play (h a c b (- n 1))
; (put (app (tower (- n 1))
; (cons n (get a s)))
; a
; (put (cons n (get a s))
; a
; s)))
; And then by simplifying (put ... a ... (put ... a ...))
; (play (h a c b (- n 1))
; (put (app (tower (- n 1))
; (cons n (get a s)))
; a
; s))
; And voila, it's [inner play].
; The induction hypothesis tells us [inner play] is equal to
; (put (app (tower (- n 1))
; (get b
; (put (cons n (get a s)) a s)))
; b
; (put (cons n (get a s)) a s))
; which we can simplify, using get and put facts, to
; (put (app (tower (- n 1))
; (get b s))
; b
; (put (cons n (get a s)) a s))
; So, using this hypothesis, we get
; lhs
; =
; (play (h b a c (- n 1)) ; [outer play]
; (do-move (list 'MOVE a c)
; (put (app (tower (- n 1)) (get b s))
; b
; (put (cons n (get a s))
; a
; s))))
; Now do the MOVE and we get
;
; lhs
; =
; (play (h b a c (- n 1))
; (put (cons n (get c s))
; c
; (put (app (tower (- n 1)) (get b s))
; b
; s)))
; (Again, using get and put simplifications.)
; Commute the put on c and b (another lemma about these
; important functions)
; lhs
; =
; (play (h b a c (- n 1))
; (put (app (tower (- n 1)) (get b s))
; b
; (put (cons n (get c s))
; c
; s)))
; Does this look familiar? We can supply an induction hypothesis
; to tell us what this term is, too!
;
; Here is the [lhs] of our theorem, again:
; (play (h a b c n)
; (put (app (tower n)
; (get a s))
; a
; s))
; Instantiate it with a := b, b := a, c := c, n := (- n 1) and
; s := (put (cons n (get c s)) c s)
; and simplify.
; So now replace this with the rhs of the second induction
; hypothesis:
; lhs
; =
; (put (app (tower (- n 1))
; (get c (put (cons n (get c s)) c s)))
; c
; (put (cons n (get c s)) c s))
; and simplify as usual
; lhs
; =
; (put (app (tower (- n 1))
; (cons n (get c s)))
; c
; s)
; =
; (put (app (tower n)
; (get c s))
; c
; s)
; = rhs!
; So you can see we need two induction hypotheses, as described by
; the two substitutions:
; a := a, b := c, c := b, n := (- n 1),
; s := (put (cons n (get a s)) a s)
; a := b, b := a, c := c, n := (- n 1),
; s := (put (cons n (get c s)) c s)
; That is what is coded below.
(defun induction-hint (a b c n s)
(if (zp n)
(list a b c n s)
(list (induction-hint a c b (- n 1)
(put (push n (get a s)) a s))
(induction-hint b a c (- n 1)
(put (push n (get c s)) c s)))))
; So here is h-lemma, the crux of the proof. The proof is
; tedious because we have to deal with the preservation of
; the big-tops hypothesis under the instantiations and
; the constant pathological possibilities that a = b or some
; other a = 4 or some other nonsense that prevents the
; nth and update-nth rules from applying. These are dealt with
; by brute force: just consider the possible values of
; a, b, and c and do the inductive argument for each one.
; Time: 14.10 seconds (prove: 12.85, print: 1.24, other: 0.01)
(defthm h-lemma
(implies (and (natp n)
(true-listp s)
(equal (len s) 3)
(perm (list a b c) '(0 1 2))
(big-tops a b c n s))
(equal (play (h a b c n)
(put (app (tower n) (get a s))
a
s))
(put (app (tower n) (get c s)) c s)))
:rule-classes nil
:hints (("Goal" :induct (induction-hint a b c n s))))
; ----------------------------------------------------------------
; The Main Theorem
(defthm hanoi-correct
(equal (play (hanoi n) (list (tower n) nil nil))
(list nil nil (tower n)))
:hints (("Goal" :use (:instance h-lemma
(a 0)
(b 1)
(c 2)
(s (list nil nil nil))))))
|