/usr/share/acl2-6.3/books/tools/flag.lisp is in acl2-books-source 6.3-5.
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 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 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 | ; Make-flag -- Introduce induction scheme for mutually recursive functions.
; Copyright (C) 2008-2010 Centaur Technology
;
; Contact:
; Centaur Technology Formal Verification Group
; 7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
; http://www.centtech.com/
;
; This program is free software; you can redistribute it and/or modify it under
; the terms of the GNU General Public License as published by the Free Software
; Foundation; either version 2 of the License, or (at your option) any later
; version. This program is distributed in the hope that it will be useful but
; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
; more details. You should have received a copy of the GNU General Public
; License along with this program; if not, write to the Free Software
; Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
;
; Original authors: Sol Swords and Jared Davis
; {sswords,jared}@centtech.com
#|| for interactive development, you'll need to ld the package first:
(ld ;; fool dependency scanner
"flag-package.lsp")
||#
(in-package "FLAG")
(include-book "xdoc/top" :dir :system)
(defxdoc make-flag
:parents (mutual-recursion)
:short "Create a flag-based @(see induction) scheme for a @(see
mutual-recursion)."
:long "<p>The @('make-flag') macro lets you quickly introduce:</p>
<ul>
<li>a \"flag function\" that mimics a @(see mutual-recursion), and</li>
<li>a macro for proving properties by induction according to the flag
function.</li>
</ul>
<p>Generally speaking, writing a corresponding flag function is the first step
toward proving any inductive property about mutually recursive definitions;
more discussion below.</p>
<h3>Using @('make-flag')</h3>
<p>Example:</p>
@({
(make-flag flag-pseudo-termp ; flag function name
pseudo-termp ; any member of the clique
;; optional arguments:
:flag-mapping ((pseudo-termp . term)
(pseudo-term-listp . list))
:defthm-macro-name defthm-pseudo-termp
:flag-var flag
:hints ((\"Goal\" ...)) ; for the measure theorem
; usually not necessary
)
})
<p>Here @('pseudo-termp') is the name of a function in a mutually recursive
clique. In this case, the clique has two functions, @('pseudo-termp') and
@('pseudo-term-listp'). name of the newly generated flag function will be
@('flag-pseudo-termp').</p>
<p>The other arguments are optional:</p>
<ul>
<li>@(':flag-mapping') specifies short names to identify with each of the
functions of the clique. By default we just use the function names themselves,
but it's usually nice to pick shorter names since you'll have to mention them
in the theorems you prove.</li>
<li>@(':defthm-macro-name') lets you name the new macro that will be generated
for proving theorems by inducting with the flag function. By default it is
named @('defthm-[flag-function-name]'), i.e., for the above example, it would
be called @('defthm-flag-psuedo-termp').</li>
<li>@(':flag-var') specifies the name of the variable to use for the flag. By
default it is just called @('flag'), and we rarely change it. To be more
precise, it is @('pkg::flag') where @('pkg') is the package of the new flag
function's name; usually this means you don't have to think about the
package.</li>
</ul>
<h3>Proving Theorems with @('make-flag')</h3>
<p>To prove an inductive theorem about a mutually-recursive function, usually
one has to effectively prove a big ugly formula that has a different case for
different theorem about each function in the clique.</p>
<p>Normally, even with the flag function written for you, this would be a
tedious process. Here is an example of how you might prove by induction that
@('pseudo-termp') and @('pseudo-term-listp') return Booleans:</p>
@({
;; ACL2 can prove these are Booleans even without induction due to
;; type reasoning, so for illustration we'll turn these off so that
;; induction is required:
(in-theory (disable (:type-prescription pseudo-termp)
(:type-prescription pseudo-term-listp)
(:executable-counterpart tau-system)))
;; Main part of the proof, ugly lemma with cases. Note that we
;; have to use :rule-classes nil here because this isn't a valid
;; rewrite rule.
(local (defthm crux
(cond ((equal flag 'term)
(booleanp (pseudo-termp x)))
((equal flag 'list)
(booleanp (pseudo-term-listp lst)))
(t
t))
:rule-classes nil
:hints((\"Goal\" :induct (flag-pseudo-termp flag x lst)))))
;; Now we have to re-prove each part of the lemma so that we can use
;; it as a proper rule.
(defthm type-of-pseudo-termp
(booleanp (pseudo-termp x))
:rule-classes :type-prescription
:hints((\"Goal\" :use ((:instance crux (flag 'term))))))
(defthm type-of-pseudo-term-listp
(booleanp (pseudo-term-listp lst))
:rule-classes :type-prescription
:hints((\"Goal\" :use ((:instance crux (flag 'list))))))
})
<p>Obviously this is tedious and makes you say everything twice. Since the
steps are so standard, @('make-flag') automatically gives you a macro to
automate the process. Here's the same proof, done with the new macro:</p>
@({
(defthm-pseudo-termp
(defthm type-of-pseudo-termp
(booleanp (pseudo-termp x))
:rule-classes :type-prescription
:flag term)
(defthm type-of-pseudo-term-listp
(booleanp (pseudo-term-listp lst))
:rule-classes :type-prescription
:flag list))
})
<p>It's worth understanding some of the details of what's going on here.</p>
<p>The macro automatically tries to induct using the induction scheme. But
<color rgb=\"#ff0000\">this only works if you're using the formals of the
flag function as the variable names in the theorems.</color> In the case of
@('pseudo-termp'), this is pretty subtle: ACL2's definition uses different
variables for the term/list cases, i.e.,</p>
@({
(mutual-recursion
(defun pseudo-termp (x) ...)
(defun pseudo-term-listp (lst) ...))
})
<p>So the theorem above only works without hints because we happened to choose
@('x') and @('lst') as our variables. If, instead, we wanted to use different
variable names in our theorems, we'd have to give an explicit induction hint.
For example:</p>
@({
(defthm-pseudo-termp
(defthm type-of-pseudo-termp
(booleanp (pseudo-termp term))
:rule-classes :type-prescription
:flag term)
(defthm type-of-pseudo-term-listp
(booleanp (pseudo-term-listp termlist))
:rule-classes :type-prescription
:flag list)
:hints((\"Goal\" :induct (flag-pseudo-termp flag term termlist))))
})
<h3>Bells and Whistles</h3>
<p>Sometimes you may only want to export one of the theorems. For instance, if
we only want to add a rule about the term case, but no the list case, we could
do this:</p>
@({
(defthm-pseudo-termp
(defthm type-of-pseudo-termp
(booleanp (pseudo-termp x))
:rule-classes :type-prescription
:flag term)
(defthm type-of-pseudo-term-listp
(booleanp (pseudo-term-listp lst))
:flag list
:skip t))
})
<p>Sometimes the theorem you want is inductive in such a way that some
functions are irrelevant; nothing needs to be proved about them in order to
prove the desired theorem about the others. The :skip keyword can be used with
a theorem of T to do this:</p>
@({
(defthm-pseudo-termp
(defthm type-of-pseudo-termp
(booleanp (pseudo-termp x))
:rule-classes :type-prescription
:flag term)
(defthm type-of-pseudo-term-listp
t
:flag list
:skip t))
})
<p>Alternatively, you can provide the :skip-others keyword to the top-level
macro and simply leave out the trivial parts:</p>
@({
(defthm-pseudo-termp
(defthm type-of-pseudo-termp
(booleanp (pseudo-termp x))
:rule-classes :type-prescription
:flag term)
:skip-others t)
})
<p>There is an older, alternate syntax for @('make-flag') that is still
available. To encourage transitioning to the new syntax, the old syntax is not
documented and should usually not be used.</p>
<h3>Advanced Hints</h3>
<p>For advanced users, note that each individual \"theorem\" can have its own
computed hints. For instance we can write:</p>
@({
(defthm-pseudo-termp
(defthm type-of-pseudo-termp
(booleanp (pseudo-termp term))
:rule-classes :type-prescription
:flag term
:hints ('(:expand ((pseudo-termp x))))
(defthm type-of-pseudo-term-listp
(booleanp (pseudo-term-listp termlist))
:rule-classes :type-prescription
:flag list
:hints ('(:expand ((pseudo-term-listp lst)))))
:hints((\"Goal\" :induct (flag-pseudo-termp flag term termlist))))
})
<p>These hints are used <b>during the mutually inductive proof</b>. Under the
top-level induction, we check the clause for the current subgoal to determine
the hypothesized setting of the flag variable, and provide the computed hints
for the appropriate case.</p>
<p>If you provide both a top-level hints form and hints on some or all of the
separate theorems, both sets of hints have an effect; try @(':trans1') on such
a defthm-flag-fn form to see what you get.</p>
<p>You may use subgoal hints as well as computed hints, but they will not have
any effect if the particular subgoal does not occur when those hints are in
effect. We simply translate subgoal hints to computed hints:</p>
@({
(\"Subgoal *1/5.2\" :in-theory (theory 'foo))
--->
(and (equal id (parse-clause-id \"Subgoal *1/5.2\"))
'(:in-theory (theory 'foo)))
})")
; Examples
#|
(include-book ;; this newline is so that this is ignored in dependency scanning
"tools/flag" :dir :system)
(FLAG::make-flag flag-pseudo-termp
pseudo-termp
:flag-var flag
:flag-mapping ((pseudo-termp . term)
(pseudo-term-listp . list))
;; :hints {for the measure theorem}
:defthm-macro-name defthm-pseudo-termp
;; make everything local but the defthm macro
:local t
)
; This introduces (flag-pseudo-termp flag x lst)
; Theorems equating it with pseudo-termp and pseudo-term-listp
; And the macro shown below.
(in-theory (disable (:type-prescription pseudo-termp)
(:type-prescription pseudo-term-listp)))
(defthm-pseudo-termp type-of-pseudo-termp
(term (booleanp (pseudo-termp x))
:rule-classes :rewrite
:doc nil)
(list (booleanp (pseudo-term-listp lst))
)
:hints(("Goal"
:induct (flag-pseudo-termp flag x lst))))
(defstobj term-bucket
(terms))
(mutual-recursion
(defun terms-into-bucket (x term-bucket)
;; Returns (mv number of terms added, term-bucket)
(declare (xargs :stobjs (term-bucket)
:verify-guards nil))
(cond ((or (atom x)
(quotep x))
(let ((term-bucket (update-terms (cons x (terms term-bucket)) term-bucket)))
(mv 1 term-bucket)))
(t
(mv-let (numterms term-bucket)
(terms-into-bucket-list (cdr x) term-bucket)
(let ((term-bucket (update-terms (cons x (terms term-bucket)) term-bucket)))
(mv (+ numterms 1) term-bucket))))))
(defun terms-into-bucket-list (x term-bucket)
(declare (xargs :stobjs (term-bucket)))
(if (atom x)
(mv 0 term-bucket)
(mv-let (num-car term-bucket)
(terms-into-bucket (car x) term-bucket)
(mv-let (num-cdr term-bucket)
(terms-into-bucket-list (cdr x) term-bucket)
(mv (+ num-car num-cdr) term-bucket))))))
(terms-into-bucket '(f x y z) term-bucket)
(FLAG::make-flag flag-terms-into-bucket
terms-into-bucket)
|#
(defthmd expand-all-hides
(equal (hide x) x)
:hints (("goal" :expand ((hide x)))))
(defun acl2::flag-is (x)
(declare (ignore x))
t)
(in-theory (disable acl2::flag-is (acl2::flag-is) (:type-prescription acl2::flag-is)))
(defevaluator flag-is-cp-ev flag-is-cp-ev-lst ((if a b c) (acl2::flag-is x) (not x)))
(defun flag-is-cp (clause name)
(declare (xargs :guard t))
(list (cons `(not (acl2::flag-is ',name))
clause)))
(defthm flag-is-cp-correct
(implies (and (pseudo-term-listp clause)
(alistp al)
(flag-is-cp-ev (acl2::conjoin-clauses
(flag-is-cp clause name))
al))
(flag-is-cp-ev (acl2::disjoin clause) al))
:hints (("goal" :expand ((:free (a b) (acl2::disjoin (cons a b))))
:in-theory (enable acl2::disjoin2 acl2::flag-is)
:do-not-induct t))
:rule-classes :clause-processor)
(program)
(defmacro id (form) form)
(defun get-clique-members (fn world)
(or (getprop fn 'recursivep nil 'current-acl2-world world)
(er hard 'get-clique-members "Expected ~s0 to be in a mutually-recursive nest.~%"
fn)))
(defun get-formals (fn world)
(getprop fn 'formals nil 'current-acl2-world world))
(defun get-body (fn world)
;; This gets the original, normalized or non-normalized body based on what
;; the user typed for the :normalize xarg. The use of "last" skips past
;; any other :definition rules that have been added since then.
(access def-body
(car (last (getprop fn 'def-bodies nil 'current-acl2-world world)))
:concl))
(defun get-measure (fn world)
(access justification
(getprop fn 'justification nil 'current-acl2-world world)
:measure))
(defun get-wfr (fn world)
(access justification
(getprop fn 'justification nil 'current-acl2-world world)
:rel))
(defun make-flag-measure-aux (alist world)
(cond ((and (consp alist)
(consp (cdr alist)))
(cons `(,(cdar alist) ,(get-measure (caar alist) world))
(make-flag-measure-aux (cdr alist) world)))
((consp alist)
(list `(otherwise ,(get-measure (caar alist) world))))
(t
(er hard 'make-flag-measure-aux "Never get here."))))
(defun make-flag-measure (flag-var alist world)
(declare (xargs :guard (symbolp flag-var)
:mode :program))
`(case ,flag-var
. ,(make-flag-measure-aux alist world)))
(defun merge-formals (alist world)
(if (consp alist)
(union-eq (get-formals (caar alist) world)
(merge-formals (cdr alist) world))
nil))
(defun merge-actuals (alist formals)
;; The alist has in it (orig-formal . actual) pairs. Walk through formals
;; and replace any orig-formal with its actual; replace any unbound new
;; formals with nil.
(if (consp formals)
(cons (cdr (assoc-eq (car formals) alist))
(merge-actuals alist (cdr formals)))
nil))
(mutual-recursion
(defun mangle-body (body fn-name alist formals world)
(cond ((atom body)
body)
((eq (car body) 'quote)
body)
((symbolp (car body))
(let ((lookup (assoc-eq (car body) alist))
(new-args (mangle-body-list (cdr body) fn-name alist formals world)))
(if lookup
(let* ((orig-formals (get-formals (car lookup) world))
(new-actuals (merge-actuals (pairlis$ orig-formals new-args) formals)))
`(,fn-name ',(cdr lookup) . ,new-actuals))
(cons (car body) new-args))))
(t
(let ((lformals (cadar body))
(lbody (caddar body))
(largs (cdr body)))
(cons (list 'lambda
lformals
(mangle-body lbody fn-name alist formals world))
(mangle-body-list largs fn-name alist formals world))))))
(defun mangle-body-list (list fn-name alist formals world)
(if (consp list)
(cons (mangle-body (car list) fn-name alist formals world)
(mangle-body-list (cdr list) fn-name alist formals world))
nil)))
(defun make-flag-body-aux (flag-var fn-name formals alist full-alist world)
(if (consp alist)
(let* ((orig-body (get-body (caar alist) world))
(new-body (mangle-body orig-body fn-name full-alist formals world)))
(cond ((consp (cdr alist))
(cons `((equal ,flag-var ',(cdar alist)) ,new-body)
(make-flag-body-aux flag-var fn-name formals (cdr alist) full-alist world)))
(t
(list `(t ,new-body)))))
(er hard 'make-flag-body-aux "Never get here.")))
(defun make-flag-body (fn-name flag-var alist hints ruler-extenders world)
(let ((formals (merge-formals alist world)))
`(defun-nx ,fn-name (,flag-var . ,formals)
(declare (xargs :verify-guards nil
:normalize nil
:measure ,(make-flag-measure flag-var alist world)
:hints ,hints
,@(and ruler-extenders
`(:ruler-extenders ,ruler-extenders))
:well-founded-relation ,(get-wfr (caar alist)
world)
:mode :logic)
(ignorable . ,formals))
(cond
.
,(make-flag-body-aux flag-var fn-name formals alist alist world)))))
(defun extract-keyword-from-args (kwd args)
(if (consp args)
(if (eq (car args) kwd)
(if (consp (cdr args))
(cadr args)
(er hard "Expected something to follow ~s0.~%" kwd))
(extract-keyword-from-args kwd (cdr args)))
nil))
(defun throw-away-keyword-parts (args)
(if (consp args)
(if (keywordp (car args))
nil
(cons (car args)
(throw-away-keyword-parts (cdr args))))
nil))
(defun translate-subgoal-to-computed-hints (hints)
(declare (xargs :mode :program))
(if (atom hints)
nil
(cons (if (and (consp (car hints))
(stringp (caar hints)))
(let ((id (acl2::parse-clause-id (caar hints))))
`(and (equal id ',id)
',(cdar hints)))
(car hints))
(translate-subgoal-to-computed-hints (cdr hints)))))
(defun find-flag-hyps (flagname clause)
(declare (xargs :mode :program))
(if (atom clause)
(mv nil nil)
(let ((lit (car clause)))
(flet ((eql-hyp-case
(a b flagname clause)
(cond ((and (equal a flagname) (quotep b))
(mv b nil))
((and (equal b flagname) (quotep a))
(mv a nil))
(t (find-flag-hyps flagname (cdr clause)))))
(uneql-hyp-case
(a b flagname clause)
(mv-let (equiv rest)
(find-flag-hyps flagname (cdr clause))
(if equiv
(mv equiv nil)
(cond ((and (equal a flagname) (quotep b))
(mv nil (cons b rest)))
((and (equal b flagname) (quotep a))
(mv nil (cons a rest)))
(t (mv nil rest)))))))
(case-match lit
(('not ('equal a b))
(eql-hyp-case a b flagname clause))
(('not ('eql a b))
(eql-hyp-case a b flagname clause))
(('equal a b)
(uneql-hyp-case a b flagname clause))
(('eql a b)
(uneql-hyp-case a b flagname clause))
(& (find-flag-hyps flagname (cdr clause))))))))
(defun flag-hint-cases-fn (flagname cases clause)
(declare (xargs :mode :program))
(mv-let (equiv inequivs)
(find-flag-hyps flagname clause)
(let ((flagval (or equiv
(let* ((possibilities (strip-cars cases))
(not-ruled-out
(set-difference-eq possibilities
(acl2::strip-cadrs inequivs))))
(and (eql (len not-ruled-out) 1)
(list 'quote (car not-ruled-out))))))
(first (extract-keyword-from-args :first cases))
(cases (throw-away-keyword-parts cases)))
(and flagval
(let ((hints (cdr (assoc (cadr flagval) cases))))
`(:computed-hint-replacement
(,@first . ,(translate-subgoal-to-computed-hints hints))
:clause-processor (flag-is-cp clause ,flagval)))))))
(defmacro flag-hint-cases (flagname &rest cases)
`(flag-hint-cases-fn ',flagname ',cases clause))
(defun flag-from-thmpart (thmpart)
(if (eq (car thmpart) 'defthm)
(extract-keyword-from-args :flag thmpart)
(car thmpart)))
(defun assoc-flag-in-thmparts (flag thmparts)
(if (atom thmparts)
nil
(if (eq (flag-from-thmpart (car thmparts)) flag)
(car thmparts)
(assoc-flag-in-thmparts flag (cdr thmparts)))))
(defun pair-up-cases-with-thmparts (flag-var alist thmparts skip-ok)
;; Each thmpart is an thing like
;; _either_ (flag <thm-body> :name ... :rule-classes ... :doc ...)
;;; (for backwards compatibility)
;; _or_ (defthm <thmname> <thm-body> :flag ... :rule-classes ... :doc ...)
(if (consp alist)
(let* ((flag (cdar alist))
(lookup (assoc-flag-in-thmparts flag thmparts)))
(if (and (not lookup) (not skip-ok))
(er hard 'pair-up-cases-with-thmparts
"Expected there to be a case for the flag ~s0.~%" flag)
(let ((body (cond ((not lookup) t)
((eq (car lookup) 'defthm)
;; (defthm name body ...)
(caddr lookup))
(t ;; (flag body ...)
(cadr lookup)))))
(if (consp (cdr alist))
(cons `((equal ,flag-var ',flag) ,body)
(pair-up-cases-with-thmparts flag-var (cdr alist) thmparts skip-ok))
(list `(t ,body))))))
(er hard 'pair-up-cases-with-thmparts
"Never get here.")))
(defun pair-up-cases-with-hints (alist thmparts skip-ok)
;; Each thmpart is an thing like
;; _either_ (flag <thm-body> :name ... :rule-classes ... :doc ...)
;;; (for backwards compatibility)
;; _or_ (defthm <thmname> <thm-body> :flag ... :rule-classes ... :doc ...)
(if (consp alist)
(let* ((flag (cdar alist))
(lookup (assoc-flag-in-thmparts flag thmparts)))
(if (not lookup)
(if skip-ok
(cons (cons flag nil)
(pair-up-cases-with-hints (cdr alist) thmparts skip-ok))
(er hard 'pair-up-cases-with-hints
"Expected there to be a case for the flag ~s0.~%" flag))
(let ((hints (extract-keyword-from-args :hints lookup)))
(cons (cons flag hints)
(pair-up-cases-with-hints (cdr alist) thmparts skip-ok)))))
nil))
(defun flag-thm-entry-thmname (explicit-name flag entry)
(if (eq (car entry) 'defthm)
(cadr entry)
(or (extract-keyword-from-args :name (cddr entry))
(if explicit-name
(intern-in-package-of-symbol
(concatenate 'string
(symbol-name explicit-name)
"-"
(symbol-name flag))
explicit-name)
(er hard 'flag-thm-entry-thmname
"~
Expected an explicit name for each theorem, since no general name was
given. The following theorem does not have a name: ~x0~%" entry)))))
(defun make-defthm-macro-fn-aux (lemma-name explicit-name flag-var alist thmparts)
;; We have just proven the lemma and it's time to instantiate it to
;; give us each thm.
(if (consp alist)
(let* ((flag (cdar alist))
(lookup (assoc-flag-in-thmparts flag thmparts)))
(if (or (not lookup)
(extract-keyword-from-args :skip (cddr lookup)))
(make-defthm-macro-fn-aux
lemma-name explicit-name flag-var (cdr alist) thmparts)
;; Not checking for lookup, already did it when we did cases.
(let ((this-name
(flag-thm-entry-thmname explicit-name flag lookup))
(body (if (eq (car lookup) 'defthm)
(caddr lookup)
(cadr lookup)))
(rule-classes (let ((mem (member :rule-classes (cddr lookup))))
(if mem (cadr mem) :rewrite)))
(doc (extract-keyword-from-args :doc (cddr lookup))))
(cons `(defthm ,this-name
,body
:rule-classes ,rule-classes
:doc ,doc
:hints(("Goal"
:in-theory (theory 'minimal-theory)
:use ((:instance ,lemma-name (,flag-var ',flag))))))
(make-defthm-macro-fn-aux
lemma-name explicit-name flag-var (cdr alist) thmparts)))))
nil))
(defun find-first-thm-name (thmparts)
(if (atom thmparts)
(er hard? 'find-first-thm-name
"No explicit name given, and no theorems are given names?")
(if (extract-keyword-from-args :skip (cddr (car thmparts)))
(find-first-thm-name (cdr thmparts))
(flag-thm-entry-thmname
nil (flag-from-thmpart (car thmparts)) (car thmparts)))))
(defun make-defthm-macro-fn (args alist flag-var flag-fncall)
(let* ((explicit-name (and (symbolp (car args)) (car args)))
(args (if explicit-name (cdr args) args))
(thmparts (throw-away-keyword-parts args))
(name (if explicit-name
(intern-in-package-of-symbol
(concatenate 'string "FLAG-LEMMA-FOR-"
(symbol-name explicit-name))
explicit-name)
(intern-in-package-of-symbol
(concatenate 'string "FLAG-LEMMA-FOR-"
(symbol-name
(find-first-thm-name thmparts)))
(car flag-fncall))))
(instructions (extract-keyword-from-args :instructions args))
(user-hints (extract-keyword-from-args :hints args))
(no-induction-hint (extract-keyword-from-args :no-induction-hint args))
(skip-ok (extract-keyword-from-args :skip-others args))
(hints (and (not instructions)
(append
(cond (no-induction-hint user-hints)
((and (consp (car user-hints))
(stringp (caar user-hints))
(equal (string-upcase (caar user-hints))
"GOAL"))
;; First hint is for goal.
(if (extract-keyword-from-args :induct (car user-hints))
;; Explicit induct hint is provided; do not override.
user-hints
;; Provide our induct hint in addition to the hints
;; provided in goal.
(cons `("Goal" :induct ,flag-fncall
. ,(cdar user-hints))
(cdr user-hints))))
;; No goal hint; cons our induction hint onto the rest.
(t (cons `("Goal" :induct ,flag-fncall)
user-hints)))
(list
`(flag-hint-cases
,flag-var
. ,(pair-up-cases-with-hints alist thmparts skip-ok)))))))
`(progn
(encapsulate
()
(local (defthm ,name
(cond . ,(pair-up-cases-with-thmparts
flag-var alist thmparts skip-ok))
:rule-classes nil
:hints ,hints
:instructions ,instructions
:otf-flg ,(extract-keyword-from-args :otf-flg args)))
. ,(make-defthm-macro-fn-aux name explicit-name flag-var alist thmparts))
(value-triple ',name))))
(defun make-defthm-macro (real-macro-name alist flag-var flag-fncall)
`(defmacro ,real-macro-name (&rest args) ;; was (name &rest args)
(make-defthm-macro-fn args ',alist ',flag-var ',flag-fncall)))
(defun make-cases-for-equiv (alist world)
(if (consp alist)
(let* ((fn (caar alist))
(flag (cdar alist))
(fn-formals (get-formals fn world)))
(if (consp (cdr alist))
(cons `(,flag (,fn . ,fn-formals))
(make-cases-for-equiv (cdr alist) world))
(list `(otherwise (,fn . ,fn-formals)))))
nil))
(defun equiv-theorem-cases (flag-fn formals alist world)
(if (consp alist)
(let* ((fn (caar alist))
(flag (cdar alist))
(fn-formals (get-formals fn world)))
(cons `(equal (,flag-fn ',flag . ,formals)
(,fn . ,fn-formals))
(equiv-theorem-cases flag-fn formals (cdr alist) world)))
nil))
; LEGACY HINT. We found cases where EXPAND-CALLS-COMPUTED-HINT was too
; aggressive and expanded 'inner' terms that shouldn't have been expanded.
; We now FLAG-EXPAND-COMPUTED-HINT instead, which is more targetted for
; exactly the expansions we want.
; BOZO we are leaving expand-calls-computed-hint here only because Sol
; uses it in other places; we may wish to clean this up eventually and
; move it to some more appropriate file.
;; Collects up any calls of functions listed in FNS that are present in x.
(mutual-recursion
(defun find-calls-of-fns-term (x fns acc)
(cond ((or (atom x) (eq (car x) 'quote)) acc)
((member-eq (car x) fns)
(find-calls-of-fns-list (cdr x) fns (cons x acc)))
(t
(find-calls-of-fns-list (cdr x) fns acc))))
(defun find-calls-of-fns-list (x fns acc)
(if (atom x)
acc
(find-calls-of-fns-term
(car x) fns
(find-calls-of-fns-list (cdr x) fns acc)))))
;; Gives an expand hint for any function in FNS present in the
;; conclusion of the clause.
(defun expand-calls-computed-hint (clause fns)
(let ((expand-list (find-calls-of-fns-term (car (last clause)) fns nil)))
`(:expand ,expand-list)))
; NEW HINT: this more limited hint seems to be better.
(defun flag-expand-computed-hint (stable-under-simplificationp clause fns)
(and stable-under-simplificationp
(let ((conclusion (car (last clause))))
(case-match conclusion
(('equal lhs rhs)
(let* ((expands (if (and (consp lhs)
(member (car lhs) fns))
(list lhs)
nil))
(expands (if (and (consp rhs)
(member (car rhs) fns))
(cons rhs expands)
expands)))
(and expands
`(:expand ,expands))))
(&
nil)))))
(defun flag-table-events (alist entry)
(if (atom alist)
nil
(cons `(table flag-fns ',(caar alist) ',entry)
(flag-table-events (cdr alist) entry))))
(defun apply-formals-subst (formals subst)
(if (atom formals)
nil
(let ((look (assoc (car formals) subst)))
(if look
(cons (cdr look) (apply-formals-subst (cdr formals) subst))
(cons (car formals) (apply-formals-subst (cdr formals) subst))))))
(defun make-flag-fn (flag-fn-name clique-member-name flag-var flag-mapping hints
defthm-macro-name
formals-subst
local ruler-extenders world)
(let* ((flag-var (or flag-var
(intern-in-package-of-symbol "FLAG" flag-fn-name)))
(alist (or flag-mapping
(pairlis$ (get-clique-members clique-member-name world)
(get-clique-members clique-member-name world))))
(defthm-macro-name (or defthm-macro-name
(intern-in-package-of-symbol
(concatenate 'string "DEFTHM-" (symbol-name flag-fn-name))
flag-fn-name)))
(equiv-thm-name (intern-in-package-of-symbol
(concatenate 'string (symbol-name flag-fn-name) "-EQUIVALENCES")
flag-fn-name))
(formals (merge-formals alist world)))
`(,@(if local '(progn) '(encapsulate nil))
;; use encapsulate instead of progn so set-ignore-ok is local to this
(logic)
,@(and (not local) '((set-ignore-ok t))) ;; can't wrap this in local --- fubar!
(,(if local 'local 'id)
,(make-flag-body flag-fn-name flag-var alist hints ruler-extenders world))
,(make-defthm-macro defthm-macro-name alist flag-var
`(,flag-fn-name ,flag-var
. ,(apply-formals-subst formals formals-subst)))
(,(if local 'local 'id)
(with-output
:off (prove event) ;; hides induction scheme, too
(encapsulate nil
(logic)
(local (defthm flag-equiv-lemma
(equal (,flag-fn-name ,flag-var . ,formals)
(case ,flag-var
,@(make-cases-for-equiv alist world)))
:hints (("Goal"
:induct
(,flag-fn-name ,flag-var . ,formals)
:in-theory
(set-difference-theories
(union-theories (theory 'minimal-theory)
'((:induction ,flag-fn-name)
(:rewrite expand-all-hides)))
'(;; Jared found mv-nth to be slowing down a couple of flag
;; function admissions. Take it out of the minimal theory.
(:definition mv-nth)
;; Jared found a case where "linear" forced some goals
;; from an equality, which were unprovable. So, turn
;; off forcing.
(:executable-counterpart force))))
(flag-expand-computed-hint stable-under-simplificationp
ACL2::clause
',(cons flag-fn-name
(strip-cars
alist))))))
(defthm ,equiv-thm-name
(and . ,(equiv-theorem-cases flag-fn-name formals alist world))))))
(progn . ,(flag-table-events alist `(,flag-fn-name
,alist
,defthm-macro-name
,equiv-thm-name)))
(,(if local 'local 'id)
(in-theory (disable (:definition ,flag-fn-name)))))))
(defmacro make-flag (flag-fn-name clique-member-name
&key
flag-var
flag-mapping
formals-subst
hints
defthm-macro-name
local
ruler-extenders)
`(make-event (make-flag-fn ',flag-fn-name
',clique-member-name
',flag-var
',flag-mapping
',hints
',defthm-macro-name
',formals-subst
',local
',ruler-extenders
(w state))))
;; Accessors for the records stored in the flag-fns table
(defun flag-present (fn world)
(consp (assoc-eq fn (table-alist 'flag::flag-fns world))))
(defun flag-fn-name (fn world)
(nth 0 (cdr (assoc-eq fn (table-alist 'flag::flag-fns world)))))
(defun flag-alist (fn world)
(nth 1 (cdr (assoc-eq fn (table-alist 'flag::flag-fns world)))))
(defun flag-defthm-macro (fn world)
(nth 2 (cdr (assoc-eq fn (table-alist 'flag::flag-fns world)))))
(defun flag-equivs-name (fn world)
(nth 3 (cdr (assoc-eq fn (table-alist 'flag::flag-fns world)))))
(logic) ;; so local events aren't skipped
#!ACL2
(local
; A couple tests to make sure things are working.
(encapsulate
()
(FLAG::make-flag flag-pseudo-termp
pseudo-termp
:flag-var flag
:flag-mapping ((pseudo-termp . term)
(pseudo-term-listp . list))
;; :hints {for the measure theorem}
:defthm-macro-name defthm-pseudo-termp
)
; This introduces (flag-pseudo-termp flag x lst)
; Theorems equating it with pseudo-termp and pseudo-term-listp
; And the macro shown below.
(in-theory (disable (:type-prescription pseudo-termp)
(:type-prescription pseudo-term-listp)))
;; A few syntactic variations on defining the same theorems:
(encapsulate
nil
(local (defthm-pseudo-termp type-of-pseudo-termp
(term (booleanp (pseudo-termp x))
:rule-classes :rewrite
:doc nil)
(list (booleanp (pseudo-term-listp lst))))))
(encapsulate
nil
(local (defthm-pseudo-termp type-of-pseudo-termp2
(defthm booleanp-of-pseudo-termp
(booleanp (pseudo-termp x))
:rule-classes :rewrite
:doc nil
:flag term)
:skip-others t)))
(encapsulate
nil
(local (in-theory (disable pseudo-termp pseudo-term-listp)))
(local (defthm-pseudo-termp type-of-pseudo-termp
(term (booleanp (pseudo-termp x))
:hints ('(:expand ((pseudo-termp x))))
:rule-classes :rewrite
:doc nil)
(list (booleanp (pseudo-term-listp lst))
:hints ('(:expand ((pseudo-term-listp lst))))))))
(encapsulate
nil
(local (defthm-pseudo-termp
(term (booleanp (pseudo-termp x))
:rule-classes :rewrite
:doc nil
:name type-of-pseudo-termp)
(list (booleanp (pseudo-term-listp lst))
:skip t))))
(encapsulate
nil
(local (defthm-pseudo-termp
(defthm type-of-pseudo-termp
(booleanp (pseudo-termp x))
:rule-classes :rewrite
:doc nil
:flag term)
(defthm type-of-pseudo-term-listp
(booleanp (pseudo-term-listp lst))
:flag list
:skip t))))
(encapsulate
nil
(local (defthm-pseudo-termp
(defthm type-of-pseudo-termp
(booleanp (pseudo-termp x))
:rule-classes :rewrite
:doc nil
:flag term)
(list
(booleanp (pseudo-term-listp lst))
:skip t))))
(defstobj term-bucket
(terms))
(mutual-recursion
(defun terms-into-bucket (x term-bucket)
;; Returns (mv number of terms added, term-bucket)
(declare (xargs :stobjs (term-bucket)
:verify-guards nil))
(cond ((or (atom x)
(quotep x))
(let ((term-bucket (update-terms (cons x (terms term-bucket)) term-bucket)))
(mv 1 term-bucket)))
(t
(mv-let (numterms term-bucket)
(terms-into-bucket-list (cdr x) term-bucket)
(let ((term-bucket (update-terms (cons x (terms term-bucket)) term-bucket)))
(mv (+ numterms 1) term-bucket))))))
(defun terms-into-bucket-list (x term-bucket)
(declare (xargs :stobjs (term-bucket)))
(if (atom x)
(mv 0 term-bucket)
(mv-let (num-car term-bucket)
(terms-into-bucket (car x) term-bucket)
(mv-let (num-cdr term-bucket)
(terms-into-bucket-list (cdr x) term-bucket)
(mv (+ num-car num-cdr) term-bucket))))))
(FLAG::make-flag flag-terms-into-bucket
terms-into-bucket)
;; previously this didn't work, now we set-ignore-ok to fix it.
(encapsulate
()
(set-ignore-ok t)
(mutual-recursion
(defun ignore-test-f (x)
(if (consp x)
(let ((y (+ x 1)))
(ignore-test-g (cdr x)))
nil))
(defun ignore-test-g (x)
(if (consp x)
(ignore-test-f (cdr x))
nil))))
(FLAG::make-flag flag-ignore-test
ignore-test-f)
))
|