Codebase list cafeobj / 1d43790
* Changed proof strategy for more easily walk through the proof tree. * Exprimental '=>' introduction. tswd 9 years ago
12 changed file(s) with 594 addition(s) and 414 deletion(s). Raw diff Collapse all Expand all
300300 (and *bool-equal* (push *bool-equal* *bi-universal-operators*))
301301 (and *bool-nonequal* (push *bool-nonequal* *bi-universal-operators*))
302302 (and *beh-eq-pred* (push *beh-eq-pred* *bi-universal-operators*))
303 (and *rwl-predicate* (push *rwl-predicate* *bi-universal-operators*))
304 (and *rwl-predicate2* (push *rwl-predicate2* *bi-universal-operators*))
303 ;; (and *rwl-predicate* (push *rwl-predicate* *bi-universal-operators*))
304 ;; (and *rwl-predicate2* (push *rwl-predicate2* *bi-universal-operators*))
305305 )
306306
307307 ;;;
99 (declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
1010 #+:chaos-debug
1111 (declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))
12
13 ;;; NOTE: these stuff are obsolate =============================================
14 ;;; not used now.
1215
1316 ;;; == DESCRIPTION =============================================================
1417 ;;; stuff supporting proof in rewriting logic.
426426 op)
427427 (trs$op-rev-table trs))))
428428 ;; other optional built-ins.
429 #|| TODO:
429430 (when (module-includes-rwl module)
430431 (push (cons (make-trs-op-name *rwl-predicate* module)
431432 *rwl-predicate*)
433434 (push (cons (make-trs-op-name *rwl-predicate2* module)
434435 *rwl-predicate2*)
435436 (trs$op-rev-table trs)))
437 ||#
436438 ;;
437439 (setf (trs$op-info-map trs)
438440 (nreverse res)))))
685687 (setq rel-infos infos)
686688 (setq if-then-axs axs))
687689 )
688 (when (or (eq *rwl-module* mod)
689 (assq *rwl-module* (module-all-submodules mod)))
690 (when nil ; (or (eq *rwl-module* mod)
691 ; (assq *rwl-module* (module-all-submodules mod)))
690692 ;; _==>_
691693 (dolist (sort top-sorts)
692694 (unless (sort-is-hidden sort)
855855 (sort= (method-coarity x)
856856 (method-coarity y))))))
857857 (method-info-table (module-opinfo-table module)))
858 (if (or (eq pmeth method)
859 ;; dirty kludge!
860 (and pmeth (method-is-of-same-operator-safe method *rwl-predicate*)))
858 (if nil ; (or (eq pmeth method)
859 ; ;; dirty kludge!
860 ; (and pmeth (method-is-of-same-operator-safe method *rwl-predicate*)))
861861 nil
862 (progn
863 (setf (get-method-info method method-info-table)
864 (make-method-info method
865 module
866 (opinfo-operator opinfo)))
867 (pushnew method (opinfo-methods opinfo))
868 (setf (opinfo-method-table opinfo) nil)
869 (when (method-is-behavioural method)
870 (if (sort-is-hidden (method-coarity method))
871 (pushnew method (module-beh-methods module))
872 (pushnew method (module-beh-attributes module))))
873 t))))
862 (progn
863 (setf (get-method-info method method-info-table)
864 (make-method-info method
865 module
866 (opinfo-operator opinfo)))
867 (pushnew method (opinfo-methods opinfo))
868 (setf (opinfo-method-table opinfo) nil)
869 (when (method-is-behavioural method)
870 (if (sort-is-hidden (method-coarity method))
871 (pushnew method (module-beh-methods module))
872 (pushnew method (module-beh-attributes module))))
873 t))))
874874
875875 (defun transfer-operator-attributes (method to-module from-module
876876 &optional theory-mod)
12151215 ("goal" (let ((name (cadr dat)))
12161216 (print-named-goal *proof-tree* name)))
12171217 ("proof" (let ((target (second dat)))
1218 (if (and target (equal target "tree"))
1219 (print-proof-tree describe)
1220 (if target
1221 (with-output-chaos-error ('unknown-option)
1222 (format t "'show proof' unknown option ~s." target))
1223 (print-proof-tree describe)))))
1218 (when (or (null target) (equal target "."))
1219 (setq target "root"))
1220 (print-proof-tree target describe)))
1221
12241222 ;;
12251223 ;; helpers
12261224 ;;
16451645 ;; CONDITION
16461646 (when (or cnd
16471647 (and *chaos-verbose* (axiom-id-condition rul)))
1648 (print-check .file-col.)
1648 ;; (print-check .file-col.)
1649 (print-next)
1650 (princ " ")
16491651 (if meta
16501652 (princ " :if ")
16511653 (princ " if "))
19181918 (parser-in-same-connected-component (second sort-list)
19191919 (third sort-list)
19201920 so))
1921 #||
19211922 ((eq method *rwl-predicate2*)
19221923 ;; _=()=>_
19231924 (parser-in-same-connected-component (first sort-list)
19241925 (third sort-list)
19251926 so))
1927 ||#
19261928 ((eq method *sort-membership*)
19271929 ;; _:is_, the first argument is a term and the second
19281930 ;; argument is built-in constant of SortId.
6868 ** NOTE: these two predicates are almost obsolate.
6969 -- kept for backward compatibility.
7070 -- _=(N:NzNat*)=>_ is equivalent to _=(1,N)=>*_
71 pred _=(_)=>_ : *Cosmos* NzNat* *Cosmos* { strat: (0) prec: 51 }
72 pred _=(_)=>_ suchThat _ : *Cosmos* NzNat* *Cosmos* Bool { strat: (0) prec: 51 }
71 -- pred _=(_)=>_ : *Cosmos* NzNat* *Cosmos* { strat: (0) prec: 51 }
72 -- pred _=(_)=>_ suchThat _ : *Cosmos* NzNat* *Cosmos* Bool { strat: (0) prec: 51 }
7373
7474 ** new search operators
7575 pred _=(_,_)=>*_ : *Cosmos* NzNat* NzNat* *Cosmos* { strat: (0) prec: 51 }
182182
183183 ** older version of `search', for backward compatibiliy
184184 **
185 eq (CXU =( N:NzNat* )=> CYU)
186 = #!! (rwl-sch-set-result (term-pattern-included-in-cexec cxu cyu n)) .
187 eq (CXU =( N:NzNat* )=> CYU suchThat COND)
188 = #!! (rwl-sch-set-result (term-pattern-included-in-cexec cxu cyu n cond)) .
185 ** eq (CXU =( N:NzNat* )=> CYU)
186 ** = #!! (rwl-sch-set-result (term-pattern-included-in-cexec cxu cyu n)) .
187 ** eq (CXU =( N:NzNat* )=> CYU suchThat COND)
188 ** = #!! (rwl-sch-set-result (term-pattern-included-in-cexec cxu cyu n cond)) .
189189 **
190190 **
191191 **
7373 pred _=b=_ : *Cosmos* *Cosmos* { prec: 51 }
7474 pred _=/=_ : *Cosmos* *Cosmos* { prec: 51 }
7575 pred _:=_ : *Cosmos* *Cosmos* { prec: 51 }
76 op _/\_ : *Condition* *Condition* -> *Condition* { strat: (1 0 2) prec: 65 r-assoc }
76 op _/\_ : *Condition* *Condition* -> *Condition* { strat: (1 2 0) prec: 67 r-assoc }
7777 }
7878 axioms {
7979 var XU : *Universal*
3030
3131 )
3232
33 ;;; ==============
34 ;;; some utilities
35 ;;; ==============
36
37 ;;;
33 ;;; *****************************************************************************
34 ;;; UTILITIES
35 ;;; ****************************************************************************
36
37 ;;; distribute-sentences : ptree-node List(axiom) -> List(goal)
38 ;;; if there are multiple sentences, distribute them into newly genereted goals for each
39 ;;;
40 (defun distribute-sentences (parent axioms tactic)
41 (declare (type ptree-node parent))
42 (let ((new-goals nil)
43 (goal nil))
44 (cond ((cdr axioms)
45 (dolist (ax axioms)
46 (setq goal (prepare-next-goal parent tactic))
47 (setf (goal-targets goal) (list ax))
48 (push goal new-goals)))
49 (t (push (ptree-node-goal parent) new-goals)))
50 (nreverse new-goals)))
51
52 #||
53 (defun distribute-sentences (parent axioms tactic)
54 (declare (type ptree-node parent))
55 (let ((new-goals nil)
56 (goal nil))
57 (dolist (ax axioms)
58 (setq goal (prepare-next-goal parent tactic))
59 (setf (goal-targets goal) (list ax))
60 (push goal new-goals))
61 (nreverse new-goals)))
62 ||#
63
3864 ;;; rule-copy-cononicalized : rule module -> rule
3965 ;;; copy rule with all variables are renewed and noralized.
4066 ;;;
4874 (rule-rhs new-rule) (second canon)
4975 (rule-condition new-rule) (third canon))
5076 new-rule))
77
78 ;;;
79 ;;; apply-substitution-to-axiom : subst axiom module -> axiom'
80 ;;;
81 (defun apply-substitution-to-axiom (subst axiom &optional label)
82 (setf (rule-lhs axiom) (substitution-image-simplifying subst (rule-lhs axiom))
83 (rule-rhs axiom) (substitution-image-simplifying subst (rule-rhs axiom))
84 (rule-condition axiom) (if (is-true? (rule-condition axiom))
85 *bool-true*
86 (substitution-image-simplifying subst (rule-condition axiom))))
87 (when label
88 (setf (rule-labels axiom) label))
89 axiom)
5190
5291 ;;; intro-const-returns-subst : module name variable -> (variable . constant-term)
5392 ;;; introduces a new constant of sort(variable) into a module.
180219 (setq result (append (term-variables lhs)
181220 (append (term-variables rhs)
182221 (term-variables cond))))
222 #||
183223 (with-citp-debug ()
184224 (dolist (v result)
185225 (print-next) (term-print-with-sort v)))
226 ||#
186227 (delete-duplicates result :test #'variable-equal)))
187228
188229 ;;;
202243
203244 ;;; normalize-term-in : module term -> term applied
204245 ;;;
205 (defun normalize-term-in (module term)
206 (with-in-module (module)
207 (setq $$matches 0)
208 (let ((*perform-on-demand-reduction* t)
209 (*rule-count* 0))
210 (unless (term-variables term)
211 ;; we only targets ground term
212 (rewrite term *current-module* :red))
213 (values term (not (= 0 *rule-count*))))))
246 (defun normalize-term-in (module term &optional (reduction-mode :red))
247 (let ((gterms (get-gterms term))
248 (applied? nil))
249 (if gterms
250 (with-in-module (module)
251 (dolist (gt gterms)
252 (setq $$matches 0)
253 (let ((*perform-on-demand-reduction* t)
254 (*rule-count* 0))
255 (rewrite gt *current-module* reduction-mode)
256 (unless (= 0 *rule-count*)
257 (setq applied? t))))
258 (values term applied?))
259 (values term nil))))
214260
215261 ;;; normalize-sentence : axiom module -> axiom' Bool
216262 ;;; [NF]
223269 (lhs (rule-lhs target))
224270 (rhs (rule-rhs target))
225271 (condition (rule-condition target))
272 (reduction-mode (if (eq (rule-type ax) :equation)
273 :red
274 :exec))
226275 (app? nil))
227 (with-citp-debug ()
228 (with-in-module (module)
229 (format t "~%[NF]: target")
230 (print-next)
231 (print-axiom-brief target)))
232 ;;
233 (multiple-value-bind (result applied)
234 (normalize-term-in module lhs)
235 ;; (declare (ignore result))
236 (or app? (setq app? applied))
237 (multiple-value-setq (result applied) (normalize-term-in module rhs))
238 (or app? (setq app? applied))
239 (unless (is-true? condition)
240 (multiple-value-setq (result applied)
241 (normalize-term-in module condition))
242 (or app? (setq app? applied)))
243 ;;
276 (flet ((set-applied (val)
277 (or app? (setq app? val))))
244278 (with-citp-debug ()
245 (if (not app?)
246 (format t "~% ...not applied.")
247 (progn
248 (print-next)
249 (princ "==> ") (print-axiom-brief target))))
279 (with-in-module (module)
280 (format t "~%[NF]: target")
281 (print-next)
282 (print-axiom-brief target)))
250283 ;;
251 (values target app?)))))
284 (multiple-value-bind (result applied)
285 (normalize-term-in module lhs :red)
286 (set-applied applied)
287 (when (eq reduction-mode :exec)
288 (multiple-value-setq (result applied) (normalize-term-in module lhs :exec))
289 (set-applied applied))
290 (multiple-value-setq (result applied) (normalize-term-in module rhs))
291 (set-applied applied)
292 (unless (is-true? condition)
293 (multiple-value-setq (result applied)
294 (normalize-term-in module condition :red))
295 (set-applied applied))
296 ;;
297 (with-citp-debug ()
298 (if (not app?)
299 (format t "~% ...not applied.")
300 (progn
301 (print-next)
302 result
303 (princ "==> ") (print-axiom-brief target))))
304 ;;
305 (values target app?))))))
252306
253307 ;;;
254308 ;;; is-contradiction : term term -> Bool
274328 ;;; check-contradiction : module -> Bool
275329 ;;; check if 'true => false' or 'false => true'
276330 ;;;
331 (defun check-true<=>false (module &optional (report-header nil))
332 (with-in-module (module)
333 (let ((t-rules (method-rules-with-different-top *bool-true-meth*))
334 (f-rules (method-rules-with-different-top *bool-false-meth*))
335 (ct-rules nil))
336 (dolist (rule (append t-rules f-rules))
337 (when (is-true? (rule-condition rule))
338 (push rule ct-rules)))
339 (when (and ct-rules report-header)
340 (format t "~%[~a]: contradiction " report-header)
341 (let ((*print-indent* (+ 2 *print-indent*)))
342 (dolist (ax (reverse ct-rules))
343 (print-next)
344 (print-axiom-brief ax))))
345 (nreverse ct-rules))))
346
277347 (defun check-contradiction (module &optional (report-header nil))
278 (with-in-module (module)
279 (let ((cont-axs (or (method-rules-with-different-top *bool-true-meth*)
280 (method-rules-with-different-top *bool-false-meth*))))
281 (when cont-axs
282 (when report-header
283 (dolist (ax cont-axs)
284 (format t "~%[~a]: contradiction " report-header)
285 (let ((*print-indent* (+ 2 *print-indent*)))
286 (print-next)
287 (print-axiom-brief ax))))
288 (return-from check-contradiction t)))
289 (let ((true-term (make-applform-simple *bool-sort* *bool-true-meth* nil))
290 (false-term (make-applform-simple *bool-sort* *bool-false-meth* nil)))
291 (let ((true=false (make-applform-simple *bool-sort* *eql-op* (list true-term false-term))))
292 (multiple-value-bind (t-result t-applied?)
293 (normalize-term-in module true=false)
294 (when (and t-applied? (is-true? t-result))
295 (when report-header
296 (format t "~%[~a]: contradiction " report-header)
297 (print-next)
298 (format t " `true = false' can be derived!"))
299 (return-from check-contradiction t))))
300 nil)))
301
302 ;;; check-sentence&mark-label sentence
303 ;;;
348 (or (check-true<=>false module report-header)
349 (with-in-module (module)
350 (let ((true-term (make-applform-simple *bool-sort* *bool-true-meth* nil))
351 (false-term (make-applform-simple *bool-sort* *bool-false-meth* nil)))
352 (let ((true=false (make-applform-simple *bool-sort* *eql-op* (list true-term false-term))))
353 (multiple-value-bind (t-result t-applied?)
354 (normalize-term-in module true=false)
355 (when (and t-applied? (is-true? t-result))
356 (when report-header
357 (format t "~%[~a]: contradiction " report-header)
358 (print-next)
359 (format t " `true = false' can be derived!"))
360 (return-from check-contradiction t))))))
361 nil))
362
363 ;;; check-sentence&mark-label : sentence module -> (<result> <normalized-sentence> <origina-sentence>)
364 ;;;
304365 (defun check-sentence&mark-label (sentence module &optional (report-header nil))
305366 (with-in-module (module)
306367 (let ((target sentence)
327388 (otherwise (values nil target sentence))))))
328389
329390 ;;;
391 ;;; set-operator-rewrite-rule : module axiom -> void
392 ;;;
393 (defun set-operator-rewrite-rule (module axiom)
394 (when (term-is-applform? (rule-lhs axiom))
395 (add-rule-to-method axiom (term-head (rule-lhs axiom)) (module-opinfo-table module))))
396
397 ;;;
398 ;;; check-goal-is-satisfied : goal -> ( <result> <normalized-target> <possibly-marked-target> )
399 ;;;
400 (defun check-goal-is-satisfied (goal &optional (report-header nil))
401 (when (cdr (goal-targets goal))
402 (with-output-chaos-error ('invalid-proof-seq)
403 (format t "Internal error. more than one target!")))
404 (with-in-module ((goal-context goal))
405 (let ((target (car (goal-targets goal)))
406 (context (goal-context goal)))
407 (multiple-value-bind (result norm-target marked-target)
408 (check-sentence&mark-label target context report-header)
409 (cond (result
410 ;; dischared by some reason
411 (setf (goal-targets goal) nil)
412 (setf (goal-proved goal) (list marked-target)))
413 ((and (is-true? (rule-condition target))
414 (eq (rule-type target) :equation))
415 (setf target (rule-copy-canonicalized target context))
416 (setf (rule-lhs target) (make-applform-simple *bool-sort*
417 *eql-op*
418 (list (rule-lhs target)
419 (rule-rhs target)))
420 (rule-rhs target) *bool-true*)
421 (multiple-value-bind (res-2 norm-target-2 marked-target-2)
422 (check-sentence&mark-label target context report-header)
423 (declare (ignore norm-target-2 marked-target-2))
424 (when res-2
425 (setf (goal-targets goal) nil)
426 (setf (goal-proved goal) (list marked-target)))))
427 (t ;; nothing to do
428 ))
429 (values result norm-target marked-target target)))))
430 ;;;
330431 ;;; try-prove-with-axioms : module List(axiom) axiom : -> { :satisfied | :ct | nil }
331432 ;;;
332433 (defparameter .trial-context-module. (%module-decl* "trial-dummy" :object :user nil))
338439 (with-in-module (tmodule)
339440 (dolist (ax axioms)
340441 (adjoin-axiom-to-module tmodule ax)
341 (add-rule-to-method ax
342 (term-method (rule-lhs ax))
343 (module-opinfo-table tmodule)))
442 (set-operator-rewrite-rule tmodule ax))
344443 (compile-module tmodule t)
345444 ;; first we check contradiction
346445 (if (check-contradiction tmodule report-header)
351450 (if applied
352451 (sentence-is-satisfied res)
353452 nil)))))))
453
454 ;;; ****************************************************************************
455 ;;; Tactic executors
456 ;;; ****************************************************************************
457
458 ;;; ===========
459 ;;; TACTIC: NIL
460 ;;; do nothing
461 ;;; ===========
462
463 (defun apply-nil (node)
464 (declare (ignore node))
465 (with-output-chaos-warning ()
466 (format t "~%Tactic [NIL] does nothing."))
467 (values nil nil))
468
469 (defun apply-nil-internal (node sentences &optional (all-together nil) (tactic .tactic-nil.))
470 (declare (type ptree-node node)
471 (type list sentences))
472 (let ((goals nil))
473 (cond (all-together
474 (let ((ngoal (prepare-next-goal node tactic)))
475 (setf (goal-targets ngoal) sentences)
476 (push ngoal goals)))
477 (t (dolist (sentence sentences)
478 (let ((ngoal (prepare-next-goal node tactic)))
479 (setf (goal-targets ngoal) (list sentence))
480 (push ngoal goals)))))
481 (values goals (nreverse goals))))
354482
355483 ;;; =======================
356484 ;;; TACTIC: IMPLICATION[IP]
367495 (push (term-copy-and-returns-list-variables sub) cps)))
368496 (setq cps (list (term-copy-and-returns-list-variables condition))))
369497 (dolist (c cps)
370 (let ((new-ax (make-new-assumption c *bool-true* *bool-true* 'ip))
371 #||
372 (make-rule :lhs c
373 :rhs *bool-true*
374 :condition *bool-true*
375 :type :equation
376 :behavioural (axiom-behavioural axiom)
377 :labels '(ip))
378 ||#
379 )
498 (let ((new-ax (make-new-assumption c *bool-true* *bool-true* 'ip)))
380499 (compute-rule-method new-ax)
381500 (push new-ax axs)))
382501 (with-citp-debug ()
389508 (defun apply-ip (ptree-node)
390509 (declare (type ptree-node ptree-node))
391510 (with-in-context (ptree-node)
392 (let ((inconsistent nil)
393 (remaining nil)
394 (discharged nil))
395 (with-in-module ((goal-context .cur-goal.))
396 (dolist (target .cur-targets.)
397 (cond ((is-true? (rule-condition target))
398 ;; target has no condition part
399 (push target remaining))
400 (t (setq target (normalize-sentence target (goal-context .cur-goal.)))
401 (let ((condition (rule-condition target)))
402 (cond ((is-true? condition)
403 ;; target is discharged.
404 (format t "~%[ip] discharged: ")
405 (print-axiom-brief target)
406 (push target discharged))
407 ((equal *bool-false* condition)
408 ;; IC (inconsistency)
409 (format t "~%[ip] inconsistent axiom")
410 (let ((*print-indent* (+ 2 *print-indent*)))
411 (print-next)
412 (print-axiom-brief target))
413 (push target inconsistent))
414 ((null (term-variables condition))
415 ;; t = t' if C
416 ;; C is a ground term and is not true.
417 ;; try if (SP + { C } |- t = t') or not..
418 ;; if this is satisfied, discharge it.
419 (let ((new-axs (generate-ip-derived-axioms *current-module* target))
420 (next-target (rule-copy-canonicalized target *current-module*))
421 (satisfied? nil))
422 (setf (rule-condition next-target) *bool-true*)
423 (setq satisfied? (try-prove-with-axioms *current-module*
424 new-axs
425 next-target
426 'ip))
427 (unless satisfied?
428 (when (eq (rule-type next-target) :equation)
429 (setf (rule-lhs next-target) (make-applform-simple *bool-sort*
430 *eql-op*
431 (list (rule-lhs next-target)
432 (rule-rhs next-target)))
433 (rule-rhs next-target) *bool-true*)
434 (setq satisfied? (try-prove-with-axioms *current-module*
435 new-axs
436 next-target))))
437
438 (with-citp-debug ()
439 (format t "~%[ip]: tries to prove ")
440 (print-axiom-brief next-target)
441 (print-next)
442 (format t "with axioms")
443 (let ((*print-indent* (+ 2 *print-indent*)))
444 (dolist (ax new-axs)
445 (print-next)
446 (print-axiom-brief ax)))
447 (print-next)
448 (format t "-- result = ~s" satisfied?))
449
450 (cond ((eq satisfied? :satisfied)
451 ;; the target is satisfied assuming new-axs
452 (push target discharged)
453 (setf (rule-labels target) (cons 'ip (rule-labels target)))
454 (format t "~%[ip] discharged: ")
455 (print-axiom-brief target))
456 ((eq satisfied? :ct)
457 ;; contradiction
458 (setf (rule-labels target) (cons 'ct (rule-labels target)))
459 (push target inconsistent)
460 (format t "~%[ip] discharged: ")
461 (print-axiom-brief target))
462 (t
463 (push target remaining)
464 ;; generate next goal to be proved
465 ;; (SP + { C } |- t = t')
466 (let ((next-goal (prepare-next-goal ptree-node .tactic-ip.)))
467 (with-in-module ((goal-context next-goal))
468 (dolist (new-ax new-axs)
469 (add-rule-to-method new-ax
470 (term-method (rule-lhs new-ax))
471 (module-opinfo-table *current-module*))
472 (adjoin-axiom-to-module *current-module* new-ax))
473 (setf (goal-assumptions next-goal) (append (goal-assumptions next-goal) new-axs))
474 (setf (goal-targets next-goal) (list next-target))
475 (push next-goal .next-goals.)))))))
476 (t ;; others
477 (push target remaining)))))))
478 ;; done for all target
479 (setf (goal-targets .cur-goal.) (nreverse remaining))
480 (setf (goal-proved .cur-goal.) (nconc (reverse discharged) (reverse inconsistent))))
481 ;;
482 (if .next-goals.
483 (values t .next-goals.)
484 (progn
485 (when (goal-proved .cur-goal.)
486 (format t "~%[ip]: discharged the goal ~s" (goal-name .cur-goal.)))
487 (values nil nil))))))
511 (let ((original-goal (ptree-node-goal ptree-node)))
512 (flet ((push-next-goal (goal)
513 (unless (eq goal original-goal) (push goal .next-goals.))))
514 (let ((target-goals (distribute-sentences ptree-node .cur-targets. .tactic-ip.)))
515 (dolist (.cur-goal. target-goals)
516 (multiple-value-bind (result target otarget)
517 (check-goal-is-satisfied .cur-goal. 'ip)
518 (declare (ignore otarget))
519 (if result
520 ;; discharged by som reason
521 (push-next-goal .cur-goal.))
522 (cond ((and (not (is-true? (rule-condition target)))
523 (null (term-variables (rule-condition target))))
524 ;; t = t' if C
525 ;; C is a ground term and is not true.
526 ;; try if (SP + { C } |- t = t') or not..
527 ;; if this is satisfied, discharge it.
528 (let ((ngoal (if (eq .cur-goal. original-goal)
529 (prepare-next-goal ptree-node .tactic-ip.)
530 .cur-goal.)))
531 (with-in-module ((goal-context ngoal))
532 (let ((new-axs (generate-ip-derived-axioms *current-module* target))
533 (next-target (rule-copy-canonicalized target *current-module*)))
534 ;; make the target
535 (setf (rule-condition next-target) *bool-true*)
536 (setf (goal-targets ngoal)
537 (list (normalize-sentence next-target *current-module*)))
538 ;; add [ip] axioms
539 (dolist (ax new-axs)
540 (adjoin-axiom-to-module *current-module* ax)
541 (set-operator-rewrite-rule *current-module* ax))
542 (setf (goal-assumptions ngoal) (reverse new-axs))
543 ;; compile & check
544 (compile-module *current-module* t)
545 ;; check-goal-isa-satisfied do all the neccessary things for us.
546 ;; (check-goal-is-satisfied ngoal 'ip)
547 (push-next-goal ngoal)))))
548 ;; nothig todo. remain the goal as is
549 (t ))))
550 ;; done for all goals
551 (values .next-goals. (nreverse .next-goals.)))))))
488552
489553 ;;; =================================
490554 ;;; TACTIC: Theorem of Constants [TC]
504568
505569 (defun apply-tc (ptree-node)
506570 (declare (type ptree-node ptree-node))
571 ;;
507572 (with-in-context (ptree-node)
508 (let ((discharged nil)
509 (remaining nil))
510 ;; for each target sentence do TC
511 (dolist (target .cur-targets.)
512 (multiple-value-bind (c-result cur-target original-sentence)
513 (check-sentence&mark-label target (goal-context .cur-goal.) 'tc)
514 (cond (c-result ; satisfied or contradiction
515 (push original-sentence discharged))
516 ((axiom-variables cur-target)
517 ;; (push target remaining)
518 (let ((next-goal (prepare-next-goal ptree-node .tactic-tc.)))
519 (with-in-module ((goal-context next-goal))
520 (let* ((next-target (rule-copy-canonicalized cur-target *current-module*))
521 (vars (axiom-variables next-target))
522 (subst (make-tc-variable-substitutions next-goal vars)))
523 (push next-goal .next-goals.)
524 (setf (rule-lhs next-target) (substitution-image-simplifying subst (rule-lhs next-target))
525 (rule-rhs next-target) (substitution-image-simplifying subst (rule-rhs next-target))
526 (rule-condition next-target) (if (is-true? (rule-condition next-target))
527 *bool-true*
528 (substitution-image-simplifying subst (rule-condition next-target))))
529 (compute-rule-method next-target)
530 (setf (goal-targets next-goal) (list next-target))))))
531 (t (push target remaining)))))
532 (setf (goal-proved .cur-goal.) (nreverse discharged))
533 (setf (goal-targets .cur-goal.) (nreverse remaining))
534 (if .next-goals.
535 (values t (nreverse .next-goals.))
536 (if (goal-targets .cur-goal.)
537 (values nil nil)
538 (progn
539 (format t "~%[tc]: discharged the goal ~s" (goal-name .cur-goal.))
540 (values t nil)))))))
573 (let ((original-goal .cur-goal.))
574 (flet ((push-next-goal (goal)
575 (unless (eq goal original-goal) (push goal .next-goals.))))
576 (let ((target-goals (distribute-sentences ptree-node .cur-targets. .tactic-tc.)))
577 (dolist (cgoal target-goals)
578 (multiple-value-bind (res sentence osentence)
579 (check-goal-is-satisfied cgoal 'rd)
580 (declare (ignore osentence))
581 (cond (res
582 ;; discharged
583 (push-next-goal cgoal))
584 ((axiom-variables sentence)
585 (when (eq cgoal original-goal)
586 (setq cgoal (prepare-next-goal ptree-node .tactic-tc.)))
587 (push-next-goal cgoal)
588 (with-in-module ((goal-context cgoal))
589 (let* ((next-target (rule-copy-canonicalized sentence *current-module*))
590 (vars (axiom-variables next-target))
591 (subst (make-tc-variable-substitutions cgoal vars)))
592 (apply-substitution-to-axiom subst next-target)
593 (compute-rule-method next-target)
594 (setf (goal-targets cgoal)
595 (list (normalize-sentence next-target *current-module*))))))
596 (t ))))
597 (values .next-goals. (nreverse .next-goals.)))))))
541598
542599 ;;; ===================================
543600 ;;; TACTIC: Simultaneous Induction [SI]
651708 (dolist (subst all-subst)
652709 (when (every #'(lambda (sub) (null (method-arity (cdr sub)))) subst)
653710 (push subst res)))
711 (with-citp-debug ()
712 (format t "~%[si] base case subst")
713 (dolist (sub res)
714 (print-next)
715 (print-substitution sub)))
654716 (nreverse res)))
655717
656718 ;;;
661723 (dolist (subst all-subst)
662724 (unless (every #'(lambda (sub) (null (method-arity (cdr sub)))) subst)
663725 (push subst res)))
726 (with-citp-debug ()
727 (format t "~%[si] get-induction-step-subsutitutions")
728 (dolist (sub res)
729 (print-next)
730 (print-substitution sub)))
664731 (nreverse res)))
665732
666733 ;;;
690757 ;;; generates base case axioms for a given target
691758 ;;;
692759 (defun set-base-cases (goal target base-substitutions)
693 (let ((all-targets nil))
760 (let ((all-targets nil)
761 (app? nil))
694762 (with-in-module ((goal-context goal))
695763 (dolist (subst base-substitutions)
696764 (let* ((new-target (rule-copy-canonicalized target *current-module*))
697765 (real-subst (make-real-induction-subst subst (axiom-variables new-target))))
698 (setf (rule-lhs new-target) (substitution-image-simplifying real-subst (rule-lhs new-target))
699 (rule-rhs new-target) (substitution-image-simplifying real-subst (rule-rhs new-target))
700 (rule-condition new-target) (substitution-image-simplifying real-subst (rule-condition new-target)))
701 (push new-target all-targets)))
702 (setf (goal-targets goal) (nconc (goal-targets goal) all-targets)))))
766 (when real-subst
767 (setq app? t)
768 (apply-substitution-to-axiom real-subst new-target)
769 (push new-target all-targets)))))
770 (setf (goal-targets goal) (nconc (goal-targets goal) all-targets))
771 app?))
703772
704773 ;;;
705774 ;;; make-step-constructor-term
718787 res))))
719788
720789 ;;;
721 ;;; make-induction-step-subst : goal axiom List((var . op)) -> substitution
790 ;;; make-induction-step-subst : goal axiom (var . op) -> substitution
722791 ;;;
723792 (defun make-induction-step-subst (goal target v-op-list)
724793 ;; we ignore all mapped operators are constant constructors.
778847 (cons (car sub) (list (third sub))))
779848 hypo-v-op))
780849 (all-subst nil))
781 ;; return if there are no possible combinations
782 (unless (cdr hypo-v-op)
783850 (with-citp-debug ()
784 (format t "~%resolve subst: no possible combinations"))
785 (return-from resolve-induction-subst (list (make-proper-alist rsubsts))))
851 (format t "~%[si] resolve induction step: given")
852 (print-next) (format t "hypo-v-op: ~s" hypo-v-op)
853 (print-next) (princ "step-subst" )
854 (print-substitution step-subst))
855 ;; return if there are no possible combinations
856 ;; (unless (cdr hypo-v-op)
857 ;; (return-from resolve-induction-subst (list (make-proper-alist rsubsts))))
786858 ;;
787859 (with-citp-debug ()
788860 (format t "~%resolve subst: given")
840912 t)
841913
842914 (defun add-hypothesis (step-goal target hypo-subst step-subst)
915 (with-citp-debug ()
916 (format t "~%[si]: add-hypothesis")
917 (print-next) (princ "-- hypo-subst ")
918 (dolist (hp hypo-subst)
919 (print-next)
920 (print-substitution hp))
921 (print-next) (princ "-- step-subst ")
922 (print-substitution step-subst))
843923 (dolist (osub hypo-subst)
844924 (dolist (sub (resolve-induction-subst step-goal osub step-subst))
845925 (unless (subst-is-equal sub step-subst)
847927 (subst (make-real-induction-step-subst sub (axiom-variables hypo))))
848928 (with-citp-debug
849929 (format t "~%>>[applying hypo subst] ")
850 (print-substitution subst))
851 (setf (rule-lhs hypo) (substitution-image-simplifying subst (rule-lhs hypo))
852 (rule-rhs hypo) (substitution-image-simplifying subst (rule-rhs hypo))
853 (rule-condition hypo) (if (is-true? (rule-condition hypo))
854 *bool-true*
855 (substitution-image-simplifying subst (rule-condition hypo)))
856 (rule-labels hypo) '(si)) ; (cons 'ind-hypo (rule-labels target))
857
930 (print-substitution subst)
931 (print-next)
932 (princ "to ")
933 (print-axiom-brief hypo))
934 (apply-substitution-to-axiom subst hypo '(si))
858935 (compute-rule-method hypo)
859 (add-rule-to-method hypo (term-method (rule-lhs hypo)) (module-opinfo-table *current-module*))
936 (set-operator-rewrite-rule *current-module* hypo)
860937 (adjoin-axiom-to-module *current-module* hypo)
938 (with-citp-debug ()
939 (format t "~%--> ")
940 (print-axiom-brief hypo))
861941 (setf (goal-assumptions step-goal) (append (goal-assumptions step-goal) (list hypo))))))))
862942
863943 ;;;
871951 (with-citp-debug
872952 (format t "~%>>[applying step subst] ")
873953 (print-substitution subst))
874 (setf (rule-lhs new-target) (substitution-image-simplifying subst (rule-lhs new-target))
875 (rule-rhs new-target) (substitution-image-simplifying subst (rule-rhs new-target))
876 (rule-condition new-target) (if (is-true? (rule-condition new-target))
877 *bool-true*
878 (substitution-image-simplifying subst (rule-condition new-target))))
954 (apply-substitution-to-axiom subst new-target)
879955 (setf (goal-targets step-goal) (nconc (goal-targets step-goal) (list new-target))))))
880956
881957 ;;;
885961 (defun induction-cases (parent-node)
886962 (declare (type ptree-node parent-node))
887963 (let* ((cur-goal (ptree-node-goal parent-node))
888 (cur-targets (goal-targets cur-goal))
964 (cur-targets nil)
889965 (indvars (goal-indvars cur-goal))
890966 (all-subst (make-indvar-comb-substitutions indvars
891967 (gather-constructor-ops (goal-context cur-goal))))
892968 (base-goal (prepare-next-goal parent-node .tactic-si.))
893 (step-goals nil))
969 (step-goals nil)
970 (need-goal nil)
971 (base-generated nil)
972 (remainings nil))
894973 ;;
895974 (with-citp-debug ()
975 (format t "~%[si] all possible substitutions")
896976 (let ((num 0))
897977 (declare (type fixnum num))
898978 (dolist (subs all-subst)
899979 (format t "~%subst #~d" (incf num))
900980 (let ((*print-indent* (+ 2 *print-indent*)))
901981 (print-next)
902 ;; (dolist (s subs) (print-substitution s))
903982 (print-substitution subs)))))
983
984 ;; implicit NF application
985 (dolist (ct (goal-targets cur-goal))
986 (multiple-value-bind (ntarget app?)
987 (normalize-sentence ct (goal-context cur-goal))
988 (when app? (setq need-goal t))
989 (push ntarget cur-targets)))
990 (setq cur-targets (nreverse cur-targets))
904991
905992 ;; generate base cases
906993 ;;
907994 (dolist (target cur-targets)
908 (set-base-cases base-goal target (get-induction-base-substitutions all-subst)))
995 (if (not (set-base-cases base-goal target (get-induction-base-substitutions all-subst)))
996 (when need-goal
997 (push target remainings))
998 (setq base-generated t)))
999 (unless base-generated (setq base-goal nil))
9091000
9101001 ;; generate step cases
9111002 ;; we generate all possible combinations of given induction variables.
9201011 (make-induction-step-subst step-goal target subst)
9211012 (add-hypothesis step-goal target hypo-subst-list step-subst)
9221013 (add-step-cases step-goal target step-subst)))
923 ;; (!setup-reduction (goal-context step-goal))
924 (push step-goal step-goals))))
1014 (cond ((goal-targets step-goal)
1015 (push step-goal step-goals))
1016 (t ))))) ; do nothig
1017 ;;
1018 (when remainings
1019 (multiple-value-bind (ap? nil-goals)
1020 (apply-nil-internal parent-node (reverse remainings) :all-togather)
1021 (declare (ignore ap?))
1022 (dolist (ng nil-goals)
1023 (push ng step-goals))))
9251024 ;;
926 (values t (cons base-goal (nreverse step-goals)))))
927
928 ;;; apply-si
1025 (if base-goal
1026 (values t (cons base-goal (nreverse step-goals)))
1027 (if step-goals
1028 ;; case remainings
1029 (values t step-goals)
1030 (values nil nil)))))
1031
1032 ;;;
1033 ;;; apply-si : ptree-node -> (applied? . List(goal))
9291034 ;;;
9301035 (defun apply-si (ptree-node)
9311036 (declare (type ptree-node ptree-node))
932 ;; internally apply-rd
933 (apply-rd ptree-node)
934 ;;
9351037 (let ((cur-goal (ptree-node-goal ptree-node)))
9361038 (unless (and (goal-indvars cur-goal) (goal-targets cur-goal))
9371039 (return-from apply-si nil))
11481250 (format t "~%>>[ca] adding an axiom to module ~s" (get-module-simple-name (goal-context next-goal)))
11491251 (print-next)
11501252 (print-axiom-brief axs))
1151 (add-rule-to-method axs (term-method (rule-lhs axs)) (module-opinfo-table *current-module*))
1253 (set-operator-rewrite-rule *current-module* axs)
11521254 (adjoin-axiom-to-module *current-module* axs)
11531255 (push axs case-axioms))))
11541256 (setf (goal-assumptions next-goal) (append (goal-assumptions next-goal)
11581260 ;;;
11591261 ;;; generate-cases : ptree-node term List(conditional-axiom)
11601262 ;;;
1161 (defun generate-cases (ptree-node target conditional-rules)
1162 (let ((gterms (get-gterms-from-axiom target))
1163 (next-goals nil))
1164 ;; g-rs-pairs: ((gterm-1 . List(rs-pairs)) .. (gterm-n . (List(rs-pairs))))
1165 ;; rs-pairs : ((rule-1 . subst-1) ... (rule-m . subst-m))
1166 (let ((g-rs-pairs nil))
1167 (dolist (gterm gterms)
1168 ;; split cases for each ground term
1169 ;; Gterm-1 : (rs-1 rs-2) (rs-3) --> ((rs-1 rs-3) (rs-2 rs-3))
1170 (let ((rs-pairs (find-gterm-matching-conditionals gterm conditional-rules)))
1171 (when rs-pairs
1172 (setf g-rs-pairs (nconc g-rs-pairs rs-pairs)))))
1173 ;; make all combinations and generate cases
1174 ;; Gterm-1 : (rs-1 rs-2) (rs-3) --> ((rs-1 rs-3) (rs-2 rs-3))
1175 ;; Gterm-2 : (rs-4) --> ((rs-4))
1176 ;; ==> ((rs-1 rs-3 rs-4) (rs-2 rs-3 rs-4)) <-- two groups, i.e, two next goals
1177 (let ((rv-combs (select-comb-elems g-rs-pairs)))
1178 (dolist (rv-com rv-combs)
1179 (with-citp-debug ()
1180 (dolist (rr rv-com)
1181 (format t "~%>>[ca] gterm rs-pair: ")
1182 (print-next)
1183 (print-axiom-brief (car rr))
1184 (print-next)
1185 (print-substitution (cdr rr))))
1186 ;; rv-com ((gterm-1 . rv-pair-1-1) (gterm-2 . rvpair-2-1) .. (gterm-n . rvpair-n-1))
1187 (let ((next-goal (prepare-next-goal ptree-node .tactic-ca.)))
1188 (setf (goal-targets next-goal) (goal-targets (ptree-node-goal ptree-node)))
1189 (generate-case-axioms next-goal rv-com)
1190 (push next-goal next-goals))))
1263 (defun generate-cases (ptree-node target conditional-rules divide?)
1264 (declare (type ptree-node ptree-node)
1265 (list conditional-rules))
1266 (multiple-value-bind (norm-target app?)
1267 (normalize-sentence target (goal-context (ptree-node-goal ptree-node)))
1268 (when app?
1269 (setq target norm-target))
1270 ;; then generate possible cases
1271 (let ((gterms (get-gterms-from-axiom target))
1272 (next-goals nil)
1273 (remainings nil))
1274 ;; g-rs-pairs: ((gterm-1 . List(rs-pairs)) .. (gterm-n . (List(rs-pairs))))
1275 ;; rs-pairs : ((rule-1 . subst-1) ... (rule-m . subst-m))
1276 (let ((g-rs-pairs nil))
1277 (dolist (gterm gterms)
1278 ;; split cases for each ground term
1279 ;; Gterm-1 : (rs-1 rs-2) (rs-3) --> ((rs-1 rs-3) (rs-2 rs-3))
1280 (let ((rs-pairs (find-gterm-matching-conditionals gterm conditional-rules)))
1281 (when rs-pairs
1282 (setf g-rs-pairs (nconc g-rs-pairs rs-pairs)))))
1283 ;; make all combinations and generate cases
1284 ;; Gterm-1 : (rs-1 rs-2) (rs-3) --> ((rs-1 rs-3) (rs-2 rs-3))
1285 ;; Gterm-2 : (rs-4) --> ((rs-4))
1286 ;; ==> ((rs-1 rs-3 rs-4) (rs-2 rs-3 rs-4)) <-- two groups, i.e, two next goals
1287 (let ((rv-combs (select-comb-elems g-rs-pairs))
1288 (next-goal nil))
1289 (cond (rv-combs
1290 (dolist (rv-com rv-combs)
1291 (with-citp-debug ()
1292 (dolist (rr rv-com)
1293 (format t "~%>>[ca] gterm rs-pair: ")
1294 (print-next)
1295 (print-axiom-brief (car rr))
1296 (print-next)
1297 (print-substitution (cdr rr))))
1298 ;; rv-com ((gterm-1 . rv-pair-1-1) (gterm-2 . rvpair-2-1) .. (gterm-n . rvpair-n-1))
1299 (setq next-goal (prepare-next-goal ptree-node .tactic-ca.))
1300 (setf (goal-targets next-goal) (goal-targets (ptree-node-goal ptree-node)))
1301 (generate-case-axioms next-goal rv-com)
1302 (push next-goal next-goals)))
1303 (t (push target remainings)))))
1304 ;;
1305 (when remainings
1306 (when (or next-goals app? divide?)
1307 (multiple-value-bind (app? nop-goals)
1308 (apply-nil-internal ptree-node (reverse remainings) nil .tactic-ca.)
1309 (declare (ignore app?))
1310 (dolist(ng nop-goals)
1311 (push ng next-goals)))))
11911312 (values next-goals (nreverse next-goals)))))
11921313
11931314 (defun apply-ca (ptree-node)
11951316 (with-in-context (ptree-node)
11961317 (with-in-module ((goal-context .cur-goal.))
11971318 (let ((crules (remove-if #'(lambda (x) (is-true? (rule-condition x)))
1198 (module-all-rules (goal-context .cur-goal.)))))
1319 (module-all-rules (goal-context .cur-goal.))))
1320 (divide? (cdr .cur-targets.)))
11991321 (dolist (target .cur-targets.)
12001322 (multiple-value-bind (applied goals)
1201 (generate-cases ptree-node target crules)
1323 (generate-cases ptree-node target crules divide?)
12021324 (declare (ignore applied))
12031325 (when goals (setq .next-goals. (nconc .next-goals. goals)))))
12041326 (values .next-goals. .next-goals.)))))
12241346 (t (with-in-module (module)
12251347 (setq ax (parse-axiom-declaration (parse-module-element-1 (cdr target-form))))
12261348 (when add-to-module
1227 (add-rule-to-method ax (term-method (rule-lhs ax)) (module-opinfo-table module))
1349 (set-operator-rewrite-rule module ax)
12281350 (adjoin-axiom-to-module module ax)
12291351 (set-needs-rule)))))
12301352 ax))
12801402 (let ((new-axiom (rule-copy-canonicalized axiom module))
12811403 (rsubst nil))
12821404 (setq rsubst (make-real-instanciation-subst subst (axiom-variables new-axiom)))
1283 (setf (rule-lhs new-axiom) (substitution-image-simplifying rsubst (rule-lhs new-axiom))
1284 (rule-rhs new-axiom) (substitution-image-simplifying rsubst (rule-rhs new-axiom))
1285 (rule-condition new-axiom) (substitution-image-simplifying rsubst (rule-condition new-axiom))
1286 (rule-labels new-axiom) '(init))
1405 (apply-substitution-to-axiom rsubst new-axiom '(init))
1406 #||
12871407 (when (axiom-variables new-axiom)
12881408 (with-output-chaos-error ('not-ground)
12891409 (format t "Instanciating an axiom, not all variable substitutions are supplied.")
12901410 (dolist (v (axiom-variables new-axiom))
12911411 (print-next)
12921412 (term-print-with-sort v))))
1413 ||#
12931414 new-axiom))
12941415
12951416 ;;;
13151436 (print-axiom-brief instance))
13161437 (pr-goal (ptree-node-goal context))
13171438 ;;
1318 (add-rule-to-method instance (term-method (rule-lhs instance)) (module-opinfo-table *current-module*))
1439 (set-operator-rewrite-rule *current-module* instance)
13191440 (adjoin-axiom-to-module *current-module* instance)
13201441 ;; (!setup-reduction *current-module*)
13211442 )))))
15371658 (setf (rule-lhs cps) lhs
15381659 (rule-rhs cps) rhs)))
15391660 (compute-rule-method cps)
1540 (add-rule-to-method cps (term-method (rule-lhs cps)) (module-opinfo-table *current-module*))
1661 (set-operator-rewrite-rule *current-module* cps)
15411662 (adjoin-axiom-to-module *current-module* cps)
15421663 (setq applied (nconc applied (list cps))))
15431664 (when applied
15641685 ;;; returns the list of generated goal nodes.
15651686 ;;; -----------------------------------------------------
15661687
1567 (defun apply-tactic (ptree-node tactic)
1688 (defun apply-tactic (ptree-node tactic &optional (verbose t))
15681689 (declare (type ptree-node ptree-node)
15691690 (type tactic tactic))
15701691 (let ((*chaos-quiet* t))
15821703 (type list next-goals))
15831704 (unless applied (return-from apply-tactic nil))
15841705 (unless next-goals
1585 #||
1586 ;; this means this goal is successfully proved
1587 (if (null (discharge-node ptree-node))
1588 ;; all over
1589 (return-from apply-tactic :done)
1590 (return-from apply-tactic nil))
1591 ||#
1706 ;; reset the current ptree-node status,
1707 ;; i.e., cancel side effents
1708 (initialize-ptree-node ptree-node)
15921709 (return-from apply-tactic nil))
15931710 (format t "~%>> Generated ~d goal~p" (length next-goals) (length next-goals))
1594 (dolist (goal next-goals)
1595 (pr-goal goal)
1596 (add-ptree-child ptree-node goal))
1711 (add-ptree-children ptree-node next-goals)
1712 (when verbose
1713 (dolist (gn (ptree-node-subnodes ptree-node))
1714 (pr-goal (ptree-node-goal gn))))
15971715 (ptree-node-subnodes ptree-node))))
15981716
15991717 ;;;
16001718 ;;; apply-tactics-to-node
16011719 ;;;
16021720 (defun apply-tactics-to-node (target-node tactics)
1721 (unless tactics (return-from apply-tactics-to-node nil))
16031722 (let ((subs (apply-tactic target-node (car tactics))))
1604 (dolist (tactic (cdr tactics))
1605 (dolist (target subs)
1606 (apply-tactic target tactic)))))
1723 (if subs
1724 (dolist (target subs)
1725 (apply-tactics-to-node target (cdr tactics)))
1726 (apply-tactics-to-node target-node (cdr tactics)))))
16071727
16081728 ;;;
16091729 ;;; apply-tactics
16101730 ;;;
16111731 (defun apply-tactics (ptree tactics)
1612 (if (next-proof-target-is-specified?)
1613 (apply-tactics-to-node (get-next-proof-context ptree) tactics)
1614 (dolist (tactic tactics)
1615 (let ((next-targets (get-unproved-nodes ptree)))
1616 (unless next-targets
1617 (format t "~%>> All goals have been discharged!")
1618 (setq *next-default-proof-node* nil)
1619 (return-from apply-tactics nil))
1620 (dolist (target next-targets)
1621 (apply-tactic target tactic)))))
1732 (declare (type ptree ptree)
1733 (type list tactics))
1734 (let ((target (get-next-proof-context ptree)))
1735 (unless target
1736 (format t "~%>> All goals have been discharged!")
1737 (return-from apply-tactics nil))
1738 (apply-tactics-to-node target tactics))
16221739 (check-success ptree))
16231740
16241741 ;;;
14321432 (report-rwl-result
14331433 (rwl-search* term pattern max-r max-d zero? final? cond pred module bind if)))))
14341434
1435 ;;; rwl-check-one-step-reachability
1436 ;;;
1437 (defun rwl-check-one-step-reachability (X Y)
1438 (declare (type term X Y))
1439 (let ((*clean-memo-in-normalize* nil)
1440 (*chaos-quiet* t))
1441 (report-rwl-result
1442 (rwl-search* X Y 1 1 t nil nil nil *current-module* nil nil))))
1443
14351444 ;;; rwl-sch-set-result
14361445 ;;;
14371446 (defun rwl-sch-set-result (raw-res)
3434 :executor 'apply-cs)) ; Case Analysis on Sequences
3535 (defparameter .tactic-rd. (make-tactic :name :rd
3636 :executor 'apply-rd)) ; Reduction
37
38 (defparameter .tactic-nil. (make-tactic :name :nop
39 :executor 'apply-nil)) ; Do nothing, used internally.
3740
3841 (defparameter .all-builtin-tactics. (list .tactic-si. .tactic-ca. .tactic-tc. .tactic-ip. .tactic-cs. .tactic-rd.))
3942
146149 (declare (type goal goal)
147150 (type stream stream)
148151 (ignore ignore))
149 (with-in-module ((goal-context goal))
150 (if (goal-tactic goal)
151 (format stream "~%~a=>~%:goal { ** ~a -----------------------------------------" (goal-tactic goal) (goal-name goal))
152 (format stream "~%:goal { ** ~a -----------------------------------------" (goal-name goal)))
153 (let ((*print-indent* (+ 2 *print-indent*))
154 (v-consts (goal-constants goal))
155 (i-consts (goal-ind-constants goal))
156 (ass (goal-assumptions goal))
157 (vs (goal-indvars goal))
158 (axs (goal-targets goal))
159 (proved (goal-proved goal))
160 (discharged (goal-is-discharged goal)))
161 (print-next)
162 (format stream "-- context module: ~a" (get-module-simple-name (goal-context goal)))
163 (when proved
152 (let ((*print-line-limit* 80)
153 (*print-xmode* :fancy)
154 )
155 (with-in-module ((goal-context goal))
156 (if (goal-tactic goal)
157 (format stream "~%~a=>~%:goal { ** ~a -----------------------------------------" (goal-tactic goal) (goal-name goal))
158 (format stream "~%:goal { ** ~a -----------------------------------------" (goal-name goal)))
159 (let ((*print-indent* (+ 2 *print-indent*))
160 (v-consts (goal-constants goal))
161 (i-consts (goal-ind-constants goal))
162 (ass (goal-assumptions goal))
163 (vs (goal-indvars goal))
164 (axs (goal-targets goal))
165 (proved (goal-proved goal))
166 (discharged (goal-is-discharged goal)))
164167 (print-next)
165 (format stream "-- discharged axiom~p" (length proved))
166 (dolist (pv proved)
167 (let ((*print-indent* (+ 2 *print-indent*)))
168 (print-next)
169 (print-axiom-brief pv) (princ " ."))))
170 (when vs
171 (print-next)
172 (format stream "-- induction variable~p" (length vs))
173 (dolist (v vs)
174 (let ((*print-indent* (+ 2 *print-indent*)))
175 (print-next)
176 (term-print-with-sort v))))
177 (when v-consts
178 (print-next)
179 (format stream "-- introduced constant~p" (length v-consts))
180 (dolist (const (reverse v-consts))
181 (let ((*print-indent* (+ 2 *print-indent*)))
182 (print-next)
183 (print-method-brief (term-head (cdr const))))))
184 (when i-consts
185 (print-next)
186 (format stream "-- constant~p for induction" (length i-consts))
187 (dolist (ic (reverse i-consts))
188 (let ((*print-indent* (+ 2 *print-indent*)))
189 (print-next)
190 (print-method-brief (term-head (cdr ic))))))
191 (when ass
192 (print-next)
193 (format stream "-- introduced axiom~p" (length ass))
194 (dolist (as ass)
195 (let ((*print-indent* (+ 2 *print-indent*)))
196 (print-next)
197 (print-axiom-brief as) (princ " ."))))
198 (when axs
199 (print-next)
200 (format stream "-- axiom~p to be proved" (length axs))
201 (dolist (ax axs)
202 (let ((*print-indent* (+ 2 *print-indent*)))
203 (print-next)
204 (print-axiom-brief ax) (princ " ."))))
205 (format stream "~%}")
206 (if discharged
207 (format t " << proved >>")))))
168 (format stream "-- context module: ~a" (get-module-simple-name (goal-context goal)))
169 (when proved
170 (print-next)
171 (format stream "-- discharged axiom~p" (length proved))
172 (dolist (pv proved)
173 (let ((*print-indent* (+ 2 *print-indent*)))
174 (print-next)
175 (print-axiom-brief pv) (princ " ."))))
176 (when vs
177 (print-next)
178 (format stream "-- induction variable~p" (length vs))
179 (dolist (v vs)
180 (let ((*print-indent* (+ 2 *print-indent*)))
181 (print-next)
182 (term-print-with-sort v))))
183 (when v-consts
184 (print-next)
185 (format stream "-- introduced constant~p" (length v-consts))
186 (dolist (const (reverse v-consts))
187 (let ((*print-indent* (+ 2 *print-indent*)))
188 (print-next)
189 (print-method-brief (term-head (cdr const))))))
190 (when i-consts
191 (print-next)
192 (format stream "-- constant~p for induction" (length i-consts))
193 (dolist (ic (reverse i-consts))
194 (let ((*print-indent* (+ 2 *print-indent*)))
195 (print-next)
196 (print-method-brief (term-head (cdr ic))))))
197 (when ass
198 (print-next)
199 (format stream "-- introduced axiom~p" (length ass))
200 (dolist (as ass)
201 (let ((*print-indent* (+ 2 *print-indent*)))
202 (print-next)
203 (print-axiom-brief as) (princ " ."))))
204 (when axs
205 (print-next)
206 (format stream "-- axiom~p to be proved" (length axs))
207 (dolist (ax axs)
208 (let ((*print-indent* (+ 2 *print-indent*)))
209 (print-next)
210 (print-axiom-brief ax) (princ " ."))))
211 (format stream "~%}")
212 (if discharged
213 (format t " << proved >>"))))))
208214
209215 ;;; -------------------------------------------------------------------------
210216 ;;; PTREE-NODE
234240 ;;; initialize-ptree-node : ptree-node -> ptree-node
235241 ;;; discard existing child nodes.
236242 ;;;
237 (defun initialize-ptree-node (node)
238 (when (ptree-node-subnodes node)
239 (with-output-chaos-warning ()
240 (format t "Discarding exsisting ~d node~p"
241 (ptree-node-num-children node)
242 (length (ptree-node-subnodes node))))
243 (setf (ptree-node-num-children node) 0
244 (ptree-node-subnodes node) nil)
245 node))
243 (defun initialize-ptree-node (node &optional (no-warn nil))
244 (unless no-warn
245 (when (ptree-node-subnodes node)
246 (with-output-chaos-warning ()
247 (format t "Discarding exsisting ~d node~p"
248 (ptree-node-num-children node)
249 (length (ptree-node-subnodes node))))))
250 (setf (ptree-node-num-children node) 0
251 (ptree-node-subnodes node) nil)
252 node)
246253
247254 ;;;
248255 ;;; node-is-discharged? : ptree-node -> Bool
255262 (or (null (goal-targets goal))
256263 (and (ptree-node-subnodes node)
257264 (every #'(lambda (x) (node-is-discharged? x)) (ptree-node-subnodes node))))))
265
266 ;;; make-it-unproved : ptree-node -> ptree-node'
267 ;;;
268 (defun make-it-unproved (ptree-node)
269 (let ((goal (ptree-node-goal ptree-node)))
270 (setf (goal-targets goal) (append (goal-targets goal) (goal-proved goal)))))
258271
259272 ;;; make-ptree-goal-name : ptree-node fixnum -> string
260273 ;;; goal has a name.
302315 next-goal)))
303316
304317 ;;;
318 ;;; give-goal-name-each-in-order : ptree-node List(goal) -> void
319 ;;; this is used for renaming goals and their context modules
320 ;;; after applied a tactic.
321 ;;;
322 (defun give-goal-name-each-in-order (parent-node list-goals)
323 (dolist (goal list-goals)
324 (let* ((gname (make-ptree-goal-name parent-node (incf (ptree-node-num-children parent-node))))
325 (mod-name (make-next-context-module-name gname)))
326 (setf (goal-name goal) gname)
327 (setf (module-name (goal-context goal)) mod-name))))
328
329 ;;;
305330 ;;; make-ptree-root : module goal -> ptree-node
306331 ;;;
307332 (defun make-ptree-root (context-module initial-goals)
319344 (defun add-ptree-child (parent-node child-goal)
320345 (declare (type ptree-node parent-node)
321346 (type goal child-goal))
322 ;; (incf (ptree-node-num-children parent-node)) ; this is done in prepare-next-goal
323347 (setf (ptree-node-subnodes parent-node)
324348 (nconc (ptree-node-subnodes parent-node)
325349 (list (make-ptree-node :datum child-goal
327351 :subnodes nil
328352 :parent parent-node)))))
329353
330 ;;;
354 ;;; add-ptree-children : ptree-node List(goal) -> ptree-node'
355 ;;; add node of given goals as a child of the node.
356 ;;; before adding goal nodes, initialize the node and
357 ;;; give each goal a name.
358 ;;;
359 (defun add-ptree-children (parent-node list-goals)
360 (declare (type ptree-node parent-node)
361 (type list list-goals))
362 (initialize-ptree-node parent-node t)
363 ;; give names to goals
364 (give-goal-name-each-in-order parent-node list-goals)
365 (dolist (goal list-goals)
366 (add-ptree-child parent-node goal))
367 parent-node)
368
331369 ;;; get-ptree-root : ptree-node -> ptree-node
332370 ;;;
333371 (defun get-ptree-root (ptree-node)
368406 (when unp
369407 (format t "~%>> Next target goal is ~s." (goal-name (ptree-node-goal (car unp))))
370408 (setq *next-default-proof-node* (car unp))
409 (format t "~%>> Remaining ~d goal~p." (length unp) (length unp))
371410 (return-from check-success nil))
372411 (format t "~%** All goals are successfully discharged.")
373412 (setq *next-default-proof-node* nil)
481520 (format t "~%>> next default-goal is ~s" (goal-name (ptree-node-goal next)))))
482521 (t (let ((node (find-goal-node *proof-tree* goal-name)))
483522 (when (node-is-discharged? node)
484 (with-output-chaos-error ('already-discharged)
485 (format t "The goal ~s is alreday discharged." (goal-name (ptree-node-goal node)))))
523 (with-output-chaos-warning ()
524 (format t "The goal ~s is alreday discharged." (goal-name (ptree-node-goal node)))
525 (print-next)
526 (format t "This will discard the current result of the goal."))
527 (make-it-unproved node))
486528 (setq *next-default-proof-node* node)
487529 (format t "~%>> setting next default goal to ~s" (goal-name (ptree-node-goal node)))
488530 node))))
507549 ;;;
508550 ;;; print-proof-tree
509551 ;;;
510 (defun print-proof-tree (&optional (describe nil))
552 (defun print-proof-tree (goal-name &optional (describe nil))
511553 (unless *proof-tree*
512554 (with-output-chaos-warning ()
513555 (format t "There is no proof tree.")
514556 (return-from print-proof-tree nil)))
515 (if describe
516 (describe-proof-tree (ptree-root *proof-tree*))
517 (!print-proof-tree (ptree-root *proof-tree*) (get-next-proof-context *proof-tree*))))
557 (let ((target-node (if goal-name
558 (or (find-goal-node *proof-tree* goal-name)
559 (with-output-chaos-error ('no-such-goal)
560 (format t "No goal with the name ~s." goal-name)))
561 (ptree-root *proof-tree*))))
562 (if describe
563 (describe-proof-tree target-node)
564 (!print-proof-tree target-node (get-next-proof-context *proof-tree*)))))
518565
519566 (defun !print-proof-tree (root-node next-target &optional (stream *standard-output*))
520567 (let* ((leaf? #'(lambda (node) (null (dag-node-subnodes node))))