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
|
; Copyright (C) 2013, Regents of the University of Texas
; Written by Matt Kaufmann, April, 2012 (revised July, 2020)
; License: A 3-clause BSD license. See the LICENSE file distributed with ACL2.
; Note: This file was originally a book, books/misc/congruent-stobjs-test.lisp.
; As of this writing it is still certifiable as a book (after changing its file
; extension from "lsp" to "lisp"), because various uses of make-event
; (including must-fail, for example) have been retained. (They could probably
; be eliminated if we don't care about certifiability.) However, in July, 2020
; I decided to automate the checking of output from this file (after adding
; tests for translate11-call, as noted below). The run-script utility was
; handy for that purpose, so this file is now treated as input for that
; utility.
; Warning: Keep the last part, labeled "Tests referenced in ACL2 source
; function translate11-call", in sync with corresponding comments in that
; function's definition.
(in-package "ACL2")
(include-book "std/testing/must-fail" :dir :system)
(include-book "std/testing/must-succeed" :dir :system)
(defmacro must-fail^ (x) `(must-fail ,x :with-output-off nil))
; We start with an example from :doc defstobj.
(defconst *mem-size* 10)
(defstobj st1
(reg1 :type (array (unsigned-byte 31) (8))
:initially 0)
(pc1 :type (unsigned-byte 31)
:initially 555)
halt1
(mem1 :type (array (unsigned-byte 31) (*mem-size*))
:initially 0 :resizable t))
(defstobj st2
(reg2 :type (array (unsigned-byte 31) (8))
:initially 0)
(pc2 :type (unsigned-byte 31)
:initially 555)
halt2
(mem2 :type (array (unsigned-byte 31) (*mem-size*))
:initially 0 :resizable t)
:congruent-to st1)
(defstobj st3
(reg3 :type (array (unsigned-byte 31) (8))
:initially 0)
(pc3 :type (unsigned-byte 31)
:initially 555)
halt3
(mem3 :type (array (unsigned-byte 31) (*mem-size*))
:initially 0 :resizable t)
:congruent-to st2)
(defun foo1 (st1)
(declare (xargs :stobjs st1))
(reg1i 3 st1))
(defun bar1 (st1)
(declare (xargs :stobjs st1))
(foo1 st1))
(defun bar2-tough (st2)
(declare (xargs :stobjs st2))
(foo1 st2))
(defun bar2 (st2)
(declare (xargs :stobjs st2))
(if (halt2 st2)
0
(foo1 st2)))
(defun update1 (x st1)
(declare (xargs :stobjs st1))
(update-halt1 x st1))
(defun update2 (x st2)
(declare (xargs :stobjs st2))
(update1 x st2))
(defmacro eval-form (form)
`(make-event
(er-progn (trans-eval ',form 'top state t)
(value '(value-triple :nil)))))
(eval-form
(update2 3 st2))
(assert-event (equal (list (halt1 st1) (halt2 st1) (halt2 st2) (halt1 st2))
'(NIL NIL 3 3)))
(eval-form
(update2 7 st1))
(assert-event (equal (list (halt1 st1) (halt2 st1) (halt2 st2) (halt1 st2))
'(7 7 3 3)))
(defun swap-st1-st2 (st1 st2)
(declare (xargs :stobjs (st1 st2)))
(mv st2 st1))
(eval-form
(swap-st1-st2 st1 st2)) ; returns (<st2> <st1>)
; Swap doesn't change stobjs:
(assert-event (equal (list (halt1 st1) (halt2 st1) (halt2 st2) (halt1 st2))
'(7 7 3 3)))
(eval-form
(swap-st1-st2 st2 st1)) ; returns (<st1> <st2>)
; Swap doesn't change stobjs:
(assert-event (equal (list (halt1 st1) (halt2 st1) (halt2 st2) (halt1 st2))
'(7 7 3 3)))
; ERROR! Not congruent.
(must-fail^
(defstobj st4
(reg3 :type (array (unsigned-byte 31) (8))
:initially 0)
(pc3 :type (unsigned-byte 31)
:initially 555)
halt3
:congruent-to st2))
(must-fail^
(defstobj st4
(reg4 :type (array (unsigned-byte 31) (8))
:initially 0)
(pc4 :type (unsigned-byte 30) ; changed from 31!
:initially 555)
halt4
(mem4 :type (array (unsigned-byte 31) (*mem-size*))
:initially 0 :resizable t)
:congruent-to st1))
(defmacro must-not-translate (form)
`(must-eval-to-t ; use essentially the translate1 call from trans-eval
(mv-let
(erp trans bindings state)
(translate1 ',form :stobjs-out '((:stobjs-out . :stobjs-out))
t
'must-not-translate (w state) state)
(declare (ignore trans bindings))
(value (not (not erp))))
:with-output-off nil))
(defmacro must-fail+ (form)
`(make-event
(mv-let (erp val state)
(trans-eval ',form 'must-fail+ state t)
(declare (ignore val))
(cond (erp (value '(value-triple :failed-as-expected)))
(t (silent-error state))))))
(defmacro must-succeed+ (form)
`(must-succeed
(trans-eval ',form 'must-not-translate state t)))
; The error message for the following isn't as helpful as it might be, but it's
; good enough; in fact it's essentially identical to the corresponding message
; when using v4-3.
(must-not-translate
(swap-st1-st2 st1 st1))
(must-succeed+
(mv-let (st1 st2)
(swap-st1-st2 st2 st1)
(mv st1 st2)))
(must-succeed+
(mv-let (st2 st1)
(swap-st1-st2 st1 st2)
(mv st1 st2)))
(must-not-translate ; this doesn't swap
(mv-let (st1 st2)
(swap-st1-st2 st1 st2)
(mv st1 st2)))
(must-not-translate ; this doesn't swap
(mv-let (st2 st1)
(swap-st1-st2 st2 st1)
(mv st1 st2)))
; Test with-local-stobj. Note that swap doesn't actually change st1 or st2,
; which is why val0 = val.
(defun test1 ()
(with-local-stobj
st1
(mv-let
(result st1)
(with-local-stobj
st2
(mv-let
(st2 st1 val0 val)
(let* ((st1 (update2 4 st1))
(st2 (update2 5 st2))
(val0 (list (halt1 st1) (halt2 st1) (halt2 st2) (halt1 st2))))
(mv-let
(st2 st1)
(swap-st1-st2 st1 st2)
(mv st2 st1
val0
(list (halt1 st1) (halt2 st1) (halt2 st2) (halt1 st2)))))
(mv (and (equal val0 '(4 4 5 5))
(equal val '(4 4 5 5)))
st1)))
result)))
; Test with-local-stobj. Note that swap doesn't actually change st1 or st2,
; which is why val0 = val.
(defun test2 ()
(with-local-stobj
st1
(mv-let
(result st1)
(with-local-stobj
st2
(mv-let
(st2 st1 val0 val)
(let* ((st1 (update2 4 st1))
(st2 (update2 5 st2))
(val0 (list (halt1 st1) (halt2 st1) (halt2 st2) (halt1 st2))))
(mv-let
(st1 st2)
(swap-st1-st2 st2 st1)
(mv st2 st1
val0
(list (halt1 st1) (halt2 st1) (halt2 st2) (halt1 st2)))))
(mv (and (equal val0 '(4 4 5 5))
(equal val '(4 4 5 5)))
st1)))
result)))
(assert-event (test1))
(assert-event (test2))
; Test guard violation messages.
(defun update3 (x st2)
(declare (xargs :stobjs st2
:guard (acl2-numberp x)))
(update1 x st2))
; Should cause same guard violation message as before.
; (Note: The with-guard-checking wrapper is needed during book certification.)
(must-fail+ (with-guard-checking t (update3 'a st2)))
; As just above:
(must-fail+ (with-guard-checking t (update3 'a st1)))
(must-not-translate (update3 3 'b))
(defun update4 (x st1 st2)
(declare (xargs :stobjs (st1 st2)
:guard (acl2-numberp x)))
(declare (ignore st1))
(update1 x st2))
; Error message explains that the problem arises in spite of congruent stobjs.
(must-not-translate (update4 3 st1 st1))
; Example from :doc print-gv.
(defstobj st fld)
(defun g (x st)
(declare (xargs :guard (consp x) :stobjs st)
(ignore x))
(fld st))
(defun test ()
(with-local-stobj
st
(mv-let (result st)
(mv (g 3 st) st)
result)))
(must-fail+ (with-guard-checking t (test)))
; Sadly, when processing this file by certifying congruent-stobjs-book.lisp,
; the output from the following call of print-gv shows up in the channel
; *standard-co*, hence in congruent-stobjs-book.cert.out, not in
; congruent-stobjs-log.out.
(make-event (er-progn (print-gv)
(value '(value-triple :ok))))
; Test memoization
(defstobj st0 fld0 :congruent-to st)
(defun h (st)
(declare (xargs :stobjs st))
(fld st))
(must-succeed+ (memoize 'h))
(must-succeed+ (update-fld 0 st0))
(must-succeed+ (update-fld 1 st))
(assert-event (equal (h st0) 0))
(assert-event (equal (h st) 1))
(assert-event (equal (h st0) 0))
(assert-event (equal (h st) 1))
;;; Tests referenced in ACL2 source function translate11-call
(defstobj s$1 fld1)
(defstobj s$2 fld2 :congruent-to s$1)
(defstobj s$3 fld3)
; Our first two examples are simple violations of single-threadedness. In both
; of these, the arguments of update-fld1 are reversed, so there is an input
; error. But in the second, there is also an output signature mismatch, which
; we (perhaps arbitrarily) report because we think it might be more useful to
; the user if we point out that every call of update-fld1 is illegal in the
; given context, rather than pointing out a problem with its arguments.
;;; (1a)
(must-fail^
(defun f (s$1)
(declare (xargs :stobjs s$1))
(let ((s$1 (update-fld1 s$1 3)))
s$1))
)
#|
ACL2 Error in ( DEFUN F ...): A single-threaded object, namely S$1,
is being used where an ordinary object is expected. Note: this error
occurred in the context (UPDATE-FLD1 S$1 3).
|#
;;; (1b)
(must-fail^
(defun f (x s$1)
(declare (xargs :stobjs s$1))
(list (update-fld1 s$1 x)))
)
#|
ACL2 Error in ( DEFUN F ...): It is illegal to invoke UPDATE-FLD1
here because of a signature mismatch. This function call returns a
result of shape S$1 where a result of shape * is required. Note:
this error occurred in the context (UPDATE-FLD1 S$1 X).
|#
; Let us now consider variants of the two examples above in which we replace
; s$1 with s$2, which is congruent to s$1. We see that the errors in (2a),
; (2a'), and (2a'') correspond to the error in (1a), complaining about an input
; since the output isn't obviously problematic; while the error in (2b)
; corresponds to the error in (2b).
;;; (2a) [change (1a), replacing s$1 by congruent stobj s$2]
(must-fail^
(defun f (s$2)
(declare (xargs :stobjs s$2))
(let ((s$2 (update-fld1 s$2 3)))
s2))
)
#|
ACL2 Error in ( DEFUN F ...): A single-threaded object, namely S$2,
is being used where an ordinary object is expected. Note: this error
occurred in the context (UPDATE-FLD1 S$2 3).
|#
;;; (2a') [subtler than (2a), but still not clearly an output problem]
(must-fail^
(defun f (s$1 s$2)
(declare (xargs :stobjs (s$1 s$2)))
(let ((s$1 (update-fld1 s$2 3)))
(mv s$1 s$2)))
)
#|
ACL2 Error in ( DEFUN F ...): A single-threaded object, namely S$2,
is being used where an ordinary object is expected. Note: this error
occurred in the context (UPDATE-FLD1 S$2 3).
|#
;;; (2a'') [like (2a') but with s$1 and s$2 reversed in the binding]
(must-fail^
(defun f (s$1 s$2)
(declare (xargs :stobjs (s$1 s$2)))
(let ((s$2 (update-fld1 s$1 3)))
(mv s$1 s$2)))
)
#|
ACL2 Error in ( DEFUN F ...): A single-threaded object, namely S$1,
is being used where an ordinary object is expected. Note: this error
occurred in the context (UPDATE-FLD1 S$1 3).
|#
;;; (2b) [as with (1b), clearly an output problem -- Note that the use of s$2
; in the wrong argument position isn't much of a hint that the output
; should be s$2 instead of s$1, so we can live with mention of s$1
; instead of s$2 in the error message.]
(must-fail^
(defun f (x s$2)
(declare (xargs :stobjs s$2))
(list (update-fld1 s$2 x)))
)
#|
ACL2 Error in ( DEFUN F ...): It is illegal to invoke UPDATE-FLD1
here because of a signature mismatch. This function call returns a
result of shape S$1 where a result of shape * is required. Note:
this error occurred in the context (UPDATE-FLD1 S$2 X).
|#
;;; (2b') [like (2b) except computed stobjs-out is (s$2) now, not (s$1)]
(must-fail^
(defun f (x s$2)
(declare (xargs :stobjs s$2))
(list (update-fld1 x s$2)))
)
#|
ACL2 Error in ( DEFUN F ...): It is illegal to invoke UPDATE-FLD1
here because of a signature mismatch. This function call returns a
result of shape S$2 (after accounting for the replacement of some input
stobjs by congruent stobjs) where a result of shape * is required.
Note: this error occurred in the context (UPDATE-FLD1 X S$2).
|#
;;; (3) [like (2a'') but using non-congruent s$3 in place of s$2,
;;; which results now in an output mismatch]
(must-fail^
(defun f (s$1 s$3)
(declare (xargs :stobjs (s$1 s$3)))
(let ((s$3 (update-fld1 s$1 3)))
(mv s$1 s$3)))
)
#|
ACL2 Error in ( DEFUN F ...): It is illegal to invoke UPDATE-FLD1
here because of a signature mismatch. This function call returns a
result of shape S$1 where a result of shape S$3 is required. Note:
this error occurred in the context (UPDATE-FLD1 S$1 3).
|#
;;; (4) [Sol Swords provided this example, where the arguments of update-fld1
;;; are in the correct order and we should report an output error, since
;;; the inputs are legal.]
(must-fail^
(defun foo (s$1 s$2)
(declare (xargs :stobjs (s$1 s$2)))
(let ((s$1 (update-fld1 0 s$2)))
(mv s$1 s$2)))
)
#|
ACL2 Error in ( DEFUN FOO ...): It is illegal to invoke UPDATE-FLD1
here because of a signature mismatch. This function call returns a
result of shape S$2 (after accounting for the replacement of some input
stobjs by congruent stobjs) where a result of shape S$1 is required.
Note: this error occurred in the context (UPDATE-FLD1 0 S$2).
|#
;;; (5) [The following example, a trivial renaming of one provided by Sol
;;; Swords, illustrates why stobjs-in-out does the best it can even when
;;; that ultimately still results in an error. If stobjs-in-out instead
;;; returned the stobjs-in and stobjs-out without accommodating the
;;; congruence of s$1 and s$2, then the complaint would be that s$2 is
;;; being used where s$1 is expected, even though that isn't the error.]
(defun foo (s$1 s$3)
(declare (xargs :stobjs (s$1 s$3))
(ignore s$1))
s$3)
(must-fail^
(defun bar (s$2 s$3)
(declare (xargs :stobjs (s$2 s$3)))
(let ((s$3 (foo s$2 s$3x)))
s$3))
)
#|
ACL2 Error in ( DEFUN BAR ...): The form S$3X is being used, as an
argument to a call of FOO, where the single-threaded object S$3 is
required. Note that the variable S$3 is required, not merely a term
that returns such a single-threaded object, so you may need to bind
S$3 with LET; see :DOC stobj. Note: this error occurred in the context
(FOO S$2 S$3X).
|#
(defun bar (s$2 s$3)
(declare (xargs :stobjs (s$2 s$3)))
(let ((s$3 (foo s$2 s$3)))
s$3))
|