| 1 |
;;;-*-Mode:LISP; Package: CHAOS; Base:10; Syntax:Common-lisp -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2000-2018, Toshimi Sawada. All rights reserved. |
| 4 |
;;; |
| 5 |
;;; Redistribution and use in source and binary forms, with or without |
| 6 |
;;; modification, are permitted provided that the following conditions |
| 7 |
;;; are met: |
| 8 |
;;; |
| 9 |
;;; * Redistributions of source code must retain the above copyright |
| 10 |
;;; notice, this list of conditions and the following disclaimer. |
| 11 |
;;; |
| 12 |
;;; * Redistributions in binary form must reproduce the above |
| 13 |
;;; copyright notice, this list of conditions and the following |
| 14 |
;;; disclaimer in the documentation and/or other materials |
| 15 |
;;; provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED |
| 18 |
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 |
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 21 |
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 23 |
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 |
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
;;; |
| 29 |
(in-package :chaos) |
| 30 |
#|============================================================================= |
| 31 |
System:CHAOS |
| 32 |
Module:thstuff |
| 33 |
File:proof-struct.lisp |
| 34 |
=============================================================================|# |
| 35 |
#-:chaos-debug |
| 36 |
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0))) |
| 37 |
#+:chaos-debug |
| 38 |
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3))) |
| 39 |
|
| 40 |
;;; ************************************************************************** |
| 41 |
;;; Here we define a generic proof structure, i.e. a tree of 'goal's. |
| 42 |
|
| 43 |
;;;--------------------------------------------------------------------------- |
| 44 |
;;; TACTIC |
| 45 |
;;; |
| 46 |
(eval-when (:compile-toplevel :execute :load-toplevel) |
| 47 |
(defstruct (tactic (:print-function pr-tactic)) |
| 48 |
(name nil :type symbol) ; name |
| 49 |
(executor nil :type (or null symbol)) ; tactic executor |
| 50 |
) |
| 51 |
|
| 52 |
;; Simultaneous Induction |
| 53 |
(defparameter .tactic-si. (make-tactic :name :si |
| 54 |
:executor 'apply-si)) |
| 55 |
;; Case Analysis |
| 56 |
(defparameter .tactic-ca. (make-tactic :name :ca |
| 57 |
:executor 'apply-ca)) |
| 58 |
;; Theorem of Constants |
| 59 |
(defparameter .tactic-tc. (make-tactic :name :tc |
| 60 |
:executor 'apply-tc)) |
| 61 |
;; Implication |
| 62 |
(defparameter .tactic-ip. (make-tactic :name :ip |
| 63 |
:executor 'apply-ip)) |
| 64 |
;; Implication by modifying the goal by 'imply' |
| 65 |
(defparameter .tactic-ip+. (make-tactic :name :ip+ |
| 66 |
:executor 'apply-ip+)) |
| 67 |
;; Reduction: goal sentence will be destructively changed |
| 68 |
(defparameter .tactic-rd. (make-tactic :name :rd |
| 69 |
:executor 'apply-rd)) |
| 70 |
;; Reduction: keeps original goal sentence if it is not reduced to be 'true' |
| 71 |
(defparameter .tactic-rd-. (make-tactic :name :rd- |
| 72 |
:executor 'apply-rd-)) |
| 73 |
;; Normalize goal sentenses |
| 74 |
(defparameter .tactic-nf. (make-tactic :name :nf |
| 75 |
:executor 'apply-nf)) |
| 76 |
;; Check contradiction, i.e. true = false |
| 77 |
(defparameter .tactic-ct. (make-tactic :name :ct |
| 78 |
:executor 'apply-ct)) |
| 79 |
;; Do nothing, used internally |
| 80 |
(defparameter .tactic-nil. (make-tactic :name :nop |
| 81 |
:executor 'apply-nil)) |
| 82 |
|
| 83 |
;; list of all builtin tactics |
| 84 |
(defparameter .all-builtin-tactics. |
| 85 |
(list .tactic-si. .tactic-ca. .tactic-tc. .tactic-ip. .tactic-ip+. |
| 86 |
.tactic-rd. .tactic-rd-. .tactic-nf. .tactic-ct.)) |
| 87 |
|
| 88 |
;; default tatics is a seriase of SI CA TC IP RD. |
| 89 |
(defparameter .default-tactics. |
| 90 |
(list .tactic-si. .tactic-ca. .tactic-tc. .tactic-ip. .tactic-rd.)) |
| 91 |
|
| 92 |
;; this is not an ordinary tactic but a command, but it generates goals |
| 93 |
(defparameter .tactic-ctf. (make-tactic :name :ctf |
| 94 |
:executor 'apply-ctf)) |
| 95 |
;; this is not an ordinary tactic but a command, but it generates goals |
| 96 |
(defparameter .tactic-csp. (make-tactic :name :csp |
| 97 |
:executor 'apply-csp)) |
| 98 |
|
| 99 |
;; :init as def(ed) tactic |
| 100 |
(defparameter .tactic-init. (make-tactic :name :init |
| 101 |
:executor 'apply-init)) |
| 102 |
;; :ind, user defined induction scheme |
| 103 |
(defparameter .tactic-ind. (make-tactic :name :ind |
| 104 |
:executor 'apply-ind)) |
| 105 |
|
| 106 |
;; user defiled tactics: assoc list of (name . list-of-tactics) |
| 107 |
;; |
| 108 |
(defvar .user-defined-tactics. nil) |
| 109 |
|
| 110 |
) |
| 111 |
|
| 112 |
(defun canonicalize-tactic-name (name) |
| 113 |
(when (stringp name) (setq name (string-upcase name))) |
| 114 |
(when (symbolp name) (setq name (symbol-name name))) |
| 115 |
name) |
| 116 |
|
| 117 |
(defun pr-tactic (tactic &optional (stream *standard-output*) &rest ignore) |
| 118 |
(declare (ignore ignore)) |
| 119 |
(format stream "[~a]" (tactic-name tactic))) |
| 120 |
|
| 121 |
;;; get-builtin-tactic : name -> tactic |
| 122 |
;;; given a name returns a tactic with this name |
| 123 |
;;; name can be symbol or string. the serach is done in case insensitive. |
| 124 |
;;; |
| 125 |
(defun get-builtin-tactic (name) |
| 126 |
(setq name (canonicalize-tactic-name name)) |
| 127 |
(find-if #'(lambda (x) (string-equal name (symbol-name (tactic-name x)))) .all-builtin-tactics.)) |
| 128 |
|
| 129 |
;;; tactic-name-is-builtin? : name -> bool |
| 130 |
;;; |
| 131 |
(defun tactic-name-is-builtin? (name) |
| 132 |
(get-builtin-tactic name)) |
| 133 |
|
| 134 |
;;; sequence of tactic :defined |
| 135 |
;;; :def <name> = (<tactic-name> ...) |
| 136 |
;;; |
| 137 |
(defstruct (tactic-seq (:include tactic) |
| 138 |
(:print-function pr-tactic-seq)) |
| 139 |
(tactics nil :type list) ; list of tactics |
| 140 |
) |
| 141 |
|
| 142 |
(defun pr-tactic-seq (obj stream &rest ignore) |
| 143 |
(declare (type tactic-seq obj) |
| 144 |
(ignore ignore)) |
| 145 |
(let ((tactics (tactic-seq-tactics obj))) |
| 146 |
(format stream "( ~{~a~^ ~a ~} )" (mapcar #'(lambda (x) (tactic-name x)) tactics)))) |
| 147 |
|
| 148 |
;;; :ctf/:csp as tactic |
| 149 |
;;; |
| 150 |
(defstruct (tactic-ctfp-common (:include tactic)) |
| 151 |
(minus nil :type (or null t)) ; t iff :ctf- or :csp- |
| 152 |
(context nil :type (or null module)) ; context module |
| 153 |
) |
| 154 |
|
| 155 |
(defstruct (tactic-ctf (:include tactic-ctfp-common (executor 'apply-ctf-tactic)) |
| 156 |
(:print-function pr-tactic-ctf)) |
| 157 |
(form nil) ; term or equation |
| 158 |
) |
| 159 |
|
| 160 |
(defun pr-tactic-ctf (obj stream &rest ignore) |
| 161 |
(declare (type tactic-ctf obj) |
| 162 |
(ignore ignore)) |
| 163 |
(let ((form (tactic-ctf-form obj))) |
| 164 |
(format stream ":ctf") |
| 165 |
(when (tactic-ctf-minus obj) |
| 166 |
(princ "-" stream)) |
| 167 |
(with-in-module ((tactic-ctf-context obj)) |
| 168 |
(cond ((axiom-p form) |
| 169 |
(princ "{" stream) |
| 170 |
(print-axiom-brief form stream) |
| 171 |
(princ " .}")) |
| 172 |
(t ; term |
| 173 |
(princ "[" stream) |
| 174 |
(term-print form stream) |
| 175 |
(princ " .]")))))) |
| 176 |
|
| 177 |
(defstruct (tactic-csp (:include tactic-ctfp-common (executor 'apply-csp-tactic)) |
| 178 |
(:print-function pr-tactic-csp)) |
| 179 |
(forms nil) ; list of equations |
| 180 |
) |
| 181 |
|
| 182 |
(defun pr-tactic-csp (obj stream &rest ignore) |
| 183 |
(declare (type tactic-csp obj) |
| 184 |
(ignore ignore)) |
| 185 |
(let ((forms (tactic-csp-forms obj))) |
| 186 |
(format stream ":csp") |
| 187 |
(with-in-module ((tactic-csp-context obj)) |
| 188 |
(when (tactic-csp-minus obj) |
| 189 |
(princ "-" stream)) |
| 190 |
(princ "{" stream) |
| 191 |
(dolist (ax forms) |
| 192 |
(print-axiom-brief ax stream) |
| 193 |
(princ " . " stream)) |
| 194 |
(princ "}" stream)))) |
| 195 |
|
| 196 |
;;; :init as tactic |
| 197 |
;;; |
| 198 |
(defstruct (tactic-init (:include tactic (executor 'apply-init-tactic)) |
| 199 |
(:print-function pr-tactic-init)) |
| 200 |
(axiom nil) ; axiom |
| 201 |
(subst nil) ; substitution |
| 202 |
(context nil) ; context module |
| 203 |
(kind nil) ; how the axiom is specified, :label or :term |
| 204 |
) |
| 205 |
|
| 206 |
(defun pr-tactic-init (obj stream &rest ignore) |
| 207 |
(declare (type tactic-init obj) |
| 208 |
(ignore ignore)) |
| 209 |
(format stream ":init") |
| 210 |
(with-in-module ((tactic-init-context obj)) |
| 211 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 212 |
(if (eq (first (tactic-init-kind obj)) :label) |
| 213 |
(format stream "[~a]" (second (tactic-init-kind obj))) |
| 214 |
(progn |
| 215 |
(princ "{") |
| 216 |
(print-axiom-brief (tactic-init-axiom obj)) |
| 217 |
(princ " .}"))) |
| 218 |
(if (tactic-init-subst obj) |
| 219 |
(progn |
| 220 |
(print-next) |
| 221 |
(princ "by subst form: ") |
| 222 |
(princ (tactic-init-subst obj))) |
| 223 |
(princ "."))))) |
| 224 |
|
| 225 |
;;; :ind as tactic |
| 226 |
;;; |
| 227 |
(defstruct (tactic-ind (:include tactic (executor 'apply-ind-tactic))) |
| 228 |
(vars nil) |
| 229 |
(base nil) |
| 230 |
(step nil)) |
| 231 |
|
| 232 |
;;; get-default-tactics |
| 233 |
;;; returns the default tactics, i.e. (:si :ca :cs :tc :ip) |
| 234 |
;;; |
| 235 |
(defun get-default-tactics () .default-tactics.) |
| 236 |
|
| 237 |
;;; make user defined tactic |
| 238 |
;;; |
| 239 |
(defun declare-tactic (name &rest tactic-names) |
| 240 |
(let ((tactics nil)) |
| 241 |
(dolist (n tactic-names) |
| 242 |
(let ((tactic (get-builtin-tactic n))) |
| 243 |
(unless tactic |
| 244 |
(with-output-chaos-error ('no-such-tactic) |
| 245 |
(format t "No tactic with the name ~s" n))) |
| 246 |
(push tactic tactics))) |
| 247 |
(setq name (canonicalize-tactic-name name)) |
| 248 |
(setq .user-defined-tactics. |
| 249 |
(acons name (nreverse tactics) .user-defined-tactics.)))) |
| 250 |
|
| 251 |
;;; ===== |
| 252 |
;;; FLAGS |
| 253 |
;;; ===== |
| 254 |
(defvar *citp-show-rwl* nil) |
| 255 |
|
| 256 |
;;; ------------------------------------------------------------------------------- |
| 257 |
;;; Various utils which controll 'switch' affected behaviour of the system . |
| 258 |
;;; |
| 259 |
|
| 260 |
|
| 261 |
;;; This variable controlls implicit applications of tactics. |
| 262 |
;;; 'true' means CITP cares application of implicite applicatins of tactics |
| 263 |
;;; such as 'normalization of the goal', 'contradiction check ('true = false')'. |
| 264 |
;;; if this is 'false', CITP does only introduces proof schems defined. |
| 265 |
(declaim (special *citp-spoiler*)) |
| 266 |
(defvar *citp-spoiler* nil) |
| 267 |
|
| 268 |
(eval-when (:compile-toplevel :execute :load-toplevel) |
| 269 |
|
| 270 |
(defmacro if-spoiler-on (&key then else) |
| 271 |
`(if *citp-spoiler* |
| 272 |
(progn ,then) |
| 273 |
(progn ,else))) |
| 274 |
|
| 275 |
(defmacro when-spoiler-on (&rest body) |
| 276 |
`(when *citp-spoiler* |
| 277 |
,@body)) |
| 278 |
|
| 279 |
(defmacro with-spoiler-on (&rest body) |
| 280 |
`(let ((*citp-spoiler* t)) |
| 281 |
(declare (special *citp-spoiler*)) |
| 282 |
,@body)) |
| 283 |
|
| 284 |
) |
| 285 |
|
| 286 |
(declaim (type fixnum *citp-max-flags*)) |
| 287 |
(defparameter *citp-max-flags* 10) |
| 288 |
|
| 289 |
(defstruct (citp-flag-struct) |
| 290 |
(name "" :type simple-string) |
| 291 |
(value nil) |
| 292 |
(hook #'(lambda(name value) |
| 293 |
(declare (ignore name value)) |
| 294 |
nil) |
| 295 |
:type (or function symbol))) |
| 296 |
|
| 297 |
(declaim (type (simple-array * (10)) *citp-flags*)) |
| 298 |
(defvar *citp-flags*) |
| 299 |
(eval-when (:execute :load-toplevel) |
| 300 |
(setq *citp-flags* (make-array *citp-max-flags*))) |
| 301 |
|
| 302 |
(defmacro citp-flag-struct (flag-index) |
| 303 |
`(aref *citp-flags* ,flag-index)) |
| 304 |
|
| 305 |
(defmacro citp-flag (flag-index) |
| 306 |
`(citp-flag-struct-value (aref *citp-flags* ,flag-index))) |
| 307 |
|
| 308 |
(defmacro citp-flag-name (flag-index) |
| 309 |
`(citp-flag-struct-name (aref *citp-flags* ,flag-index))) |
| 310 |
|
| 311 |
(defmacro citp-flag-hook (flag-index) |
| 312 |
`(citp-flag-struct-hook (aref *citp-flags* ,flag-index))) |
| 313 |
|
| 314 |
;;; flag indexes |
| 315 |
(defconstant citp-all 0) |
| 316 |
(defconstant citp-verbose 1) |
| 317 |
(defconstant citp-show-rwl 2) |
| 318 |
(defconstant citp-spoiler 3) |
| 319 |
(defconstant citp-print-message 4) |
| 320 |
(defconstant citp-normalize-init 5) |
| 321 |
(defconstant citp-normalize-lhs 6) |
| 322 |
|
| 323 |
;;; FIND-CITP-FLAG-INDEX : Name -> Index |
| 324 |
;;; |
| 325 |
(defun find-citp-flag-index (given-name) |
| 326 |
(declare (type simple-string given-name) |
| 327 |
(values (or null fixnum))) |
| 328 |
(let ((i 0) |
| 329 |
(name (concatenate 'string "citp-" given-name))) |
| 330 |
(declare (type fixnum i)) |
| 331 |
(dotimes (x *citp-max-flags*) |
| 332 |
(when (string= name (citp-flag-name x)) |
| 333 |
(return-from find-citp-flag-index i)) |
| 334 |
(incf i)) |
| 335 |
nil)) |
| 336 |
|
| 337 |
;;; print-citp-flag : index -> void |
| 338 |
;;; |
| 339 |
(defun print-citp-flag (index) |
| 340 |
(if (= index citp-all) |
| 341 |
(do ((idx 1 (1+ idx))) |
| 342 |
((<= *citp-max-flags* idx)) |
| 343 |
(pr-citp-flag-internal idx)) |
| 344 |
(pr-citp-flag-internal index))) |
| 345 |
|
| 346 |
(defun pr-citp-flag-internal (index) |
| 347 |
(unless (equal "" (citp-flag-name index)) |
| 348 |
(format t "~&- Flag ~a is ~a" (subseq (citp-flag-name index) 5) (if (citp-flag index) "on" "off")))) |
| 349 |
|
| 350 |
;;; help-citp-flag : index |
| 351 |
;;; |
| 352 |
(defun help-citp-flag (index) |
| 353 |
(let ((flag (citp-flag-struct index))) |
| 354 |
flag)) |
| 355 |
|
| 356 |
;;; flag initialization |
| 357 |
;;; |
| 358 |
(defun initialize-citp-flag () |
| 359 |
(dotimes (idx *citp-max-flags*) |
| 360 |
(setf (citp-flag-struct idx) (make-citp-flag-struct :value nil))) |
| 361 |
(setf (citp-flag-name citp-all) "citp-all" |
| 362 |
(citp-flag-name citp-verbose) "citp-verbose" |
| 363 |
(citp-flag-name citp-show-rwl) "citp-show-rwl" |
| 364 |
(citp-flag-name citp-spoiler) "citp-spoiler" |
| 365 |
(citp-flag-name citp-print-message) "citp-print-message" |
| 366 |
(citp-flag-name citp-normalize-init) "citp-normalize-init" |
| 367 |
(citp-flag-name citp-normalize-lhs) "citp-normalize-lhs") |
| 368 |
;; set default |
| 369 |
(setf (citp-flag citp-print-message) t) ; others are 'off' |
| 370 |
(setf (citp-flag citp-normalize-init) t) |
| 371 |
(setf (citp-flag citp-normalize-lhs) t) |
| 372 |
;; verbose flag hook |
| 373 |
(setf (citp-flag-hook citp-verbose) |
| 374 |
#'(lambda (name value) |
| 375 |
(declare (ignore name)) |
| 376 |
(setf *citp-verbose* value))) |
| 377 |
;; show-rwl hook |
| 378 |
(setf (citp-flag-hook citp-show-rwl) |
| 379 |
#'(lambda (name value) |
| 380 |
(declare (ignore name)) |
| 381 |
(setf *citp-show-rwl* value))) |
| 382 |
;; citp-spoiler hook |
| 383 |
(setf (citp-flag-hook citp-spoiler) |
| 384 |
#'(lambda (name value) |
| 385 |
(declare (ignore name)) |
| 386 |
(setf *citp-spoiler* value))) |
| 387 |
) |
| 388 |
|
| 389 |
(eval-when (:execute :load-toplevel) |
| 390 |
(initialize-citp-flag) |
| 391 |
) |
| 392 |
|
| 393 |
;;; ------------------------------------------------------------------------- |
| 394 |
;;; PTREE-NODE |
| 395 |
;;; A node of a proof tree. Contains a goal as its datum. |
| 396 |
;;; |
| 397 |
(defstruct (ptree-node (:include bdag) |
| 398 |
(:print-function pr-ptree-node)) |
| 399 |
(num-children 0 :type fixnum) ; number of children |
| 400 |
(next-child 0 :type fixnum) ; next child to be proved |
| 401 |
(my-num 0 :type fixnum) ; position in siblings, first = 1 |
| 402 |
(my-name "" :type string) ; name |
| 403 |
(done nil)) ; t iff the node is dischaged |
| 404 |
|
| 405 |
;;;----------------------------------------------------------------------------- |
| 406 |
;;; PTREE : proof tree |
| 407 |
;;; whole proof tree structure. |
| 408 |
;;; |
| 409 |
(defstruct (ptree (:print-function pr-ptree)) |
| 410 |
(context nil :type (or null module)) ; context module |
| 411 |
(num-gen-const 0 :type fixnum) ; number of generated constants so far |
| 412 |
(num-gen-const-ind 0 :type fixnum) ; number of generated constants for induction so far |
| 413 |
(root nil :type (or null ptree-node)) ; root goal |
| 414 |
(indvar-subst nil :type list) ; <variable> -> <constantForInduction> |
| 415 |
(var-subst nil :type list) ; <variable> -> <constantForTheremOfConstants> |
| 416 |
(defs-so-far nil :type list) ; :defined name so far |
| 417 |
(constructor-ops nil :type list) ; list of constructor operators |
| 418 |
) |
| 419 |
|
| 420 |
;;;--------------------------------------------------------------------------- |
| 421 |
;;; GOAL |
| 422 |
;;; a goal is a set of conditional sentences (in a form of CafeOBJ axiom) to be proved |
| 423 |
;;; with context module, introduced constants, introduced axioms (hypothesis), e.t.c. |
| 424 |
;;; it holds all the information about a goal. |
| 425 |
;;; |
| 426 |
(defstruct (goal (:print-function pr-goal)) |
| 427 |
(name "" :type string) ; the name of the goal, we will refer |
| 428 |
; this goal by this name |
| 429 |
(context nil :type (or null module)) ; context module |
| 430 |
(constants nil :type list) ; list of (var . constant) introduced for TC/CA/SI |
| 431 |
(ind-constants nil :type list) ; list of constants introduced for induction |
| 432 |
(indvars nil :type list) ; list of induction variables |
| 433 |
(skolems nil :type list) ; list of skolem functions |
| 434 |
(assumptions nil :type list) ; list of hypothesis |
| 435 |
(tactic nil :type (or null tactic)) ; tactic which derived this goal |
| 436 |
(targets nil :type list) ; axioms to be proved |
| 437 |
(proved nil :type list) ; proved targets |
| 438 |
(critical-pairs nil :type list) ; list of critical pairs not yet axiomatized |
| 439 |
(defs nil :type list) ; list of :defined tactics |
| 440 |
(base-ops nil :type list) ; constructors |
| 441 |
(step-ops nil :type list) ; induction step operators |
| 442 |
(bases nil :type list) ; list of user specified induction base patterns |
| 443 |
(hypos nil :type list) ; list of user specified hypothesis patterns |
| 444 |
(steps nil :type list) ; list of user specifief induction step patterns |
| 445 |
) |
| 446 |
|
| 447 |
(defun goal-is-discharged (goal) |
| 448 |
(declare (type goal goal)) |
| 449 |
(null (goal-targets goal))) |
| 450 |
|
| 451 |
(defun get-module-simple-name (module) |
| 452 |
(module-print-name module)) |
| 453 |
|
| 454 |
(defun pr-goal (goal &optional (stream *standard-output*) &rest ignore) |
| 455 |
(declare (type goal goal) |
| 456 |
(type stream stream) |
| 457 |
(ignore ignore)) |
| 458 |
(let ((*print-line-limit* 80) |
| 459 |
;; (*print-xmode* :fancy) |
| 460 |
) |
| 461 |
(with-in-module ((goal-context goal)) |
| 462 |
(if (goal-tactic goal) |
| 463 |
(format stream "~%~a=>~%:goal { ** ~a -----------------------------------------" |
| 464 |
(goal-tactic goal) (goal-name goal)) |
| 465 |
(format stream "~%:goal { ** ~a -----------------------------------------" |
| 466 |
(goal-name goal))) |
| 467 |
(let ((*print-indent* (+ 2 *print-indent*)) |
| 468 |
(v-consts (goal-constants goal)) |
| 469 |
(i-consts (goal-ind-constants goal)) |
| 470 |
(skolems (goal-skolems goal)) |
| 471 |
(ass (goal-assumptions goal)) |
| 472 |
(vs (goal-indvars goal)) |
| 473 |
(axs (goal-targets goal)) |
| 474 |
(proved (goal-proved goal)) |
| 475 |
(discharged (goal-is-discharged goal)) |
| 476 |
(bases (goal-bases goal)) |
| 477 |
(steps (goal-steps goal))) |
| 478 |
(print-next) |
| 479 |
(format stream "-- context module: ~a" |
| 480 |
(get-module-simple-name (ptree-context *proof-tree*))) |
| 481 |
(when proved |
| 482 |
(print-next) |
| 483 |
(format stream "-- discharged sentence~p" (length proved)) |
| 484 |
(dolist (pv proved) |
| 485 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 486 |
(print-next) |
| 487 |
(print-axiom-brief pv) (princ " .")))) |
| 488 |
(when vs |
| 489 |
(print-next) |
| 490 |
(format stream "-- induction variable~p" (length vs)) |
| 491 |
(dolist (v vs) |
| 492 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 493 |
(print-next) |
| 494 |
(term-print-with-sort v)))) |
| 495 |
(when bases |
| 496 |
(print-next) |
| 497 |
(format stream "-- user specified induction base~p" (length bases)) |
| 498 |
(dolist (b bases) |
| 499 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 500 |
(print-next) |
| 501 |
(term-print-with-sort b)))) |
| 502 |
(when steps |
| 503 |
(print-next) |
| 504 |
(format stream "-- user specified induction step~p" (length steps)) |
| 505 |
(dolist (s steps) |
| 506 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 507 |
(print-next) |
| 508 |
(term-print-with-sort s)))) |
| 509 |
(when v-consts |
| 510 |
(print-next) |
| 511 |
(format stream "-- introduced constant~p" (length v-consts)) |
| 512 |
(dolist (const (reverse v-consts)) |
| 513 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 514 |
(print-next) |
| 515 |
(print-method-brief (term-head (cdr const)))))) |
| 516 |
(when i-consts |
| 517 |
(print-next) |
| 518 |
(format stream "-- constant~p for induction" (length i-consts)) |
| 519 |
(dolist (ic (reverse i-consts)) |
| 520 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 521 |
(print-next) |
| 522 |
(print-method-brief (term-head (cdr ic)))))) |
| 523 |
(when skolems |
| 524 |
(print-next) |
| 525 |
(format stream "-- introduced skolem function~p" (length skolems)) |
| 526 |
(dolist (sk skolems) |
| 527 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 528 |
(print-next) |
| 529 |
(print-method-brief sk)))) |
| 530 |
(when ass |
| 531 |
(print-next) |
| 532 |
(format stream "-- introduced axiom~p" (length ass)) |
| 533 |
(dolist (as ass) |
| 534 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 535 |
(print-next) |
| 536 |
(print-axiom-brief as) (princ " .")))) |
| 537 |
(when axs |
| 538 |
(print-next) |
| 539 |
(format stream "-- sentence~p to be proved" (length axs)) |
| 540 |
(dolist (ax axs) |
| 541 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 542 |
(print-next) |
| 543 |
(print-axiom-brief ax) (princ " .")))) |
| 544 |
(format stream "~%}") |
| 545 |
(if discharged |
| 546 |
(format t " << proved >>")))))) |
| 547 |
|
| 548 |
(defun pr-ptree-node (ptree-node &optional (stream *standard-output*) &rest ignore) |
| 549 |
(declare (type ptree-node ptree-node) |
| 550 |
(type stream stream) |
| 551 |
(ignore ignore)) |
| 552 |
(format stream "[Node] sub nodes = ~d, discharged? = ~a ---------------" |
| 553 |
(ptree-node-num-children ptree-node) |
| 554 |
(ptree-node-done ptree-node)) |
| 555 |
(pr-goal (ptree-node-datum ptree-node) stream)) |
| 556 |
|
| 557 |
|
| 558 |
;;; get-defined-tactic |
| 559 |
;;; |
| 560 |
(defun get-defined-tactic (goal name) |
| 561 |
(setq name (canonicalize-tactic-name name)) |
| 562 |
(let ((defs (goal-defs goal))) |
| 563 |
(find-if #'(lambda (x) (string-equal name (canonicalize-tactic-name (tactic-name x)))) defs))) |
| 564 |
|
| 565 |
;;; use-sentence-in-goal : goal list-axioms |
| 566 |
;;; |
| 567 |
|
| 568 |
(defun incorporate-sentences-into-module (module new-axs) |
| 569 |
(unless module |
| 570 |
(with-output-chaos-error ('no-context) |
| 571 |
(format t "No context module is specified."))) |
| 572 |
(with-in-module (module) |
| 573 |
(dolist (ax new-axs) |
| 574 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 575 |
(print-next) |
| 576 |
(print-axiom-brief ax)) |
| 577 |
(adjoin-axiom-to-module module ax) |
| 578 |
(set-operator-rewrite-rule module ax)) |
| 579 |
(compile-module module))) |
| 580 |
|
| 581 |
(defun use-sentences-in-goal (goal new-axs) |
| 582 |
(declare (type goal goal)) |
| 583 |
(let ((num (length new-axs))) |
| 584 |
(unless (zerop num) |
| 585 |
(format t "~%** In goal ~s, use the following sentence~p as new axiom~p:" (goal-name goal) num num) |
| 586 |
(incorporate-sentences-into-module (goal-context goal) new-axs) |
| 587 |
(setf (goal-assumptions goal) |
| 588 |
(append (goal-assumptions goal) new-axs))))) |
| 589 |
|
| 590 |
(defun use-theory-in-goal (goal axiom theory-decl) |
| 591 |
(declare (type goal goal) |
| 592 |
(type axiom axiom)) |
| 593 |
(with-in-module ((goal-context goal)) |
| 594 |
(use-theory-in-module *current-module* |
| 595 |
(term-head (axiom-lhs axiom)) |
| 596 |
theory-decl |
| 597 |
"[:use]"))) |
| 598 |
|
| 599 |
(defun use-theory-in-module (module meth theory-decl &optional (msg-header "")) |
| 600 |
(flet ((theory-decl-to-theory (td) |
| 601 |
(let ((tdd (caar td))) |
| 602 |
(cond ((member tdd '("assoc" "associative") :test #'equal) |
| 603 |
(list :assoc)) |
| 604 |
((member tdd '("comm" "commutative") :test #'equal) |
| 605 |
(list :comm)) |
| 606 |
(t (list (cons :id (cdar td)))))))) |
| 607 |
(let ((theory (theory-decl-to-theory theory-decl))) |
| 608 |
(with-in-module (module) |
| 609 |
(add-and-merge-method-theory meth theory module) |
| 610 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 611 |
(format t "~%~a adding operator theory ~a into module ~a" msg-header theory (module-print-name module)) |
| 612 |
(prepare-for-parsing module t) |
| 613 |
(print-next) |
| 614 |
(print-method-brief meth) |
| 615 |
(print-next)))))) |
| 616 |
|
| 617 |
;;; embed-sentences-in-module (module new-axs) |
| 618 |
;;; |
| 619 |
(defparameter .citp-embed-module. (%module-decl* "for_embed_dummy" :module :system nil)) |
| 620 |
|
| 621 |
(defun make-embed-module (name context-module) |
| 622 |
(let ((decl-form (copy-tree .citp-embed-module.))) |
| 623 |
(setf (%module-decl-name decl-form) name) |
| 624 |
(let ((embeded-module (eval-ast decl-form))) |
| 625 |
(import-module embeded-module :protecting context-module)))) |
| 626 |
|
| 627 |
(defun embed-sentences-in-module (module new-axs) |
| 628 |
(let ((num (length new-axs))) |
| 629 |
(unless (zerop num) |
| 630 |
(format t "~%[:embed] adding setence~p as new axiom~p into module ~a:" num num |
| 631 |
(get-module-simple-name module)) |
| 632 |
(incorporate-sentences-into-module module new-axs)))) |
| 633 |
|
| 634 |
(defun embed-theory-in-module (module meth theory) |
| 635 |
(use-theory-in-module module meth theory "[:embed]")) |
| 636 |
|
| 637 |
;;; the-goal-needs-undo : goal -> bool |
| 638 |
;;; returns t iff the goal is generated by :defined :ctf- or :csp- |
| 639 |
;;; |
| 640 |
(defun the-goal-needs-undo (goal) |
| 641 |
(declare (type goal goal)) |
| 642 |
(let ((goal-tactic (goal-tactic goal))) |
| 643 |
(and (tactic-ctfp-common-p goal-tactic) |
| 644 |
(tactic-ctfp-common-minus goal-tactic)))) |
| 645 |
|
| 646 |
(defmacro ptree-node-goal (ptree-node) |
| 647 |
`(ptree-node-datum ,ptree-node)) |
| 648 |
|
| 649 |
(defun clear-induction-scheme (ptree-node) |
| 650 |
(let ((goal (ptree-node-goal ptree-node))) |
| 651 |
(setf (goal-base-ops goal) nil |
| 652 |
(goal-step-ops goal) nil |
| 653 |
(goal-bases goal) nil |
| 654 |
(goal-steps goal) nil))) |
| 655 |
|
| 656 |
;;; initialize-ptree-node : ptree-node -> ptree-node |
| 657 |
;;; discard existing child nodes. |
| 658 |
;;; |
| 659 |
(defun initialize-ptree-node (node &optional (no-warn nil)) |
| 660 |
(unless no-warn |
| 661 |
(when (ptree-node-subnodes node) |
| 662 |
(with-output-chaos-warning () |
| 663 |
(format t "Discarding exsisting ~d node~p" |
| 664 |
(ptree-node-num-children node) |
| 665 |
(length (ptree-node-subnodes node)))))) |
| 666 |
(setf (ptree-node-num-children node) 0 |
| 667 |
(ptree-node-subnodes node) nil) |
| 668 |
node) |
| 669 |
|
| 670 |
;;; node-is-discharged? : ptree-node -> Bool |
| 671 |
;;; returns if the node's goal is discharged, |
| 672 |
;;; i.e., own goal has no target axioms to be proved, |
| 673 |
;;; or every subnodes are discharged. |
| 674 |
;;; |
| 675 |
(defun node-is-discharged? (node) |
| 676 |
(let ((goal (ptree-node-goal node))) |
| 677 |
(or (null (goal-targets goal)) |
| 678 |
(and (ptree-node-subnodes node) |
| 679 |
(every #'(lambda (x) (node-is-discharged? x)) (ptree-node-subnodes node)))))) |
| 680 |
|
| 681 |
;;; make-it-unproved : ptree-node -> ptree-node' |
| 682 |
;;; |
| 683 |
(defun make-it-unproved (ptree-node) |
| 684 |
(let ((goal (ptree-node-goal ptree-node))) |
| 685 |
(setf (goal-targets goal) (append (goal-targets goal) (goal-proved goal))))) |
| 686 |
|
| 687 |
;;; make-ptree-goal-name : ptree-node fixnum -> string |
| 688 |
;;; goal has a name. |
| 689 |
;;; |
| 690 |
(defun make-ptree-goal-name (parent-node my-num) |
| 691 |
(declare (type (or null ptree-node) parent-node) |
| 692 |
(type fixnum my-num)) |
| 693 |
(if parent-node |
| 694 |
(let ((p-name (goal-name (ptree-node-goal parent-node)))) |
| 695 |
(if (equal p-name "root") |
| 696 |
(format nil "~d" my-num) |
| 697 |
(format nil "~a-~d" p-name my-num))) |
| 698 |
"root")) |
| 699 |
|
| 700 |
;;; context module creator |
| 701 |
;;; |
| 702 |
(defparameter .next-context-module. (%module-decl* "next-context-dummy" :module :system nil)) |
| 703 |
|
| 704 |
(defun make-next-context-module-name (goal-name) |
| 705 |
(declare (type string goal-name)) |
| 706 |
(format nil "#Goal-~a" goal-name)) |
| 707 |
|
| 708 |
;;; prepare-next-goal : ptree-node -> goal |
| 709 |
;;; prepare next goal structure with associated context module |
| 710 |
;;; |
| 711 |
(defvar .goals-so-far. nil) |
| 712 |
|
| 713 |
(defun prepare-next-goal (ptree-node &optional (tactic nil)) |
| 714 |
(let ((goal-name (make-ptree-goal-name ptree-node (incf (ptree-node-num-children ptree-node)))) |
| 715 |
(decl-form (copy-tree .next-context-module.))) |
| 716 |
(setf (%module-decl-name decl-form) (make-next-context-module-name goal-name)) |
| 717 |
(let ((next-context (eval-ast decl-form)) |
| 718 |
(cur-goal (ptree-node-goal ptree-node)) |
| 719 |
(next-goal (make-goal :name goal-name |
| 720 |
:tactic tactic))) |
| 721 |
;; goal module is hidden from user |
| 722 |
(setf (module-hidden next-context) t) |
| 723 |
(push (%module-decl-name decl-form) .goals-so-far.) |
| 724 |
;; import original context module |
| 725 |
(import-module next-context :including (goal-context cur-goal)) |
| 726 |
;; inherit current goal |
| 727 |
(setf (goal-context next-goal) next-context |
| 728 |
(goal-constants next-goal) (goal-constants cur-goal) |
| 729 |
(goal-ind-constants next-goal) (goal-ind-constants cur-goal) |
| 730 |
(goal-indvars next-goal) (goal-indvars cur-goal) |
| 731 |
(goal-skolems next-goal) (goal-skolems cur-goal) |
| 732 |
(goal-assumptions next-goal) (goal-assumptions cur-goal) |
| 733 |
(goal-defs next-goal) (goal-defs cur-goal) |
| 734 |
(goal-base-ops next-goal) (goal-base-ops cur-goal) |
| 735 |
(goal-step-ops next-goal) (goal-step-ops cur-goal) |
| 736 |
(goal-bases next-goal) (goal-bases cur-goal) |
| 737 |
(goal-steps next-goal) (goal-steps cur-goal)) |
| 738 |
(prepare-for-parsing next-context) |
| 739 |
(setq *next-default-proof-node* nil) ; we reset the next default target |
| 740 |
next-goal))) |
| 741 |
|
| 742 |
;;; give-goal-name-each-in-order : ptree-node List(goal) -> void |
| 743 |
;;; this is used for renaming goals and their context modules |
| 744 |
;;; after applied a tactic. |
| 745 |
;;; |
| 746 |
(defun give-goal-name-each-in-order (parent-node list-goals) |
| 747 |
(dolist (goal list-goals) |
| 748 |
(let* ((gname (make-ptree-goal-name parent-node (incf (ptree-node-num-children parent-node)))) |
| 749 |
(mod-name (make-next-context-module-name gname))) |
| 750 |
(setf (goal-name goal) gname) |
| 751 |
(setf (module-name (goal-context goal)) mod-name)))) |
| 752 |
|
| 753 |
;;; make-ptree-root : module goal -> ptree-node |
| 754 |
;;; |
| 755 |
(defun make-ptree-root (context-module initial-goals) |
| 756 |
(declare (type module context-module) |
| 757 |
(type list initial-goals)) |
| 758 |
(let ((root-node (make-ptree-node :subnodes nil :parent nil))) |
| 759 |
(setf (ptree-node-goal root-node) |
| 760 |
(make-goal :name (make-ptree-goal-name nil (ptree-node-my-num root-node)) |
| 761 |
:context context-module |
| 762 |
:skolems (reverse (module-skolem-functions context-module)) |
| 763 |
:targets initial-goals)) |
| 764 |
root-node)) |
| 765 |
|
| 766 |
;;; add-ptree-child : ptree-node module List(axiom) -> List(goal) |
| 767 |
;;; |
| 768 |
(defun add-ptree-child (parent-node child-goal) |
| 769 |
(declare (type ptree-node parent-node) |
| 770 |
(type goal child-goal)) |
| 771 |
(setf (ptree-node-subnodes parent-node) |
| 772 |
(nconc (ptree-node-subnodes parent-node) |
| 773 |
(list (make-ptree-node :datum child-goal |
| 774 |
:my-num (ptree-node-num-children parent-node) |
| 775 |
:subnodes nil |
| 776 |
:parent parent-node))))) |
| 777 |
|
| 778 |
;;; add-ptree-children : ptree-node List(goal) -> ptree-node' |
| 779 |
;;; add node of given goals as a child of the node. |
| 780 |
;;; before adding goal nodes, initialize the node and |
| 781 |
;;; give each goal a name. |
| 782 |
;;; |
| 783 |
(defun add-ptree-children (parent-node list-goals) |
| 784 |
(declare (type ptree-node parent-node) |
| 785 |
(type list list-goals)) |
| 786 |
(initialize-ptree-node parent-node t) |
| 787 |
;; give names to goals |
| 788 |
(give-goal-name-each-in-order parent-node list-goals) |
| 789 |
(dolist (goal list-goals) |
| 790 |
(add-ptree-child parent-node goal)) |
| 791 |
parent-node) |
| 792 |
|
| 793 |
;;; get-ptree-root : ptree-node -> ptree-node |
| 794 |
;;; |
| 795 |
(defun get-ptree-root (ptree-node) |
| 796 |
(let ((parent (ptree-node-parent ptree-node))) |
| 797 |
(unless parent (return-from get-ptree-root ptree-node)) |
| 798 |
(get-ptree-root parent))) |
| 799 |
|
| 800 |
;;; ptree utils |
| 801 |
;;; |
| 802 |
|
| 803 |
;;; the-node-needs-undo |
| 804 |
;;; returns t iff the node is generated by :def(ined) |
| 805 |
;;; :ctf- or :csp- |
| 806 |
(defun the-node-needs-undo (node) |
| 807 |
(declare (type ptree-node node)) |
| 808 |
(the-goal-needs-undo (ptree-node-goal node))) |
| 809 |
|
| 810 |
;;; parent-nedds-undo |
| 811 |
;;; returns t iff the parent node is generated by :def(ined) |
| 812 |
;;; :ctf- or :csp- |
| 813 |
;;; |
| 814 |
(defun parent-needs-undo (pnode) |
| 815 |
(declare (type (or null ptree-node) pnode)) |
| 816 |
(let ((node (ptree-node-parent pnode))) |
| 817 |
(unless node (return-from parent-needs-undo nil)) |
| 818 |
(the-node-needs-undo node))) |
| 819 |
|
| 820 |
(defun pr-ptree (ptree &optional (stream *standard-output*) &rest ignore) |
| 821 |
(declare (type ptree ptree) |
| 822 |
(type stream stream) |
| 823 |
(ignore ignore)) |
| 824 |
(let ((*standard-output* stream)) |
| 825 |
(format t "~%Proof Tree ===================================") |
| 826 |
(with-in-module ((goal-context (ptree-node-goal (ptree-root ptree)))) |
| 827 |
(let ((indvar-subst (ptree-indvar-subst ptree)) |
| 828 |
(*print-indent* (+ 2 *print-indent*))) |
| 829 |
(format t "~%-- induction variable bases:") |
| 830 |
(if indvar-subst |
| 831 |
(dolist (is indvar-subst) |
| 832 |
(print-next) |
| 833 |
(term-print-with-sort (car is)) |
| 834 |
(princ " => ") |
| 835 |
(princ (cdr is))) |
| 836 |
(progn (print-next) (princ "none" stream)))) |
| 837 |
(format stream "~%-- introduced constants:") |
| 838 |
(let ((var-subst (ptree-var-subst ptree)) |
| 839 |
(*print-indent* (+ 2 *print-indent*))) |
| 840 |
(if var-subst |
| 841 |
(dolist (is var-subst) |
| 842 |
(print-next) |
| 843 |
(term-print-with-sort (car is)) |
| 844 |
(princ " => ") |
| 845 |
(princ (cdr is))) |
| 846 |
(progn (print-next) (princ "none" stream)))) |
| 847 |
(format stream "~%-- constructors:") |
| 848 |
(let ((num 0)) |
| 849 |
(dolist (op (ptree-constructor-ops ptree)) |
| 850 |
(print-next) |
| 851 |
(format t "(~d) " (incf num)) |
| 852 |
(print-method-brief op))) |
| 853 |
(format stream "~%-- root node") |
| 854 |
(pr-goal (ptree-node-goal (ptree-root ptree)))))) |
| 855 |
|
| 856 |
(defun reset-proof (ptree) |
| 857 |
(setf (ptree-num-gen-const ptree) 0 |
| 858 |
(ptree-indvar-subst ptree) nil |
| 859 |
(ptree-var-subst ptree) nil)) |
| 860 |
|
| 861 |
(defun existing-def-name? (ptree name) |
| 862 |
(setq name (canonicalize-tactic-name name)) |
| 863 |
(member name (ptree-defs-so-far ptree) :test #'equal)) |
| 864 |
|
| 865 |
;;; PROOF |
| 866 |
;;; |
| 867 |
(defstruct citp-proof |
| 868 |
(context nil) ; context module in which proof is performed |
| 869 |
(discharged nil) ; list of proved sentences |
| 870 |
) |
| 871 |
|
| 872 |
(defvar *the-citp-proof* (make-citp-proof :context :none :discharged nil)) |
| 873 |
|
| 874 |
(defun get-discharged-sentence-with-label (label) |
| 875 |
(declare (type string label)) |
| 876 |
(let ((name (intern label)) |
| 877 |
(res nil)) |
| 878 |
(dolist (ax (citp-proof-discharged *the-citp-proof*)) |
| 879 |
(when (member name (axiom-labels ax)) |
| 880 |
(push ax res))) |
| 881 |
(when (> (length res) 1) |
| 882 |
(with-output-chaos-warning () |
| 883 |
(format t "Found more than 1 sentences with name ~s." label))) |
| 884 |
(unless res |
| 885 |
(with-output-chaos-error () |
| 886 |
(format t "No such discharged sentence with name ~s." label))) |
| 887 |
res)) |
| 888 |
|
| 889 |
(defun find-discharged-sentences-with-label (list-labels) |
| 890 |
(let ((axs nil)) |
| 891 |
(dolist (label list-labels) |
| 892 |
(setq axs (nconc axs |
| 893 |
(get-discharged-sentence-with-label label)))) |
| 894 |
axs)) |
| 895 |
|
| 896 |
;;;-------------------------------------------------------------------- |
| 897 |
;;; Support functions for introducing new constants used in the proof. |
| 898 |
;;; |
| 899 |
;;; messaging when :verbose on |
| 900 |
;;; |
| 901 |
(eval-when (:compile-toplevel :execute :load-toplevel) |
| 902 |
|
| 903 |
(defmacro when-citp-verbose (&rest body) |
| 904 |
`(when *citp-verbose* |
| 905 |
(let ((*print-indent* (+ 2 *print-indent*)) |
| 906 |
(*print-line-limit* 90)) |
| 907 |
(declare (type fixnum *print-indent* *print-line-limit*)) |
| 908 |
,@body))) |
| 909 |
|
| 910 |
) |
| 911 |
|
| 912 |
;;; citp standard running env. |
| 913 |
;;; |
| 914 |
(defvar *citp-silent* t) |
| 915 |
(eval-when (:compile-toplevel :execute :load-toplevel) |
| 916 |
(defmacro with-citp-env (&rest body) |
| 917 |
`(if *citp-silent* |
| 918 |
(let ((*chaos-quiet* t) |
| 919 |
(*rwl-search-no-state-report* |
| 920 |
(if *citp-show-rwl* |
| 921 |
nil |
| 922 |
t))) |
| 923 |
,@body) |
| 924 |
(progn |
| 925 |
,@body))) |
| 926 |
|
| 927 |
(defmacro with-next-context ((&optional (node *proof-tree*)) &rest body) |
| 928 |
`(let ((.context. (get-next-proof-context ,node))) |
| 929 |
(unless .context. |
| 930 |
(with-output-chaos-error ('no-context) |
| 931 |
(format t "No proof context is established."))) |
| 932 |
(with-in-module ((goal-context (ptree-node-goal .context.))) |
| 933 |
(with-citp-env () |
| 934 |
,@body)))) |
| 935 |
|
| 936 |
) |
| 937 |
|
| 938 |
;;; for debugging |
| 939 |
;;; |
| 940 |
(eval-when (:compile-toplevel :execute :load-toplevel) |
| 941 |
|
| 942 |
(defmacro with-citp-debug (&rest body) |
| 943 |
`(when *debug-citp* |
| 944 |
(let ((*print-indent* (+ 2 *print-indent*)) |
| 945 |
(*print-line-limit* 90)) |
| 946 |
(declare (type fixnum *print-indent* *print-line-limit*)) |
| 947 |
,@body))) |
| 948 |
|
| 949 |
) |
| 950 |
|
| 951 |
;;; intro-const-returns-subst : module name variable -> (variable . constant-term) |
| 952 |
;;; introduces a new constant of sort(variable) into a module. |
| 953 |
;;; returns a pair (variable . constant-term) |
| 954 |
;;; |
| 955 |
(defun citp-intro-const (module name sort) |
| 956 |
(multiple-value-bind (op meth) |
| 957 |
(declare-operator-in-module (list name) |
| 958 |
nil ; arity |
| 959 |
sort ; coarity |
| 960 |
module ; |
| 961 |
nil ; constructor? |
| 962 |
nil ; behavioural? always nil. |
| 963 |
nil ; not coherent |
| 964 |
) |
| 965 |
(declare (ignore op)) |
| 966 |
(prepare-for-parsing module t t) ; force |
| 967 |
meth)) |
| 968 |
|
| 969 |
(defun intro-const-returns-subst (module name variable) |
| 970 |
(cons variable (make-applform (variable-sort variable) |
| 971 |
(citp-intro-const module name (variable-sort variable)) |
| 972 |
nil))) |
| 973 |
|
| 974 |
;;; make-tc-const-name : variable -> string |
| 975 |
;;; |
| 976 |
(defun make-tc-const-name (name sort) |
| 977 |
(format nil "~:@(~a~)@~a" name (string (sort-name sort)))) |
| 978 |
|
| 979 |
(defun make-tc-pconst-name (name) |
| 980 |
(format nil "`~:@(~a~)" name)) |
| 981 |
|
| 982 |
;;; introduces on-the-fly constant |
| 983 |
;;; |
| 984 |
(defun intro-fresh-pconstant (goal name-seed sort) |
| 985 |
(declare (ignore goal)) |
| 986 |
(let ((name (make-tc-pconst-name name-seed))) |
| 987 |
(make-pconst-term sort name))) |
| 988 |
|
| 989 |
;;; variable->constant : goal variable -> term |
| 990 |
;;; In the given goal, introduces fresh constant which should substitute the given varirable. |
| 991 |
;;; used by tactic TC. |
| 992 |
;;; |
| 993 |
(defun find-variable-subst-in (alist variable) |
| 994 |
(assoc variable alist :test #'variable-equal)) |
| 995 |
|
| 996 |
(defun list-submodules (module) |
| 997 |
(mapcar #'car (module-all-submodules module))) |
| 998 |
|
| 999 |
(defun variable->constant (goal variable) |
| 1000 |
(let ((vc-assoc (find-variable-subst-in (goal-constants goal) variable))) |
| 1001 |
(or (cdr vc-assoc) |
| 1002 |
(let ((name (cdr (find-variable-subst-in (ptree-var-subst *proof-tree*) variable))) |
| 1003 |
(v-const nil)) |
| 1004 |
(unless name |
| 1005 |
(setq name (make-tc-const-name (variable-name variable) (variable-sort variable))) |
| 1006 |
(push (cons variable name) (ptree-var-subst *proof-tree*))) |
| 1007 |
(setq v-const (intro-const-returns-subst (goal-context goal) |
| 1008 |
name |
| 1009 |
variable)) |
| 1010 |
(push v-const (goal-constants goal)) |
| 1011 |
(cdr v-const))))) |
| 1012 |
|
| 1013 |
;;; ----------------------------------------------------------- |
| 1014 |
;;; Utils for constructors |
| 1015 |
;;; |
| 1016 |
|
| 1017 |
;;; gather-constructor-ops : module -> List(constructor) |
| 1018 |
;;; list up all the constructor ops in a given module |
| 1019 |
;;; |
| 1020 |
(defun gather-constructor-ops (module) |
| 1021 |
(flet ((method-is-user-defined-or-tf? (x) |
| 1022 |
(or (eq x *bool-true-meth*) |
| 1023 |
(eq x *bool-false-meth*) |
| 1024 |
(and (not (sort= (method-coarity x) *sort-id-sort*)) |
| 1025 |
(module-is-user-module (method-module x)))))) |
| 1026 |
(let ((res nil)) |
| 1027 |
(with-in-module (module) |
| 1028 |
(dolist (opinfo (module-all-operators module)) |
| 1029 |
(dolist (meth (opinfo-methods opinfo)) |
| 1030 |
(when (and (method-is-constructor? meth) |
| 1031 |
(method-is-user-defined-or-tf? meth)) |
| 1032 |
(push meth res)))) |
| 1033 |
res)))) |
| 1034 |
|
| 1035 |
;;; get-constructors-of-sort : sort -> List[constructor] |
| 1036 |
;;; returns the list of constructors of sort. |
| 1037 |
;;; |
| 1038 |
(defun get-constructors-of-sort (sort &optional (context (get-context-module))) |
| 1039 |
(let ((ops nil)) |
| 1040 |
(dolist (meth (ptree-constructor-ops *proof-tree*)) |
| 1041 |
(when (and (method-is-constructor? meth) |
| 1042 |
(sort<= (method-coarity meth) sort (module-sort-order context))) |
| 1043 |
(push meth ops))) |
| 1044 |
(reverse ops))) |
| 1045 |
|
| 1046 |
;;; default-constructor-order |
| 1047 |
;;; |
| 1048 |
(defun default-constructor-order (constructors) |
| 1049 |
(sort constructors |
| 1050 |
#'(lambda (x y) |
| 1051 |
;; first precedence is number of arguments |
| 1052 |
(let ((ax (method-num-args x)) |
| 1053 |
(ay (method-num-args y))) |
| 1054 |
(if (< ax ay) |
| 1055 |
t |
| 1056 |
(if (> ax ay) |
| 1057 |
nil |
| 1058 |
(if (eq x *bool-true-meth*) |
| 1059 |
;; this orders true > ... > false |
| 1060 |
t |
| 1061 |
(if (eq y *bool-true-meth*) |
| 1062 |
nil |
| 1063 |
(if (eq (op-lex-precedence x y) :greater) |
| 1064 |
nil |
| 1065 |
t))))))))) |
| 1066 |
|
| 1067 |
;;; order-constructors : List[constructor] -> List[constructor]' |
| 1068 |
;;; |
| 1069 |
(defun order-constructors (constructors &optional (order-spec nil)) |
| 1070 |
(cond ((null order-spec) (default-constructor-order constructors)) |
| 1071 |
(t |
| 1072 |
;; order is specified by :order command |
| 1073 |
;; first initialize the order to default |
| 1074 |
(let* ((pos-star (position :* order-spec)) |
| 1075 |
(first-half (subseq order-spec 0 pos-star)) |
| 1076 |
(second-half (subseq order-spec (1+ pos-star)))) |
| 1077 |
(setq constructors (set-difference constructors |
| 1078 |
(append first-half second-half))) |
| 1079 |
(setq constructors (default-constructor-order constructors)) |
| 1080 |
(append first-half constructors second-half))))) |
| 1081 |
|
| 1082 |
;;; show-constructor-order : ptree -> void |
| 1083 |
;;; |
| 1084 |
(defun show-constructor-order (ptree) |
| 1085 |
(let ((num 0)) |
| 1086 |
(with-output-msg () |
| 1087 |
(format t "Current order of constructors:") |
| 1088 |
(dolist (op (ptree-constructor-ops ptree)) |
| 1089 |
(print-next) |
| 1090 |
(format t "(~d) " (incf num)) |
| 1091 |
(print-method-brief op))) |
| 1092 |
(terpri))) |
| 1093 |
|
| 1094 |
;;; handler of :order command |
| 1095 |
;;; :order (<op>, ..., <op>) |
| 1096 |
;;; |
| 1097 |
(defun citp-eval-order (ast) |
| 1098 |
(declare (notinline %pn-lex-ops)) |
| 1099 |
(check-context-module-and-ptree) |
| 1100 |
(with-in-module ((get-context-module)) |
| 1101 |
(let ((optokens (%pn-lex-ops ast))) |
| 1102 |
(cond ((and (null (cdr optokens)) |
| 1103 |
(null (car optokens))) |
| 1104 |
(show-constructor-order *proof-tree*)) |
| 1105 |
(t (with-output-msg () |
| 1106 |
(format t "start setting constructor ordering...")) |
| 1107 |
(set-constructor-order *proof-tree* optokens) |
| 1108 |
(format t "~%done.") |
| 1109 |
(when-citp-verbose () |
| 1110 |
(show-constructor-order *proof-tree*))))))) |
| 1111 |
|
| 1112 |
(defun set-constructor-order (ptree optokens) |
| 1113 |
(let ((prec-list nil)) |
| 1114 |
(dolist (e optokens) |
| 1115 |
(cond ((equal e '("*")) |
| 1116 |
(if (not (memq :* prec-list)) |
| 1117 |
(setq prec-list (append prec-list '(:*))) |
| 1118 |
(with-output-chaos-warning () |
| 1119 |
(format t "* is already specified.")))) |
| 1120 |
(t (let ((parsedop (parse-op-name e))) |
| 1121 |
(multiple-value-bind (ops mod) |
| 1122 |
(resolve-operator-reference parsedop) |
| 1123 |
(with-in-module (mod) |
| 1124 |
(let ((overs nil)) |
| 1125 |
(dolist (opinfo ops) |
| 1126 |
(dolist (meth (opinfo-methods opinfo)) |
| 1127 |
(if (method-is-constructor? meth) |
| 1128 |
(if (member meth prec-list) |
| 1129 |
(with-output-chaos-warning () |
| 1130 |
(format t "operator ~{~a~}/~d is already ordered, ignored." |
| 1131 |
(method-symbol meth) |
| 1132 |
(method-num-args meth))) |
| 1133 |
(push meth overs)) |
| 1134 |
(unless (method-is-error-method meth) |
| 1135 |
(with-output-chaos-warning () |
| 1136 |
(format t "operator ~{~s~} is not a constructor, ignored." |
| 1137 |
(method-symbol meth))))))) |
| 1138 |
;; order overloaded ops by number of arguments |
| 1139 |
(setq overs (sort overs #'(lambda (x y) |
| 1140 |
(let ((x-num (method-num-args x)) |
| 1141 |
(y-num (method-num-args y))) |
| 1142 |
(declare (type fixnum x-num y-num)) |
| 1143 |
(< x-num y-num))))) |
| 1144 |
;; set the result |
| 1145 |
(setq prec-list (append prec-list overs))))))))) |
| 1146 |
(unless (memq :* prec-list) |
| 1147 |
(setq prec-list (append prec-list '(:*)))) |
| 1148 |
(setf (ptree-constructor-ops ptree) |
| 1149 |
(order-constructors (ptree-constructor-ops ptree) |
| 1150 |
prec-list)))) |
| 1151 |
|
| 1152 |
;;; variable->constructor : goal variable op -> term |
| 1153 |
;;; |
| 1154 |
(defun make-ind-const-name (name-prefix sort) |
| 1155 |
(format nil "~a#~a" (string name-prefix) (string (sort-name sort)))) |
| 1156 |
|
| 1157 |
(defun variable->constructor (goal variable &key (sort nil) (op nil)) |
| 1158 |
(let ((svar (if sort |
| 1159 |
(make-variable-term sort (intern (format nil "~a_~a" |
| 1160 |
(variable-name variable) |
| 1161 |
(sort-name sort)))) |
| 1162 |
variable))) |
| 1163 |
(flet ((make-iv-const (name) |
| 1164 |
(if op |
| 1165 |
(let ((constant (make-applform (method-coarity op) op nil))) |
| 1166 |
(push (cons variable constant) (goal-ind-constants goal)) |
| 1167 |
constant) |
| 1168 |
(let ((con (intro-const-returns-subst (goal-context goal) |
| 1169 |
name |
| 1170 |
svar))) |
| 1171 |
(push con (goal-ind-constants goal)) |
| 1172 |
(cdr con))))) |
| 1173 |
(let ((v-assoc (find-variable-subst-in (goal-ind-constants goal) svar))) |
| 1174 |
(or (cdr v-assoc) |
| 1175 |
(let ((v-name (cdr (find-variable-subst-in (ptree-indvar-subst *proof-tree*) svar))) |
| 1176 |
(vconst nil)) |
| 1177 |
(unless v-name |
| 1178 |
(setq v-name (make-ind-const-name (variable-name variable) |
| 1179 |
(or sort (variable-sort svar))))) |
| 1180 |
(setq vconst (make-iv-const v-name)) |
| 1181 |
(pushnew (cons svar v-name) (ptree-indvar-subst *proof-tree*) :test #'equal) |
| 1182 |
vconst)))))) |
| 1183 |
|
| 1184 |
;;; intro-fresh-constant : goal -> term (introduced constant) |
| 1185 |
;;; introduces brand new constant in the current proof context |
| 1186 |
;;; |
| 1187 |
(defun intro-fresh-constant (goal name-seed sort) |
| 1188 |
(let* ((name (make-ind-const-name name-seed sort)) |
| 1189 |
(meth (citp-intro-const (goal-context goal) name sort)) |
| 1190 |
(v-const (make-applform sort meth nil))) |
| 1191 |
(push (cons meth v-const) (goal-ind-constants goal)) |
| 1192 |
v-const)) |
| 1193 |
|
| 1194 |
;;; SKOLEMITIZE |
| 1195 |
;;; allow citp to represent the goal sentence in FOPLE-SENTENCE |
| 1196 |
(defun skolemize-if-need (fax) |
| 1197 |
(unless (eq (axiom-type fax) :pignose-axiom) |
| 1198 |
(return-from skolemize-if-need fax)) |
| 1199 |
(with-citp-debug () |
| 1200 |
(format t "~%[skolemize]: ") |
| 1201 |
(print-axiom-brief fax)) |
| 1202 |
(let* ((sentence (axiom-lhs fax)) |
| 1203 |
(type (fopl-sentence-type sentence)) |
| 1204 |
(*sk-function-num* nil)) |
| 1205 |
(declare (type symbol type) |
| 1206 |
(special *sk-function-num*)) |
| 1207 |
(when (and (memq type '(:eq :beq)) |
| 1208 |
(term-is-lisp-form? (term-arg-2 sentence))) |
| 1209 |
(return-from skolemize-if-need fax)) |
| 1210 |
;; normalize quantified formula |
| 1211 |
;; \Q[v1...vn]S --> \Q[v1]\Q[v2]...\Q[vn]S |
| 1212 |
(normalize-quantifiers sentence) |
| 1213 |
;; convert to NNF(negation normal form.) |
| 1214 |
(setq sentence (neg-normal-form sentence)) |
| 1215 |
;; skolemization -- eliminate \Es |
| 1216 |
(skolemize sentence) |
| 1217 |
;; skolemize may introduce new operators. |
| 1218 |
(prepare-for-parsing *current-module*) |
| 1219 |
;; eliminate quantifiers -- eliminate \As |
| 1220 |
(zap-quantifiers sentence) |
| 1221 |
;; convert to CNF(conjunctive normal form). |
| 1222 |
(conj-normal-form sentence) |
| 1223 |
;; make it an equation |
| 1224 |
(let ((ax (make-rule :lhs sentence |
| 1225 |
:rhs *bool-true* |
| 1226 |
:condition *bool-true* |
| 1227 |
:labels (axiom-labels fax) |
| 1228 |
:behavioural (axiom-is-behavioural fax) |
| 1229 |
:type :equation))) |
| 1230 |
(adjoin-axiom-to-module *current-module* ax) |
| 1231 |
ax))) |
| 1232 |
|
| 1233 |
;;; |
| 1234 |
;;; initialize-proof-tree : module goal -> ptree |
| 1235 |
;;; |
| 1236 |
(defun initialize-proof-tree (context-module goal-module initial-goals) |
| 1237 |
(with-in-module (goal-module) |
| 1238 |
(let ((*sk-function-num* nil)) |
| 1239 |
(declare (special *sk-function-num*)) |
| 1240 |
(let* ((targets (mapcar #'skolemize-if-need initial-goals)) |
| 1241 |
(root (make-ptree-root goal-module targets))) |
| 1242 |
(setq *next-default-proof-node* nil) |
| 1243 |
(make-ptree :root root |
| 1244 |
:context context-module |
| 1245 |
:constructor-ops |
| 1246 |
(order-constructors (gather-constructor-ops context-module) nil)))))) |
| 1247 |
|
| 1248 |
;;; |
| 1249 |
;;; check-success : ptree -> Bool |
| 1250 |
;;; |
| 1251 |
(defun check-success (ptree) |
| 1252 |
(let ((unp (get-unproved-nodes ptree))) |
| 1253 |
(when unp |
| 1254 |
(format t "~%>> Next target goal is ~s." (goal-name (ptree-node-goal (car unp)))) |
| 1255 |
(setq *next-default-proof-node* (car unp)) |
| 1256 |
(format t "~%>> Remaining ~d goal~p.~%" (length unp) (length unp)) |
| 1257 |
(return-from check-success nil)) |
| 1258 |
(format t "~%** All goals are successfully discharged.~%") |
| 1259 |
(save-discharged-sentences) |
| 1260 |
(setq *next-default-proof-node* nil) |
| 1261 |
t)) |
| 1262 |
|
| 1263 |
;;; ------------------------------------------------------------------ |
| 1264 |
;;; roll-back : ptree -> Bool |
| 1265 |
;;; roll back to parent. returns true (non-nil) iff roll back is done. |
| 1266 |
;;; |
| 1267 |
(defun roll-back (ptree) |
| 1268 |
(declare (type ptree ptree)) |
| 1269 |
(let* ((current-target (get-next-proof-context ptree)) |
| 1270 |
(parent (and current-target (ptree-node-parent current-target)))) |
| 1271 |
(unless parent |
| 1272 |
(format t "~%**> :roll back, already at root.") |
| 1273 |
(setq *next-default-proof-node* nil) |
| 1274 |
(return-from roll-back nil)) |
| 1275 |
(setf (ptree-node-subnodes parent) nil |
| 1276 |
(ptree-node-num-children parent) 0 |
| 1277 |
(ptree-node-next-child parent) 0) |
| 1278 |
(format t "~%**> :roll back") |
| 1279 |
(setq *next-default-proof-node* nil) |
| 1280 |
(setq current-target (get-next-proof-context ptree)) |
| 1281 |
(when current-target |
| 1282 |
(format t "~% next default target is ~s" (goal-name (ptree-node-goal current-target)))) |
| 1283 |
current-target)) |
| 1284 |
|
| 1285 |
;;; |
| 1286 |
;;; find-goal-node : ptree string -> { ptree-node | nil } |
| 1287 |
;;; |
| 1288 |
(defun find-goal-node (ptree name) |
| 1289 |
(declare (type ptree ptree) |
| 1290 |
(type string name)) |
| 1291 |
(dag-wfs (ptree-root ptree) |
| 1292 |
#'(lambda (n) (let ((goal (ptree-node-goal n))) |
| 1293 |
(when (string= (goal-name goal) name) |
| 1294 |
(return-from find-goal-node n)))))) |
| 1295 |
|
| 1296 |
;;; |
| 1297 |
;;; print-named-goal : name -> void |
| 1298 |
;;; |
| 1299 |
(defun print-named-goal (ptree name) |
| 1300 |
(unless ptree |
| 1301 |
(with-output-chaos-warning () |
| 1302 |
(format t "There is no proof tree.") |
| 1303 |
(return-from print-named-goal nil))) |
| 1304 |
(let ((goal-node (if name |
| 1305 |
(find-goal-node ptree name) |
| 1306 |
(or (get-next-proof-context ptree) |
| 1307 |
(with-output-chaos-error ('no-goal) |
| 1308 |
(princ "No default goal is specified.")))))) |
| 1309 |
(unless goal-node |
| 1310 |
(with-output-chaos-error ('no-such-goal) |
| 1311 |
(format t "No such goal with the name ~s" name))) |
| 1312 |
(pr-goal (ptree-node-goal goal-node)))) |
| 1313 |
|
| 1314 |
;;; |
| 1315 |
;;; get-unproved-nodes : ptree -> List(ptree-node) |
| 1316 |
;;; uses depth first search |
| 1317 |
(defun get-unproved-nodes (ptree) |
| 1318 |
(let ((nodes nil)) |
| 1319 |
(dag-dfs (ptree-root ptree) |
| 1320 |
#'(lambda (x) |
| 1321 |
(declare (type ptree-node)) |
| 1322 |
(unless (or (ptree-node-subnodes x) |
| 1323 |
(goal-is-discharged (ptree-node-goal x))) |
| 1324 |
(push x nodes)))) |
| 1325 |
(nreverse nodes))) |
| 1326 |
|
| 1327 |
;;; get-unproved-goals : ptree -> List(goal) |
| 1328 |
;;; |
| 1329 |
(defun get-unproved-goals (ptree) |
| 1330 |
(mapcar #'(lambda (y) (ptree-node-goal y)) (get-unproved-nodes ptree))) |
| 1331 |
|
| 1332 |
;;; print-unproved-goals |
| 1333 |
;;; |
| 1334 |
(defun print-unproved-goals (ptree &optional (stream *standard-output*)) |
| 1335 |
(unless ptree |
| 1336 |
(with-output-chaos-warning () |
| 1337 |
(format t "No goal is specified yet.") |
| 1338 |
(return-from print-unproved-goals nil))) |
| 1339 |
(dolist (goal (get-unproved-goals ptree)) |
| 1340 |
(pr-goal goal stream))) |
| 1341 |
|
| 1342 |
;;; get-next-pfoof-context : ptree -> ptree-node |
| 1343 |
;;; |
| 1344 |
(defun get-next-proof-context (ptree) |
| 1345 |
(and ptree (or *next-default-proof-node* |
| 1346 |
(car (get-unproved-nodes ptree))))) |
| 1347 |
|
| 1348 |
(defun next-proof-target-is-specified? () |
| 1349 |
*next-default-proof-node*) |
| 1350 |
|
| 1351 |
;;; get-target-goal-node |
| 1352 |
;;; given goal-name or NULL, returns the next targetted goal node. |
| 1353 |
;;; |
| 1354 |
(defun get-target-goal-node (&optional goal-name) |
| 1355 |
(let ((next-goal-node (if goal-name |
| 1356 |
(find-goal-node *proof-tree* goal-name) |
| 1357 |
(get-next-proof-context *proof-tree*)))) |
| 1358 |
(unless next-goal-node |
| 1359 |
(with-output-chaos-error ('no-target) |
| 1360 |
(if goal-name |
| 1361 |
(format t "Could not find the goal ~s." goal-name) |
| 1362 |
(format t "No default target goal.")))) |
| 1363 |
next-goal-node)) |
| 1364 |
|
| 1365 |
;;; |
| 1366 |
;;; select-next-goal : goal-name |
| 1367 |
;;; |
| 1368 |
(defun select-next-goal (goal-name) |
| 1369 |
(declare (type string goal-name)) |
| 1370 |
(unless *proof-tree* |
| 1371 |
(with-output-chaos-error ('no-proof-tree) |
| 1372 |
(format t "No proof is ongoing."))) |
| 1373 |
(cond ((string= goal-name ".") |
| 1374 |
(setq *next-default-proof-node* nil) |
| 1375 |
(let ((next (get-next-proof-context *proof-tree*))) |
| 1376 |
(format t "~%:select resetting next default target ...") |
| 1377 |
(unless next |
| 1378 |
(with-output-chaos-warning () |
| 1379 |
(format t "There is no unproved goal.") |
| 1380 |
(return-from select-next-goal nil))) |
| 1381 |
(format t "~%>> next default-goal is ~s" (goal-name (ptree-node-goal next))))) |
| 1382 |
(t (let ((node (find-goal-node *proof-tree* goal-name))) |
| 1383 |
(unless node |
| 1384 |
(with-output-chaos-error ('no-goal) |
| 1385 |
(format t "No such goal ~s" goal-name))) |
| 1386 |
(when (node-is-discharged? node) |
| 1387 |
(with-output-chaos-warning () |
| 1388 |
(format t "The goal ~s is alreaday discharged." (goal-name (ptree-node-goal node))) |
| 1389 |
(print-next) |
| 1390 |
(format t "This will discard the current status of the goal.")) |
| 1391 |
(make-it-unproved node)) |
| 1392 |
(setq *next-default-proof-node* node) |
| 1393 |
(when (eq node (ptree-root *proof-tree*)) |
| 1394 |
(reset-proof *proof-tree*)) |
| 1395 |
(format t "~%>> setting next default goal to ~s" (goal-name (ptree-node-goal node))) |
| 1396 |
node)))) |
| 1397 |
|
| 1398 |
;;; Getting TACTIC |
| 1399 |
;;; get-tactic : name -> LIST(tactic) |
| 1400 |
;;; |
| 1401 |
(defun get-tactic (name) |
| 1402 |
(let ((context (get-next-proof-context *proof-tree*))) |
| 1403 |
(let ((tactic (or (and context (get-defined-tactic (ptree-node-goal context) name)) |
| 1404 |
(get-builtin-tactic name)))) |
| 1405 |
(unless tactic |
| 1406 |
(with-output-chaos-error ('no-such-tactic) |
| 1407 |
(format t "No such tactic is defined with the name ~s" name))) |
| 1408 |
(if (atom tactic) |
| 1409 |
(list tactic) |
| 1410 |
tactic)))) |
| 1411 |
|
| 1412 |
;;; ==================== |
| 1413 |
;;; TOP LEVEL FUNCTIONS |
| 1414 |
;;; ==================== |
| 1415 |
|
| 1416 |
;;; begin-proof |
| 1417 |
;;; starting the new proof |
| 1418 |
;;; |
| 1419 |
(defparameter .root-context-module. (%module-decl* "#Goal-root" :object :user nil)) |
| 1420 |
|
| 1421 |
(defun reset-proof-session () |
| 1422 |
(with-output-simple-msg () |
| 1423 |
(format t "** !! Discarding the current proof...")) |
| 1424 |
(setf (citp-proof-context *the-citp-proof*) nil |
| 1425 |
(citp-proof-discharged *the-citp-proof*) nil) |
| 1426 |
(setf .user-defined-tactics. nil) |
| 1427 |
(reset-proof *proof-tree*) |
| 1428 |
(setq *proof-tree* nil)) |
| 1429 |
|
| 1430 |
(defun citp-reset-proof-if-need (redefined-module) |
| 1431 |
(when (eq redefined-module (citp-proof-context *the-citp-proof*)) |
| 1432 |
(reset-proof-session))) |
| 1433 |
|
| 1434 |
(defun save-discharged-sentences () |
| 1435 |
(setf (citp-proof-discharged *the-citp-proof*) |
| 1436 |
(append (citp-proof-discharged *the-citp-proof*) |
| 1437 |
(goal-targets (ptree-node-goal (ptree-root *proof-tree*)))))) |
| 1438 |
|
| 1439 |
(defun print-discharged-sentences () |
| 1440 |
(let ((discharged (citp-proof-discharged *the-citp-proof*))) |
| 1441 |
(if discharged |
| 1442 |
(with-in-module ((citp-proof-context *the-citp-proof*)) |
| 1443 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 1444 |
(format t "~&** Discharged sentence~p" (length discharged)) |
| 1445 |
(format t "~%-- context module: ~a" (module-print-name *current-module*)) |
| 1446 |
(dolist (ax discharged) |
| 1447 |
(print-next) |
| 1448 |
(print-axiom-brief ax) |
| 1449 |
(princ " .")))) |
| 1450 |
(format t "None~%")))) |
| 1451 |
|
| 1452 |
;;; for LE check |
| 1453 |
;; (defvar .int-module. nil) |
| 1454 |
(defvar .ls-pat. nil) ; X < Y |
| 1455 |
(defvar .le-pat. nil) ; X <= Y |
| 1456 |
|
| 1457 |
(defun prepare-root-context (root-module context-module) |
| 1458 |
(unless .int-module. |
| 1459 |
(setq .int-module. (eval-modexp "INT")) |
| 1460 |
(with-in-module (.int-module.) |
| 1461 |
(let ((less (find-operator '("_" "<" "_") 2 .int-module.)) |
| 1462 |
(le (find-operator '("_" "<=" "_") 2 .int-module.)) |
| 1463 |
(less-m nil) |
| 1464 |
(le-m nil) |
| 1465 |
(var-x nil) |
| 1466 |
(var-y nil) |
| 1467 |
(int-sort (find-sort-in .int-module. '|Int|))) |
| 1468 |
(setq less-m (lowest-method* (car (opinfo-methods less)))) |
| 1469 |
(setq le-m (lowest-method* (car (opinfo-methods le)))) |
| 1470 |
(setq var-x (make-variable-term int-sort 'X)) |
| 1471 |
(setq var-y (make-variable-term int-sort 'Y)) |
| 1472 |
(setq .ls-pat. (make-applform *bool-sort* less-m (list var-x var-y))) |
| 1473 |
(setq .le-pat. (make-applform *bool-sort* le-m (list var-x var-y)))))) |
| 1474 |
(import-module root-module :protecting context-module) |
| 1475 |
(import-module root-module :protecting .int-module.) |
| 1476 |
(compile-module root-module t)) |
| 1477 |
|
| 1478 |
(defun begin-proof (context-module goal-axioms) |
| 1479 |
(declare (type module context-module) |
| 1480 |
(type list goal-axioms)) |
| 1481 |
(unless goal-axioms (return-from begin-proof nil)) |
| 1482 |
;; if the context module is changed, we begin a brand new proof session |
| 1483 |
(unless (eq (citp-proof-context *the-citp-proof*) |
| 1484 |
context-module) |
| 1485 |
(with-output-simple-msg () |
| 1486 |
(format t "** Beginning a new proof in ~a" (module-print-name context-module))) |
| 1487 |
(setq *the-citp-proof* |
| 1488 |
(make-citp-proof :context context-module :discharged nil))) |
| 1489 |
(let* ((*chaos-quiet* t) |
| 1490 |
(root-module (eval-ast .root-context-module.))) |
| 1491 |
(setf (module-hidden root-module) t) |
| 1492 |
(prepare-root-context root-module context-module) |
| 1493 |
(when .goals-so-far. |
| 1494 |
(setq *modules-so-far-table* (remove-if #'(lambda (x) |
| 1495 |
(member (car x) .goals-so-far. :test #'equal)) |
| 1496 |
*modules-so-far-table*)) |
| 1497 |
(setq .goals-so-far. nil)) |
| 1498 |
(setq *proof-tree* (initialize-proof-tree context-module root-module goal-axioms)) |
| 1499 |
(pr-goal (ptree-node-goal (ptree-root *proof-tree*))) |
| 1500 |
(format t "~%** Initial goal (root) is generated. **") |
| 1501 |
(setq *next-default-proof-node* (ptree-root *proof-tree*)) |
| 1502 |
*proof-tree*)) |
| 1503 |
|
| 1504 |
;;; -------- |
| 1505 |
;;; PRiNTERS |
| 1506 |
;;; -------- |
| 1507 |
|
| 1508 |
;;; print-proof-tree |
| 1509 |
;;; |
| 1510 |
(defvar *show-proof-mode* :horizontal) |
| 1511 |
|
| 1512 |
(defun print-proof-tree (goal-name &optional (describe nil)) |
| 1513 |
(unless *proof-tree* |
| 1514 |
(with-output-chaos-warning () |
| 1515 |
(format t "There is no proof tree.") |
| 1516 |
(return-from print-proof-tree nil))) |
| 1517 |
(let ((target-node (if goal-name |
| 1518 |
(or (find-goal-node *proof-tree* goal-name) |
| 1519 |
(with-output-chaos-error ('no-such-goal) |
| 1520 |
(format t "No goal with the name ~s." goal-name))) |
| 1521 |
(ptree-root *proof-tree*)))) |
| 1522 |
(if describe |
| 1523 |
(describe-proof-tree target-node) |
| 1524 |
(!print-proof-tree target-node (get-next-proof-context *proof-tree*) *show-proof-mode*)))) |
| 1525 |
|
| 1526 |
(defun !print-proof-tree (root-node next-target mode &optional (stream *standard-output*)) |
| 1527 |
(if (eq mode :horizontal) |
| 1528 |
(!print-proof-horizontal root-node next-target stream) |
| 1529 |
(!print-proof-vertical root-node next-target stream))) |
| 1530 |
|
| 1531 |
(defun !print-proof-vertical (root-node next-target stream) |
| 1532 |
(let* ((leaf? #'(lambda (node) (null (dag-node-subnodes node)))) |
| 1533 |
(leaf-name #'(lambda (node) |
| 1534 |
(with-output-to-string (s) |
| 1535 |
(let ((goal (ptree-node-goal node))) |
| 1536 |
(when (eq node next-target) |
| 1537 |
(princ ">" s)) |
| 1538 |
(if (goal-tactic goal) |
| 1539 |
(format s "[~a] ~a" (tactic-name (goal-tactic goal)) (goal-name goal)) |
| 1540 |
(princ (goal-name goal) s)) |
| 1541 |
(when (node-is-discharged? node) |
| 1542 |
(princ "*" s))) |
| 1543 |
s))) |
| 1544 |
(leaf-info #'(lambda (node) (declare (ignore node)) t)) |
| 1545 |
(int-node-name #'(lambda (node) (funcall leaf-name node))) |
| 1546 |
(int-node-children #'(lambda (node) (ptree-node-subnodes node)))) |
| 1547 |
(force-output stream) |
| 1548 |
(print-next nil *print-indent* stream) |
| 1549 |
(print-trees (list (augment-tree root-node)) stream))) |
| 1550 |
|
| 1551 |
(defun !print-proof-horizontal (node next-target stream) |
| 1552 |
(let ((*standard-output* stream)) |
| 1553 |
(let ((goal (ptree-node-goal node))) |
| 1554 |
(with-in-module ((goal-context goal)) |
| 1555 |
(when (eq node next-target) |
| 1556 |
(princ ">")) |
| 1557 |
(if (goal-tactic goal) |
| 1558 |
(format t "[~a]~6T~a" (tactic-name (goal-tactic goal)) (goal-name goal)) |
| 1559 |
(format t "~a" (goal-name goal))) |
| 1560 |
(when (node-is-discharged? node) |
| 1561 |
(princ "*")))) |
| 1562 |
(let ((subnodes (ptree-node-subnodes node))) |
| 1563 |
(when subnodes |
| 1564 |
(dolist (sub subnodes) |
| 1565 |
(print-next-prefix #\Space) |
| 1566 |
(!print-proof-horizontal sub next-target stream)))))) |
| 1567 |
|
| 1568 |
(defparameter *proof-indent* 0) |
| 1569 |
|
| 1570 |
(defun describe-proof-tree (node) |
| 1571 |
(declare (type ptree-node node)) |
| 1572 |
(flet ((proved? () |
| 1573 |
(format nil "~:[ ~;*~]" (node-is-discharged? node)))) |
| 1574 |
(let ((goal (ptree-node-goal node)) |
| 1575 |
(*print-line-limit* 80) |
| 1576 |
;; (*print-xmode* :fancy) |
| 1577 |
) |
| 1578 |
(with-in-module ((goal-context goal)) |
| 1579 |
(if (goal-tactic goal) |
| 1580 |
(format t "[~a]~8T~a~a" (tactic-name (goal-tactic goal)) (goal-name goal) (proved?)) |
| 1581 |
(format t "==> ~a~a" (goal-name goal) (proved?))) |
| 1582 |
;; (princ " ------------------------") |
| 1583 |
(let ((*print-indent* (+ 4 *print-indent*))) |
| 1584 |
(print-next) |
| 1585 |
(format t "-- context module: ~a" (get-module-simple-name *current-module*)) |
| 1586 |
(let ((assumptions (goal-assumptions goal))) |
| 1587 |
(when assumptions |
| 1588 |
(print-next) |
| 1589 |
(format t "-- assumption~p" (length assumptions)) |
| 1590 |
(let ((*print-indent* (+ 2 *print-indent*)) |
| 1591 |
;; (*print-xmode* :fancy) |
| 1592 |
) |
| 1593 |
(dolist (as assumptions) |
| 1594 |
(print-next) |
| 1595 |
(print-axiom-brief as) |
| 1596 |
(princ " ."))))) |
| 1597 |
(let ((proved (goal-proved goal))) |
| 1598 |
(when proved |
| 1599 |
(print-next) |
| 1600 |
(format t "-- discharged sentence~p:" (length proved)) |
| 1601 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 1602 |
(dolist (ax proved) |
| 1603 |
(print-next) |
| 1604 |
(print-axiom-brief ax) |
| 1605 |
(princ " ."))))) |
| 1606 |
(let ((targets (goal-targets goal))) |
| 1607 |
(when targets |
| 1608 |
(print-next) |
| 1609 |
(if (node-is-discharged? node) |
| 1610 |
(format t "-- targeted sentence~p:" (length targets)) |
| 1611 |
(format t "-- sentence~p to be proved:" (length targets))) |
| 1612 |
(let ((*print-indent* (+ 2 *print-indent*))) |
| 1613 |
(dolist (target targets) |
| 1614 |
(print-next) |
| 1615 |
(print-axiom-brief target) |
| 1616 |
(princ " ."))))))) |
| 1617 |
(let ((subnodes (ptree-node-subnodes node))) |
| 1618 |
(when subnodes |
| 1619 |
(let ((*print-indent* (+ *proof-indent* *print-indent*))) |
| 1620 |
(dolist (sub subnodes) |
| 1621 |
(print-next-prefix #\.) |
| 1622 |
(describe-proof-tree sub)))))))) |
| 1623 |
|
| 1624 |
;;; print-current-goal : mode -> void |
| 1625 |
;;; |
| 1626 |
(defun print-current-goal (describe) |
| 1627 |
(let ((current (get-next-proof-context *proof-tree*))) |
| 1628 |
(if current |
| 1629 |
(if describe ; :describe |
| 1630 |
(pr-goal (ptree-node-goal current)) |
| 1631 |
(format t "~%The current goal is ~a" (goal-name (ptree-node-goal current)))) |
| 1632 |
(with-output-chaos-warning () |
| 1633 |
(format t "All goals have been discharged."))))) |
| 1634 |
|
| 1635 |
;;; print-defs |
| 1636 |
;;; |
| 1637 |
(defun print-defs (describe &optional goal-name) |
| 1638 |
(declare (ignore describe)) |
| 1639 |
(let ((current (if goal-name |
| 1640 |
(find-goal-node *proof-tree* goal-name) |
| 1641 |
(get-next-proof-context *proof-tree*)))) |
| 1642 |
(if current |
| 1643 |
(let* ((goal (ptree-node-goal current)) |
| 1644 |
(defs (goal-defs goal))) |
| 1645 |
(unless defs |
| 1646 |
(format t "~%The goal ~a has no defs.~%" (goal-name goal)) |
| 1647 |
(return-from print-defs nil)) |
| 1648 |
(dolist (def defs) |
| 1649 |
(format t "~a = " (tactic-name def)) |
| 1650 |
(princ def) |
| 1651 |
(print-next))) |
| 1652 |
(with-output-chaos-warning () |
| 1653 |
(format t "No current goal."))))) |
| 1654 |
|
| 1655 |
;;; with-in-context : ptree-node |
| 1656 |
;;; construct a lexical environment for applying a tactic. |
| 1657 |
;;; |
| 1658 |
(eval-when (:compile-toplevel :execute :load-toplevel) |
| 1659 |
|
| 1660 |
(defmacro with-in-context ((ptree-node) &rest body) |
| 1661 |
(once-only (ptree-node) |
| 1662 |
`(block :exit |
| 1663 |
(let* ((.cur-goal. (ptree-node-goal ,ptree-node)) |
| 1664 |
(.cur-targets. (goal-targets .cur-goal.)) |
| 1665 |
(.next-goals. nil)) |
| 1666 |
(unless .cur-targets. (return-from :exit nil)) |
| 1667 |
,@body)))) |
| 1668 |
|
| 1669 |
) |
| 1670 |
|
| 1671 |
;;; EOF |