| 1 |
;;;-*-Mode:LISP; Package: CHAOS; Base:10; Syntax:Common-lisp -*- |
| 2 |
;;; |
| 3 |
;;; Copyright (c) 2000-2016, 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:citp.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 |
(defun check-context-module () |
| 41 |
(unless *current-module* |
| 42 |
(with-output-chaos-error ('no-context) |
| 43 |
(format t "No context module is specified, please 'select' or 'open' a module.")))) |
| 44 |
|
| 45 |
(defun check-ptree () |
| 46 |
(unless *proof-tree* |
| 47 |
(with-output-chaos-error ('no-proof-tree) |
| 48 |
(format t "No proof is ongoing.")))) |
| 49 |
|
| 50 |
(defun check-on-going() |
| 51 |
(unless (and *proof-tree* (get-unproved-nodes *proof-tree*)) |
| 52 |
(with-output-chaos-error ('no-target) |
| 53 |
(format t "There are no unproved goals.")))) |
| 54 |
|
| 55 |
(defun check-context-module-and-ptree () |
| 56 |
(check-context-module) |
| 57 |
(check-ptree)) |
| 58 |
|
| 59 |
;;; ============================ |
| 60 |
;;; CITP related command parsers |
| 61 |
;;; ============================ |
| 62 |
|
| 63 |
;;; |
| 64 |
;;; :goal { <axiom> . <axiom> . .... <axiom> . } |
| 65 |
;;; |
| 66 |
(defun citp-parse-goal (args) |
| 67 |
(let ((ax-decls nil)) |
| 68 |
(dolist (elem (third args)) |
| 69 |
(push (parse-module-element-1 elem) ax-decls)) |
| 70 |
(nreverse ax-decls))) |
| 71 |
|
| 72 |
;;; |
| 73 |
;;; :apply [to <goal-name>] (<tactic> ...) |
| 74 |
;;; |
| 75 |
;;; (":apply" ("(" ("tc" "rd" "si") ")")) |
| 76 |
;;; (":apply" ("to" ("1-1-1")) ("(" ("RD") ")")) |
| 77 |
;;; |
| 78 |
(defun citp-parse-apply (args) |
| 79 |
(let ((tactic-forms nil) |
| 80 |
(tactics nil) |
| 81 |
(target nil)) |
| 82 |
(cond ((string-equal (car (second args)) "to") |
| 83 |
(setq target (car (second (second args)))) |
| 84 |
(setq tactic-forms (second (third args)))) |
| 85 |
(t (setq tactic-forms (second (second args))))) |
| 86 |
(dolist (tac tactic-forms) |
| 87 |
(let ((tactic (get-tactic tac))) |
| 88 |
(setq tactics (nconc tactics tactic)))) |
| 89 |
(cons target tactics))) |
| 90 |
|
| 91 |
;;; citp-parse-ind-on |
| 92 |
;;; :ind on (var ... var) |
| 93 |
;;; (":ind" ("on" "(" ("A:S-1" "B:S-2" "C:S-3") ")")) |
| 94 |
;;; (":ind" ("{" ("on" #1="(" ("L1:List") #2=")") |
| 95 |
;;; ("base" #1# ("nil") "." nil #2#) |
| 96 |
;;; ("step" #1# ("X:Elm" "T:List") "." nil #2#) "}")) |
| 97 |
;;; (":ind" ("{" ("on" #1="(" ("S1:Seq") #2=")") |
| 98 |
;;; ("base" #1# ("nil") "." nil #2#) |
| 99 |
;;; ("hypo" #1# ("SQ:Seq") "." nil #2#) |
| 100 |
;;; ("step" #1# ("EL:Elt" "SQ:Seq") "." nil #2#) "}")) |
| 101 |
(defun citp-parse-seq-of-terms (module token-list) |
| 102 |
(let ((terms nil)) |
| 103 |
(when (atom token-list) |
| 104 |
(return-from citp-parse-seq-of-terms nil)) |
| 105 |
(dolist (term? token-list) |
| 106 |
;; (format t "~%<<>> ~s" term?) |
| 107 |
(when (consp term?) |
| 108 |
(let* ((*parse-variables* nil) |
| 109 |
(target-term (simple-parse module term? *cosmos*))) |
| 110 |
(when (or (null (term-sort target-term)) |
| 111 |
(sort<= (term-sort target-term) *syntax-err-sort* *chaos-sort-order*)) |
| 112 |
(with-output-chaos-error ('invalid-term) |
| 113 |
(format t "Could not parse ~s" term?))) |
| 114 |
(push target-term terms)))) |
| 115 |
(nreverse terms))) |
| 116 |
|
| 117 |
(defun citp-parse-ind-on (args) |
| 118 |
(check-context-module) |
| 119 |
(with-in-module (*current-module*) |
| 120 |
(let ((ind-type (first (second args)))) |
| 121 |
(flet ((parse-vars (decls) |
| 122 |
(let ((vars nil)) |
| 123 |
(dolist (var-decl decls) |
| 124 |
(let ((var (simple-parse-from-string var-decl))) |
| 125 |
(when (term-is-an-error var) |
| 126 |
(with-output-chaos-error ('no-parse) |
| 127 |
(format t "Illegal variable form: ~a" var-decl))) |
| 128 |
(unless (term-is-variable? var) |
| 129 |
(with-output-chaos-error ('no-var) |
| 130 |
(format t "Invalid argument to ':ind' command: ~a" var-decl))) |
| 131 |
(push var vars))) |
| 132 |
(nreverse vars)))) |
| 133 |
(if (equal ind-type "on") |
| 134 |
(let ((vars (parse-vars (third (second args))))) |
| 135 |
(cons :simple vars)) |
| 136 |
;; :ind { on () base() [hypo ()] step() } |
| 137 |
(let* ((decl (second args)) |
| 138 |
(vars (parse-vars (third (second decl)))) |
| 139 |
(bases (citp-parse-seq-of-terms *current-module* (third decl))) |
| 140 |
(hypo (citp-parse-seq-of-terms *current-module* (fourth decl))) |
| 141 |
(steps (citp-parse-seq-of-terms *current-module* (fifth decl)))) |
| 142 |
(if steps |
| 143 |
(list :user vars bases hypo steps) |
| 144 |
(list :user vars bases nil hypo)))))))) |
| 145 |
|
| 146 |
;;; |
| 147 |
;;; :auto |
| 148 |
;;; |
| 149 |
(defun citp-parse-auto (args) |
| 150 |
(declare (ignore args)) |
| 151 |
(cons :auto (get-default-tactics))) |
| 152 |
|
| 153 |
;;; |
| 154 |
;;; :roll back |
| 155 |
;;; |
| 156 |
(defun citp-parse-roll-back (args) |
| 157 |
(declare (ignore args)) |
| 158 |
:roll-back) |
| 159 |
|
| 160 |
;;; |
| 161 |
;;; :init {[<label>] | (<axiom>)} by { <var> <- <term>; ...<var> <- <term>; } |
| 162 |
;;; |
| 163 |
;;; first second third four fifth |
| 164 |
;;; (":init" ("[" ("test1") "]") "by" "{" (("X:S" "<-" ("X#S")) ";") "}")) |
| 165 |
;;; first second third fourth |
| 166 |
;;; (":init" ("as" "ts-ss-1") (#1="{" ("eq" ("ts.." "(" "SS:SeqSym" ")") "=" ("true") ".") #3="}") ("by" #1# (("SS:SeqSym" "<-" ("ss")) #2=";" ("XX:Bar" "<-" ("bb")) #2#) #3#))) |
| 167 |
;;; (":init" ("as" "ts-ss-1") (#1="(" ("eq" ("ts.." #1# "SS:SeqSym" #2=")") "=" ("true") ".") #2#) "by" ("{" ("SS:SeqSym" "<-" ("ss")) ";" "}")) |
| 168 |
;;; |
| 169 |
(defun make-axiom-pattern (target) |
| 170 |
(if (stringp target) |
| 171 |
(cons :label (list target)) |
| 172 |
(cons :axiom target))) |
| 173 |
|
| 174 |
(defun citp-parse-init (args) |
| 175 |
(let ((name (if (equal (first (second args)) "as") |
| 176 |
(second (second args)) |
| 177 |
nil))) |
| 178 |
(let ((target-form (make-axiom-pattern (if name |
| 179 |
(second (third args)) |
| 180 |
(second (second args))))) |
| 181 |
(subst-list (if name |
| 182 |
(third (fourth args)) |
| 183 |
(third (third args)))) |
| 184 |
(subst-pairs nil)) |
| 185 |
(dolist (subst-form subst-list) |
| 186 |
(unless (atom subst-form) |
| 187 |
(push (cons (first subst-form) (third subst-form)) subst-pairs))) |
| 188 |
(with-citp-debug () |
| 189 |
(format t "~%[:init] target = ~s" target-form) |
| 190 |
(format t "~% subst = ~s" subst-pairs)) |
| 191 |
(list (first args) target-form subst-pairs name)))) |
| 192 |
|
| 193 |
;;; :imply [<label>] by { <var> <- <term>; ...<var> <- <term>; } |
| 194 |
;;; |
| 195 |
;;; (":imp" ("[" ("test2") "]") ("by" "{" (("Y:S" "<-" ("X#S")) ";") "}")) |
| 196 |
;;; (":imp" ("[" ("gt") "]") (".")) |
| 197 |
;;; |
| 198 |
(defun citp-parse-imp (args) |
| 199 |
(let ((target-form (make-axiom-pattern (second args))) |
| 200 |
(subst-list (third (third args))) |
| 201 |
(subst-pairs nil)) |
| 202 |
(dolist (subst-form subst-list) |
| 203 |
(unless (atom subst-form) |
| 204 |
(push (cons (first subst-form) (third subst-form)) subst-pairs))) |
| 205 |
(list target-form subst-pairs))) |
| 206 |
|
| 207 |
;;; :cp |
| 208 |
;;; (":cp" ("[" ("label-1") "]") "><" ("[" ("label-2") "]")) |
| 209 |
;;; (":cp" ("(" ("ceq" ("LHS") "=" ("RHS") "if" ("C") ".") ")") |
| 210 |
;;; "><" ("(" ("ceq" ("LHS-2") "=" ("RHS-2") "if" ("C-2") ".") ")")) |
| 211 |
;;; |
| 212 |
(defun citp-parse-critical-pair (args) |
| 213 |
(let ((pat-1 (make-axiom-pattern (second args))) |
| 214 |
(pat-2 (make-axiom-pattern (fourth args)))) |
| 215 |
(with-citp-debug () |
| 216 |
(format t "~%[cp] ~s" pat-1) |
| 217 |
(format t "~% ~s" pat-2)) |
| 218 |
(list pat-1 pat-2))) |
| 219 |
|
| 220 |
;;; :equation/:rule |
| 221 |
;;; |
| 222 |
(defun citp-parse-equation (args) |
| 223 |
(if (member (car args) '(":equation" "equation") :test #'equal) |
| 224 |
:equation |
| 225 |
:rule)) |
| 226 |
|
| 227 |
;;; :backward equation/rule |
| 228 |
;;; |
| 229 |
(defun citp-parse-backward (args) |
| 230 |
(if (member (second args) '(":equation" "equation") :test #'equal) |
| 231 |
:equation |
| 232 |
:rule)) |
| 233 |
|
| 234 |
;;; :select <GoalName> |
| 235 |
;;; |
| 236 |
(defun citp-parse-select (args) |
| 237 |
(let ((goal-name (car (second args)))) |
| 238 |
goal-name)) |
| 239 |
|
| 240 |
;;; citp-parse-red |
| 241 |
;;; |
| 242 |
(defun citp-parse-red (e) |
| 243 |
(let (goal-name |
| 244 |
preterm |
| 245 |
mode) |
| 246 |
(case-equal (first e) |
| 247 |
((":red" ":lred" "lred") (setq mode :red)) |
| 248 |
((":exec") (setq mode :exec)) |
| 249 |
((":bred") (setq mode :bred))) |
| 250 |
(if (= 4 (length e)) |
| 251 |
(progn |
| 252 |
(setq goal-name (cadr (cadr e))) |
| 253 |
(setq preterm (nth 2 e))) |
| 254 |
(progn |
| 255 |
(setq goal-name nil) |
| 256 |
(setq preterm (nth 1 e)))) |
| 257 |
(list mode goal-name preterm))) |
| 258 |
|
| 259 |
;;; citp-parse-verbose |
| 260 |
;;; :verbose {on | off} |
| 261 |
(defun citp-parse-verbose (args) |
| 262 |
(second args)) |
| 263 |
|
| 264 |
;;; citp-parse-normalize |
| 265 |
;;; :normalize {on | off} |
| 266 |
(defun citp-parse-normalize (args) |
| 267 |
(second args)) |
| 268 |
|
| 269 |
;;; citp-parse-ctf |
| 270 |
;;; :ctf { eq <term> = <term> .} |
| 271 |
;;; (":ctf" ("{" ("eq" ("1" ">" "2") "=" ("true") ".") "}")) |
| 272 |
;;; :ctf [ <term> . ] |
| 273 |
;;; (":ctf" ("[" ("1" ">" "2") "." "]")) |
| 274 |
;;; ==> (form . (term? . :ctf-?)) |
| 275 |
;;; |
| 276 |
(defun citp-parse-ctf (args) |
| 277 |
(let ((form (second (second args))) |
| 278 |
(term? (equal (first (second args)) "["))) |
| 279 |
(if (equal (first args) ":ctf-") |
| 280 |
(cons form (cons term? :dash)) |
| 281 |
(cons form (cons term? nil))))) |
| 282 |
|
| 283 |
;;; citp-parse-csp |
| 284 |
;;; :csp { <axiom> ... } |
| 285 |
;;; |
| 286 |
(defun citp-parse-csp (args) |
| 287 |
(let ((ax-decls nil) |
| 288 |
(dash? (equal (car args) ":csp-"))) |
| 289 |
(dolist (elem (third args)) |
| 290 |
(push elem ax-decls)) |
| 291 |
(cons (nreverse ax-decls) dash?))) |
| 292 |
|
| 293 |
;;; citp-parse-define |
| 294 |
;;; :def <symbol> = <ctf> | <csp> | <init> | |
| 295 |
;;; |
| 296 |
;;; (":def" "cf1" "=" (":ctf" ("[" (<Term>) "." "]"))) |
| 297 |
;;; ==> (:ctf "cf1" nil (:term (<Term>))) |
| 298 |
;;; (":def" "cf2" "=" (":ctf-" ("{" (<Equation>) "." "}"))) |
| 299 |
;;; ==> (:ctf "cf2" t (:eq (<Equation>))) |
| 300 |
;;; (":def" "sp1" "=" (":csp" "{" ((<Equation> ".") (<Equation> ".")) "}")) |
| 301 |
;;; ==> (:csp "sp1" nil ((<Equation> ".") (<Equation> "."))) |
| 302 |
;;; (":def" "tactic-1" "=" ("(" ("si" "rd" "tc") ")")) |
| 303 |
;;; ==> (:seq "tactic-1" ("si" "rd" "tc")) |
| 304 |
;;; (":define" "*disr" "=" (":init" ("[" ("*disr") "]") "by" "{" (("X:PNat.PNAT" "<-" ("X#PNat")) #1=";" ("Y:PNat.PNAT" "<-" ("Y@PNat")) #1# ("Z:PNat.PNAT" "<-" ("Z@PNat")) #1#) "}")) |
| 305 |
;;; (":define" "ts-ss2" "="(":init" ("as" "ts-ss-1") (#1="(" ("eq" ("ts.." #1# "SS:SeqSym" #2=")") "=" ("true") ".") #2#) "by" ("{" ("SS:SeqSym" "<-" ("ss")) ";" "}"))) |
| 306 |
;;; ==> (:init "*disr" nil (":init" ("[" ("*disr") "]") "by" "{" (("X:PNat.PNAT" "<-" #) #1=";" ("Y:PNat.PNAT" "<-" #) #1# ("Z:PNat.PNAT" "<-" #) #1#) "}")) |
| 307 |
|
| 308 |
(defun citp-parse-define (args) |
| 309 |
(flet ((name-to-com (name) |
| 310 |
(cond ((equal name "(") |
| 311 |
:seq) |
| 312 |
((equal (subseq name 0 4) ":ctf") |
| 313 |
:ctf) |
| 314 |
((equal (subseq name 0 4) ":csp") |
| 315 |
:csp) |
| 316 |
((equal (subseq name 0 4) ":ini") |
| 317 |
:init) |
| 318 |
(t (with-output-chaos-error ('invalid-def) |
| 319 |
(format t "Internal error, :def accepted ~a" name)))))) |
| 320 |
(let* ((name (second args)) |
| 321 |
(com-name (first (fourth args))) |
| 322 |
(command (name-to-com com-name)) |
| 323 |
(dash-or-bang (or (and (member command '(:ctf :csp)) |
| 324 |
(> (length com-name) 4)) |
| 325 |
(and (eq command :init) |
| 326 |
(> (length com-name) 5)))) |
| 327 |
(body-form (case command |
| 328 |
(:ctf (if (equal "[" (first (second (fourth args)))) |
| 329 |
(list :term (second (second (fourth args)))) |
| 330 |
(list :eq (second (second (fourth args)))))) |
| 331 |
(:csp (third (fourth args))) |
| 332 |
(:init (citp-parse-init (fourth args))) |
| 333 |
(:seq (second (fourth args))) |
| 334 |
(otherwise (with-output-chaos-error ('invalid-command) |
| 335 |
(format t "Internal error, :def invalid ~s" command)))))) |
| 336 |
(list command name dash-or-bang body-form)))) |
| 337 |
|
| 338 |
;;; { :show | :describe } <something> |
| 339 |
;;; |
| 340 |
(defun citp-parse-show (inp) |
| 341 |
(let ((tag (car inp)) |
| 342 |
(args (cdr inp)) |
| 343 |
(com nil)) |
| 344 |
(cond ((member tag '(":show" ":sh") :test #'equal) |
| 345 |
(setq com :show)) |
| 346 |
((member tag '(":describe" ":desc") :test #'equal) |
| 347 |
(setq com :describe)) |
| 348 |
(t (with-output-chaos-error ('internal) |
| 349 |
(format t "Internal error, unknown citp command ~s" tag)))) |
| 350 |
(cons com args))) |
| 351 |
|
| 352 |
;;; :spoiler { on | off } |
| 353 |
;;; |
| 354 |
(defun citp-parse-spoiler (form) |
| 355 |
(let* ((on-off (second form)) |
| 356 |
(value (if (equal on-off '("on")) |
| 357 |
t |
| 358 |
(if (equal on-off '("off")) |
| 359 |
nil |
| 360 |
(progn (format t "~&:spoiler flag is ~s" (if *citp-spoiler* "on" "off")) |
| 361 |
(return-from citp-parse-spoiler t)))))) |
| 362 |
(setq *citp-spoiler* value) |
| 363 |
(setf (citp-flag citp-spoiler) value) |
| 364 |
t)) |
| 365 |
|
| 366 |
;;; {:binspect | binspect} [in {<goal-name> | <module-name>} : ] <boolean-term> . |
| 367 |
;;; |
| 368 |
(defun parse-citp-binspect (args) |
| 369 |
(parse-term-in-context args)) |
| 370 |
|
| 371 |
(defun parse-term-in-context (args) |
| 372 |
(let (mode |
| 373 |
goal-name |
| 374 |
preterm |
| 375 |
(type (first args))) |
| 376 |
(if (and (stringp type) |
| 377 |
(eql (elt type 0) #\:)) |
| 378 |
(setq mode :citp) |
| 379 |
(setq mode :general)) |
| 380 |
(if (= 4 (length args)) |
| 381 |
(progn |
| 382 |
(setq goal-name (cadr (cadr args))) |
| 383 |
(setq preterm (nth 2 args))) |
| 384 |
(progn |
| 385 |
(setq goal-name nil) |
| 386 |
(setq preterm (nth 1 args)))) |
| 387 |
(list mode goal-name preterm))) |
| 388 |
|
| 389 |
;;; {:bgrind | bgrind} [in {<goal-name> | <module-name>} : ] <boolean-term> . |
| 390 |
;;; |
| 391 |
(defun parse-citp-bgrind (args) |
| 392 |
(parse-term-in-context args)) |
| 393 |
|
| 394 |
;;; {bshow | :bshow} [ { tree | grind } ] |
| 395 |
;;; |
| 396 |
(defun citp-parse-bshow (args) |
| 397 |
(let ((param (cadr args))) |
| 398 |
(or param "."))) |
| 399 |
|
| 400 |
;;; :set(<name>, {on | off | show | ? }) |
| 401 |
;;; |
| 402 |
(defun citp-parse-set (inp) |
| 403 |
(declare (type list inp)) |
| 404 |
(let ((name (third inp)) |
| 405 |
(value (fifth inp))) |
| 406 |
(list name value))) |
| 407 |
|
| 408 |
;;; {bstart | :bstart} <variable-name> |
| 409 |
;;; |
| 410 |
(defun citp-parse-bstart (arg) |
| 411 |
(cadr arg)) |
| 412 |
|
| 413 |
;;; {bguess | :bguess} <strategy> &optional args |
| 414 |
;;; 'bguess <strategy>' |
| 415 |
;;; <strategy> ::= { implies | and | or } |
| 416 |
;;; p implies q = true (p and q = p) |
| 417 |
;;; p and q = false |
| 418 |
;;; p or q = true |
| 419 |
(defun citp-parse-bguess (args) |
| 420 |
(cdr args)) |
| 421 |
|
| 422 |
;;; citp-parse-use |
| 423 |
;;; :use (<label> ... <label>) |
| 424 |
;;; :use [<label>] { assoc | comm | id: <term> } |
| 425 |
;;; (":use" ("(" ("a" "b" "c") ")")) |
| 426 |
;;; (":use" ("[" "foo" "]" "{" (("assoc")) "}")) |
| 427 |
(defun citp-parse-use (args) |
| 428 |
(let* ((body (second args)) |
| 429 |
(type (first body))) |
| 430 |
(cond ((equal type "(") |
| 431 |
(cons :use (second body))) |
| 432 |
((equal type "[") |
| 433 |
(let ((label (second body)) |
| 434 |
(theory (fifth body))) |
| 435 |
(list* :use-theory label theory))) |
| 436 |
(t (with-output-chaos-error ('invalid-form) |
| 437 |
(format t "invalid form of :use ~s" type)))))) |
| 438 |
|
| 439 |
;;; citp-parse-embed |
| 440 |
;;; :embed (<label> ... <label>) as <module_name> |
| 441 |
;;; (":embed" ("(" ("a" "b" "c") ")" "as" "foo")) |
| 442 |
;;; (":embed" ("[" "foo" "]" "{" (("assoc") ("comm")) "}" "as" "foo")) |
| 443 |
(defun citp-parse-embed (args) |
| 444 |
(let* ((body (second args)) |
| 445 |
(type (first body))) |
| 446 |
(cond ((equal type "(") |
| 447 |
(let ((labels (second body)) |
| 448 |
(mod-name (fifth body)) |
| 449 |
(into (equal (fourth body) "into"))) |
| 450 |
(list :embed labels mod-name into))) |
| 451 |
((equal type "[") |
| 452 |
(let ((label (second body)) |
| 453 |
(theory (fifth body)) |
| 454 |
(into (equal (seventh body) "into")) |
| 455 |
(mod-name (eighth body))) |
| 456 |
(list :embed-theory label theory mod-name into))) |
| 457 |
(t (with-output-chaos-error ('invalid-form) |
| 458 |
(format t "invalid :embed type ~s" type)))))) |
| 459 |
|
| 460 |
;;; citp-parse-reset |
| 461 |
;;; :reset |
| 462 |
;;; |
| 463 |
(defun citp-parse-reset (args) |
| 464 |
args) |
| 465 |
|
| 466 |
;;; citp-process-opattr-declaration-form |
| 467 |
;;; |
| 468 |
(defun citp-process-opattr-declaration-form (args &rest ignore) |
| 469 |
(declare (ignore ignore)) |
| 470 |
(format t "~s" args)) |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
;;; ================================ |
| 476 |
;;; CITP related command evaluators |
| 477 |
;;; ================================ |
| 478 |
|
| 479 |
;;; :goal |
| 480 |
;;; |
| 481 |
(defun eval-citp-goal (goal-ax-decls) |
| 482 |
(check-context-module) |
| 483 |
(with-in-module (*current-module*) |
| 484 |
(let ((axs nil)) |
| 485 |
(dolist (a-decl goal-ax-decls) |
| 486 |
(cond ((eq (car a-decl) '%fax) |
| 487 |
(push (parse-fax-declaration a-decl) axs)) |
| 488 |
(t (push (parse-axiom-declaration a-decl) axs)))) |
| 489 |
(begin-proof *current-module* (nreverse axs))))) |
| 490 |
|
| 491 |
;;; :apply/:auto |
| 492 |
(defun eval-citp-apply (list-tactic) |
| 493 |
(check-ptree) |
| 494 |
(let ((target (car list-tactic)) |
| 495 |
(tactics (cdr list-tactic))) |
| 496 |
(let ((*chaos-verbose* nil) |
| 497 |
(*chaos-quiet* t)) |
| 498 |
(if target |
| 499 |
(case target |
| 500 |
(:auto (apply-auto *proof-tree*)) |
| 501 |
(otherwise |
| 502 |
(apply-tactics-to-goal *proof-tree* target tactics))) |
| 503 |
(apply-tactics *proof-tree* tactics))))) |
| 504 |
|
| 505 |
;;; :ind on |
| 506 |
;;; |
| 507 |
(defun eval-citp-ind-on (args) |
| 508 |
(check-ptree) |
| 509 |
(with-in-module ((get-context-module)) |
| 510 |
(let ((type (car args)) |
| 511 |
(ind-form (cdr args))) |
| 512 |
(if (eq type :simple) |
| 513 |
(set-induction-variables ind-form) |
| 514 |
;; :user defined induction scheme |
| 515 |
(let ((vars (first ind-form)) |
| 516 |
(bases (second ind-form)) |
| 517 |
(hypo (third ind-form)) |
| 518 |
(steps (fourth ind-form))) |
| 519 |
(check-on-going) |
| 520 |
(let ((target-node (get-next-proof-context *proof-tree*))) |
| 521 |
(set-induction-variables-and-scheme target-node vars bases hypo steps) |
| 522 |
;; then do the job |
| 523 |
(apply-user-defined-induction-scheme target-node))))))) |
| 524 |
|
| 525 |
;;; :roll back |
| 526 |
;;; |
| 527 |
(defun eval-citp-roll-back (&rest com) |
| 528 |
(declare (ignore com)) |
| 529 |
(check-ptree) |
| 530 |
(with-in-module (*current-module*) |
| 531 |
(roll-back *proof-tree*))) |
| 532 |
|
| 533 |
;;; :init |
| 534 |
;;; |
| 535 |
(defun eval-citp-init (args) |
| 536 |
(let ((target-goal (ptree-node-goal (get-target-goal-node)))) |
| 537 |
(instanciate-axiom target-goal |
| 538 |
(second args) ; target |
| 539 |
(third args) ; variable-term pairs |
| 540 |
(fourth args) ; label |
| 541 |
))) |
| 542 |
|
| 543 |
;;; :imp |
| 544 |
;;; |
| 545 |
(defun eval-citp-imp (args) |
| 546 |
(check-ptree) |
| 547 |
(with-in-module ((goal-context (ptree-node-goal (get-target-goal-node)))) |
| 548 |
(introduce-implication-to-goal (first args) ; target |
| 549 |
(second args)))) ;variable-term pairs |
| 550 |
|
| 551 |
;;; :cp |
| 552 |
(defun eval-citp-critical-pair (args) |
| 553 |
(check-ptree) |
| 554 |
(with-in-module (*current-module*) |
| 555 |
(apply-cp (first args) (second args)))) |
| 556 |
|
| 557 |
;;; :equation : {:equation| :rule} -> void |
| 558 |
(defun eval-citp-equation (arg) |
| 559 |
(check-ptree) |
| 560 |
(add-critical-pairs arg :forward)) |
| 561 |
|
| 562 |
;;; :backward |
| 563 |
(defun eval-citp-backward (arg) |
| 564 |
(check-ptree) |
| 565 |
(add-critical-pairs arg :backward)) |
| 566 |
|
| 567 |
;;; :select |
| 568 |
(defun eval-citp-select (goal-name) |
| 569 |
(check-ptree) |
| 570 |
(select-next-goal goal-name)) |
| 571 |
|
| 572 |
;;; :red, :exec, :bred |
| 573 |
(defun eval-citp-red (token-seq) |
| 574 |
(check-ptree) |
| 575 |
(let ((mode (first token-seq)) |
| 576 |
(goal-name (second token-seq)) |
| 577 |
(pre-term (third token-seq))) |
| 578 |
(reduce-in-goal mode goal-name pre-term))) |
| 579 |
|
| 580 |
;;; :verbose |
| 581 |
(defun eval-citp-verbose (token) |
| 582 |
(if (string-equal token "on") |
| 583 |
(setq *citp-verbose* t) |
| 584 |
(if (string-equal token "off") |
| 585 |
(setq *citp-verbose* nil) |
| 586 |
(if (string-equal token ".") |
| 587 |
(format t "~&:verbose flag is ~s" (if *citp-verbose* "on" "off")) |
| 588 |
(with-output-chaos-error ('invlid-value) |
| 589 |
(format t "Unknown parameter ~s." token)))))) |
| 590 |
|
| 591 |
;;; :normalize init |
| 592 |
(defun eval-citp-normalize (token) |
| 593 |
(if (string-equal token "on") |
| 594 |
(setq *citp-normalize-instance* t) |
| 595 |
(if (string-equal token "off") |
| 596 |
(setq *citp-normalize-instance* nil) |
| 597 |
(if (string-equal token ".") |
| 598 |
(format t "~&:normalize flag is ~s" (if *citp-normalize-instance* "on" "off")) |
| 599 |
(with-output-chaos-error ('invalid-value) |
| 600 |
(format t ":nomalize instance: unknown parameter ~s." token)))))) |
| 601 |
|
| 602 |
;;; :ctf |
| 603 |
;;; ax-form ::= (form . (term? . :ctf-?)) |
| 604 |
;;; |
| 605 |
(defun eval-citp-ctf (ax-form) |
| 606 |
(check-ptree) |
| 607 |
(with-in-module ((get-context-module)) |
| 608 |
(reset-rewrite-counters) |
| 609 |
(begin-rewrite) |
| 610 |
(apply-ctf (car ax-form) (cadr ax-form) (cddr ax-form)) |
| 611 |
(end-rewrite) |
| 612 |
(report-citp-stat) |
| 613 |
(check-success *proof-tree*))) |
| 614 |
|
| 615 |
;;; :csp |
| 616 |
(defun eval-citp-csp (goal-ax-decls) |
| 617 |
(check-ptree) |
| 618 |
(with-in-module ((get-context-module)) |
| 619 |
(reset-rewrite-counters) |
| 620 |
(begin-rewrite) |
| 621 |
(apply-csp (car goal-ax-decls) (cdr goal-ax-decls)) |
| 622 |
(end-rewrite) |
| 623 |
(report-citp-stat) |
| 624 |
(check-success *proof-tree*))) |
| 625 |
|
| 626 |
;;; :show, :describe |
| 627 |
(defun eval-citp-show (token) |
| 628 |
(let* ((com (car token)) |
| 629 |
(describe (eq com :describe)) |
| 630 |
(target (cadr token)) |
| 631 |
(rest-args (cddr token))) |
| 632 |
(cond ((member target '("unproved" "unp") :test #'equal) |
| 633 |
(check-ptree) |
| 634 |
(print-unproved-goals *proof-tree*)) |
| 635 |
((member target '("proved" "discharged") :test #'equal) |
| 636 |
(check-ptree) |
| 637 |
(print-discharged-sentences)) |
| 638 |
((equal target "goal") |
| 639 |
(check-ptree) |
| 640 |
(let ((name (car rest-args))) |
| 641 |
(print-named-goal *proof-tree* name))) |
| 642 |
((equal target "proof") |
| 643 |
(let ((name (car rest-args))) |
| 644 |
(when (or (null name) (equal name ".")) |
| 645 |
(setq name "root")) |
| 646 |
(print-proof-tree name describe))) |
| 647 |
((member target '("." "current") :test #'equal) |
| 648 |
(check-ptree) |
| 649 |
(print-current-goal describe)) |
| 650 |
((member target '(":def" ":define" "def" "define" ":defs" "defs") :test #'equal) |
| 651 |
(check-ptree) |
| 652 |
(let ((goal-name (first rest-args))) |
| 653 |
(print-defs describe goal-name))) |
| 654 |
((member target '("switch" "switches" "flag" "flags") :test #'equal) |
| 655 |
(print-citp-flag citp-all)) |
| 656 |
(t (with-output-chaos-error ('unknown) |
| 657 |
(format t "Unknown parameter to :show/:describe ~S" target)))))) |
| 658 |
|
| 659 |
;;; { :binspect | binspect } |
| 660 |
;;; |
| 661 |
(defun eval-citp-binspect (args) |
| 662 |
(let ((mode (first args)) |
| 663 |
(goal-or-mod (second args)) |
| 664 |
(preterm (third args))) |
| 665 |
(binspect-in mode goal-or-mod preterm))) |
| 666 |
|
| 667 |
|
| 668 |
;;; { :bgrind | bgrind } |
| 669 |
;;; |
| 670 |
(defun eval-citp-bgrind (args) |
| 671 |
(bgrind-in (first args) ; mode |
| 672 |
(second args) ; goal or module |
| 673 |
(third args) ; term tokens |
| 674 |
)) |
| 675 |
|
| 676 |
;;; eval-citp-define : arg -> tactic-ctf or tactic-csp |
| 677 |
;;; (:ctf "cf1" nil (:term (<Term>))) |
| 678 |
;;; (:ctf "cf2" t (:eq (<Equation>))) |
| 679 |
;;; (:csp "sp1" nil ((<Equation> ".") ...)) |
| 680 |
;;; (:csp "sp2" t ((<Equation> ".") ...)) |
| 681 |
;;; (:init "ini1" nil (:init ....)) |
| 682 |
;;; (:seq "tactic-1" nil (<tactic-name> ....)) |
| 683 |
;;; |
| 684 |
(defun eval-citp-define (arg) |
| 685 |
(check-ptree) |
| 686 |
(let ((tactic-name (first arg)) |
| 687 |
(name (second arg)) |
| 688 |
(dash (third arg)) |
| 689 |
(form (fourth arg)) |
| 690 |
(tactic nil)) |
| 691 |
(when (tactic-name-is-builtin? name) |
| 692 |
(with-output-chaos-error ('invalid-name) |
| 693 |
(format t "You can not use the name of builtin tactic ~a." name))) |
| 694 |
(when (existing-def-name? *proof-tree* name) |
| 695 |
(with-output-chaos-warning () |
| 696 |
(format t "The name ~a is already defined in the current proof context." name) |
| 697 |
(format t "~%Please use the different name.") |
| 698 |
(return-from eval-citp-define nil))) |
| 699 |
(let ((current (get-next-proof-context *proof-tree*)) |
| 700 |
(goal nil)) |
| 701 |
(unless current |
| 702 |
(with-output-chaos-error ('no-context) |
| 703 |
(format t "No target goal."))) |
| 704 |
(setq goal (ptree-node-goal current)) |
| 705 |
(with-in-module ((goal-context goal)) |
| 706 |
(let ((*chaos-quiet* t)) |
| 707 |
(cond ((eq tactic-name :ctf) |
| 708 |
;; ctf |
| 709 |
(setq tactic (make-tactic-ctf :name name |
| 710 |
:form (parse-axiom-or-term (second form) |
| 711 |
(eq :term (first form))) |
| 712 |
:context *current-module* |
| 713 |
:minus dash))) |
| 714 |
((eq tactic-name :csp) |
| 715 |
;; csp |
| 716 |
(let ((forms nil)) |
| 717 |
(dolist (ax form) |
| 718 |
(push (parse-axiom-declaration (parse-module-element-1 ax)) forms)) |
| 719 |
(setq tactic (make-tactic-csp :name name |
| 720 |
:forms (nreverse forms) |
| 721 |
:minus dash |
| 722 |
:context *current-module*)))) |
| 723 |
((eq tactic-name :init) |
| 724 |
(let ((ax (get-target-axiom *current-module* (second form)))) |
| 725 |
(setq tactic (make-tactic-init :name name |
| 726 |
:axiom ax |
| 727 |
:subst (third form) |
| 728 |
:context *current-module* |
| 729 |
:kind (car (last form)))))) |
| 730 |
((eq tactic-name :ind) |
| 731 |
;; ind |
| 732 |
(setq tactic (make-tactic-ind :name name |
| 733 |
:vars () |
| 734 |
:base () |
| 735 |
:step ()))) |
| 736 |
((eq tactic-name :seq) |
| 737 |
(setq tactic (make-tactic-seq :name name |
| 738 |
:tactics (mapcar #'(lambda (x) |
| 739 |
(or (get-defined-tactic goal x) |
| 740 |
(get-builtin-tactic x) |
| 741 |
(with-output-chaos-error ('no-such-tactic) |
| 742 |
(format t "No such tactic ~a" x)))) |
| 743 |
form)))) |
| 744 |
(t ;; internal error |
| 745 |
(with-output-chaos-error ('internal-error) |
| 746 |
(format t "Invalid :def parameter ~s" tactic-name)))) |
| 747 |
(format t "~&~a defined as " name) |
| 748 |
(princ tactic) |
| 749 |
(setf (goal-defs goal) |
| 750 |
(nconc (goal-defs goal) (list tactic))))) |
| 751 |
(push (canonicalize-tactic-name name) (ptree-defs-so-far *proof-tree*))))) |
| 752 |
|
| 753 |
;;; |
| 754 |
;;; SET-FLAG/CLEAR-FLAG |
| 755 |
;;; |
| 756 |
(defun citp-eval-set (args) |
| 757 |
(let ((name (first args)) |
| 758 |
(given-value (second args))) |
| 759 |
(let ((value nil) |
| 760 |
(index (find-citp-flag-index name))) |
| 761 |
(unless index |
| 762 |
(with-output-chaos-error ('no-such-flag) |
| 763 |
(format t "No such flag ~a" name))) |
| 764 |
(cond ((or (equal given-value "on") |
| 765 |
(equal given-value "set")) |
| 766 |
(setq value t)) |
| 767 |
((equal given-value "show") |
| 768 |
(print-citp-flag index) |
| 769 |
(return-from citp-eval-set nil)) |
| 770 |
((equal given-value "?") |
| 771 |
(help-citp-flag index) |
| 772 |
(return-from citp-eval-set nil))) |
| 773 |
(when (citp-flag citp-print-message) |
| 774 |
(with-output-msg () |
| 775 |
(format t "setting flag ~a to ~a" name given-value))) |
| 776 |
(if (= citp-all index) |
| 777 |
(dotimes (x *citp-max-flags*) |
| 778 |
(setf (citp-flag x) value)) |
| 779 |
(setf (citp-flag index) value)) |
| 780 |
;; run hook |
| 781 |
(funcall (citp-flag-hook index) name value)))) |
| 782 |
|
| 783 |
;;; |
| 784 |
;;; :use |
| 785 |
;;; |
| 786 |
(defun citp-eval-use (args) |
| 787 |
(check-ptree) |
| 788 |
(let ((type (car args)) |
| 789 |
(body (cdr args))) |
| 790 |
(case type |
| 791 |
(:use |
| 792 |
(use-discharged-goals body)) |
| 793 |
(:use-theory |
| 794 |
(let ((label (car body)) |
| 795 |
(theory (cdr body))) |
| 796 |
(use-theory label theory)))))) |
| 797 |
|
| 798 |
;;; |
| 799 |
;;; :embed |
| 800 |
;;; |
| 801 |
(defun citp-eval-embed (args) |
| 802 |
(check-ptree) |
| 803 |
(let ((type (car args)) |
| 804 |
(body (cdr args))) |
| 805 |
(case type |
| 806 |
(:embed |
| 807 |
(let ((labels (car body)) |
| 808 |
(mod-name (cadr body)) |
| 809 |
(into (caddr body))) |
| 810 |
(embed-discharged-goals labels mod-name into))) |
| 811 |
(:embed-theory |
| 812 |
(let ((label (first body)) |
| 813 |
(theory (second body)) |
| 814 |
(mod-name (third body)) |
| 815 |
(into (fourth body))) |
| 816 |
(embed-theory label theory mod-name into)))))) |
| 817 |
|
| 818 |
;;; |
| 819 |
;;; :reset |
| 820 |
;;; |
| 821 |
(defun citp-eval-reset (&rest args) |
| 822 |
(declare (ignore args)) |
| 823 |
(check-ptree) |
| 824 |
(reset-proof-session)) |
| 825 |
|
| 826 |
;;; |
| 827 |
;;; :theory <opname> : <arity> -> <coarity> { assoc | comm | id: <term> } |
| 828 |
;;; |
| 829 |
(defun add-method-theory (decl) |
| 830 |
(check-ptree) |
| 831 |
(let ((theory (%opattrs-theory (%theory-decl-attribute decl))) |
| 832 |
(name (%theory-decl-name decl)) |
| 833 |
(arity (%theory-decl-arity decl)) |
| 834 |
(coarity (%theory-decl-coarity decl)) |
| 835 |
(goal (ptree-node-goal (get-next-proof-context *proof-tree*)))) |
| 836 |
(with-in-module ((goal-context goal)) |
| 837 |
(let ((r-arity (mapcar #'(lambda (x) (or (find-sort-in *current-module* x) |
| 838 |
(with-output-chaos-error ('no-sort) |
| 839 |
(princ "No such sort ") |
| 840 |
(print-sort-ref x)))) |
| 841 |
arity)) |
| 842 |
(r-coarity (or (find-sort-in *current-module* coarity) |
| 843 |
(with-output-chaos-error ('no-sort) |
| 844 |
(princ "No such sort ") |
| 845 |
(print-sort-ref coarity))))) |
| 846 |
(let ((meth (find-method-in *current-module* name r-arity r-coarity))) |
| 847 |
(unless meth |
| 848 |
(with-output-chaos-error ('no-op) |
| 849 |
(format t "No such operator ~s" name))) |
| 850 |
(add-and-merge-method-theory meth theory *current-module*) |
| 851 |
(set-needs-parse) |
| 852 |
(set-needs-rule)))))) |
| 853 |
|
| 854 |
;;; EOF |