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 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
|
;;;-*-Mode:LISP; Package: CHAOS; Base:10; Syntax:Common-lisp -*-
;;;
;;; Copyright (c) 2000-2018, Toshimi Sawada. All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;
(in-package :chaos)
#|=============================================================================
System:CHAOS
Module:thstuff
File:proof-struct.lisp
=============================================================================|#
#-:chaos-debug
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
#+:chaos-debug
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))
;;; **************************************************************************
;;; Here we define a generic proof structure, i.e. a tree of 'goal's.
;;;---------------------------------------------------------------------------
;;; TACTIC
;;;
(eval-when (:compile-toplevel :execute :load-toplevel)
(defstruct (tactic (:print-function pr-tactic))
(name nil :type symbol) ; name
(executor nil :type (or null symbol)) ; tactic executor
)
;; Simultaneous Induction
(defparameter .tactic-si. (make-tactic :name :si
:executor 'apply-si))
;; Case Analysis
(defparameter .tactic-ca. (make-tactic :name :ca
:executor 'apply-ca))
;; Theorem of Constants
(defparameter .tactic-tc. (make-tactic :name :tc
:executor 'apply-tc))
;; Implication
(defparameter .tactic-ip. (make-tactic :name :ip
:executor 'apply-ip))
;; Implication by modifying the goal by 'imply'
(defparameter .tactic-ip+. (make-tactic :name :ip+
:executor 'apply-ip+))
;; Reduction: goal sentence will be destructively changed
(defparameter .tactic-rd. (make-tactic :name :rd
:executor 'apply-rd))
;; Reduction: keeps original goal sentence if it is not reduced to be 'true'
(defparameter .tactic-rd-. (make-tactic :name :rd-
:executor 'apply-rd-))
;; Normalize goal sentenses
(defparameter .tactic-nf. (make-tactic :name :nf
:executor 'apply-nf))
;; Check contradiction, i.e. true = false
(defparameter .tactic-ct. (make-tactic :name :ct
:executor 'apply-ct))
;; Do nothing, used internally
(defparameter .tactic-nil. (make-tactic :name :nop
:executor 'apply-nil))
;; list of all builtin tactics
(defparameter .all-builtin-tactics.
(list .tactic-si. .tactic-ca. .tactic-tc. .tactic-ip. .tactic-ip+.
.tactic-rd. .tactic-rd-. .tactic-nf. .tactic-ct.))
;; default tatics is a seriase of SI CA TC IP RD.
(defparameter .default-tactics.
(list .tactic-si. .tactic-ca. .tactic-tc. .tactic-ip. .tactic-rd.))
;; this is not an ordinary tactic but a command, but it generates goals
(defparameter .tactic-ctf. (make-tactic :name :ctf
:executor 'apply-ctf))
;; this is not an ordinary tactic but a command, but it generates goals
(defparameter .tactic-csp. (make-tactic :name :csp
:executor 'apply-csp))
;; :init as def(ed) tactic
(defparameter .tactic-init. (make-tactic :name :init
:executor 'apply-init))
;; :ind, user defined induction scheme
(defparameter .tactic-ind. (make-tactic :name :ind
:executor 'apply-ind))
;; user defiled tactics: assoc list of (name . list-of-tactics)
;;
(defvar .user-defined-tactics. nil)
)
(defun canonicalize-tactic-name (name)
(when (stringp name) (setq name (string-upcase name)))
(when (symbolp name) (setq name (symbol-name name)))
name)
(defun pr-tactic (tactic &optional (stream *standard-output*) &rest ignore)
(declare (ignore ignore))
(format stream "[~a]" (tactic-name tactic)))
;;; get-builtin-tactic : name -> tactic
;;; given a name returns a tactic with this name
;;; name can be symbol or string. the serach is done in case insensitive.
;;;
(defun get-builtin-tactic (name)
(setq name (canonicalize-tactic-name name))
(find-if #'(lambda (x) (string-equal name (symbol-name (tactic-name x)))) .all-builtin-tactics.))
;;; tactic-name-is-builtin? : name -> bool
;;;
(defun tactic-name-is-builtin? (name)
(get-builtin-tactic name))
;;; sequence of tactic :defined
;;; :def <name> = (<tactic-name> ...)
;;;
(defstruct (tactic-seq (:include tactic)
(:print-function pr-tactic-seq))
(tactics nil :type list) ; list of tactics
)
(defun pr-tactic-seq (obj stream &rest ignore)
(declare (type tactic-seq obj)
(ignore ignore))
(let ((tactics (tactic-seq-tactics obj)))
(format stream "( ~{~a~^ ~a ~} )" (mapcar #'(lambda (x) (tactic-name x)) tactics))))
;;; :ctf/:csp as tactic
;;;
(defstruct (tactic-ctfp-common (:include tactic))
(minus nil :type (or null t)) ; t iff :ctf- or :csp-
(context nil :type (or null module)) ; context module
)
(defstruct (tactic-ctf (:include tactic-ctfp-common (executor 'apply-ctf-tactic))
(:print-function pr-tactic-ctf))
(form nil) ; term or equation
)
(defun pr-tactic-ctf (obj stream &rest ignore)
(declare (type tactic-ctf obj)
(ignore ignore))
(let ((form (tactic-ctf-form obj)))
(format stream ":ctf")
(when (tactic-ctf-minus obj)
(princ "-" stream))
(with-in-module ((tactic-ctf-context obj))
(cond ((axiom-p form)
(princ "{" stream)
(print-axiom-brief form stream)
(princ " .}"))
(t ; term
(princ "[" stream)
(term-print form stream)
(princ " .]"))))))
(defstruct (tactic-csp (:include tactic-ctfp-common (executor 'apply-csp-tactic))
(:print-function pr-tactic-csp))
(forms nil) ; list of equations
)
(defun pr-tactic-csp (obj stream &rest ignore)
(declare (type tactic-csp obj)
(ignore ignore))
(let ((forms (tactic-csp-forms obj)))
(format stream ":csp")
(with-in-module ((tactic-csp-context obj))
(when (tactic-csp-minus obj)
(princ "-" stream))
(princ "{" stream)
(dolist (ax forms)
(print-axiom-brief ax stream)
(princ " . " stream))
(princ "}" stream))))
;;; :init as tactic
;;;
(defstruct (tactic-init (:include tactic (executor 'apply-init-tactic))
(:print-function pr-tactic-init))
(axiom nil) ; axiom
(subst nil) ; substitution
(context nil) ; context module
(kind nil) ; how the axiom is specified, :label or :term
)
(defun pr-tactic-init (obj stream &rest ignore)
(declare (type tactic-init obj)
(ignore ignore))
(format stream ":init")
(with-in-module ((tactic-init-context obj))
(let ((*print-indent* (+ 2 *print-indent*)))
(if (eq (first (tactic-init-kind obj)) :label)
(format stream "[~a]" (second (tactic-init-kind obj)))
(progn
(princ "{")
(print-axiom-brief (tactic-init-axiom obj))
(princ " .}")))
(if (tactic-init-subst obj)
(progn
(print-next)
(princ "by subst form: ")
(princ (tactic-init-subst obj)))
(princ ".")))))
;;; :ind as tactic
;;;
(defstruct (tactic-ind (:include tactic (executor 'apply-ind-tactic)))
(vars nil)
(base nil)
(step nil))
;;; get-default-tactics
;;; returns the default tactics, i.e. (:si :ca :cs :tc :ip)
;;;
(defun get-default-tactics () .default-tactics.)
;;; make user defined tactic
;;;
(defun declare-tactic (name &rest tactic-names)
(let ((tactics nil))
(dolist (n tactic-names)
(let ((tactic (get-builtin-tactic n)))
(unless tactic
(with-output-chaos-error ('no-such-tactic)
(format t "No tactic with the name ~s" n)))
(push tactic tactics)))
(setq name (canonicalize-tactic-name name))
(setq .user-defined-tactics.
(acons name (nreverse tactics) .user-defined-tactics.))))
;;; =====
;;; FLAGS
;;; =====
(defvar *citp-show-rwl* nil)
;;; -------------------------------------------------------------------------------
;;; Various utils which controll 'switch' affected behaviour of the system .
;;;
;;; This variable controlls implicit applications of tactics.
;;; 'true' means CITP cares application of implicite applicatins of tactics
;;; such as 'normalization of the goal', 'contradiction check ('true = false')'.
;;; if this is 'false', CITP does only introduces proof schems defined.
(declaim (special *citp-spoiler*))
(defvar *citp-spoiler* nil)
(eval-when (:compile-toplevel :execute :load-toplevel)
(defmacro if-spoiler-on (&key then else)
`(if *citp-spoiler*
(progn ,then)
(progn ,else)))
(defmacro when-spoiler-on (&rest body)
`(when *citp-spoiler*
,@body))
(defmacro with-spoiler-on (&rest body)
`(let ((*citp-spoiler* t))
(declare (special *citp-spoiler*))
,@body))
)
(declaim (type fixnum *citp-max-flags*))
(defparameter *citp-max-flags* 10)
(defstruct (citp-flag-struct)
(name "" :type simple-string)
(value nil)
(hook #'(lambda(name value)
(declare (ignore name value))
nil)
:type (or function symbol)))
(declaim (type (simple-array * (10)) *citp-flags*))
(defvar *citp-flags*)
(eval-when (:execute :load-toplevel)
(setq *citp-flags* (make-array *citp-max-flags*)))
(defmacro citp-flag-struct (flag-index)
`(aref *citp-flags* ,flag-index))
(defmacro citp-flag (flag-index)
`(citp-flag-struct-value (aref *citp-flags* ,flag-index)))
(defmacro citp-flag-name (flag-index)
`(citp-flag-struct-name (aref *citp-flags* ,flag-index)))
(defmacro citp-flag-hook (flag-index)
`(citp-flag-struct-hook (aref *citp-flags* ,flag-index)))
;;; flag indexes
(defconstant citp-all 0)
(defconstant citp-verbose 1)
(defconstant citp-show-rwl 2)
(defconstant citp-spoiler 3)
(defconstant citp-print-message 4)
(defconstant citp-normalize-init 5)
(defconstant citp-normalize-lhs 6)
;;; FIND-CITP-FLAG-INDEX : Name -> Index
;;;
(defun find-citp-flag-index (given-name)
(declare (type simple-string given-name)
(values (or null fixnum)))
(let ((i 0)
(name (concatenate 'string "citp-" given-name)))
(declare (type fixnum i))
(dotimes (x *citp-max-flags*)
(when (string= name (citp-flag-name x))
(return-from find-citp-flag-index i))
(incf i))
nil))
;;; print-citp-flag : index -> void
;;;
(defun print-citp-flag (index)
(if (= index citp-all)
(do ((idx 1 (1+ idx)))
((<= *citp-max-flags* idx))
(pr-citp-flag-internal idx))
(pr-citp-flag-internal index)))
(defun pr-citp-flag-internal (index)
(unless (equal "" (citp-flag-name index))
(format t "~&- Flag ~a is ~a" (subseq (citp-flag-name index) 5) (if (citp-flag index) "on" "off"))))
;;; help-citp-flag : index
;;;
(defun help-citp-flag (index)
(let ((flag (citp-flag-struct index)))
flag))
;;; flag initialization
;;;
(defun initialize-citp-flag ()
(dotimes (idx *citp-max-flags*)
(setf (citp-flag-struct idx) (make-citp-flag-struct :value nil)))
(setf (citp-flag-name citp-all) "citp-all"
(citp-flag-name citp-verbose) "citp-verbose"
(citp-flag-name citp-show-rwl) "citp-show-rwl"
(citp-flag-name citp-spoiler) "citp-spoiler"
(citp-flag-name citp-print-message) "citp-print-message"
(citp-flag-name citp-normalize-init) "citp-normalize-init"
(citp-flag-name citp-normalize-lhs) "citp-normalize-lhs")
;; set default
(setf (citp-flag citp-print-message) t) ; others are 'off'
(setf (citp-flag citp-normalize-init) t)
(setf (citp-flag citp-normalize-lhs) t)
;; verbose flag hook
(setf (citp-flag-hook citp-verbose)
#'(lambda (name value)
(declare (ignore name))
(setf *citp-verbose* value)))
;; show-rwl hook
(setf (citp-flag-hook citp-show-rwl)
#'(lambda (name value)
(declare (ignore name))
(setf *citp-show-rwl* value)))
;; citp-spoiler hook
(setf (citp-flag-hook citp-spoiler)
#'(lambda (name value)
(declare (ignore name))
(setf *citp-spoiler* value)))
)
(eval-when (:execute :load-toplevel)
(initialize-citp-flag)
)
;;; -------------------------------------------------------------------------
;;; PTREE-NODE
;;; A node of a proof tree. Contains a goal as its datum.
;;;
(defstruct (ptree-node (:include bdag)
(:print-function pr-ptree-node))
(num-children 0 :type fixnum) ; number of children
(next-child 0 :type fixnum) ; next child to be proved
(my-num 0 :type fixnum) ; position in siblings, first = 1
(my-name "" :type string) ; name
(done nil)) ; t iff the node is dischaged
;;;-----------------------------------------------------------------------------
;;; PTREE : proof tree
;;; whole proof tree structure.
;;;
(defstruct (ptree (:print-function pr-ptree))
(context nil :type (or null module)) ; context module
(num-gen-const 0 :type fixnum) ; number of generated constants so far
(num-gen-const-ind 0 :type fixnum) ; number of generated constants for induction so far
(root nil :type (or null ptree-node)) ; root goal
(indvar-subst nil :type list) ; <variable> -> <constantForInduction>
(var-subst nil :type list) ; <variable> -> <constantForTheremOfConstants>
(defs-so-far nil :type list) ; :defined name so far
(constructor-ops nil :type list) ; list of constructor operators
)
;;;---------------------------------------------------------------------------
;;; GOAL
;;; a goal is a set of conditional sentences (in a form of CafeOBJ axiom) to be proved
;;; with context module, introduced constants, introduced axioms (hypothesis), e.t.c.
;;; it holds all the information about a goal.
;;;
(defstruct (goal (:print-function pr-goal))
(name "" :type string) ; the name of the goal, we will refer
; this goal by this name
(context nil :type (or null module)) ; context module
(constants nil :type list) ; list of (var . constant) introduced for TC/CA/SI
(ind-constants nil :type list) ; list of constants introduced for induction
(indvars nil :type list) ; list of induction variables
(skolems nil :type list) ; list of skolem functions
(assumptions nil :type list) ; list of hypothesis
(tactic nil :type (or null tactic)) ; tactic which derived this goal
(targets nil :type list) ; axioms to be proved
(proved nil :type list) ; proved targets
(critical-pairs nil :type list) ; list of critical pairs not yet axiomatized
(defs nil :type list) ; list of :defined tactics
(base-ops nil :type list) ; constructors
(step-ops nil :type list) ; induction step operators
(bases nil :type list) ; list of user specified induction base patterns
(hypos nil :type list) ; list of user specified hypothesis patterns
(steps nil :type list) ; list of user specifief induction step patterns
)
(defun goal-is-discharged (goal)
(declare (type goal goal))
(null (goal-targets goal)))
(defun get-module-simple-name (module)
(module-print-name module))
(defun pr-goal (goal &optional (stream *standard-output*) &rest ignore)
(declare (type goal goal)
(type stream stream)
(ignore ignore))
(let ((*print-line-limit* 80)
;; (*print-xmode* :fancy)
)
(with-in-module ((goal-context goal))
(if (goal-tactic goal)
(format stream "~%~a=>~%:goal { ** ~a -----------------------------------------"
(goal-tactic goal) (goal-name goal))
(format stream "~%:goal { ** ~a -----------------------------------------"
(goal-name goal)))
(let ((*print-indent* (+ 2 *print-indent*))
(v-consts (goal-constants goal))
(i-consts (goal-ind-constants goal))
(skolems (goal-skolems goal))
(ass (goal-assumptions goal))
(vs (goal-indvars goal))
(axs (goal-targets goal))
(proved (goal-proved goal))
(discharged (goal-is-discharged goal))
(bases (goal-bases goal))
(steps (goal-steps goal)))
(print-next)
(format stream "-- context module: ~a"
(get-module-simple-name (ptree-context *proof-tree*)))
(when proved
(print-next)
(format stream "-- discharged sentence~p" (length proved))
(dolist (pv proved)
(let ((*print-indent* (+ 2 *print-indent*)))
(print-next)
(print-axiom-brief pv) (princ " ."))))
(when vs
(print-next)
(format stream "-- induction variable~p" (length vs))
(dolist (v vs)
(let ((*print-indent* (+ 2 *print-indent*)))
(print-next)
(term-print-with-sort v))))
(when bases
(print-next)
(format stream "-- user specified induction base~p" (length bases))
(dolist (b bases)
(let ((*print-indent* (+ 2 *print-indent*)))
(print-next)
(term-print-with-sort b))))
(when steps
(print-next)
(format stream "-- user specified induction step~p" (length steps))
(dolist (s steps)
(let ((*print-indent* (+ 2 *print-indent*)))
(print-next)
(term-print-with-sort s))))
(when v-consts
(print-next)
(format stream "-- introduced constant~p" (length v-consts))
(dolist (const (reverse v-consts))
(let ((*print-indent* (+ 2 *print-indent*)))
(print-next)
(print-method-brief (term-head (cdr const))))))
(when i-consts
(print-next)
(format stream "-- constant~p for induction" (length i-consts))
(dolist (ic (reverse i-consts))
(let ((*print-indent* (+ 2 *print-indent*)))
(print-next)
(print-method-brief (term-head (cdr ic))))))
(when skolems
(print-next)
(format stream "-- introduced skolem function~p" (length skolems))
(dolist (sk skolems)
(let ((*print-indent* (+ 2 *print-indent*)))
(print-next)
(print-method-brief sk))))
(when ass
(print-next)
(format stream "-- introduced axiom~p" (length ass))
(dolist (as ass)
(let ((*print-indent* (+ 2 *print-indent*)))
(print-next)
(print-axiom-brief as) (princ " ."))))
(when axs
(print-next)
(format stream "-- sentence~p to be proved" (length axs))
(dolist (ax axs)
(let ((*print-indent* (+ 2 *print-indent*)))
(print-next)
(print-axiom-brief ax) (princ " ."))))
(format stream "~%}")
(if discharged
(format t " << proved >>"))))))
(defun pr-ptree-node (ptree-node &optional (stream *standard-output*) &rest ignore)
(declare (type ptree-node ptree-node)
(type stream stream)
(ignore ignore))
(format stream "[Node] sub nodes = ~d, discharged? = ~a ---------------"
(ptree-node-num-children ptree-node)
(ptree-node-done ptree-node))
(pr-goal (ptree-node-datum ptree-node) stream))
;;; get-defined-tactic
;;;
(defun get-defined-tactic (goal name)
(setq name (canonicalize-tactic-name name))
(let ((defs (goal-defs goal)))
(find-if #'(lambda (x) (string-equal name (canonicalize-tactic-name (tactic-name x)))) defs)))
;;; use-sentence-in-goal : goal list-axioms
;;;
(defun incorporate-sentences-into-module (module new-axs)
(unless module
(with-output-chaos-error ('no-context)
(format t "No context module is specified.")))
(with-in-module (module)
(dolist (ax new-axs)
(let ((*print-indent* (+ 2 *print-indent*)))
(print-next)
(print-axiom-brief ax))
(adjoin-axiom-to-module module ax)
(set-operator-rewrite-rule module ax))
(compile-module module)))
(defun use-sentences-in-goal (goal new-axs)
(declare (type goal goal))
(let ((num (length new-axs)))
(unless (zerop num)
(format t "~%** In goal ~s, use the following sentence~p as new axiom~p:" (goal-name goal) num num)
(incorporate-sentences-into-module (goal-context goal) new-axs)
(setf (goal-assumptions goal)
(append (goal-assumptions goal) new-axs)))))
(defun use-theory-in-goal (goal axiom theory-decl)
(declare (type goal goal)
(type axiom axiom))
(with-in-module ((goal-context goal))
(use-theory-in-module *current-module*
(term-head (axiom-lhs axiom))
theory-decl
"[:use]")))
(defun use-theory-in-module (module meth theory-decl &optional (msg-header ""))
(flet ((theory-decl-to-theory (td)
(let ((tdd (caar td)))
(cond ((member tdd '("assoc" "associative") :test #'equal)
(list :assoc))
((member tdd '("comm" "commutative") :test #'equal)
(list :comm))
(t (list (cons :id (cdar td))))))))
(let ((theory (theory-decl-to-theory theory-decl)))
(with-in-module (module)
(add-and-merge-method-theory meth theory module)
(let ((*print-indent* (+ 2 *print-indent*)))
(format t "~%~a adding operator theory ~a into module ~a" msg-header theory (module-print-name module))
(prepare-for-parsing module t)
(print-next)
(print-method-brief meth)
(print-next))))))
;;; embed-sentences-in-module (module new-axs)
;;;
(defparameter .citp-embed-module. (%module-decl* "for_embed_dummy" :module :system nil))
(defun make-embed-module (name context-module)
(let ((decl-form (copy-tree .citp-embed-module.)))
(setf (%module-decl-name decl-form) name)
(let ((embeded-module (eval-ast decl-form)))
(import-module embeded-module :protecting context-module))))
(defun embed-sentences-in-module (module new-axs)
(let ((num (length new-axs)))
(unless (zerop num)
(format t "~%[:embed] adding setence~p as new axiom~p into module ~a:" num num
(get-module-simple-name module))
(incorporate-sentences-into-module module new-axs))))
(defun embed-theory-in-module (module meth theory)
(use-theory-in-module module meth theory "[:embed]"))
;;; the-goal-needs-undo : goal -> bool
;;; returns t iff the goal is generated by :defined :ctf- or :csp-
;;;
(defun the-goal-needs-undo (goal)
(declare (type goal goal))
(let ((goal-tactic (goal-tactic goal)))
(and (tactic-ctfp-common-p goal-tactic)
(tactic-ctfp-common-minus goal-tactic))))
(defmacro ptree-node-goal (ptree-node)
`(ptree-node-datum ,ptree-node))
(defun clear-induction-scheme (ptree-node)
(let ((goal (ptree-node-goal ptree-node)))
(setf (goal-base-ops goal) nil
(goal-step-ops goal) nil
(goal-bases goal) nil
(goal-steps goal) nil)))
;;; initialize-ptree-node : ptree-node -> ptree-node
;;; discard existing child nodes.
;;;
(defun initialize-ptree-node (node &optional (no-warn nil))
(unless no-warn
(when (ptree-node-subnodes node)
(with-output-chaos-warning ()
(format t "Discarding exsisting ~d node~p"
(ptree-node-num-children node)
(length (ptree-node-subnodes node))))))
(setf (ptree-node-num-children node) 0
(ptree-node-subnodes node) nil)
node)
;;; node-is-discharged? : ptree-node -> Bool
;;; returns if the node's goal is discharged,
;;; i.e., own goal has no target axioms to be proved,
;;; or every subnodes are discharged.
;;;
(defun node-is-discharged? (node)
(let ((goal (ptree-node-goal node)))
(or (null (goal-targets goal))
(and (ptree-node-subnodes node)
(every #'(lambda (x) (node-is-discharged? x)) (ptree-node-subnodes node))))))
;;; make-it-unproved : ptree-node -> ptree-node'
;;;
(defun make-it-unproved (ptree-node)
(let ((goal (ptree-node-goal ptree-node)))
(setf (goal-targets goal) (append (goal-targets goal) (goal-proved goal)))))
;;; make-ptree-goal-name : ptree-node fixnum -> string
;;; goal has a name.
;;;
(defun make-ptree-goal-name (parent-node my-num)
(declare (type (or null ptree-node) parent-node)
(type fixnum my-num))
(if parent-node
(let ((p-name (goal-name (ptree-node-goal parent-node))))
(if (equal p-name "root")
(format nil "~d" my-num)
(format nil "~a-~d" p-name my-num)))
"root"))
;;; context module creator
;;;
(defparameter .next-context-module. (%module-decl* "next-context-dummy" :module :system nil))
(defun make-next-context-module-name (goal-name)
(declare (type string goal-name))
(format nil "#Goal-~a" goal-name))
;;; prepare-next-goal : ptree-node -> goal
;;; prepare next goal structure with associated context module
;;;
(defvar .goals-so-far. nil)
(defun prepare-next-goal (ptree-node &optional (tactic nil))
(let ((goal-name (make-ptree-goal-name ptree-node (incf (ptree-node-num-children ptree-node))))
(decl-form (copy-tree .next-context-module.)))
(setf (%module-decl-name decl-form) (make-next-context-module-name goal-name))
(let ((next-context (eval-ast decl-form))
(cur-goal (ptree-node-goal ptree-node))
(next-goal (make-goal :name goal-name
:tactic tactic)))
;; goal module is hidden from user
(setf (module-hidden next-context) t)
(push (%module-decl-name decl-form) .goals-so-far.)
;; import original context module
(import-module next-context :including (goal-context cur-goal))
;; inherit current goal
(setf (goal-context next-goal) next-context
(goal-constants next-goal) (goal-constants cur-goal)
(goal-ind-constants next-goal) (goal-ind-constants cur-goal)
(goal-indvars next-goal) (goal-indvars cur-goal)
(goal-skolems next-goal) (goal-skolems cur-goal)
(goal-assumptions next-goal) (goal-assumptions cur-goal)
(goal-defs next-goal) (goal-defs cur-goal)
(goal-base-ops next-goal) (goal-base-ops cur-goal)
(goal-step-ops next-goal) (goal-step-ops cur-goal)
(goal-bases next-goal) (goal-bases cur-goal)
(goal-steps next-goal) (goal-steps cur-goal))
(prepare-for-parsing next-context)
(setq *next-default-proof-node* nil) ; we reset the next default target
next-goal)))
;;; give-goal-name-each-in-order : ptree-node List(goal) -> void
;;; this is used for renaming goals and their context modules
;;; after applied a tactic.
;;;
(defun give-goal-name-each-in-order (parent-node list-goals)
(dolist (goal list-goals)
(let* ((gname (make-ptree-goal-name parent-node (incf (ptree-node-num-children parent-node))))
(mod-name (make-next-context-module-name gname)))
(setf (goal-name goal) gname)
(setf (module-name (goal-context goal)) mod-name))))
;;; make-ptree-root : module goal -> ptree-node
;;;
(defun make-ptree-root (context-module initial-goals)
(declare (type module context-module)
(type list initial-goals))
(let ((root-node (make-ptree-node :subnodes nil :parent nil)))
(setf (ptree-node-goal root-node)
(make-goal :name (make-ptree-goal-name nil (ptree-node-my-num root-node))
:context context-module
:skolems (reverse (module-skolem-functions context-module))
:targets initial-goals))
root-node))
;;; add-ptree-child : ptree-node module List(axiom) -> List(goal)
;;;
(defun add-ptree-child (parent-node child-goal)
(declare (type ptree-node parent-node)
(type goal child-goal))
(setf (ptree-node-subnodes parent-node)
(nconc (ptree-node-subnodes parent-node)
(list (make-ptree-node :datum child-goal
:my-num (ptree-node-num-children parent-node)
:subnodes nil
:parent parent-node)))))
;;; add-ptree-children : ptree-node List(goal) -> ptree-node'
;;; add node of given goals as a child of the node.
;;; before adding goal nodes, initialize the node and
;;; give each goal a name.
;;;
(defun add-ptree-children (parent-node list-goals)
(declare (type ptree-node parent-node)
(type list list-goals))
(initialize-ptree-node parent-node t)
;; give names to goals
(give-goal-name-each-in-order parent-node list-goals)
(dolist (goal list-goals)
(add-ptree-child parent-node goal))
parent-node)
;;; get-ptree-root : ptree-node -> ptree-node
;;;
(defun get-ptree-root (ptree-node)
(let ((parent (ptree-node-parent ptree-node)))
(unless parent (return-from get-ptree-root ptree-node))
(get-ptree-root parent)))
;;; ptree utils
;;;
;;; the-node-needs-undo
;;; returns t iff the node is generated by :def(ined)
;;; :ctf- or :csp-
(defun the-node-needs-undo (node)
(declare (type ptree-node node))
(the-goal-needs-undo (ptree-node-goal node)))
;;; parent-nedds-undo
;;; returns t iff the parent node is generated by :def(ined)
;;; :ctf- or :csp-
;;;
(defun parent-needs-undo (pnode)
(declare (type (or null ptree-node) pnode))
(let ((node (ptree-node-parent pnode)))
(unless node (return-from parent-needs-undo nil))
(the-node-needs-undo node)))
(defun pr-ptree (ptree &optional (stream *standard-output*) &rest ignore)
(declare (type ptree ptree)
(type stream stream)
(ignore ignore))
(let ((*standard-output* stream))
(format t "~%Proof Tree ===================================")
(with-in-module ((goal-context (ptree-node-goal (ptree-root ptree))))
(let ((indvar-subst (ptree-indvar-subst ptree))
(*print-indent* (+ 2 *print-indent*)))
(format t "~%-- induction variable bases:")
(if indvar-subst
(dolist (is indvar-subst)
(print-next)
(term-print-with-sort (car is))
(princ " => ")
(princ (cdr is)))
(progn (print-next) (princ "none" stream))))
(format stream "~%-- introduced constants:")
(let ((var-subst (ptree-var-subst ptree))
(*print-indent* (+ 2 *print-indent*)))
(if var-subst
(dolist (is var-subst)
(print-next)
(term-print-with-sort (car is))
(princ " => ")
(princ (cdr is)))
(progn (print-next) (princ "none" stream))))
(format stream "~%-- constructors:")
(let ((num 0))
(dolist (op (ptree-constructor-ops ptree))
(print-next)
(format t "(~d) " (incf num))
(print-method-brief op)))
(format stream "~%-- root node")
(pr-goal (ptree-node-goal (ptree-root ptree))))))
(defun reset-proof (ptree)
(setf (ptree-num-gen-const ptree) 0
(ptree-indvar-subst ptree) nil
(ptree-var-subst ptree) nil))
(defun existing-def-name? (ptree name)
(setq name (canonicalize-tactic-name name))
(member name (ptree-defs-so-far ptree) :test #'equal))
;;; PROOF
;;;
(defstruct citp-proof
(context nil) ; context module in which proof is performed
(discharged nil) ; list of proved sentences
)
(defvar *the-citp-proof* (make-citp-proof :context :none :discharged nil))
(defun get-discharged-sentence-with-label (label)
(declare (type string label))
(let ((name (intern label))
(res nil))
(dolist (ax (citp-proof-discharged *the-citp-proof*))
(when (member name (axiom-labels ax))
(push ax res)))
(when (> (length res) 1)
(with-output-chaos-warning ()
(format t "Found more than 1 sentences with name ~s." label)))
(unless res
(with-output-chaos-error ()
(format t "No such discharged sentence with name ~s." label)))
res))
(defun find-discharged-sentences-with-label (list-labels)
(let ((axs nil))
(dolist (label list-labels)
(setq axs (nconc axs
(get-discharged-sentence-with-label label))))
axs))
;;;--------------------------------------------------------------------
;;; Support functions for introducing new constants used in the proof.
;;;
;;; messaging when :verbose on
;;;
(eval-when (:compile-toplevel :execute :load-toplevel)
(defmacro when-citp-verbose (&rest body)
`(when *citp-verbose*
(let ((*print-indent* (+ 2 *print-indent*))
(*print-line-limit* 90))
(declare (type fixnum *print-indent* *print-line-limit*))
,@body)))
)
;;; citp standard running env.
;;;
(defvar *citp-silent* t)
(eval-when (:compile-toplevel :execute :load-toplevel)
(defmacro with-citp-env (&rest body)
`(if *citp-silent*
(let ((*chaos-quiet* t)
(*rwl-search-no-state-report*
(if *citp-show-rwl*
nil
t)))
,@body)
(progn
,@body)))
(defmacro with-next-context ((&optional (node *proof-tree*)) &rest body)
`(let ((.context. (get-next-proof-context ,node)))
(unless .context.
(with-output-chaos-error ('no-context)
(format t "No proof context is established.")))
(with-in-module ((goal-context (ptree-node-goal .context.)))
(with-citp-env ()
,@body))))
)
;;; for debugging
;;;
(eval-when (:compile-toplevel :execute :load-toplevel)
(defmacro with-citp-debug (&rest body)
`(when *debug-citp*
(let ((*print-indent* (+ 2 *print-indent*))
(*print-line-limit* 90))
(declare (type fixnum *print-indent* *print-line-limit*))
,@body)))
)
;;; intro-const-returns-subst : module name variable -> (variable . constant-term)
;;; introduces a new constant of sort(variable) into a module.
;;; returns a pair (variable . constant-term)
;;;
(defun citp-intro-const (module name sort)
(multiple-value-bind (op meth)
(declare-operator-in-module (list name)
nil ; arity
sort ; coarity
module ;
nil ; constructor?
nil ; behavioural? always nil.
nil ; not coherent
)
(declare (ignore op))
(prepare-for-parsing module t t) ; force
meth))
(defun intro-const-returns-subst (module name variable)
(cons variable (make-applform (variable-sort variable)
(citp-intro-const module name (variable-sort variable))
nil)))
;;; make-tc-const-name : variable -> string
;;;
(defun make-tc-const-name (name sort)
(format nil "~:@(~a~)@~a" name (string (sort-name sort))))
(defun make-tc-pconst-name (name)
(format nil "`~:@(~a~)" name))
;;; introduces on-the-fly constant
;;;
(defun intro-fresh-pconstant (goal name-seed sort)
(declare (ignore goal))
(let ((name (make-tc-pconst-name name-seed)))
(make-pconst-term sort name)))
;;; variable->constant : goal variable -> term
;;; In the given goal, introduces fresh constant which should substitute the given varirable.
;;; used by tactic TC.
;;;
(defun find-variable-subst-in (alist variable)
(assoc variable alist :test #'variable-equal))
(defun list-submodules (module)
(mapcar #'car (module-all-submodules module)))
(defun variable->constant (goal variable)
(let ((vc-assoc (find-variable-subst-in (goal-constants goal) variable)))
(or (cdr vc-assoc)
(let ((name (cdr (find-variable-subst-in (ptree-var-subst *proof-tree*) variable)))
(v-const nil))
(unless name
(setq name (make-tc-const-name (variable-name variable) (variable-sort variable)))
(push (cons variable name) (ptree-var-subst *proof-tree*)))
(setq v-const (intro-const-returns-subst (goal-context goal)
name
variable))
(push v-const (goal-constants goal))
(cdr v-const)))))
;;; -----------------------------------------------------------
;;; Utils for constructors
;;;
;;; gather-constructor-ops : module -> List(constructor)
;;; list up all the constructor ops in a given module
;;;
(defun gather-constructor-ops (module)
(flet ((method-is-user-defined-or-tf? (x)
(or (eq x *bool-true-meth*)
(eq x *bool-false-meth*)
(and (not (sort= (method-coarity x) *sort-id-sort*))
(module-is-user-module (method-module x))))))
(let ((res nil))
(with-in-module (module)
(dolist (opinfo (module-all-operators module))
(dolist (meth (opinfo-methods opinfo))
(when (and (method-is-constructor? meth)
(method-is-user-defined-or-tf? meth))
(push meth res))))
res))))
;;; get-constructors-of-sort : sort -> List[constructor]
;;; returns the list of constructors of sort.
;;;
(defun get-constructors-of-sort (sort &optional (context (get-context-module)))
(let ((ops nil))
(dolist (meth (ptree-constructor-ops *proof-tree*))
(when (and (method-is-constructor? meth)
(sort<= (method-coarity meth) sort (module-sort-order context)))
(push meth ops)))
(reverse ops)))
;;; default-constructor-order
;;;
(defun default-constructor-order (constructors)
(sort constructors
#'(lambda (x y)
;; first precedence is number of arguments
(let ((ax (method-num-args x))
(ay (method-num-args y)))
(if (< ax ay)
t
(if (> ax ay)
nil
(if (eq x *bool-true-meth*)
;; this orders true > ... > false
t
(if (eq y *bool-true-meth*)
nil
(if (eq (op-lex-precedence x y) :greater)
nil
t)))))))))
;;; order-constructors : List[constructor] -> List[constructor]'
;;;
(defun order-constructors (constructors &optional (order-spec nil))
(cond ((null order-spec) (default-constructor-order constructors))
(t
;; order is specified by :order command
;; first initialize the order to default
(let* ((pos-star (position :* order-spec))
(first-half (subseq order-spec 0 pos-star))
(second-half (subseq order-spec (1+ pos-star))))
(setq constructors (set-difference constructors
(append first-half second-half)))
(setq constructors (default-constructor-order constructors))
(append first-half constructors second-half)))))
;;; show-constructor-order : ptree -> void
;;;
(defun show-constructor-order (ptree)
(let ((num 0))
(with-output-msg ()
(format t "Current order of constructors:")
(dolist (op (ptree-constructor-ops ptree))
(print-next)
(format t "(~d) " (incf num))
(print-method-brief op)))
(terpri)))
;;; handler of :order command
;;; :order (<op>, ..., <op>)
;;;
(defun citp-eval-order (ast)
(declare (notinline %pn-lex-ops))
(check-context-module-and-ptree)
(with-in-module ((get-context-module))
(let ((optokens (%pn-lex-ops ast)))
(cond ((and (null (cdr optokens))
(null (car optokens)))
(show-constructor-order *proof-tree*))
(t (with-output-msg ()
(format t "start setting constructor ordering..."))
(set-constructor-order *proof-tree* optokens)
(format t "~%done.")
(when-citp-verbose ()
(show-constructor-order *proof-tree*)))))))
(defun set-constructor-order (ptree optokens)
(let ((prec-list nil))
(dolist (e optokens)
(cond ((equal e '("*"))
(if (not (memq :* prec-list))
(setq prec-list (append prec-list '(:*)))
(with-output-chaos-warning ()
(format t "* is already specified."))))
(t (let ((parsedop (parse-op-name e)))
(multiple-value-bind (ops mod)
(resolve-operator-reference parsedop)
(with-in-module (mod)
(let ((overs nil))
(dolist (opinfo ops)
(dolist (meth (opinfo-methods opinfo))
(if (method-is-constructor? meth)
(if (member meth prec-list)
(with-output-chaos-warning ()
(format t "operator ~{~a~}/~d is already ordered, ignored."
(method-symbol meth)
(method-num-args meth)))
(push meth overs))
(unless (method-is-error-method meth)
(with-output-chaos-warning ()
(format t "operator ~{~s~} is not a constructor, ignored."
(method-symbol meth)))))))
;; order overloaded ops by number of arguments
(setq overs (sort overs #'(lambda (x y)
(let ((x-num (method-num-args x))
(y-num (method-num-args y)))
(declare (type fixnum x-num y-num))
(< x-num y-num)))))
;; set the result
(setq prec-list (append prec-list overs)))))))))
(unless (memq :* prec-list)
(setq prec-list (append prec-list '(:*))))
(setf (ptree-constructor-ops ptree)
(order-constructors (ptree-constructor-ops ptree)
prec-list))))
;;; variable->constructor : goal variable op -> term
;;;
(defun make-ind-const-name (name-prefix sort)
(format nil "~a#~a" (string name-prefix) (string (sort-name sort))))
(defun variable->constructor (goal variable &key (sort nil) (op nil))
(let ((svar (if sort
(make-variable-term sort (intern (format nil "~a_~a"
(variable-name variable)
(sort-name sort))))
variable)))
(flet ((make-iv-const (name)
(if op
(let ((constant (make-applform (method-coarity op) op nil)))
(push (cons variable constant) (goal-ind-constants goal))
constant)
(let ((con (intro-const-returns-subst (goal-context goal)
name
svar)))
(push con (goal-ind-constants goal))
(cdr con)))))
(let ((v-assoc (find-variable-subst-in (goal-ind-constants goal) svar)))
(or (cdr v-assoc)
(let ((v-name (cdr (find-variable-subst-in (ptree-indvar-subst *proof-tree*) svar)))
(vconst nil))
(unless v-name
(setq v-name (make-ind-const-name (variable-name variable)
(or sort (variable-sort svar)))))
(setq vconst (make-iv-const v-name))
(pushnew (cons svar v-name) (ptree-indvar-subst *proof-tree*) :test #'equal)
vconst))))))
;;; intro-fresh-constant : goal -> term (introduced constant)
;;; introduces brand new constant in the current proof context
;;;
(defun intro-fresh-constant (goal name-seed sort)
(let* ((name (make-ind-const-name name-seed sort))
(meth (citp-intro-const (goal-context goal) name sort))
(v-const (make-applform sort meth nil)))
(push (cons meth v-const) (goal-ind-constants goal))
v-const))
;;; SKOLEMITIZE
;;; allow citp to represent the goal sentence in FOPLE-SENTENCE
(defun skolemize-if-need (fax)
(unless (eq (axiom-type fax) :pignose-axiom)
(return-from skolemize-if-need fax))
(with-citp-debug ()
(format t "~%[skolemize]: ")
(print-axiom-brief fax))
(let* ((sentence (axiom-lhs fax))
(type (fopl-sentence-type sentence))
(*sk-function-num* nil))
(declare (type symbol type)
(special *sk-function-num*))
(when (and (memq type '(:eq :beq))
(term-is-lisp-form? (term-arg-2 sentence)))
(return-from skolemize-if-need fax))
;; normalize quantified formula
;; \Q[v1...vn]S --> \Q[v1]\Q[v2]...\Q[vn]S
(normalize-quantifiers sentence)
;; convert to NNF(negation normal form.)
(setq sentence (neg-normal-form sentence))
;; skolemization -- eliminate \Es
(skolemize sentence)
;; skolemize may introduce new operators.
(prepare-for-parsing *current-module*)
;; eliminate quantifiers -- eliminate \As
(zap-quantifiers sentence)
;; convert to CNF(conjunctive normal form).
(conj-normal-form sentence)
;; make it an equation
(let ((ax (make-rule :lhs sentence
:rhs *bool-true*
:condition *bool-true*
:labels (axiom-labels fax)
:behavioural (axiom-is-behavioural fax)
:type :equation)))
(adjoin-axiom-to-module *current-module* ax)
ax)))
;;;
;;; initialize-proof-tree : module goal -> ptree
;;;
(defun initialize-proof-tree (context-module goal-module initial-goals)
(with-in-module (goal-module)
(let ((*sk-function-num* nil))
(declare (special *sk-function-num*))
(let* ((targets (mapcar #'skolemize-if-need initial-goals))
(root (make-ptree-root goal-module targets)))
(setq *next-default-proof-node* nil)
(make-ptree :root root
:context context-module
:constructor-ops
(order-constructors (gather-constructor-ops context-module) nil))))))
;;;
;;; check-success : ptree -> Bool
;;;
(defun check-success (ptree)
(let ((unp (get-unproved-nodes ptree)))
(when unp
(format t "~%>> Next target goal is ~s." (goal-name (ptree-node-goal (car unp))))
(setq *next-default-proof-node* (car unp))
(format t "~%>> Remaining ~d goal~p.~%" (length unp) (length unp))
(return-from check-success nil))
(format t "~%** All goals are successfully discharged.~%")
(save-discharged-sentences)
(setq *next-default-proof-node* nil)
t))
;;; ------------------------------------------------------------------
;;; roll-back : ptree -> Bool
;;; roll back to parent. returns true (non-nil) iff roll back is done.
;;;
(defun roll-back (ptree)
(declare (type ptree ptree))
(let* ((current-target (get-next-proof-context ptree))
(parent (and current-target (ptree-node-parent current-target))))
(unless parent
(format t "~%**> :roll back, already at root.")
(setq *next-default-proof-node* nil)
(return-from roll-back nil))
(setf (ptree-node-subnodes parent) nil
(ptree-node-num-children parent) 0
(ptree-node-next-child parent) 0)
(format t "~%**> :roll back")
(setq *next-default-proof-node* nil)
(setq current-target (get-next-proof-context ptree))
(when current-target
(format t "~% next default target is ~s" (goal-name (ptree-node-goal current-target))))
current-target))
;;;
;;; find-goal-node : ptree string -> { ptree-node | nil }
;;;
(defun find-goal-node (ptree name)
(declare (type ptree ptree)
(type string name))
(dag-wfs (ptree-root ptree)
#'(lambda (n) (let ((goal (ptree-node-goal n)))
(when (string= (goal-name goal) name)
(return-from find-goal-node n))))))
;;;
;;; print-named-goal : name -> void
;;;
(defun print-named-goal (ptree name)
(unless ptree
(with-output-chaos-warning ()
(format t "There is no proof tree.")
(return-from print-named-goal nil)))
(let ((goal-node (if name
(find-goal-node ptree name)
(or (get-next-proof-context ptree)
(with-output-chaos-error ('no-goal)
(princ "No default goal is specified."))))))
(unless goal-node
(with-output-chaos-error ('no-such-goal)
(format t "No such goal with the name ~s" name)))
(pr-goal (ptree-node-goal goal-node))))
;;;
;;; get-unproved-nodes : ptree -> List(ptree-node)
;;; uses depth first search
(defun get-unproved-nodes (ptree)
(let ((nodes nil))
(dag-dfs (ptree-root ptree)
#'(lambda (x)
(declare (type ptree-node))
(unless (or (ptree-node-subnodes x)
(goal-is-discharged (ptree-node-goal x)))
(push x nodes))))
(nreverse nodes)))
;;; get-unproved-goals : ptree -> List(goal)
;;;
(defun get-unproved-goals (ptree)
(mapcar #'(lambda (y) (ptree-node-goal y)) (get-unproved-nodes ptree)))
;;; print-unproved-goals
;;;
(defun print-unproved-goals (ptree &optional (stream *standard-output*))
(unless ptree
(with-output-chaos-warning ()
(format t "No goal is specified yet.")
(return-from print-unproved-goals nil)))
(dolist (goal (get-unproved-goals ptree))
(pr-goal goal stream)))
;;; get-next-pfoof-context : ptree -> ptree-node
;;;
(defun get-next-proof-context (ptree)
(and ptree (or *next-default-proof-node*
(car (get-unproved-nodes ptree)))))
(defun next-proof-target-is-specified? ()
*next-default-proof-node*)
;;; get-target-goal-node
;;; given goal-name or NULL, returns the next targetted goal node.
;;;
(defun get-target-goal-node (&optional goal-name)
(let ((next-goal-node (if goal-name
(find-goal-node *proof-tree* goal-name)
(get-next-proof-context *proof-tree*))))
(unless next-goal-node
(with-output-chaos-error ('no-target)
(if goal-name
(format t "Could not find the goal ~s." goal-name)
(format t "No default target goal."))))
next-goal-node))
;;;
;;; select-next-goal : goal-name
;;;
(defun select-next-goal (goal-name)
(declare (type string goal-name))
(unless *proof-tree*
(with-output-chaos-error ('no-proof-tree)
(format t "No proof is ongoing.")))
(cond ((string= goal-name ".")
(setq *next-default-proof-node* nil)
(let ((next (get-next-proof-context *proof-tree*)))
(format t "~%:select resetting next default target ...")
(unless next
(with-output-chaos-warning ()
(format t "There is no unproved goal.")
(return-from select-next-goal nil)))
(format t "~%>> next default-goal is ~s" (goal-name (ptree-node-goal next)))))
(t (let ((node (find-goal-node *proof-tree* goal-name)))
(unless node
(with-output-chaos-error ('no-goal)
(format t "No such goal ~s" goal-name)))
(when (node-is-discharged? node)
(with-output-chaos-warning ()
(format t "The goal ~s is alreaday discharged." (goal-name (ptree-node-goal node)))
(print-next)
(format t "This will discard the current status of the goal."))
(make-it-unproved node))
(setq *next-default-proof-node* node)
(when (eq node (ptree-root *proof-tree*))
(reset-proof *proof-tree*))
(format t "~%>> setting next default goal to ~s" (goal-name (ptree-node-goal node)))
node))))
;;; Getting TACTIC
;;; get-tactic : name -> LIST(tactic)
;;;
(defun get-tactic (name)
(let ((context (get-next-proof-context *proof-tree*)))
(let ((tactic (or (and context (get-defined-tactic (ptree-node-goal context) name))
(get-builtin-tactic name))))
(unless tactic
(with-output-chaos-error ('no-such-tactic)
(format t "No such tactic is defined with the name ~s" name)))
(if (atom tactic)
(list tactic)
tactic))))
;;; ====================
;;; TOP LEVEL FUNCTIONS
;;; ====================
;;; begin-proof
;;; starting the new proof
;;;
(defparameter .root-context-module. (%module-decl* "#Goal-root" :object :user nil))
(defun reset-proof-session ()
(with-output-simple-msg ()
(format t "** !! Discarding the current proof..."))
(setf (citp-proof-context *the-citp-proof*) nil
(citp-proof-discharged *the-citp-proof*) nil)
(setf .user-defined-tactics. nil)
(reset-proof *proof-tree*)
(setq *proof-tree* nil))
(defun citp-reset-proof-if-need (redefined-module)
(when (eq redefined-module (citp-proof-context *the-citp-proof*))
(reset-proof-session)))
(defun save-discharged-sentences ()
(setf (citp-proof-discharged *the-citp-proof*)
(append (citp-proof-discharged *the-citp-proof*)
(goal-targets (ptree-node-goal (ptree-root *proof-tree*))))))
(defun print-discharged-sentences ()
(let ((discharged (citp-proof-discharged *the-citp-proof*)))
(if discharged
(with-in-module ((citp-proof-context *the-citp-proof*))
(let ((*print-indent* (+ 2 *print-indent*)))
(format t "~&** Discharged sentence~p" (length discharged))
(format t "~%-- context module: ~a" (module-print-name *current-module*))
(dolist (ax discharged)
(print-next)
(print-axiom-brief ax)
(princ " ."))))
(format t "None~%"))))
;;; for LE check
;; (defvar .int-module. nil)
(defvar .ls-pat. nil) ; X < Y
(defvar .le-pat. nil) ; X <= Y
(defun prepare-root-context (root-module context-module)
(unless .int-module.
(setq .int-module. (eval-modexp "INT"))
(with-in-module (.int-module.)
(let ((less (find-operator '("_" "<" "_") 2 .int-module.))
(le (find-operator '("_" "<=" "_") 2 .int-module.))
(less-m nil)
(le-m nil)
(var-x nil)
(var-y nil)
(int-sort (find-sort-in .int-module. '|Int|)))
(setq less-m (lowest-method* (car (opinfo-methods less))))
(setq le-m (lowest-method* (car (opinfo-methods le))))
(setq var-x (make-variable-term int-sort 'X))
(setq var-y (make-variable-term int-sort 'Y))
(setq .ls-pat. (make-applform *bool-sort* less-m (list var-x var-y)))
(setq .le-pat. (make-applform *bool-sort* le-m (list var-x var-y))))))
(import-module root-module :protecting context-module)
(import-module root-module :protecting .int-module.)
(compile-module root-module t))
(defun begin-proof (context-module goal-axioms)
(declare (type module context-module)
(type list goal-axioms))
(unless goal-axioms (return-from begin-proof nil))
;; if the context module is changed, we begin a brand new proof session
(unless (eq (citp-proof-context *the-citp-proof*)
context-module)
(with-output-simple-msg ()
(format t "** Beginning a new proof in ~a" (module-print-name context-module)))
(setq *the-citp-proof*
(make-citp-proof :context context-module :discharged nil)))
(let* ((*chaos-quiet* t)
(root-module (eval-ast .root-context-module.)))
(setf (module-hidden root-module) t)
(prepare-root-context root-module context-module)
(when .goals-so-far.
(setq *modules-so-far-table* (remove-if #'(lambda (x)
(member (car x) .goals-so-far. :test #'equal))
*modules-so-far-table*))
(setq .goals-so-far. nil))
(setq *proof-tree* (initialize-proof-tree context-module root-module goal-axioms))
(pr-goal (ptree-node-goal (ptree-root *proof-tree*)))
(format t "~%** Initial goal (root) is generated. **")
(setq *next-default-proof-node* (ptree-root *proof-tree*))
*proof-tree*))
;;; --------
;;; PRiNTERS
;;; --------
;;; print-proof-tree
;;;
(defvar *show-proof-mode* :horizontal)
(defun print-proof-tree (goal-name &optional (describe nil))
(unless *proof-tree*
(with-output-chaos-warning ()
(format t "There is no proof tree.")
(return-from print-proof-tree nil)))
(let ((target-node (if goal-name
(or (find-goal-node *proof-tree* goal-name)
(with-output-chaos-error ('no-such-goal)
(format t "No goal with the name ~s." goal-name)))
(ptree-root *proof-tree*))))
(if describe
(describe-proof-tree target-node)
(!print-proof-tree target-node (get-next-proof-context *proof-tree*) *show-proof-mode*))))
(defun !print-proof-tree (root-node next-target mode &optional (stream *standard-output*))
(if (eq mode :horizontal)
(!print-proof-horizontal root-node next-target stream)
(!print-proof-vertical root-node next-target stream)))
(defun !print-proof-vertical (root-node next-target stream)
(let* ((leaf? #'(lambda (node) (null (dag-node-subnodes node))))
(leaf-name #'(lambda (node)
(with-output-to-string (s)
(let ((goal (ptree-node-goal node)))
(when (eq node next-target)
(princ ">" s))
(if (goal-tactic goal)
(format s "[~a] ~a" (tactic-name (goal-tactic goal)) (goal-name goal))
(princ (goal-name goal) s))
(when (node-is-discharged? node)
(princ "*" s)))
s)))
(leaf-info #'(lambda (node) (declare (ignore node)) t))
(int-node-name #'(lambda (node) (funcall leaf-name node)))
(int-node-children #'(lambda (node) (ptree-node-subnodes node))))
(force-output stream)
(print-next nil *print-indent* stream)
(print-trees (list (augment-tree root-node)) stream)))
(defun !print-proof-horizontal (node next-target stream)
(let ((*standard-output* stream))
(let ((goal (ptree-node-goal node)))
(with-in-module ((goal-context goal))
(when (eq node next-target)
(princ ">"))
(if (goal-tactic goal)
(format t "[~a]~6T~a" (tactic-name (goal-tactic goal)) (goal-name goal))
(format t "~a" (goal-name goal)))
(when (node-is-discharged? node)
(princ "*"))))
(let ((subnodes (ptree-node-subnodes node)))
(when subnodes
(dolist (sub subnodes)
(print-next-prefix #\Space)
(!print-proof-horizontal sub next-target stream))))))
(defparameter *proof-indent* 0)
(defun describe-proof-tree (node)
(declare (type ptree-node node))
(flet ((proved? ()
(format nil "~:[ ~;*~]" (node-is-discharged? node))))
(let ((goal (ptree-node-goal node))
(*print-line-limit* 80)
;; (*print-xmode* :fancy)
)
(with-in-module ((goal-context goal))
(if (goal-tactic goal)
(format t "[~a]~8T~a~a" (tactic-name (goal-tactic goal)) (goal-name goal) (proved?))
(format t "==> ~a~a" (goal-name goal) (proved?)))
;; (princ " ------------------------")
(let ((*print-indent* (+ 4 *print-indent*)))
(print-next)
(format t "-- context module: ~a" (get-module-simple-name *current-module*))
(let ((assumptions (goal-assumptions goal)))
(when assumptions
(print-next)
(format t "-- assumption~p" (length assumptions))
(let ((*print-indent* (+ 2 *print-indent*))
;; (*print-xmode* :fancy)
)
(dolist (as assumptions)
(print-next)
(print-axiom-brief as)
(princ " .")))))
(let ((proved (goal-proved goal)))
(when proved
(print-next)
(format t "-- discharged sentence~p:" (length proved))
(let ((*print-indent* (+ 2 *print-indent*)))
(dolist (ax proved)
(print-next)
(print-axiom-brief ax)
(princ " .")))))
(let ((targets (goal-targets goal)))
(when targets
(print-next)
(if (node-is-discharged? node)
(format t "-- targeted sentence~p:" (length targets))
(format t "-- sentence~p to be proved:" (length targets)))
(let ((*print-indent* (+ 2 *print-indent*)))
(dolist (target targets)
(print-next)
(print-axiom-brief target)
(princ " .")))))))
(let ((subnodes (ptree-node-subnodes node)))
(when subnodes
(let ((*print-indent* (+ *proof-indent* *print-indent*)))
(dolist (sub subnodes)
(print-next-prefix #\.)
(describe-proof-tree sub))))))))
;;; print-current-goal : mode -> void
;;;
(defun print-current-goal (describe)
(let ((current (get-next-proof-context *proof-tree*)))
(if current
(if describe ; :describe
(pr-goal (ptree-node-goal current))
(format t "~%The current goal is ~a" (goal-name (ptree-node-goal current))))
(with-output-chaos-warning ()
(format t "All goals have been discharged.")))))
;;; print-defs
;;;
(defun print-defs (describe &optional goal-name)
(declare (ignore describe))
(let ((current (if goal-name
(find-goal-node *proof-tree* goal-name)
(get-next-proof-context *proof-tree*))))
(if current
(let* ((goal (ptree-node-goal current))
(defs (goal-defs goal)))
(unless defs
(format t "~%The goal ~a has no defs.~%" (goal-name goal))
(return-from print-defs nil))
(dolist (def defs)
(format t "~a = " (tactic-name def))
(princ def)
(print-next)))
(with-output-chaos-warning ()
(format t "No current goal.")))))
;;; with-in-context : ptree-node
;;; construct a lexical environment for applying a tactic.
;;;
(eval-when (:compile-toplevel :execute :load-toplevel)
(defmacro with-in-context ((ptree-node) &rest body)
(once-only (ptree-node)
`(block :exit
(let* ((.cur-goal. (ptree-node-goal ,ptree-node))
(.cur-targets. (goal-targets .cur-goal.))
(.next-goals. nil))
(unless .cur-targets. (return-from :exit nil))
,@body))))
)
;;; EOF
|