Debian Codegraph @

thstuff/apply-tactic.lisp

raw
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:apply-tactic.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 ;;; UTILITIES
42 ;;; ****************************************************************************
43
44 ;;; distribute-sentences : ptree-node List(axiom) -> List(goal)
45 ;;; if there are multiple sentences, distribute them into newly genereted goals for each
46 ;;;
47 (defun distribute-sentences (parent axioms tactic)
48 (declare (type ptree-node parent))
49 (let ((new-goals nil)
50 (goal nil))
51 (cond ((cdr axioms)
52 (dolist (ax axioms)
53 (setq goal (prepare-next-goal parent tactic))
54 (setf (goal-targets goal) (list ax))
55 (push goal new-goals)))
56 (t (push (ptree-node-goal parent) new-goals)))
57 (nreverse new-goals)))
58
59 ;;; rule-copy-canonicalized : rule module -> rule
60 ;;; copy rule with all variables are renewed and noralized.
61 ;;;
62 (defun remove-nonexec (rule)
63 (let ((l nil))
64 (dolist (lb (rule-labels rule))
65 (unless (member lb non-exec-labels)
66 (push lb l)))
67 (setf (rule-labels rule) (nreverse l))
68 (setf (rule-non-exec rule) nil)
69 rule))
70
71 (defun rule-copy-canonicalized (rule module &optional label remove-non-exec)
72 (with-in-module (module)
73 (let* ((new-rule (if remove-non-exec
74 (remove-nonexec (rule-copy rule))
75 (rule-copy rule)))
76 (canon (canonicalize-variables (list (rule-lhs new-rule)
77 (rule-rhs new-rule)
78 (rule-condition new-rule))
79 module)))
80 (setf (rule-lhs new-rule) (first canon)
81 (rule-rhs new-rule) (second canon)
82 (rule-condition new-rule) (third canon))
83 (when label
84 (setf (rule-labels new-rule) (append (rule-labels rule) (list label))))
85 new-rule)))
86
87 ;;; apply-substitution-to-axiom : subst axiom [label] [add] -> axiom'
88 ;;;
89 (defun apply-substitution-to-axiom (subst axiom &optional label add)
90 (setf (rule-lhs axiom) (substitution-image-simplifying subst (rule-lhs axiom))
91 (rule-rhs axiom) (substitution-image-simplifying subst (rule-rhs axiom))
92 (rule-condition axiom) (if (is-true? (rule-condition axiom))
93 *bool-true*
94 (substitution-image-simplifying subst (rule-condition axiom))))
95 (when (and label (atom label))
96 (setf label (list label)))
97 (let ((new-label (if add
98 (append label
99 (rule-labels axiom))
100 ;; overwrite, but preserves :nonexec
101 (if (axiom-non-exec axiom)
102 (append label :nonexec)
103 label))))
104 (setf (rule-labels axiom) new-label)
105 axiom))
106
107 ;;; copy-constant-term
108 ;;;
109 (defun copy-constant-term (constant)
110 (make-applform (term-sort constant) (term-head constant) nil))
111
112 ;;; select-comb-elems : List(List) -> List
113 ;;;
114 (defun select-combs-aux (max-idx list-of-list)
115 (declare (type fixnum max-idx)
116 (type list list-of-list))
117 (let* ((result nil)
118 (target (car list-of-list))
119 (rest (cdr list-of-list))
120 (len (length target)))
121 (declare (type fixnum len)
122 (type list result target rest))
123 (if target
124 (let ((idx 0))
125 (declare (type fixnum idx))
126 (while (< idx max-idx)
127 (let ((elt (nth (mod idx len) target))
128 (rr (select-combs-aux max-idx rest)))
129 (if rr
130 (dolist (r rr)
131 (pushnew (cons elt r) result :test #'equal))
132 (pushnew (list elt) result :test #'equal))
133 (incf idx)))
134 (nreverse result))
135 nil)))
136
137 (defun select-combs-aux-indexed (max-idx list-of-list index)
138 (declare (type fixnum max-idx index)
139 (type list list-of-list))
140 (let* ((result nil)
141 (target (car list-of-list))
142 (rest (cdr list-of-list))
143 (len (length target)))
144 (declare (type fixnum len)
145 (type list result target rest))
146 (if target
147 (let ((idx 0))
148 (declare (type fixnum idx))
149 (while (< idx max-idx)
150 (let ((elt (nth (mod idx len) target))
151 (rr (select-combs-aux-indexed max-idx rest (1+ index))))
152 (if rr
153 (dolist (r rr)
154 (pushnew (cons (cons index elt) r) result :test #'equal))
155 (pushnew (list (cons index elt)) result :test #'equal))
156 (incf idx)))
157 (nreverse result))
158 nil)))
159
160 (defun select-comb-elems (list-of-list &optional (indexed nil))
161 (declare (type list list-of-list))
162 (unless list-of-list (return-from select-comb-elems nil))
163 (let ((max-idx (apply #'max (mapcar #'(lambda (x) (length x)) list-of-list))))
164 (declare (type fixnum max-idx))
165 (if indexed
166 (select-combs-aux-indexed max-idx list-of-list 0)
167 (select-combs-aux max-idx list-of-list))))
168
169 ;;; axiom-variables : axiom -> List(variable)
170 ;;; returns a list of variables contained in the given axiom
171 ;;;
172 (defun axiom-variables (ax)
173 (let ((lhs (axiom-lhs ax))
174 (rhs (axiom-rhs ax))
175 (cond (axiom-condition ax))
176 (result nil))
177 (declare (type list result))
178 (setq result (append (term-variables lhs)
179 (append (term-variables rhs)
180 (term-variables cond))))
181 (delete-duplicates result :test #'variable-equal)))
182
183 ;;; normalize-term-in : module term -> term Bool
184 ;;; reduces the ground terms in given term by rewriting.
185 ;;; if rewritten, returned term is distructively changed.
186 ;;;
187 (defun normalize-term-in (module term &optional (reduction-mode :red) var-is-const)
188 (let ((applied? nil)
189 (rule-count-save (number-rewritings))
190 (*variable-as-constant* var-is-const))
191 (declare (type fixnum rule-count-save)
192 (optimize (speed 3) (safety 0)))
193 (reducer-no-stat term module reduction-mode)
194 (unless (= rule-count-save (the fixnum (number-rewritings)))
195 (setq applied? t))
196 (values term applied?)))
197
198 ;;; normalize-sentence : axiom module -> axiom' Bool
199 ;;; normalize an axiom by reduction, returns the result.
200 ;;; NOTE: given axiom is preserved (not changed).
201 ;;;
202 (defun normalize-sentence (ax module &optional lhs-only variable-is-constant)
203 (declare (type rewrite-rule ax)
204 (type module module)
205 (type (or null t) lhs-only variable-is-constant)
206 (optimize (speed 3) (safety 0)))
207 (if-spoiler-on
208 ;; normalize sentence only if :spoiler is on
209 :then (let ((target (rule-copy-canonicalized ax module)))
210 (with-in-module (module)
211 (let ((lhs (rule-lhs target))
212 (rhs (rule-rhs target))
213 (condition (rule-condition target))
214 (reduction-mode (if (eq (rule-type ax) :equation)
215 :red
216 :exec))
217 (applied nil)
218 (app? nil))
219 (flet ((set-applied (val)
220 (or app? (setq app? val))))
221 (with-citp-debug ()
222 (with-in-module (module)
223 (format t "~%[NF] target:")
224 (print-next)
225 (print-axiom-brief target)))
226 (let ((rsubst nil))
227 ;; variables->pconst if requred
228 (when variable-is-constant
229 (setq rsubst (nconc (variables->pconstants lhs)
230 (nconc (variables->pconstants rhs)
231 (variables->pconstants condition)))))
232 ;; normalize lhs
233 (multiple-value-setq (lhs applied)
234 (normalize-term-in module (reset-reduced-flag lhs) :red))
235 (set-applied applied)
236 (when (eq reduction-mode :exec)
237 (multiple-value-setq (lhs applied)
238 (normalize-term-in module (reset-reduced-flag lhs) :exec))
239 (set-applied applied))
240 ;; normalize rhs
241 (unless lhs-only
242 (multiple-value-setq (rhs applied)
243 (normalize-term-in module (reset-reduced-flag rhs)))
244 (set-applied applied))
245 ;; normalize condition
246 (unless (or lhs-only (is-true? condition))
247 (multiple-value-setq (condition applied)
248 (normalize-term-in module (reset-reduced-flag condition) :red))
249 (set-applied applied))
250 ;; pconsts -> variables
251 (when rsubst
252 (revert-pconstants lhs rsubst)
253 (revert-pconstants rhs rsubst)
254 (revert-pconstants condition rsubst))
255 (setf (rule-lhs target) lhs)
256 (setf (rule-rhs target) rhs)
257 (setf (rule-condition target) condition)
258 (with-citp-debug ()
259 (if (not app?)
260 (format t "~% ...not applied: ")
261 (progn
262 (print-next)
263 (princ "==> ") (print-axiom-brief target))))
264 (return-from normalize-sentence (values target app?)))))))
265 ;; do nothing if :spoiler is off
266 :else (values ax nil)))
267
268 ;;; is-contradiction : term term -> Bool
269 ;;; returns true if true ~ false, or false ~ true
270 ;;;
271 (defun is-contradiction (t1 t2)
272 (declare (ignore t1 t2))
273 nil)
274
275 ;;; sentence-is-satisfied : axiom module -> { :satisfied | :ct | nil }
276 ;;;
277 (defun sentence-is-satisfied (sentence module)
278 (let ((old-condition (rule-condition sentence)))
279 (multiple-value-bind (norm-sen app?)
280 (normalize-sentence sentence module)
281 (declare (ignore app?))
282 (let ((lhs (rule-lhs norm-sen))
283 (rhs (rule-rhs norm-sen))
284 (condition (rule-condition norm-sen))
285 (result nil))
286 (with-citp-debug ()
287 (format t "~%[satisfied?]: ")
288 (print-axiom-brief norm-sen))
289 (cond ((and (not (is-true? old-condition)) (is-true? condition))
290 (if (is-contradiction lhs rhs)
291 (setq result :ct)
292 (setq result :st)))
293 ((is-true? condition) ; originally the axiom was non-conditional
294 (if (is-contradiction lhs rhs)
295 (setq result :ct)
296 (let ((x-lhs (normalize-term-in module (reset-reduced-flag lhs)))
297 (x-rhs (normalize-term-in module (reset-reduced-flag rhs))))
298 (when (term-equational-equal x-lhs x-rhs)
299 (setq result :st)))))
300 (t (setq result nil)))
301 (with-citp-debug ()
302 (format t "~% --> ~a: " result)
303 (print-next)
304 (print-axiom-brief norm-sen))
305 (values result norm-sen)))))
306
307 ;;; check-contradiction : goal -> Bool
308 ;;; check if
309 ;;; * 'true => false' or
310 ;;; * 'false => true' or
311 ;;; * (t = t) = false
312 (defun check-true<=>false (module &optional (report-header nil))
313 (with-in-module (module)
314 (let ((t-rules (method-rules-with-different-top *bool-true-meth*))
315 (f-rules (method-rules-with-different-top *bool-false-meth*))
316 (ct-rule nil))
317 (dolist (rule (append t-rules f-rules))
318 (with-citp-debug ()
319 (format t "~%check true<=> false")
320 (print-next)
321 (print-axiom-brief rule))
322 (when (or (is-true? (rule-condition rule))
323 (is-true? (normalize-term-in module
324 (reset-reduced-flag (term-copy-and-returns-list-variables
325 (rule-condition rule))))))
326 (unless (term-equational-equal (rule-lhs rule) (rule-rhs rule))
327 (setq ct-rule rule)
328 (with-citp-debug ()
329 (format t "~% CT found!"))
330 (return nil))))
331 (when (and ct-rule report-header)
332 (format t "~%[~a] contradiction: " report-header)
333 (let ((*print-indent* (+ 2 *print-indent*)))
334 (print-next)
335 (print-axiom-brief ct-rule)))
336 ct-rule)))
337
338 (defun check-contradictory-assumptions (goal &optional (report-header nil))
339 (let ((ams (goal-assumptions goal))
340 (contra? nil))
341 (with-in-module ((goal-context goal))
342 (dolist (ax ams)
343 (when (or (and (is-false? (rule-rhs ax))
344 (is-true? (rule-lhs ax)))
345 (and (is-false? (rule-lhs ax))
346 (is-true? (rule-rhs ax))))
347 (when report-header
348 (format t "~%[~a] contradictory assumption: " report-header)
349 (print-next)
350 (print-axiom-brief ax))
351 (setf contra? t)))
352 contra?)))
353
354 (defun check-contradiction (goal &optional (report-header nil))
355 (let ((module (goal-context goal)))
356 (or (check-true<=>false module report-header)
357 (check-contradictory-assumptions goal report-header)
358 (with-in-module (module)
359 (let ((true-term (make-applform *bool-sort* *bool-true-meth* nil))
360 (false-term (make-applform *bool-sort* *bool-false-meth* nil)))
361 (let ((true=false (make-applform *bool-sort* *eql-op* (list true-term false-term))))
362 (multiple-value-bind (t-result t-applied?)
363 (normalize-term-in module true=false)
364 (when (and t-applied? (is-true? t-result))
365 (when report-header
366 (format t "~%[~a] contradiction: " report-header)
367 (print-next)
368 (format t " `true = false' can be derived!"))
369 (return-from check-contradiction t))))))
370 nil)))
371
372 ;;; check-le : goal -> goal'
373 ;;;
374 (defun check-le (goal)
375 (let ((mod (goal-context goal)))
376 (with-in-module (mod)
377 (let ((axs (module-equations mod))
378 (ls-pats nil)
379 (le-pats nil))
380 (dolist (ax axs)
381 (block next
382 (unless (is-true? (rule-condition ax)) (return-from next))
383 (when (axiom-variables ax) (return-from next))
384 (let ((lhs (rule-lhs ax)))
385 (multiple-value-bind (match? subst)
386 (@pat-match .ls-pat. lhs)
387 (cond (match? (push (cons (term-arg-1 lhs) (term-arg-2 lhs)) ls-pats))
388 (t (multiple-value-setq (match? subst)
389 (@pat-match .le-pat. lhs))
390 (when match? (push (cons (term-arg-1 lhs) (term-arg-2 lhs)) le-pats))))))))
391 (let ((ls-r nil)
392 (le-r nil))
393 (dolist (ls ls-pats)
394 (let ((ls-pair (find (cdr ls) ls-pats :key #'car :test #'term-equational-equal))
395 (le-pair (find (cdr ls) le-pats :key #'car :test #'term-equational-equal)))
396 ;; G1 < G2 < G3
397 (when ls-pair (push (cons (car ls) (cdr ls-pair)) ls-r)) ; < check G3 < G1
398 ;; G1 < G2 <= G3
399 (when le-pair (push (cons (car ls) (cdr le-pair)) le-r)))) ; <= check G3 <= G1
400 (dolist (le le-pats)
401 (let ((ls-pair (find (cdr le) ls-pats :key #'car :test #'term-equational-equal))
402 (le-pair (find (cdr le) le-pats :key #'car :test #'term-equational-equal)))
403 ;; G1 <= G2 < G3
404 (when ls-pair (push (cons (car le) (cdr ls-pair)) le-r)) ; check G3 <= G1
405 (when le-pair (push (cons (car le) (cdr le-pair)) ls-r)))) ; check G3 < G1
406 ;;
407 (with-citp-debug ()
408 (format t "~%[le] check in goal ~s: " (goal-name goal))
409 (dolist (ls ls-r)
410 (print-next)
411 (term-print (cdr ls)) (princ " < ")
412 (term-print (car ls)))
413 (dolist (le le-r)
414 (print-next)
415 (term-print (cdr le)) (princ " <= ")
416 (term-print (car le))))
417 (flet ((do-check (pat op)
418 (dolist (ls pat)
419 (let ((rg (make-applform *bool-sort*
420 op
421 (list (cdr ls) (car ls)))))
422 (with-citp-debug ()
423 (format t "~% target term : ")
424 (term-print-with-sort rg))
425 (when (is-true? (normalize-term-in *current-module* rg))
426 ;; discharge the goal
427 (let ((target (rule-copy-canonicalized (car (goal-targets goal))
428 *current-module*)))
429 (setf (rule-labels target) (cons 'le (rule-labels target)))
430 (setf (goal-targets goal) nil)
431 (setf (goal-proved goal) (list target))
432 (format t "~%[le] discharged the goal ~s" (goal-name goal)))
433 (return nil))))))
434 (do-check ls-r (term-head .ls-pat.))
435 (do-check le-r (term-head .le-pat.))))))))
436
437 ;;; make-new-assumption : module lhs rhs -> new-lhs new-rhs axiom-type
438 ;;;
439 (defun boolean-constant? (term)
440 (or (is-true? term)(is-false? term)))
441
442 (defun is-builtin= (op)
443 (or (method= *eql-op* op)
444 (memq *eql-op* (method-overloaded-methods op))))
445
446 (defun simplify-boolean-axiom (lhs rhs)
447 (let ((r-lhs lhs)
448 (r-rhs rhs)
449 (type :equation))
450 (with-citp-debug ()
451 (format t "~%== simplify: ")
452 (format t "~% lhs = ")(term-print-with-sort lhs)
453 (format t "~% rhs = ")(term-print-with-sort rhs))
454 (cond ((is-builtin= (term-head lhs))
455 (with-citp-debug ()
456 (format t "~%** case _=_"))
457 ;; (T1 = T2) = true/false ==> T1 = T2
458 (let* ((arg1 (term-arg-1 lhs))
459 (arg2 (term-arg-2 lhs))
460 (arg1-is-bconstant (boolean-constant? arg1))
461 (arg2-is-bconstant (boolean-constant? arg2)))
462 (cond ((is-true? rhs)
463 (with-citp-debug ()
464 (format t "~%-- (T1 = T2) = true"))
465 ;; (T1 = T2) = true
466 (cond ((term-equational-equal arg1 arg2)
467 ;; (T1 = T1) = true : dangerous tautology
468 (with-citp-debug ()
469 (format t "~%-- (T = T) = true, tautology."))
470 (let ((*chaos-quiet* nil))
471 (with-output-chaos-warning ()
472 (format t "Found the new assumption is tautology:")
473 (print-next)
474 (format t "LHS: ") (term-print-with-sort arg1)
475 (print-next)
476 (format t "RHS: ") (term-print-with-sort arg2)
477 (format t "~%... ignored.")))
478 (setf r-lhs nil
479 r-rhs nil))
480 ((and arg1-is-bconstant arg2-is-bconstant)
481 (with-citp-debug ()
482 (format t "~%-- (true = false) = true, (false = true) = true."))
483 ;; (true = false), (false = true) = true .
484 ;; contradiction
485 (setf r-lhs arg1
486 r-rhs arg2)
487 (let ((*print-indent* (+ 2 *print-indent*)))
488 (let ((*chaos-quiet* nil))
489 (with-output-chaos-warning ()
490 (format t "Caution!, you are introducing contradiction:")
491 (print-next)
492 (format t "LHS: ") (term-print-with-sort r-lhs)
493 (print-next)
494 (format t "RHS: ") (term-print-with-sort r-rhs)))))
495 (t
496 ;; (T1 = T2) = true --> T1 = T2
497 (with-citp-debug ()
498 (format t "~% trying to simplify.."))
499 (setf r-lhs (if arg1-is-bconstant
500 arg2
501 arg1))
502 (setf r-rhs (if arg1-is-bconstant
503 arg1
504 arg2)))))
505 ((is-false? rhs)
506 ;; (T1 = T2) = false
507 (with-citp-debug ()
508 (format t "~%-- (T1 = T2) = false"))
509 (cond ((term-equational-equal arg1 arg2)
510 ;; (T = T) = false
511 ;; contradiction
512 (with-citp-debug ()
513 (format t "~% (T = T) = false, contradiction!"))
514 (let ((*print-indent* (+ 2 *print-indent*))
515 (*chaos-quiet* nil))
516 (with-output-chaos-warning ()
517 (format t "Caution! you are introducing contradiction:")
518 (print-next)
519 (format t "LHS: ") (term-print-with-sort lhs)
520 (print-next)
521 (format t "RHS: ") (term-print-with-sort rhs))))
522 ((and arg1-is-bconstant arg2-is-bconstant)
523 ;; (true = false) = false, (false = true) = false
524 (with-citp-debug ()
525 (format t "~%(true = false) = false, (false = true) = false"))
526 (let ((*print-indent* (+ 2 *print-indent*))
527 (*chaos-quiet* nil))
528 (with-output-chaos-warning ()
529 (format t "Redundant assumption: ")
530 (print-next)
531 (format t "LHS: ") (term-print-with-sort lhs)
532 (print-next)
533 (format t "RHS: ") (term-print-with-sort rhs))
534 (format t "~%... ignored."))
535 (setf r-lhs nil
536 r-rhs nil))
537 (t
538 ;;
539 (with-citp-debug ()
540 (format t "-- trying to simplify.."))
541 (if (is-true? arg1)
542 (setf r-lhs arg2
543 r-rhs *bool-false*)
544 (if (is-true? arg2)
545 (setf r-lhs arg1
546 r-rhs *bool-false*)
547 (if (is-false? arg1)
548 (setf r-lhs arg2
549 r-rhs *bool-true*)
550 (if (is-false? arg2)
551 (setf r-lhs arg1
552 r-rhs *bool-true*)
553 (setf r-lhs lhs
554 r-rhs rhs)))))))))))
555 ((method= *bool-match* (term-head lhs))
556 ;; (T1 := T2) = true ==> T2 = T1
557 (setf r-lhs (term-arg-2 lhs)
558 r-rhs (term-arg-1 lhs)))
559 ((method= *rwl-predicate* (term-head lhs))
560 ;; (T1 => T2) = true ==> T1 => T2
561 (setf r-lhs (term-arg-1 lhs)
562 r-rhs (term-arg-2 lhs))
563 (setq type :rule)))
564
565 (with-citp-debug ()
566 (when r-lhs
567 (format t "~%=> ")
568 (format t "~%LHS: ")(term-print-with-sort r-lhs)
569 (format t "~%RHS: ")(term-print-with-sort r-rhs)
570 (format t "~%type: ~a" type)))
571 (if r-lhs
572 (values r-lhs r-rhs type)
573 (values nil nil nil))))
574
575 (defun make-new-assumption (module lhs &optional (label-prefix nil))
576 (with-in-module (module)
577 (let ((r-lhs lhs)
578 (r-rhs *bool-true*)
579 (type :equation))
580 (when (is-builtin= (term-head lhs))
581 ;; (T1 = T2) = true ==> T1 = T2
582 (setf r-lhs (term-arg-1 lhs)
583 r-rhs (term-arg-2 lhs)))
584 (when (method= *bool-match* (term-head lhs))
585 ;; (T1 := T2) = true ==> T2 = T1
586 (setf r-lhs (term-arg-2 lhs)
587 r-rhs (term-arg-1 lhs)))
588 (when (method= *rwl-predicate* (term-head lhs))
589 ;; (T1 => T2) = true ==> T1 => T2
590 (setf r-lhs (term-arg-1 lhs)
591 r-rhs (term-arg-2 lhs))
592 (setq type :rule))
593 (compile-module module)
594 (let ((axiom (make-rule :lhs (normalize-term-in module (reset-reduced-flag r-lhs))
595 :rhs (normalize-term-in module (reset-reduced-flag r-rhs))
596 :condition *bool-true*
597 :type type
598 :behavioural nil
599 :labels (if label-prefix (list label-prefix) nil))))
600 ;; check tautology
601 (when (term-equational-equal r-lhs r-rhs)
602 (return-from make-new-assumption nil))
603 (with-citp-debug ()
604 (format t "~%** new assumption: ")
605 (print-axiom-brief axiom))
606 axiom))))
607
608 ;;; condition->axioms : module term -> List(axiom)
609 ;;;
610 (defun condition->axioms (module condition &optional (rule-label nil))
611 (with-in-module (module)
612 (let ((axs nil)
613 (cps nil))
614 (if (method= *bool-cond-op* (term-head condition))
615 (let ((subs (list-assoc-subterms condition *bool-cond-op*)))
616 (dolist (sub subs)
617 (push (term-copy-and-returns-list-variables sub) cps)))
618 (setq cps (list (term-copy-and-returns-list-variables condition))))
619 (dolist (c cps)
620 (let ((new-ax (make-new-assumption module c rule-label)))
621 (when new-ax
622 (compute-rule-method new-ax)
623 (pushnew new-ax axs :test #'rule-is-similar?))))
624 (with-citp-debug ()
625 (format t "~%[~a] generated axioms:" rule-label)
626 (dolist (ax axs)
627 (print-next)
628 (print-axiom-brief ax)))
629 axs)))
630
631 (defun axiom-is-an-instance-of (ax cx module)
632 (with-in-module (module)
633 (with-citp-debug ()
634 (print-next)
635 (format t "* ax: ") (print-axiom-brief ax)
636 (print-next)
637 (format t "* cx: ") (print-axiom-brief cx))
638 (multiple-value-bind (gs subst no-match E-equal)
639 (funcall (rule-first-match-method cx) (rule-lhs cx) (rule-lhs ax))
640 (when no-match (return-from axiom-is-an-instance-of nil))
641 (when e-equal (setq subst nil))
642 (let ((pat-instance (substitution-image-simplifying subst (rule-rhs cx)))
643 (t-instance (rule-rhs ax))
644 (next-match-method nil))
645 (with-citp-debug ()
646 (format t "~%* matched: ")
647 (print-substitution subst)
648 (print-next)
649 (format t "pat: ") (term-print-with-sort pat-instance)
650 (print-next)
651 (format t "rhs: ") (term-print-with-sort t-instance))
652
653 (when (term-equational-equal t-instance pat-instance)
654 (return-from axiom-is-an-instance-of t))
655 ;; try other match
656 (setq next-match-method (rule-next-match-method cx))
657 (loop
658 (multiple-value-setq (gs subst no-match)
659 (funcall next-match-method gs))
660 (when no-match (return-from axiom-is-an-instance-of nil))
661 (setq pat-instance (substitution-image-simplifying subst (rule-rhs cx)))
662 (when (term-equational-equal t-instance pat-instance)
663 (return-from axiom-is-an-instance-of t))))
664 nil)))
665
666 (defun check-ct-with-axioms (goal axioms &optional report-header)
667 (declare (type goal goal)
668 (type list axioms))
669 (with-in-module ((goal-context goal))
670 (let ((tf-rules (append (method-rules-with-different-top *bool-true-meth*)
671 (method-rules-with-different-top *bool-false-meth*))))
672 ;; first do light weight check
673 (dolist (rule tf-rules)
674 (when (is-true? (rule-condition rule))
675 ;; already CT
676 (when report-header
677 (format t "~%[~a] found contradiction: " report-header)
678 (print-axiom-brief rule))
679 (return-from check-ct-with-axioms :ct)))
680 (dolist (rule tf-rules)
681 (let ((cond-axioms (condition->axioms *current-module* (rule-condition rule))))
682 (let ((remaining cond-axioms))
683 (do* ((cax (car remaining) (car remaining))
684 (axs axioms (cdr axs))
685 (ax (car axs) (car axs)))
686 ((or (null cax) (null axs)))
687 (when (axiom-is-an-instance-of ax cax *current-module*)
688 (setq remaining (remove cax remaining))))
689 (unless remaining
690 (when report-header
691 (format t "~%[~a] found contradiction: " report-header)
692 (print-axiom-brief rule))
693 (return-from check-ct-with-axioms :ct)))))
694 nil)))
695
696 ;;; check-sentence&mark-label : sentence module -> (<result> <normalized-sentence> <origina-sentence>)
697 ;;;
698 (defun check-sentence&mark-label (sentence goal &optional (report-header nil))
699 (flet ((make-st-label ()
700 (let ((lbl (or report-header 'st)))
701 (cons lbl (rule-labels sentence))))
702 (make-ct-label ()
703 (let ((lbl (if report-header
704 (intern (format nil "CT-~A" report-header))
705 'ct)))
706 (cons lbl (rule-labels sentence))))
707 (make-ic-label ()
708 (let ((lbl (if report-header
709 (intern (format nil "IC-~A" report-header))
710 'ic)))
711 (cons lbl (rule-labels sentence)))))
712 ;;
713 (let ((module (goal-context goal)))
714 (with-in-module (module)
715 (let ((target sentence)
716 (res nil)
717 (*print-indent* (+ 2 *print-indent*))
718 (*print-line-limit* 80)
719 (*print-xmode* *print-xmode*))
720 (if (check-contradiction goal report-header)
721 (setq res :ct)
722 (multiple-value-setq (res target)
723 (sentence-is-satisfied sentence module)))
724 (when res
725 ;; discharged by certain reson
726 (setq sentence (rule-copy-canonicalized sentence *current-module*)))
727 (with-in-module (module)
728 ;; check how did we did dischage
729 (case res
730 (:st (when report-header
731 (format t "~%[~a] discharged: " report-header)
732 (print-next)
733 (print-axiom-brief sentence))
734 (setf (rule-labels sentence) (make-st-label))
735 (values :st target sentence))
736 (:ct (when report-header
737 (format t "~%[~a] discharged: " report-header)
738 (print-next)
739 (print-axiom-brief sentence))
740 (setf (rule-labels sentence) (make-ct-label))
741 (values :ct target sentence))
742 (:ic (when report-header
743 (format t "~%[~a] discharged: " report-header)
744 (print-next)
745 (print-axiom-brief sentence))
746 (setf (rule-labels sentence) (make-ic-label))
747 (values :ic target sentence))
748 ;; could not discharge
749 (otherwise (values nil target sentence)))))))))
750
751 ;;; set-operator-rewrite-rule : module axiom -> void
752 ;;;
753 (defun set-operator-rewrite-rule (module axiom)
754 (check-axiom-validity axiom module)
755 (when (and (not (axiom-non-exec axiom))
756 (term-is-applform? (rule-lhs axiom)))
757 (add-rule-to-method axiom (term-head (rule-lhs axiom)) (module-opinfo-table module))))
758
759 ;;; add-assumptions-to-goal : goal assumptions -> void
760 ;;;
761 (defun add-assumptions-to-goal (goal assumptions)
762 (let ((module (goal-context goal)))
763 (with-in-module (module)
764 (dolist (ax assumptions)
765 (adjoin-axiom-to-module module ax)
766 (set-operator-rewrite-rule module ax))
767 (compile-module module t))))
768
769 ;;; check-goal-is-satisfied : goal -> ( <result> <normalized-target> <possibly-marked-target> )
770 ;;;
771 (defun check-goal-is-satisfied (goal &optional (report-header nil))
772 (when (cdr (goal-targets goal))
773 (with-output-chaos-error ('invalid-proof-seq)
774 (format t "Internal error. more than one target!")))
775 (if-spoiler-on
776 :then (let ((target (car (goal-targets goal))))
777 (multiple-value-bind (discharged normalized-target original-target)
778 (do-check-sentence target goal report-header)
779 (when discharged
780 (setf (goal-targets goal) nil
781 (goal-proved goal) (list original-target)))
782 (values discharged normalized-target original-target)))
783 :else (values nil nil (car (goal-targets goal)))))
784
785 (defun make= (lhs rhs mod)
786 (let ((eq-form (make-applform *bool-sort*
787 *eql-op* ; _=_
788 (list lhs rhs))))
789 (with-in-module (mod)
790 (update-lowest-parse eq-form))))
791
792 (defun do-check-sentence (target goal &optional report-header)
793 (let ((mod (goal-context goal)))
794 (multiple-value-bind (result norm-target marked-target)
795 (check-sentence&mark-label target goal report-header)
796 (cond (result
797 ;; goal has been dischared already by some reason
798 )
799 ((and (is-true? (rule-condition target))
800 (eq (rule-type target) :equation))
801 (setf target (rule-copy-canonicalized target mod))
802 (setf (rule-lhs target) (make= (rule-lhs target) (rule-rhs target) mod)
803 (rule-rhs target) *bool-true*)
804 (multiple-value-bind (res-2 norm-target-2 marked-target-2)
805 (check-sentence&mark-label target goal report-header)
806 (declare (ignore norm-target-2 marked-target-2))
807 (when res-2
808 (setf result res-2))))
809 (t ;; nothing to do
810 ))
811 (values result norm-target marked-target))))
812
813 ;;; try-prove-with-axioms : module List(axiom) axiom : -> { :satisfied | :ct | nil }
814 ;;;
815 (defparameter .trial-context-module. (%module-decl* "trial-dummy" :object :user nil))
816
817 (defun try-prove-with-axioms (goal axioms target &optional (report-header nil))
818 (let ((module (goal-context goal)))
819 (with-citp-env ()
820 (let ((tmodule (eval-ast .trial-context-module.)))
821 (import-module tmodule :including module)
822 (with-in-module (tmodule)
823 (dolist (ax axioms)
824 (adjoin-axiom-to-module tmodule ax)
825 (set-operator-rewrite-rule tmodule ax))
826 (compile-module tmodule t)
827 ;; first we check contradiction
828 (if (check-contradiction goal report-header)
829 :ct
830 ;; the module is consistent, try
831 (sentence-is-satisfied target tmodule)))))))
832
833 ;;; already-proved? :
834 ;;;
835 (defun already-proved? (node-or-goal &optional (warn t))
836 (declare (type (or ptree-node goal) node-or-goal))
837 (let ((goal (if (ptree-node-p node-or-goal)
838 (ptree-node-goal node-or-goal)
839 node-or-goal)))
840 (unless (goal-is-discharged goal)
841 (return-from already-proved? nil))
842 (when warn
843 (with-output-chaos-warning ()
844 (format t "** The goal ~s has already been proved!."
845 (goal-name goal))))
846 t))
847
848 ;;; ****************************************************************************
849 ;;; Tactic executors
850 ;;; ****************************************************************************
851
852 ;;; ===========
853 ;;; TACTIC: NIL
854 ;;; do nothing, but distribute multiple targets into each new goal.
855 ;;; ===========
856
857 (defun apply-nil (node)
858 (declare (ignore node))
859 (with-output-chaos-warning ()
860 (format t "~%Tactic [NIL] does nothing."))
861 (values nil nil))
862
863 (defun apply-nil-internal (node sentences &optional (all-together nil) (tactic .tactic-nil.))
864 (declare (type ptree-node node)
865 (type list sentences))
866 (let ((goals nil))
867 (cond (all-together
868 (let ((ngoal (prepare-next-goal node tactic)))
869 (setf (goal-targets ngoal) sentences)
870 (push ngoal goals)))
871 (t (dolist (sentence sentences)
872 (let ((ngoal (prepare-next-goal node tactic)))
873 (setf (goal-targets ngoal) (list sentence))
874 (push ngoal goals)))))
875 (values goals (nreverse goals))))
876
877 ;;; =======================
878 ;;; TACTIC: IMPLICATION[IP]
879 ;;; =======================
880
881 (defun generate-ip-derived-axioms (module axiom)
882 (condition->axioms module (axiom-condition axiom) 'ip))
883
884 (defun simplify-eq-form (assmp)
885 ;; (t1 = true) or (true = t1) ==> t1
886 (when (is-builtin= (term-head assmp))
887 (cond ((is-true? (term-arg-1 assmp))
888 (setq assmp (term-arg-2 assmp)))
889 ((is-true? (term-arg-2 assmp))
890 (setq assmp (term-arg-1 assmp)))
891 (t ;; do nothing
892 )))
893 assmp)
894
895 ;;; a or b or c ... or x imply lhs
896 (defun make-impl-form+ (axiom assumptions)
897 (flet ((make-pre (ax)
898 (simplify-eq-form (make-applform *bool-sort*
899 *eql-op*
900 (list (axiom-lhs ax)
901 (axiom-rhs ax)))))
902 (make-post (ax)
903 (let ((op (if (eq (axiom-type ax) :equation)
904 *eql-op*
905 *rwl-predicate*)))
906 (simplify-eq-form (make-applform *bool-sort*
907 op
908 (list (axiom-lhs ax) (axiom-rhs ax)))))))
909 (if (cdr assumptions)
910 (let ((as (mapcar #'make-pre assumptions)))
911 (make-applform *bool-sort*
912 *bool-imply*
913 (list (make-right-assoc-normal-form *bool-and* as)
914 (make-post axiom))))
915 (make-applform *bool-sort*
916 *bool-imply*
917 (list (make-pre (car assumptions))
918 (make-post axiom))))))
919
920 (defun apply-ip (ptree-node &optional (modify-goal nil) &rest ignore)
921 (declare (type ptree-node ptree-node)
922 (ignore ignore))
923 (with-in-context (ptree-node)
924 (let ((original-goal (ptree-node-goal ptree-node)))
925 (flet ((push-next-goal (goal)
926 (unless (eq goal original-goal) (push goal .next-goals.))))
927 (let ((target-goals (distribute-sentences ptree-node .cur-targets. .tactic-ip.)))
928 (dolist (.cur-goal. target-goals)
929 (let ((target (normalize-sentence (car (goal-targets .cur-goal.)) (goal-context .cur-goal.))))
930 (cond ((and (not (is-true? (rule-condition target)))
931 (null (term-variables (rule-condition target))))
932 ;; t = t' if C
933 ;; C is a ground term and is not true.
934 ;; try if (SP + { C } |- t = t') or not..
935 ;; if this is satisfied, discharge it.
936 (let ((ngoal (if (eq .cur-goal. original-goal)
937 (prepare-next-goal ptree-node .tactic-ip.)
938 .cur-goal.)))
939 (with-in-module ((goal-context ngoal))
940 (let ((new-axs (generate-ip-derived-axioms *current-module* target))
941 (next-target (rule-copy-canonicalized target *current-module*)))
942 ;; make the target
943 (cond ((eq modify-goal :modify-goal)
944 ;; introduce implication modifying the goal sentence
945 ;; eq LHS = RHS --> LHS(new-axs) imply LHS = RHS .
946 (let ((new-lhs (make-impl-form+ next-target
947 new-axs))
948 (*print-indent* (+ 2 *print-indent*)))
949 (setf (axiom-lhs next-target) new-lhs)
950 (setf (axiom-rhs next-target) *bool-true*)
951 (setf (axiom-condition next-target) *bool-true*)
952 (setf (axiom-labels next-target)
953 (cons :ip+ (axiom-labels next-target)))
954 (format t "~%[ip+] target sentence is converted ...")
955 (print-next)
956 (princ "=> ")
957 (print-next)
958 (print-axiom-brief next-target)
959 (setf (goal-targets ngoal) (list next-target))))
960 (t
961 ;; introduce implication as hypothesis
962 (setf (rule-condition next-target) *bool-true*)
963 (setf (goal-targets ngoal) (list next-target))
964 ;; add [ip] axioms
965 (dolist (ax new-axs)
966 (adjoin-axiom-to-module *current-module* ax)
967 (set-operator-rewrite-rule *current-module* ax))
968 (setf (goal-assumptions ngoal)
969 (append (goal-assumptions ngoal) (reverse new-axs)))))
970 ;; compile
971 (compile-module *current-module* t)
972 (push-next-goal ngoal)))))
973 (t
974 ;; nothing to do
975 (push-next-goal .cur-goal.)))))
976 ;; done for all goals
977 (setq .next-goals. (nreverse .next-goals.))
978 (dolist (ngoal .next-goals.)
979 (multiple-value-bind (discharged norm-sentence org-sentence)
980 (check-goal-is-satisfied ngoal 'ip)
981 (declare (ignore norm-sentence org-sentence))
982 (when discharged
983 (format t "~%[ip] discharged the goal ~s" (goal-name ngoal)))))
984 ;;
985 (values .next-goals. (nreverse .next-goals.)))))))
986
987 ;;; =========================
988 ;;; TACTIC: IMPLICATION [IP+]
989 ;;; by modifying the goal by 'imply'
990 ;;; =========================
991
992 (defun apply-ip+ (ptree-node &rest ignore)
993 (declare (type ptree-node ptree-node)
994 (ignore ignore))
995 (apply-ip ptree-node :modify-goal))
996
997 ;;; =================================
998 ;;; TACTIC: Theorem of Constants [TC]
999 ;;; =================================
1000
1001 (defun make-tc-variable-substitutions (goal vars)
1002 (declare (type goal goal)
1003 (type list vars))
1004 (let ((subst nil))
1005 (dolist (var vars)
1006 (push (cons var (variable->constant goal var)) subst))
1007 (with-citp-debug ()
1008 (format t "~%[tc] variable substitution:")
1009 (print-next)
1010 (print-substitution subst))
1011 subst))
1012
1013 (defun apply-tc (ptree-node &rest ignore)
1014 (declare (type ptree-node ptree-node)
1015 (ignore ignore))
1016 (with-in-context (ptree-node)
1017 (let ((original-goal .cur-goal.))
1018 (flet ((push-next-goal (goal)
1019 (unless (eq goal original-goal) (push goal .next-goals.))))
1020 (let ((target-goals (distribute-sentences ptree-node .cur-targets. .tactic-tc.)))
1021 (dolist (cgoal target-goals)
1022 ;; variables --> constants
1023 (let ((sentence (car (goal-targets cgoal))))
1024 (cond ((axiom-variables sentence)
1025 (when (eq cgoal original-goal)
1026 (setq cgoal (prepare-next-goal ptree-node .tactic-tc.)))
1027 (push-next-goal cgoal)
1028 (with-in-module ((goal-context cgoal))
1029 (let* ((next-target (rule-copy-canonicalized sentence *current-module*))
1030 (vars (axiom-variables next-target))
1031 (subst (make-tc-variable-substitutions cgoal vars)))
1032 (apply-substitution-to-axiom subst next-target 'tc t)
1033 (compute-rule-method next-target)
1034 (compile-module *current-module* t)
1035 (setf (goal-targets cgoal)
1036 (list (normalize-sentence next-target *current-module*))))))
1037 (t
1038 ;; the sentence does not contain any variables.
1039 (push-next-goal cgoal)))))
1040 (setq .next-goals. (nreverse .next-goals.))
1041 ;; check goal is satisfied or not
1042 (dolist (cgoal .next-goals.)
1043 (multiple-value-bind (res sentence osentence)
1044 (check-goal-is-satisfied cgoal 'tc)
1045 (declare (ignore osentence sentence))
1046 (when res
1047 (format t "~%[tc] discharged the goal ~s" (goal-name cgoal)))))
1048 (values .next-goals. .next-goals.))))))
1049
1050 ;;; ===================================
1051 ;;; TACTIC: Simultaneous Induction [SI]
1052 ;;; ===================================
1053
1054 ;;; set-indvars : ptree-node List(variable) -> List(variable)
1055 ;;; handler of ':ind on' command
1056 ;;;
1057 (defun set-indvars (ptree-node variables)
1058 (declare (type ptree-node ptree-node))
1059 (let* ((cur-goal (ptree-node-goal ptree-node))
1060 (cur-targets (goal-targets cur-goal))
1061 (ind-vars nil))
1062 (dolist (cur-target cur-targets)
1063 (let ((target-variables (axiom-variables cur-target)))
1064 (dolist (v variables)
1065 (let ((tv (find-if #'(lambda (x) (and (eq (variable-name v) (variable-name x))
1066 (eq (variable-sort v) (variable-sort x))))
1067 target-variables)))
1068 (if tv (pushnew v ind-vars :test #'equal :key #'(lambda (x) (variable-name x)))
1069 (with-output-chaos-error ('no-such-variable)
1070 (format t "Setting induction variable, no such variable ~a:~a in target axiom."
1071 (variable-name v) (variable-sort v))))))))
1072 (setf (goal-indvars cur-goal) (nreverse ind-vars))
1073 (format t "~%**> Induction will be conducted on ")
1074 (dolist (var (goal-indvars cur-goal))
1075 (term-print-with-sort var) (princ " "))
1076 ind-vars))
1077
1078 ;;; set-induction-variables
1079 ;;; top level function.
1080 (defun set-induction-variables (variables)
1081 (declare (type list variables))
1082 (let ((node (car (get-unproved-nodes *proof-tree*))))
1083 (unless node
1084 (with-output-chaos-error ('no-unproved)
1085 (format t "There is no unproved goals.")))
1086 (set-indvars node variables)
1087 (clear-induction-scheme node)))
1088
1089 ;;;==========================================
1090 ;;; applying induction based on constructors
1091 ;;;
1092
1093 ;;; get-induction-variable-constructors : variable -> List(constructor)
1094 ;;; returns a list of constructors of a given induction variable
1095 ;;;
1096 (defun get-induction-variable-constructors (v constructors)
1097 (let ((ops nil))
1098 (dolist (op constructors)
1099 (when (sort<= (method-coarity op) (variable-sort v) (module-sort-order *current-module*))
1100 (push op ops)))
1101 (unless ops
1102 (with-output-chaos-error ('internal-error)
1103 (format t "Finding constructor of sort ~a, none was found." (string (sort-name (variable-sort v))))))
1104 (nreverse ops)))
1105
1106 ;;; get-indvar-constructors
1107 ;;; returns a list of (indvar . const1 const2 ...constn) for an induction variable indvar.
1108 ;;; (((idvar-1 . const-1) ... (idvar-1 ... const-n))
1109 ;;; ((idvar-2 . const-2) ... (idvar-2 ... const-2-m))
1110 ;;; :
1111 ;;; ((idva-i . const-i) ... (idvar-i ... const-i-k)))
1112 ;;;
1113 (defun get-indvar-constructors (indvars constructors)
1114 (let ((ivar-map nil))
1115 (dolist (iv indvars)
1116 (push (mapcar #'(lambda (cts) (cons iv cts))
1117 (get-induction-variable-constructors iv constructors))
1118 ivar-map))
1119 (nreverse ivar-map)))
1120
1121 ;;; make-indvar-comb-substitutions : List(variable) List(constructor) -> List(substitution)
1122 ;;; returns all possible substitution patterns of induction variables.
1123 ;;; ex. for induction variables A, B, C, there are constructors
1124 ;;; op a-1 : -> A . op a-2 : -> A
1125 ;;; op b-1 : B -> B
1126 ;;; op c-1 : -> C . op c-2 : -> C . op c-3 : C -> C
1127 ;;; this will produces the following substitutions:
1128 ;;; (((A . A-1) (B . B-1) (C . C-1))
1129 ;;; ((A . A-2) (B . B-1) (C . C-2))
1130 ;;; ((A . A-1) (B . B-1) (C . C-3))
1131 ;;; ((A . A-2) (B . B-1) (C . C-1))
1132 ;;; ((A . A-1) (B . B-1) (C . C-2))
1133 ;;; ((A . A-2) (B . B-1) (C . C-3)))
1134 ;;;
1135 (defun make-indvar-comb-substitutions (indvars constructors)
1136 (let ((list-of-alist (get-indvar-constructors indvars constructors)))
1137 (declare (type list list-of-alist))
1138 (select-comb-elems list-of-alist)))
1139
1140 ;;; get-induction-base-substitutions : List(substitution) -> List(substitution)
1141 ;;;
1142 (defun get-induction-base-substitutions (all-subst)
1143 (let ((res nil))
1144 (dolist (subst all-subst)
1145 (when (every #'(lambda (sub) (null (method-arity (cdr sub)))) subst)
1146 (push subst res)))
1147 (with-citp-debug ()
1148 (format t "~%[si] base case subst")
1149 (dolist (sub res)
1150 (print-next)
1151 (print-substitution sub)))
1152 (nreverse res)))
1153
1154 ;;; get-induction-step-substitutions : List(substitution) -> List(substitution)
1155 ;;;
1156 (defun get-induction-step-substitutions (all-subst)
1157 (let ((res nil))
1158 (dolist (subst all-subst)
1159 (unless (every #'(lambda (sub) (null (method-arity (cdr sub)))) subst)
1160 (push subst res)))
1161 (with-citp-debug ()
1162 (format t "~%[si] get-induction-step-subsutitutions")
1163 (dolist (sub res)
1164 (print-next)
1165 (print-substitution sub)))
1166 (nreverse res)))
1167
1168 ;;; get-real-target-variable : variable List(variable) -> { variable | null }
1169 ;;; finds an variable from a list of variables.
1170 ;;;
1171 (defun get-real-target-variable (indvar axiom-variables)
1172 (find-if #'(lambda (m) (and (sort= (variable-sort m) (variable-sort indvar))
1173 (equal (variable-name m) (variable-name indvar))))
1174 axiom-variables))
1175
1176 ;;; make-real-induction-subst
1177 ;;;
1178 (defun make-real-induction-subst (subst axiom-vars)
1179 (let ((rsubst nil))
1180 (dolist (sub subst)
1181 (let* ((iv (car sub))
1182 (op-or-term (cdr sub))
1183 (term (if (term? op-or-term)
1184 (term-copy-and-returns-list-variables op-or-term)
1185 (make-applform (method-coarity op-or-term)
1186 op-or-term
1187 nil)))
1188 (rv nil))
1189 (when (setq rv (get-real-target-variable iv axiom-vars))
1190 (setq rsubst (acons rv term rsubst)))))
1191 rsubst))
1192
1193 ;;; set-base-cases
1194 ;;; generates base case axioms for a given target
1195 ;;;
1196 (defun set-base-cases (goal target base-substitutions)
1197 (let ((all-targets nil)
1198 (app? nil))
1199 (with-in-module ((goal-context goal))
1200 (dolist (subst base-substitutions)
1201 (let* ((new-target (rule-copy-canonicalized target *current-module*))
1202 (real-subst (make-real-induction-subst subst (axiom-variables new-target))))
1203 (when real-subst
1204 (setq app? t)
1205 (apply-substitution-to-axiom real-subst new-target 'induction-base)
1206 (push new-target all-targets)))))
1207 (setf (goal-targets goal) (nconc (goal-targets goal) all-targets))
1208 app?))
1209
1210 ;;; make-step-constructor-term
1211 ;;;
1212 (defun make-step-constructor-term (goal op one-arg variable)
1213 (with-in-module ((goal-context goal))
1214 (let ((arity (method-arity op))
1215 (arg-list nil)
1216 (arg-var-assoc nil)
1217 (n 0))
1218 (setq arg-var-assoc
1219 (mapcar #'(lambda (x) (cons x 0)) arity))
1220 (dolist (s arity)
1221 (cond ((sort<= (term-sort one-arg) s *current-sort-order*)
1222 (when (< 1 (incf n))
1223 (with-output-chaos-error ('sorry)
1224 (format t "Sorry, but system does not handle a constructor having multiple arguments of the same sort.")))
1225 (push one-arg arg-list))
1226 (t (let* ((var-assoc (assoc s arg-var-assoc))
1227 (ind-var (if (zerop (cdr var-assoc))
1228 (progn (incf (cdr var-assoc)) variable)
1229 (make-variable-term s
1230 (intern (format nil "~A~D"
1231 (string (variable-name variable))
1232 (incf (cdr var-assoc)))))))
1233 (constant (variable->constructor goal ind-var :sort s)))
1234 (push constant arg-list)))))
1235 (let ((res (make-applform (method-coarity op) op (nreverse arg-list))))
1236 res))))
1237
1238 ;;; make-induction-step-subst : goal axiom (var . op-or-term) -> substitution
1239 ;;;
1240 (defun make-induction-step-subst (goal target v-op-list)
1241 ;; we ignore all mapped operators are constant constructors.
1242 (when (every #'(lambda (v-op)
1243 (let ((op (cdr v-op)))
1244 (and (null (method-arity op))
1245 (method-is-constructor? op))))
1246 v-op-list)
1247 (return-from make-induction-step-subst nil))
1248 (make-induction-step-subst-from-op goal target v-op-list))
1249
1250 (defun make-induction-step-subst-from-op (goal target v-op-list)
1251 (let ((hypo-v-op nil)
1252 (step-v-op nil)
1253 (axiom-vars (axiom-variables target)))
1254 ;; we generate the following case for each induction variable v:
1255 ;; 1) (v . <term of constant constructor>)
1256 ;; 2) (v . <constant term of non-constant-constructor>)
1257 ;; 3) (v . <application form of non-constant-constructor>)
1258 ;;
1259 (dolist (sub v-op-list)
1260 (let* ((iv (car sub)) ; induction variable
1261 (op (cdr sub)) ; operator
1262 (rv nil)) ; real induction variable in target
1263 (when (setq rv (get-real-target-variable iv axiom-vars))
1264 (cond ((null (method-arity op))
1265 (let* ((ct (variable->constructor goal rv :op op))
1266 (c-subst (cons iv ct)))
1267 ;; operator is constant constructor
1268 (push (list (cons iv (list op ct))) hypo-v-op)
1269 (push c-subst step-v-op)))
1270 (t ;; operator is non-constant constructor
1271 (let ((const-term (variable->constructor goal rv)))
1272 (push (list (cons rv (list op const-term))) hypo-v-op)
1273 (push (cons rv (make-step-constructor-term goal op const-term rv)) step-v-op)))))))
1274 (values (select-comb-elems (nreverse hypo-v-op))
1275 (nreverse step-v-op))))
1276
1277 (defun make-real-induction-step-subst (subst variables)
1278 (let ((rsubst nil))
1279 (dolist (sub subst)
1280 (let ((iv (car sub))
1281 (term (cdr sub))
1282 (rv nil))
1283 (when (setq rv (get-real-target-variable iv variables))
1284 (setq rsubst (acons rv term rsubst)))))
1285 (nreverse rsubst)))
1286
1287 (defun resolve-induction-subst (goal hypo-v-op step-subst)
1288 (declare (ignore goal))
1289 (flet ((make-proper-alist (sub)
1290 (mapcar #'(lambda (s) (cons (car s) (cadr s))) sub)))
1291 (unless hypo-v-op
1292 (with-output-chaos-warning ()
1293 (format t "No subst given.")
1294 (return-from resolve-induction-subst nil)))
1295 (let ((rsubsts (mapcar #'(lambda (sub)
1296 (cons (car sub) (list (third sub))))
1297 hypo-v-op))
1298 (all-subst nil))
1299 (with-citp-debug ()
1300 (format t "~%[si] resolve induction step: given")
1301 (print-next) (format t "hypo-v-op: ~s" hypo-v-op)
1302 (print-next) (princ "step-subst" )
1303 (print-substitution step-subst))
1304 ;; return if there are no possible combinations
1305 ;; (unless (cdr hypo-v-op)
1306 ;; (return-from resolve-induction-subst (list (make-proper-alist rsubsts))))
1307 ;;
1308 (with-citp-debug ()
1309 (format t "~%resolve subst: given")
1310 (dolist (v-op hypo-v-op)
1311 (let ((*print-indent* (+ 2 *print-indent*)))
1312 (print-next)
1313 (format t "(~a . ~a <- " (variable-name (first v-op)) (car (method-name (second v-op))))
1314 (term-print-with-sort (third v-op))
1315 (princ ")"))))
1316
1317 ;; make all possible hypothesis substitutions
1318 (let ((vop-hash (make-hash-table :test #'eq))
1319 (vcombs nil))
1320 (dolist (v-op hypo-v-op)
1321 (let ((v (first v-op))
1322 (as nil))
1323 (unless (setq as (assoc v rsubsts))
1324 (with-output-chaos-error ('internal-err)
1325 (format t "!! cannot find variable subst ~s" (variable-name v))))
1326 (setf (gethash v vop-hash) (list as))
1327 (let ((st (assoc v step-subst :test #'equal))
1328 (hentry (gethash v vop-hash))
1329 (new-element nil))
1330 (unless st (with-output-chaos-error ('no-step-term)
1331 (format t "No step term found for variable ~a" (variable-name v))))
1332 (setq new-element (cons v (list (cdr st))))
1333 (unless (member new-element hentry :test #'equal)
1334 (setf (gethash v vop-hash) (append hentry (list new-element)))))))
1335 (maphash #'(lambda (x vl) (declare (ignore x)) (push vl vcombs)) vop-hash)
1336 (setq all-subst (select-comb-elems vcombs))
1337 (with-citp-debug ()
1338 (format t "~%resolve subt: all possibilities")
1339 (let ((*print-indent* (+ 2 *print-indent*))
1340 (num 0))
1341 (declare (type fixnum num))
1342 (dolist (vcom all-subst)
1343 (print-next)
1344 (format t "=== (#~d) " (incf num))
1345 (dolist (rs vcom)
1346 (format t "~a |-> " (variable-name (car rs)))
1347 (term-print-with-sort (cadr rs)) (princ " ")))))
1348 ;;
1349 (mapcar #'make-proper-alist all-subst)))))
1350
1351 ;;; add-hypothesis
1352 ;;; Note: assumes computing module context is established.
1353 ;;;
1354 (defun subst-is-equal (sub1 sub2)
1355 (dolist (entry sub1)
1356 (let ((entry2 (assoc (car entry) sub2 :test #'equal)))
1357 (unless entry2 (return-from subst-is-equal nil))
1358 (unless (equal (cdr entry) (cdr entry2))
1359 (return-from subst-is-equal nil))))
1360 t)
1361
1362 (defun add-hypothesis (step-goal target hypo-subst step-subst)
1363 (with-citp-debug ()
1364 (format t "~%[si] add-hypothesis:")
1365 (print-next) (princ "-- hypo-subst ")
1366 (dolist (hp hypo-subst)
1367 (print-next)
1368 (print-substitution hp))
1369 (print-next) (princ "-- step-subst ")
1370 (print-substitution step-subst))
1371 (dolist (osub hypo-subst)
1372 (dolist (sub (resolve-induction-subst step-goal osub step-subst))
1373 (unless (subst-is-equal sub step-subst)
1374 (let* ((hypo (rule-copy-canonicalized target *current-module* nil :delete-non-exec))
1375 (subst (make-real-induction-step-subst sub (axiom-variables hypo))))
1376 (with-citp-debug
1377 (format t "~%[applying hypo subst] ")
1378 (print-substitution subst)
1379 (print-next)
1380 (princ "to ")
1381 (print-axiom-brief hypo))
1382 (apply-substitution-to-axiom subst hypo 'si t)
1383 (compute-rule-method hypo)
1384 (set-operator-rewrite-rule *current-module* hypo)
1385 (adjoin-axiom-to-module *current-module* hypo)
1386 (with-citp-debug ()
1387 (format t "~%--> ")
1388 (print-axiom-brief hypo))
1389 (setf (goal-assumptions step-goal) (append (goal-assumptions step-goal) (list hypo))))))))
1390
1391 ;;; add-step-cases
1392 ;;; Note: assumes computing module context is established.
1393 ;;;
1394 (defun add-step-cases (step-goal target step-subst)
1395 (let* ((new-target (rule-copy-canonicalized target *current-module*))
1396 (subst (make-real-induction-step-subst step-subst (axiom-variables new-target))))
1397 (when (car subst)
1398 (with-citp-debug
1399 (format t "~%[applying step subst] ")
1400 (print-substitution subst))
1401 (apply-substitution-to-axiom subst new-target 'step)
1402 (setf (goal-targets step-goal) (nconc (goal-targets step-goal) (list new-target))))))
1403
1404 ;;; induction-cases
1405 ;;; Note: assumes there properly set induction variables in the current goal.
1406 ;;;
1407 (defun get-induction-constructors (current-goal)
1408 (declare (ignore current-goal))
1409 (ptree-constructor-ops *proof-tree*))
1410
1411 (defun induction-cases (parent-node)
1412 (declare (type ptree-node parent-node))
1413 (let* ((cur-goal (ptree-node-goal parent-node))
1414 (cur-targets nil)
1415 (indvars (goal-indvars cur-goal))
1416 (all-subst (make-indvar-comb-substitutions indvars
1417 (get-induction-constructors cur-goal)))
1418 (base-goal (prepare-next-goal parent-node .tactic-si.))
1419 (step-goals nil)
1420 (need-goal nil)
1421 (base-generated nil)
1422 (remainings nil))
1423 ;;
1424 (with-citp-debug ()
1425 (format t "~%[si] all possible substitutions")
1426 (let ((num 0))
1427 (declare (type fixnum num))
1428 (dolist (subs all-subst)
1429 (format t "~%subst #~d" (incf num))
1430 (let ((*print-indent* (+ 2 *print-indent*)))
1431 (print-next)
1432 (print-substitution subs)))))
1433
1434 ;; implicit NF application
1435 (dolist (ct (goal-targets cur-goal))
1436 (multiple-value-bind (ntarget app?)
1437 (normalize-sentence ct (goal-context cur-goal))
1438 (when app? (setq need-goal t))
1439 (push ntarget cur-targets)))
1440 (setq cur-targets (nreverse cur-targets))
1441
1442 ;; generate base cases
1443 ;;
1444 (dolist (target cur-targets)
1445 (if (not (set-base-cases base-goal target (get-induction-base-substitutions all-subst)))
1446 (when need-goal
1447 (push target remainings))
1448 (setq base-generated t)))
1449 (unless base-generated (setq base-goal nil))
1450
1451 ;; generate step cases
1452 ;; we generate all possible combinations of given induction variables.
1453 ;; for each combination, we will construct a new goal.
1454 ;;
1455 (dolist (subst (get-induction-step-substitutions all-subst))
1456 (let ((step-goal (prepare-next-goal parent-node .tactic-si.)))
1457 (with-in-module ((goal-context step-goal))
1458 ;; following functions and their callies can assume the computing context is established.
1459 (dolist (target cur-targets)
1460 (multiple-value-bind (hypo-subst-list step-subst)
1461 (make-induction-step-subst step-goal target subst)
1462 (add-hypothesis step-goal target hypo-subst-list step-subst)
1463 (add-step-cases step-goal target step-subst)))
1464 (cond ((goal-targets step-goal)
1465 (push step-goal step-goals))
1466 (t ))))) ; do nothig
1467 ;;
1468 (when remainings
1469 (multiple-value-bind (ap? nil-goals)
1470 (apply-nil-internal parent-node (reverse remainings) :all-togather .tactic-si.)
1471 (declare (ignore ap?))
1472 (dolist (ng nil-goals)
1473 (push ng step-goals))))
1474 ;;
1475 (if base-goal
1476 (values t (cons base-goal (nreverse step-goals)))
1477 (if step-goals
1478 ;; case remainings
1479 (values t step-goals)
1480 (values nil nil)))))
1481
1482 ;;; apply-si : ptree-node tactic -> (applied? . List(goal))
1483 ;;;
1484 (defun apply-si (ptree-node &rest ignore)
1485 (declare (type ptree-node ptree-node)
1486 (ignore ignore))
1487 (let ((cur-goal (ptree-node-goal ptree-node)))
1488 (unless (and (goal-indvars cur-goal) (goal-targets cur-goal))
1489 (return-from apply-si nil))
1490 (multiple-value-bind (applied new-goals)
1491 (induction-cases ptree-node)
1492 (if applied
1493 (values applied new-goals)
1494 (values nil nil)))))
1495
1496 ;;; =======================================
1497 ;;; applying user defined induction scheme
1498 ;;;
1499 (defun set-induction-variables-and-scheme (node variables bases hypos steps)
1500 (declare (type list variables bases steps))
1501 (let ((goal-node (ptree-node-goal node)))
1502 (set-indvars node variables)
1503 (set-indbases goal-node bases)
1504 (set-indhypos goal-node hypos)
1505 (set-indsteps goal-node steps)))
1506
1507 (defun set-indbases (goal-node terms)
1508 (setf (goal-bases goal-node) terms)
1509 (let ((bases (goal-bases goal-node)))
1510 (format t "~%--> :ind{} setting induction base~p" (length bases))
1511 (dolist (b bases)
1512 (let ((*print-indent* (+ 2 *print-indent*)))
1513 (print-next)
1514 (term-print b)))
1515 (flush-all)))
1516
1517 (defun set-indhypos (goal-node terms)
1518 (setf (goal-hypos goal-node) terms)
1519 (let ((hypos (goal-hypos goal-node)))
1520 (format t "~%--> :ind{} setting hypothesis pattern~p" (length hypos))
1521 (dolist (h hypos)
1522 (let ((*print-indent* (+ 2 *print-indent*)))
1523 (print-next)
1524 (term-print h)))
1525 (flush-all)))
1526
1527 (defun set-indsteps (goal-node terms)
1528 (setf (goal-steps goal-node) terms)
1529 (let ((steps (goal-steps goal-node)))
1530 (format t "~%--> :ind{} setting induction step~p" (length steps))
1531 (dolist (s steps)
1532 (let ((*print-indent* (+ 2 *print-indent*)))
1533 (print-next)
1534 (term-print s)))
1535 (flush-all)))
1536
1537 (defun get-ind-substitutions (indvars bases)
1538 (let ((ivar-map nil))
1539 (dolist (var indvars)
1540 (let ((vsort (variable-sort var)))
1541 (let ((subst nil))
1542 (dolist (base bases)
1543 (when (sort= vsort (term-sort base))
1544 (push (cons var base) subst)))
1545 (when subst
1546 (push subst ivar-map)))))
1547 (with-citp-debug ()
1548 (when ivar-map
1549 (format t "~%--> :ind{}: substitution")
1550 (let ((*print-indent* (+ 2 *print-indent*)))
1551 (dolist (subst ivar-map)
1552 (print-next)
1553 (print-substitution subst)))))
1554 (nreverse ivar-map)))
1555
1556 (defun make-induction-step-subst-from-term (goal target v-term-list)
1557 (let ((hypo-v-op nil)
1558 (step-v-op nil)
1559 (axiom-vars (axiom-variables target)))
1560 ;; we generate the following case for each induction variable v:
1561 ;; 1) (v . <term of constant constructor>)
1562 ;; 2) (v . <constant term of non-constant-constructor>)
1563 ;; 3) (v . <application form of non-constant-constructor>)
1564 ;;
1565 (dolist (sub v-term-list)
1566 (let* ((iv (car sub)) ; induction variable
1567 (term (cdr sub)) ; term
1568 (op (term-head term))
1569 (rv nil)) ; real induction variable in target
1570 (when (setq rv (get-real-target-variable iv axiom-vars))
1571 (cond ((null (term-subterms term))
1572 (let* ((ct (variable->constructor goal rv :op op))
1573 (c-subst (cons iv ct)))
1574 ;; operator is constant constructor
1575 (push (list (cons iv (list op ct))) hypo-v-op)
1576 (push c-subst step-v-op)))
1577 (t ;; operator is non-constant constructor
1578 (let ((const-term (variable->constructor goal rv)))
1579 (push (list (cons rv (list op const-term))) hypo-v-op)
1580 (push (cons rv (make-step-constructor-term goal op const-term rv)) step-v-op)))))))
1581 (values (select-comb-elems (nreverse hypo-v-op))
1582 (nreverse step-v-op))))
1583
1584 ;;; user-induction-cases
1585 ;;;
1586 (defun user-induction-cases (ptree-node)
1587 (declare (type ptree-node ptree-node))
1588 (let ((goal (ptree-node-goal ptree-node)))
1589 (let* ((*chaos-quiet* t)
1590 (indvars (goal-indvars goal))
1591 (targets (goal-targets goal))
1592 (base-generated nil)
1593 (base-goal (prepare-next-goal ptree-node .tactic-ind.))
1594 (given-hypos (goal-hypos goal))
1595 (step-goals nil)
1596 (remainings nil))
1597 ;; just for now
1598 (declare (ignore given-hypos))
1599 ;; base cases
1600 (dolist (target targets)
1601 (if (not (set-base-cases base-goal target (get-ind-substitutions indvars (goal-bases goal))))
1602 (push target remainings)
1603 (setq base-generated t)))
1604 (unless base-generated (setq base-goal nil))
1605 ;; step cases
1606 (dolist (subst (get-ind-substitutions indvars (goal-steps goal)))
1607 (let ((step-goal (prepare-next-goal ptree-node .tactic-ind.)))
1608 (with-in-module ((goal-context step-goal))
1609 (dolist (target targets)
1610 (multiple-value-bind (hypo-subst-list step-subst)
1611 (make-induction-step-subst-from-term step-goal target subst)
1612 (add-hypothesis step-goal target hypo-subst-list step-subst)
1613 (add-step-cases step-goal target step-subst)))
1614 (cond ((goal-targets step-goal)
1615 (push step-goal step-goals))
1616 (t ))))) ; do nothing
1617 ;;
1618 (when remainings
1619 (multiple-value-bind (ap? nil-goals)
1620 (apply-nil-internal ptree-node (reverse remainings) :all-togather .tactic-ind.)
1621 (declare (ignore ap?))
1622 (dolist (ng nil-goals)
1623 (push ng step-goals))))
1624 ;;
1625 (if base-goal
1626 (values t (cons base-goal (nreverse step-goals)))
1627 (values nil nil)))))
1628
1629 ;;; apply-user-defined-induction-scheme
1630 ;;;
1631 (defun apply-user-defined-induction-scheme (ptree-node)
1632 (declare (type ptree-node ptree-node))
1633 (multiple-value-bind (applied new-goals)
1634 (user-induction-cases ptree-node)
1635 (when applied
1636 ;; add generated nodes as children
1637 (add-ptree-children ptree-node new-goals)
1638 (when-citp-verbose ()
1639 (dolist (gn (ptree-node-subnodes ptree-node))
1640 (pr-goal (ptree-node-goal gn))))
1641 (ptree-node-subnodes ptree-node))))
1642
1643 ;;; =======================
1644 ;;; TACTIC: REDUCTION [RD]
1645 ;;; =======================
1646
1647 ;;; do-apply-rd
1648 ;;;
1649 (defun do-apply-rd (cur-goal next-goal do-undo tactic)
1650 (let* ((target-goal (or next-goal cur-goal))
1651 (cur-targets (goal-targets target-goal))
1652 (reduced-targets nil)
1653 (discharged nil)
1654 (result nil)
1655 (tactic-name (tactic-name tactic)))
1656 (unless cur-targets
1657 (with-citp-debug ()
1658 (format t "~%[rd] no target sentences."))
1659 (return-from do-apply-rd (values nil nil)))
1660 (compile-module (goal-context target-goal) t)
1661 (dolist (target cur-targets)
1662 (multiple-value-bind (c-result cur-target original-sentence)
1663 (do-check-sentence target (or next-goal cur-goal) tactic-name)
1664 (cond (c-result ; satisfied or contradition
1665 (setq result t)
1666 (push original-sentence discharged))
1667 ;; reduced but not discharged
1668 (t (if do-undo
1669 (push original-sentence reduced-targets)
1670 (push cur-target reduced-targets))))))
1671 ;; set new (reduced sentences) as targets
1672 (setf (goal-targets target-goal) (nreverse reduced-targets))
1673 ;; set discharged sentences
1674 (setf (goal-proved cur-goal) (nreverse discharged))
1675 (unless reduced-targets
1676 ;; this means all sentences are discharged
1677 (setf (goal-targets cur-goal) nil)
1678 (format t "~%[~a] discharged goal ~s." tactic-name (goal-name cur-goal))
1679 (return-from do-apply-rd (values result nil)))
1680 ;; there remains
1681 (values t (list (or next-goal cur-goal)))))
1682
1683 ;;; apply-rd-internal : ptree-node undo? tactic
1684 ;;; working horse of apply-rd(-)
1685 ;;;
1686 (defun apply-rd-internal (ptree-node do-undo &optional (tactic .tactic-rd.))
1687 (declare (type ptree-node ptree-node)
1688 (type tactic tactic))
1689 ;; we set :spoiler on
1690 ;; forcing application of implicit tactics(NF,CF, e.t.c.)
1691 (with-spoiler-on ()
1692 (let ((cur-goal (ptree-node-goal ptree-node)))
1693 (when (goal-is-discharged cur-goal)
1694 (with-output-chaos-warning ()
1695 (format t "** The goal ~s has already been proved!"
1696 (goal-name cur-goal)))
1697 (return-from apply-rd-internal (values nil nil)))
1698 (unless (goal-targets cur-goal)
1699 (return-from apply-rd-internal nil))
1700 (let ((undo? (or do-undo (the-goal-needs-undo cur-goal))))
1701 ;; undo? = true means the current goal is generatd by
1702 ;; :defined :ctf- or :csp-, AND
1703 ;; this RD application follows it, i.e., :apply(... ctf-n rd ...)
1704 ;; in this case we don't prepare next-goal and ..
1705 (let ((next-goal (if undo?
1706 nil
1707 (prepare-next-goal ptree-node .tactic-rd.))))
1708 (unless undo?
1709 (setf (goal-targets next-goal) (goal-targets cur-goal)))
1710 (with-citp-debug ()
1711 (format t "~%[rd] target: ~a" (if next-goal
1712 (goal-name next-goal)
1713 (goal-name cur-goal))))
1714 (multiple-value-bind (applied next-goals)
1715 (do-apply-rd cur-goal next-goal undo? tactic)
1716 (declare (ignore applied))
1717 (if undo?
1718 ;; the original goal rolled back, no new goal is needed.
1719 (values next-goals nil)
1720 (values next-goals next-goals))))))))
1721
1722 ;;; apply-rd
1723 ;;; explicit application of tactic RD.
1724 (defun apply-rd (ptree-node &optional (tactic .tactic-rd.))
1725 (apply-rd-internal ptree-node nil tactic))
1726
1727 ;;; apply-rd-
1728 ;;; explicit application of tactic RD,
1729 ;;; but if the sentence was not reduced to 'true'
1730 ;;; preserves the original goal sentence
1731 ;;;
1732 (defun apply-rd- (ptree-node &optional (tactic .tactic-rd-.))
1733 (apply-rd-internal ptree-node :undo tactic))
1734
1735 ;;; ==========================
1736 ;;; TACTIC: Case Analysis [CA]
1737 ;;; ==========================
1738
1739 ;;; get-gterms : term -> List(ground-term)
1740 ;;; returns a list of ground terms in given term.
1741 ;;;
1742 (defun get-gterms (term)
1743 (declare (type term term))
1744 (let ((gterms nil))
1745 (declare (type list gterms))
1746 (when (term-is-applform? term)
1747 (unless (term-variables term)
1748 (push term gterms))
1749 (dolist (arg (term-subterms term))
1750 (setq gterms (nconc gterms (get-gterms arg)))))
1751 gterms))
1752
1753 ;;; get-gterms-from-axiom : axiom -> List(ground-term)
1754 ;;; returns the list of ground terms contained in the given axiom.
1755 ;;;
1756 (defun get-gterms-from-axiom (axiom &optional (condition-only nil))
1757 (declare (type axiom axiom))
1758 (let ((gterms nil))
1759 (declare (type list gterms))
1760 (cond (condition-only
1761 (unless (is-true? (axiom-condition axiom))
1762 (setq gterms (remove-duplicates (get-gterms (axiom-condition axiom))
1763 :test #'equal))))
1764 (t (setq gterms (delete-duplicates (append (get-gterms (axiom-lhs axiom))
1765 (append (get-gterms (axiom-rhs axiom))
1766 (get-gterms-from-axiom axiom t)))
1767 :test #'equal))))
1768
1769 gterms))
1770
1771 ;;; gsubterm-has-matching-rule : term List(axiom) -> Bool
1772 ;;; returns t iff there is an axiom x in List(axiom) st.
1773 ;;; sigma(s) = lhs(x), where s is one of the true subterm of given ground term.
1774 ;;;
1775 (defun gsubterm-has-matching-rule (gterm c-rules)
1776 (declare (type term gterm)
1777 (type list c-rules))
1778 (dolist (term (delete gterm (get-gterms gterm)))
1779 (with-citp-debug ()
1780 (format t "~% check : ")
1781 (term-print-with-sort term))
1782 (dolist (crule c-rules)
1783 (multiple-value-bind (gs sub no-match eeq)
1784 (@matcher (axiom-lhs crule) term :match)
1785 (declare (ignore eeq sub gs))
1786 (unless no-match
1787 (return-from gsubterm-has-matching-rule t)))))
1788 nil)
1789
1790 ;;; ca-instantiate-condition : goal term -> term'
1791 ;;; returns a term t' by replacing every variable in the given term t
1792 ;;; by a constant.
1793 ;;;
1794 (defun ca-instantiate-condition (goal condition)
1795 (declare (type goal goal)
1796 (type term condition))
1797 (let ((vars (term-variables condition))
1798 (subst nil))
1799 (declare (type list vars subst))
1800 (cond (vars (dolist (v vars)
1801 (push (cons v (variable->constant goal v)) subst))
1802 (substitution-image-simplifying subst condition))
1803 (t condition))))
1804
1805 ;;; find-gterm-matching-conditionals : goal term List(conditional axioms)
1806 ;;; -> List(<subst, axiom, condition>)
1807 ;;; returns all possible cases for a given ground term.
1808 ;;;
1809 (defvar .duplicated. nil)
1810 (defvar .subst-so-far. nil)
1811
1812
1813 (defun find-gterm-matching-conditionals (goal gterm conditional-rules idx)
1814 (declare (type goal goal)
1815 (type term gterm)
1816 (type list conditional-rules)
1817 (type fixnum idx))
1818 (let ((res nil)
1819 (rejected nil))
1820 (dolist (rule conditional-rules)
1821 (block next
1822 (unless (is-true? (rule-condition rule))
1823 (multiple-value-bind (gs sub no-match eeq)
1824 (@matcher (axiom-lhs rule) gterm :match)
1825 (declare (ignore eeq))
1826 (when no-match (return-from next nil))
1827 (let ((cond-instance
1828 (ca-instantiate-condition goal
1829 (substitution-image-simplifying sub (rule-condition rule)))))
1830 (cond ((not (member cond-instance .subst-so-far. :test #'term-equational-equal))
1831 (push cond-instance .subst-so-far.)
1832 (push cond-instance res))
1833 (t
1834 (push cond-instance rejected)))
1835 (loop
1836 (let ((n-subst nil)
1837 (n-cond-inst nil))
1838 (multiple-value-setq (gs n-subst no-match)
1839 (next-match gs))
1840 (when no-match (return-from next))
1841 (with-citp-debug ()
1842 (format t "~%[ca] adding extra."))
1843 (setq n-cond-inst
1844 (ca-instantiate-condition goal
1845 (substitution-image-simplifying n-subst (rule-condition rule))))
1846 (cond ((not (member n-cond-inst .subst-so-far. :test #'term-equational-equal))
1847 (unless (term-equational-equal n-cond-inst cond-instance)
1848 (push n-cond-inst .subst-so-far.)
1849 (push n-cond-inst res)))
1850 (t
1851 ;; (push cond-instance res) ; ***
1852 (push cond-instance rejected))))))))))
1853 ;;
1854 (with-citp-debug ()
1855 (when res
1856 (format t "~%found cases for ") (term-print-with-sort gterm)
1857 (dolist (i res)
1858 (print-next)
1859 (term-print-with-sort i)))
1860 (when rejected
1861 (format t "~%rejected cases")
1862 (dolist (i rejected)
1863 (print-next)
1864 (term-print-with-sort i))))
1865 (when rejected
1866 (setf (aref .duplicated. idx) (remove-duplicates rejected :test #'term-equational-equal)))
1867 ;;
1868 (remove-duplicates res :test #'term-equational-equal)))
1869
1870 ;;; generate-case-axioms : goal List(< rule . subst >) -> List(axiom)
1871 ;;;
1872 (defvar .new-axs-so-far. nil)
1873
1874 (defun generate-case-axioms (next-goal conditions)
1875 (with-in-module ((goal-context next-goal))
1876 (let ((case-axioms nil))
1877 (dolist (condition conditions)
1878 (let ((list-lhs nil))
1879 (if (method= *bool-cond-op* (term-head condition))
1880 (dolist (arg (list-assoc-subterms condition *bool-cond-op*))
1881 (push arg list-lhs))
1882 (setq list-lhs (list condition)))
1883 (dolist (condition list-lhs)
1884 (let ((axs (make-new-assumption *current-module* condition 'ca)))
1885 (when axs
1886 (unless (member axs .new-axs-so-far. :test #'rule-is-similar?)
1887 (push axs .new-axs-so-far.)
1888 (compute-rule-method axs)
1889 (with-citp-debug ()
1890 (format t "~%[ca] adding an axiom to module ~s" (get-module-simple-name (goal-context next-goal)))
1891 (print-next)
1892 (print-axiom-brief axs))
1893 (set-operator-rewrite-rule *current-module* axs)
1894 (adjoin-axiom-to-module *current-module* axs)
1895 (push axs case-axioms)))))))
1896 (compile-module *current-module* t)
1897 (setf (goal-assumptions next-goal) (append (goal-assumptions next-goal)
1898 (nreverse case-axioms))))))
1899
1900 ;;; normalize-cases : List(List(term)) -> List(List(term))'
1901 ;;;
1902
1903 (defun find-sub-case-in (case l-case)
1904 (declare (type list case l-case))
1905 (let ((size (length case)))
1906 (declare (type fixnum size))
1907 (dolist (xc l-case)
1908 (when (and (<= size (length xc))
1909 (every #'(lambda (x) (find x xc :test #'term-equational-equal)) case))
1910 (return-from find-sub-case-in xc)))
1911 nil))
1912
1913 (defun case-is-valid (idxs term)
1914 (dolist (idx idxs)
1915 (when (member term (aref .duplicated. idx) :test #'term-equational-equal)
1916 (with-citp-debug ()
1917 (format t "~% ... rejected."))
1918 (return-from case-is-valid nil)))
1919 term)
1920
1921 (defun remove-exclusive-cases (case)
1922 (let ((idxs (mapcar #'(lambda (x) (car x)) case))
1923 (result nil))
1924 (declare (type list idxs result))
1925 (with-citp-debug ()
1926 (format t "~%-- check these combination")
1927 (dolist (c case)
1928 (print-next)
1929 (format t "~idx ~d: " (car c))
1930 (term-print-with-sort (cdr c))))
1931 (dolist (c case)
1932 (let ((term (cdr c)))
1933 (when (case-is-valid idxs term)
1934 (push term result))))
1935 result))
1936
1937 (defun normalize-cases (l-case ptree-node all-cases)
1938 (declare (type list l-case)
1939 (type ptree-node ptree-node))
1940 (let ((mod (goal-context (ptree-node-goal ptree-node)))
1941 (dist-cases nil))
1942 (with-in-module (mod)
1943 (flet ((distribute-cond (term)
1944 (if (method= *bool-cond-op* (term-head term))
1945 (list-assoc-subterms term *bool-cond-op*)
1946 (list term))))
1947 (with-citp-debug ()
1948 (when .duplicated.
1949 (format t "~%== .duplicated. === ")
1950 (dotimes (x (1- (length .duplicated.)))
1951 (format t "~%(~d)" x)
1952 (dolist (trm (aref .duplicated. x))
1953 (print-next)
1954 (term-print-with-sort trm)))))
1955 (dolist (case l-case)
1956 (block next
1957 ;; case ::= (t0 t1 ... tn)
1958 ;; first we remove exclusive cases
1959 (setq case (remove-exclusive-cases case))
1960 (unless case (return-from next nil))
1961 (dolist (c case)
1962 (setq all-cases (delete c all-cases :test #'term-equational-equal)))
1963 ;; then divide /\ into each cases
1964 (let ((dcase nil))
1965 (dolist (c case)
1966 (setq dcase (nconc dcase (distribute-cond c))))
1967 (push (delete-duplicates dcase :test #'term-equational-equal) dist-cases)))
1968 (setq dist-cases (nreverse dist-cases)))
1969 ;;
1970 (let ((result nil))
1971 ;; for each case
1972 (dolist (case dist-cases)
1973 (unless (find-sub-case-in case result)
1974 (setq result (nconc result (list case)))))
1975 (when all-cases
1976 ;; remaining sole cases
1977 (dolist (c all-cases)
1978 (push (list c) result)))
1979 ;;
1980 result)))))
1981
1982 ;;; generate-cases : ptree-node term List(conditional-axiom)
1983 ;;;
1984 (defun generate-cases (ptree-node target conditional-rules divide?)
1985 (declare (type ptree-node ptree-node)
1986 (list conditional-rules))
1987 (multiple-value-bind (norm-target app?)
1988 (normalize-sentence target (goal-context (ptree-node-goal ptree-node)))
1989 (when app?
1990 (setq target norm-target))
1991 (with-citp-debug ()
1992 (format t "~%** Case Analysis: target -----------")
1993 (print-next)
1994 (print-axiom-brief target))
1995 ;; then generate possible cases
1996 (let ((gterms (get-gterms-from-axiom target))
1997 (next-goals nil)
1998 (remainings nil)
1999 (all-cases nil)
2000 (gt-idx 0)
2001 (.subst-so-far. nil)
2002 (.duplicated. nil))
2003 (declare (type fixnum gt-idx)
2004 (type list gterms next-goals remainings .subst-so-far.))
2005 (setf .duplicated. (make-array (length gterms) :initial-element nil))
2006 ;;
2007 (let ((g-conditions nil))
2008 (dolist (gterm gterms)
2009 (unless (gsubterm-has-matching-rule gterm conditional-rules)
2010 (let ((conds (find-gterm-matching-conditionals (ptree-node-goal ptree-node)
2011 gterm
2012 conditional-rules
2013 gt-idx)))
2014 (when conds
2015 (incf gt-idx)
2016 (push conds g-conditions)))))
2017 (setq g-conditions (nreverse g-conditions))
2018 (with-citp-debug ()
2019 (format t "~%All the conditions")
2020 (print-next)
2021 (dolist (gc g-conditions)
2022 (princ "====")
2023 (print-next)
2024 (dolist (cond gc)
2025 (term-print-with-sort cond))))
2026 ;;
2027 (dolist (gc g-conditions)
2028 (dolist (c gc)
2029 (pushnew c all-cases :test #'term-equational-equal)))
2030
2031 ;; make all combinations and generate cases
2032 (let ((rv-combs (select-comb-elems g-conditions t))
2033 (next-goal nil))
2034 ;; distribute /\ and delete duplicated conditions
2035 (with-citp-debug ()
2036 (format t "~%[ca] gterm conditions --BEFORE normalization: ")
2037 (if rv-combs
2038 (let ((rv-com nil))
2039 (dotimes (x (length rv-combs))
2040 (setq rv-com (nth x rv-combs))
2041 (print-next)
2042 (format t "--(~d)--" (1+ x))
2043 (dolist (rr rv-com)
2044 (print-next)
2045 (format t "~d:" (car rr))
2046 (term-print-with-sort (cdr rr)))))
2047 (format t "NONE.")))
2048 ;; eliminate exclusive combinations and dupulicated cases.
2049 (setq rv-combs (normalize-cases rv-combs ptree-node all-cases))
2050
2051 (with-citp-debug ()
2052 (format t "~%[ca] gterm conditions --AFTER normalization: ")
2053 (if rv-combs
2054 (let ((rv-com nil))
2055 (dotimes (x (length rv-combs))
2056 (setq rv-com (nth x rv-combs))
2057 (print-next)
2058 (format t "--(~d)--" (1+ x))
2059 (dolist (rr rv-com)
2060 (print-next)
2061 (term-print-with-sort rr))))
2062 (format t "NONE.")))
2063 (cond (rv-combs
2064 (dolist (rv-com rv-combs)
2065 (let ((.new-axs-so-far. nil))
2066 (setq next-goal (prepare-next-goal ptree-node .tactic-ca.))
2067 (setf (goal-targets next-goal) (list target))
2068 (generate-case-axioms next-goal rv-com)
2069 (push next-goal next-goals)))
2070 ;; normalize the target after adding cases
2071 (normalize-sentence target *current-module*))
2072 (t
2073 ;; no case is generated for the target
2074 (push target remainings)))))
2075 ;;
2076 (when remainings
2077 (when (or next-goals app? divide?)
2078 (multiple-value-bind (app? nop-goals)
2079 (apply-nil-internal ptree-node (reverse remainings) nil .tactic-ca.)
2080 (declare (ignore app?))
2081 (if-spoiler-on
2082 :then (dolist(ng nop-goals)
2083 (let ((target (car (goal-targets ng))))
2084 ;; no case is generated: apply normalization & check the result
2085 (multiple-value-bind (discharged normalized-target original-target)
2086 (do-check-sentence target ng)
2087 (when discharged
2088 (format t "~%[ca] discharged: ")
2089 (print-axiom-brief normalized-target)
2090 (setf (goal-targets ng) nil
2091 (goal-proved ng) (list original-target))))
2092 (push ng next-goals)))
2093 :else (setq next-goals nop-goals)))))
2094 ;; check LE
2095 (setq next-goals (nreverse next-goals))
2096 (dolist (goal next-goals)
2097 (check-le goal))
2098 ;;
2099 (values next-goals next-goals))))
2100
2101 (defun rule-is-for-case (rule)
2102 (and (not (is-true? (rule-condition rule)))
2103 (let ((labels (rule-labels rule)))
2104 (dolist (lb labels nil)
2105 (let ((lstr (string lb)))
2106 (when (and (>= (length lstr) 3)
2107 (string-equal (subseq lstr 0 3) "CA-"))
2108 (return-from rule-is-for-case t)))))))
2109
2110 (defun get-ca-rules (module)
2111 (remove-if-not #'rule-is-for-case (module-all-rules module)))
2112
2113 ;;; apply-ca
2114 ;;; toplevel of :ca
2115 (defun apply-ca (ptree-node &rest ignore)
2116 (declare (type ptree-node ptree-node)
2117 (ignore ignore))
2118 (with-in-context (ptree-node)
2119 (with-in-module ((goal-context .cur-goal.))
2120 (let ((crules (get-ca-rules *current-module*))
2121 (divide? (cdr .cur-targets.)))
2122 (dolist (target .cur-targets.)
2123 (multiple-value-bind (applied goals)
2124 (generate-cases ptree-node target crules divide?)
2125 (declare (ignore applied))
2126 (when goals (setq .next-goals. (nconc .next-goals. goals)))))
2127 (values .next-goals. .next-goals.)))))
2128
2129 ;;; for debug
2130 (defun rule-is-case-generated (rule)
2131 (and (is-true? (rule-condition rule))
2132 (let ((labels (rule-labels rule)))
2133 (dolist (lb labels nil)
2134 (let ((lstr (string lb)))
2135 (when (and (= 2 (length lstr))
2136 (string-equal lstr "CA"))
2137 (return-from rule-is-case-generated t)))))))
2138
2139 (defun print-case-axioms (node)
2140 (let ((mod (goal-context (ptree-node-goal node)))
2141 (cas nil))
2142 (with-in-module (mod)
2143 (let ((all-rules (module-all-rules mod)))
2144 (dolist (rule all-rules)
2145 (when (rule-is-case-generated rule)
2146 (push rule cas))))
2147 (when cas
2148 (format t "~%** generated axioms in goal ~s" (goal-name (ptree-node-goal node)))
2149 (let ((*print-indent* (+ 2 *print-indent*)))
2150 (dolist (rl cas)
2151 (print-next)
2152 (print-axiom-brief rl)))))))
2153
2154 (defun all-cases ()
2155 (unless *proof-tree*
2156 (with-output-chaos-error ('no-context)
2157 (format t "No proof tree!")))
2158 (dag-wfs (ptree-root *proof-tree*)
2159 #'print-case-axioms))
2160
2161 ;;; ======================================
2162 ;;; TACTIC: Case Analysis on Sequence [CS]
2163 ;;; TODO
2164 ;;; ======================================
2165 (defun apply-cs (ptree-node &rest ignore)
2166 (declare (ignore ignore))
2167 ptree-node)
2168
2169 ;;; ==========================================
2170 ;;; INSTANCIATE (non-executable) axiom (:init)
2171 ;;; ==========================================
2172
2173 ;;; get-target-axiom : module target-form -> {nil | axiom}
2174 ;;; target-form : (<kind> <form>)
2175 ;;;
2176 (defun get-target-axiom (module target-form &optional (add-to-module nil))
2177 (let ((kind (first target-form))
2178 (ax nil))
2179 (cond ((eq :label kind) (setq ax (get-rule-labelled module (second target-form))))
2180 (t (with-in-module (module)
2181 (let ((*chaos-quiet* nil))
2182 (setq ax (parse-axiom-declaration (parse-module-element-1 (cdr target-form)))))
2183 (when add-to-module
2184 (set-operator-rewrite-rule module ax)
2185 (adjoin-axiom-to-module module ax)
2186 (set-needs-rule)))))
2187 ax))
2188
2189 ;;; resolve-subst-form
2190 ;;;
2191 (defun resolve-subst-form (context subst-forms &optional (normalize nil))
2192 (unless subst-forms (return-from resolve-subst-form nil))
2193 (with-in-module (context)
2194 (let ((subst nil)
2195 (*parse-variables* nil))
2196 (dolist (subst-form subst-forms)
2197 (let ((var-form (first subst-form))
2198 (term-form (rest subst-form))
2199 (var nil)
2200 (term nil))
2201 (with-citp-debug ()
2202 (format t "~%resolving subst form:")
2203 (print-next)
2204 (format t " var=~s, term=~s" var-form term-form))
2205 (let ((*chaos-quiet* nil))
2206 (setq var (simple-parse context var-form)))
2207 (when (or (term-is-an-error var) (not (term-is-variable? var)))
2208 (with-output-chaos-error ('invalid-var-form)
2209 (format t "Invalid variable in substitution: ~s" var-form)))
2210 (let ((*chaos-quiet* nil))
2211 (setq term (simple-parse context term-form)))
2212 (when (term-is-an-error term)
2213 (with-output-chaos-error ('invalid-term)
2214 (format t "No parse..: ~s" term-form)))
2215 (unless (sort<= (term-sort term) (variable-sort var) *current-sort-order*)
2216 (with-output-chaos-error ('sort-mismatch)
2217 (format t "Sort mismatch for the substitution")
2218 (print-next)
2219 (format t " variable: ") (term-print-with-sort var)
2220 (print-next)
2221 (format t " term: ") (term-print-with-sort term)))
2222 (when normalize
2223 (with-spoiler-on
2224 (setq term (normalize-term-in context term))))
2225 (push (cons var term) subst)))
2226 subst)))
2227
2228 ;;;
2229 (defun make-real-instanciation-subst (subst axiom-vars)
2230 (let ((rsubst nil)
2231 rv)
2232 (dolist (vt-pair subst)
2233 (if (setq rv (get-real-target-variable (car vt-pair) axiom-vars))
2234 (setq rsubst (acons rv (cdr vt-pair) rsubst))
2235 (with-output-chaos-error ('no-var)
2236 (format t "Instanciating an axiom, no such variable ")
2237 (term-print-with-sort (car vt-pair)))))
2238 rsubst))
2239
2240 ;;; make-axiom-instance : module substitution axiom label -> axiom'
2241 ;;; terms in resulting axiom must be ground terms.
2242 ;;;
2243 (defun make-axiom-instance (module subst axiom &optional (label nil))
2244 (flet ((make-proper-label (label)
2245 (if (stringp label)
2246 (intern label)
2247 label)))
2248 (with-in-module (module)
2249 (let ((new-axiom (rule-copy-canonicalized axiom module)))
2250 (if subst
2251 (apply-substitution-to-axiom (make-real-instanciation-subst subst
2252 (axiom-variables new-axiom))
2253 new-axiom
2254 (if label
2255 (make-proper-label label)
2256 'init)
2257 (if label
2258 nil
2259 t))
2260 (setf (rule-labels new-axiom) (if label
2261 (make-proper-label label)
2262 (cons (make-proper-label label) (rule-labels new-axiom)))))
2263 new-axiom))))
2264
2265 ;;; instanciate-axiom
2266 ;;;
2267 (defun instanciate-axiom (goal target-form subst-form &optional (label nil))
2268 (let* ((*current-module* (goal-context goal))
2269 (target-axiom (get-target-axiom *current-module* target-form)))
2270 (instanciate-axiom-in-goal goal target-axiom subst-form label)))
2271
2272 ;;; apply-init-tactic : tactic-init -> void
2273 ;;; apply :def(ed) :init command to the current goal
2274 ;;;
2275 (defun apply-init-tactic (ptree-node tactic)
2276 (declare (type ptree-node ptree-node)
2277 (type tactic-init tactic))
2278 (let ((goal (ptree-node-goal ptree-node)))
2279 (let ((ax (tactic-init-axiom tactic))
2280 (subst (tactic-init-subst tactic))
2281 (kind (tactic-init-kind tactic)))
2282 (instanciate-axiom-in-goal goal ax subst (if (stringp kind)
2283 kind
2284 nil)))))
2285
2286 ;;; supporting function around :init
2287
2288 (defun report-instanciated-axiom (instance)
2289 (let ((*print-indent* (+ 2 *print-indent*))
2290 (*print-line-limit* 80))
2291 (print-next)
2292 (print-axiom-brief instance)))
2293
2294 (defun introduce-instanciated-axiom-to-module (instance module)
2295 (with-in-module (module)
2296 (setf (axiom-kind instance) nil) ; reset bad flag
2297 (set-operator-rewrite-rule module instance)
2298 (adjoin-axiom-to-module module instance)
2299 (compile-module module t)))
2300
2301 (defun instanciate-axiom-in-goal (goal target-axiom subst-form &optional (label nil))
2302 (let* ((module (goal-context goal))
2303 (subst (resolve-subst-form module
2304 subst-form
2305 (citp-flag citp-normalize-init))))
2306 (let ((instance (remove-nonexec (make-axiom-instance module subst target-axiom label))))
2307 (when (citp-flag citp-normalize-lhs)
2308 ;; we normalize the LHS of the instance
2309 (with-spoiler-on
2310 (multiple-value-bind (n-sen applied?)
2311 (normalize-sentence instance module :lhs-only :variable-is-constant)
2312 (when applied?
2313 (setf instance n-sen)))))
2314 ;; input the instance to current context
2315 (with-in-module (module)
2316 (setf (goal-assumptions goal) (append (goal-assumptions goal) (list instance)))
2317 (format t "~%**> initialized the axiom in goal ~s" (goal-name goal))
2318 (report-instanciated-axiom instance)
2319 (introduce-instanciated-axiom-to-module instance module)
2320 (when-citp-verbose ()
2321 (pr-goal goal))))))
2322
2323 (defun instanciate-axiom-in-module (module target-axiom subst &optional (label nil))
2324 (with-in-module (module)
2325 (let ((instance (remove-nonexec (make-axiom-instance module subst target-axiom label))))
2326 (format t "~%**> initialized the axiom in module ~a" (get-module-simple-name module))
2327 (report-instanciated-axiom instance)
2328 (introduce-instanciated-axiom-to-module instance module))))
2329
2330 ;;; ================================
2331 ;;; Target sentence T -> A implies T [:imp]
2332 ;;; ================================
2333
2334 (defun make-impl-assumption (ax)
2335 (simplify-eq-form (axiom-lhs ax)))
2336
2337 ;;; a imply lhs
2338 (defun make-impl-form (lhs assumption)
2339 (make-applform *bool-sort* *bool-imply*
2340 (list (make-impl-assumption assumption) lhs)))
2341
2342 (defun introduce-implication-to-goal (target-form subst-form)
2343 (let ((target-axiom (get-target-axiom *current-module* target-form t))
2344 (subst (if subst-form
2345 (resolve-subst-form *current-module* subst-form)
2346 nil))
2347 (instance nil)
2348 (new-targets nil))
2349 (setq instance (make-axiom-instance *current-module* subst target-axiom))
2350 (with-next-context (*proof-tree*)
2351 (unless (and (is-true? (axiom-rhs instance))
2352 (is-true? (axiom-condition instance)))
2353 (with-output-chaos-error ('invalid-axiom)
2354 (format t "~%[:imp] invalid instance")
2355 (print-next)
2356 (print-axiom-brief instance)))
2357 ;; modify the target sentence.
2358 ;; instance: eq p = true .
2359 ;; goal: eq l = r .
2360 ;; -> new goal eq p implies l = r .
2361 (dolist (target (goal-targets (ptree-node-goal .context.)))
2362 (let ((new-lhs (make-impl-form (axiom-lhs target) instance))
2363 (*print-indent* (+ *print-indent* 2))
2364 (rtarget (rule-copy-canonicalized target *current-module*)))
2365 (with-citp-debug ()
2366 (format t "~%[:imp] target: ")
2367 (print-axiom-brief target))
2368 (if (sort= (term-sort (axiom-rhs rtarget)) *bool-sort*)
2369 (progn
2370 (push rtarget new-targets)
2371 (setf (axiom-lhs rtarget) new-lhs
2372 (axiom-labels rtarget) (cons :imp (axiom-labels rtarget)))
2373 (format t "~%[:imp] target sentence is converted...")
2374 (print-next)
2375 (princ "=> ")
2376 (print-next)
2377 (print-axiom-brief rtarget))
2378 (with-output-chaos-warning ()
2379 (format t "[:imp] sort of target sentence is not Bool. Ignored.")
2380 (print-next)
2381 (print-axiom-brief target)))))
2382 (setf (goal-targets (ptree-node-goal .context.)) (nreverse new-targets)))))
2383
2384 ;;; ==============
2385 ;;; CRITICAL PAIRS [:cp]
2386 ;;; ==============
2387
2388 (defun citp-rename-term-variables (suffix list-vars)
2389 (let ((done nil))
2390 (dolist (var list-vars)
2391 (unless (member var done)
2392 (push var done)
2393 (setf (variable-name var) (intern (format nil "~a~a" (variable-name var) suffix)))))))
2394
2395 (declaim (type fixnum *renamed-variable-number*))
2396 (defvar *renamed-variable-number* 0)
2397 (let ((*renamed-variable-number* 0))
2398 (declare (type fixnum *renamed-variable-number*)
2399 (special *renamed-variable-number*))
2400 (defun citp-rename-axiom-variables (axiom)
2401 (citp-rename-term-variables (incf *renamed-variable-number*) (axiom-variables axiom))
2402 axiom)
2403 )
2404
2405 (defstruct (cpp (:print-function pr-cpp))
2406 (t1 nil :type term) ; sigma(lhs[pos])
2407 (t2 nil :type term) ; sigma(lhs)
2408 (pos nil :type list) ; occurence of t1 in root term
2409 (subst nil :type list) ; mgu
2410 (cpairs nil :type list)) ; generated critical pairs in a form of axiom
2411
2412 (defun pr-cpp (cpp &optional (stream *standard-output*) &rest ignore)
2413 (declare (ignore ignore))
2414 (format stream "~%Critical Pair ---------")
2415 (let ((*print-indent* (+ *print-indent*))
2416 (*standard-output* stream))
2417 (print-next)
2418 (princ "term 1: ") (term-print-with-sort (cpp-t1 cpp))
2419 (print-next)
2420 (princ "term 2: ") (term-print-with-sort (cpp-t2 cpp))
2421 (print-next)
2422 (format t "position: ~a" (cpp-pos cpp))
2423 (print-next)
2424 (princ "substitution: ") (print-substitution (cpp-subst cpp))
2425 (when (cpp-cpairs cpp)
2426 (setq *print-indent* (+ 2 *print-indent*))
2427 (format t "~%- ~d critical pairs" (length (cpp-cpairs cpp)))
2428 (dolist (cpair (cpp-cpairs cpp))
2429 (print-next)
2430 (print-axiom-brief cpair)))))
2431
2432 (defun compute-overwraps (t1 t2 occur)
2433 (let ((cpps nil))
2434 (cond ((term-is-applform? t1)
2435 (multiple-value-bind (subst no-match e-eq)
2436 (unify t1 t2)
2437 (declare (ignore e-eq))
2438 (unless no-match
2439 (push (make-cpp :t1 (substitution-image-simplifying subst t1)
2440 :t2 (substitution-image-simplifying subst t2)
2441 :subst subst
2442 :pos occur) cpps))
2443 (let ((pos 0))
2444 (declare (type fixnum pos))
2445 (dolist (sub (term-subterms t1))
2446 (setq cpps (append cpps (compute-overwraps sub t2 (append occur (cons pos occur)))))
2447 (incf pos)))))
2448 (t nil))
2449 cpps))
2450
2451 (defun term-at-pos (pos term)
2452 (if pos
2453 (term-at-pos (cdr pos) (term-arg-n term (car pos)))
2454 term))
2455
2456 (defun replace-term-at (pos term repl-term)
2457 (let ((target (term-at-pos pos term)))
2458 (term-replace target repl-term)
2459 term))
2460
2461 ;;; compute-all-overwrapps : axiom axiom -> List(cpp)
2462 ;;;
2463 (defun compute-axiom-overwrapps (ax-1 ax-2)
2464 (let ((lhs-1 (rule-lhs ax-1))
2465 (lhs-2 (rule-lhs ax-2))
2466 (cpps nil))
2467 (setq cpps (compute-overwraps lhs-1 lhs-2 '()))
2468 cpps))
2469
2470 (defun generate-critical-pairs (cpps ax-1 ax-2)
2471 (dolist (cpp cpps)
2472 (let ((subst (cpp-subst cpp))
2473 (cpairs nil))
2474 (let ((cond-1 (substitution-image-simplifying subst (rule-condition ax-1)))
2475 (cond-2 (substitution-image-simplifying subst (rule-condition ax-2)))
2476 (rhs (substitution-image-simplifying subst (rule-rhs ax-1)))
2477 (lhs (replace-term-at (cpp-pos cpp)
2478 (substitution-image-simplifying subst (axiom-lhs ax-1))
2479 (substitution-image-simplifying subst (axiom-rhs ax-2)))))
2480 (with-citp-debug ()
2481 (format t "~%cond-1: ")(term-print-with-sort cond-1)
2482 (format t "~%cond-2: ")(term-print-with-sort cond-2)
2483 (format t "~%lhs: ") (term-print-with-sort lhs)
2484 (format t "~%rhs: ") (term-print-with-sort rhs))
2485
2486 (let ((*perform-on-demand-reduction* t))
2487 (compile-module *current-module* t)
2488 ;; LHS
2489 (rewrite lhs *current-module*)
2490 ;; RHS
2491 (rewrite rhs *current-module*)
2492 (unless (term-equational-equal lhs rhs)
2493 (let ((ordered-pair (sort (list lhs rhs) #'lrpo)))
2494 (pushnew (make-rule :lhs (first ordered-pair)
2495 :rhs (second ordered-pair)
2496 :condition *bool-true*
2497 :type :equation ; might be changed later by command :equqtion or :rule
2498 :labels '(cp))
2499 cpairs
2500 :test #'rule-is-similar?)))
2501
2502 ;; Condition
2503 (let ((new-cond (make-applform *condition-sort* *bool-cond-op* (list cond-1 cond-2))))
2504 (with-citp-debug ()
2505 (format t "~%[cp] generated condition: ")
2506 (term-print-with-sort new-cond))
2507 (rewrite new-cond *current-module*)
2508 (with-citp-debug ()
2509 (format t "~% after normalized :")
2510 (print-next)
2511 (term-print-with-sort new-cond))
2512 (unless (is-true? new-cond)
2513 (cond ((eq *bool-cond-op* (term-head new-cond))
2514 (let ((subs (list-assoc-subterms new-cond *bool-cond-op*)))
2515 (setq subs (sort subs #'lrpo))
2516 (do* ((sl subs (cdr sl))
2517 (lhs (car sl) (car sl))
2518 (rhs (cadr sl)))
2519 ((null rhs))
2520 (pushnew (make-rule :lhs lhs
2521 :rhs rhs
2522 :condition *bool-true*
2523 :type :equation
2524 :labels '(cpc))
2525 cpairs
2526 :test #'rule-is-similar?))))
2527 (t (pushnew (make-rule :lhs new-cond
2528 :rhs *bool-true*
2529 :condition *bool-true*
2530 :type :equation
2531 :labels '(cpc))
2532 cpairs
2533 :test #'rule-is-similar?)))))))
2534 (setf (cpp-cpairs cpp) cpairs))))
2535
2536 (defun compute-critical-pairs (module axiom1 axiom2)
2537 (with-in-module (module)
2538 (let ((ax-1 (citp-rename-axiom-variables (rule-copy-canonicalized axiom1 module)))
2539 (ax-2 (citp-rename-axiom-variables (rule-copy-canonicalized axiom2 module)))
2540 (cpp-1 nil)
2541 (cpp-2 nil))
2542 (setq cpp-1 (compute-axiom-overwrapps ax-1 ax-2))
2543 (setq cpp-2 (compute-axiom-overwrapps ax-2 ax-1))
2544 (generate-critical-pairs cpp-1 ax-1 ax-2)
2545 (generate-critical-pairs cpp-2 ax-2 ax-1)
2546
2547 (with-citp-debug ()
2548 (format t "~%------- cpp-1")
2549 (print cpp-1)
2550 (format t "~%------- cpp-2")
2551 (print cpp-2))
2552
2553 (let ((all-cpairs nil))
2554 (dolist (cp1 (nconc cpp-1 cpp-2))
2555 (setq all-cpairs (nconc all-cpairs (cpp-cpairs cp1))))
2556 (remove-duplicates all-cpairs :test #'rule-is-similar?)))))
2557
2558 ;;; apply-cp : axiom-form axiom-form -> void
2559 ;;;
2560 (defun apply-cp (target-1 target-2)
2561 (let ((context (get-next-proof-context *proof-tree*)))
2562 (unless context
2563 (with-output-chaos-error ('no-context)
2564 (format t "Applying [cp], no context module is established.")))
2565 (let ((goal (ptree-node-goal context)))
2566 (with-in-module ((goal-context goal))
2567 (let ((t1axiom (get-target-axiom *current-module* target-1))
2568 (t2axiom (get-target-axiom *current-module* target-2))
2569 (cpps nil))
2570 (setq cpps
2571 (setf (goal-critical-pairs goal) (compute-critical-pairs *current-module* t1axiom t2axiom)))
2572 (when cpps
2573 (format t "~%[cp] :")
2574 (let ((*print-indent* (+ 2 *print-indent*)))
2575 (dotimes (x (length cpps))
2576 (print-next)
2577 (format t "(~d) " (1+ x))
2578 (let ((ax (nth x cpps)))
2579 (term-print-with-sort (axiom-lhs ax))
2580 (print-next)
2581 (princ " => ")
2582 (term-print-with-sort (axiom-rhs ax)))))))))))
2583
2584 ;;; add-critical-pairs
2585 ;;;
2586 (defun add-critical-pairs (type direction)
2587 (let ((context (get-next-proof-context *proof-tree*)))
2588 (unless context
2589 (with-output-chaos-error ('no-context)
2590 (format t "Applying [cp], no context module is established.")))
2591 (let ((goal (ptree-node-goal context))
2592 (applied nil))
2593 (with-in-module ((goal-context goal))
2594 (dolist (cps (goal-critical-pairs goal))
2595 (setf (rule-type cps) type)
2596 (when (eq direction :backward)
2597 (let ((rhs (rule-lhs cps))
2598 (lhs (rule-rhs cps)))
2599 (setf (rule-lhs cps) lhs
2600 (rule-rhs cps) rhs)))
2601 (compute-rule-method cps)
2602 (set-operator-rewrite-rule *current-module* cps)
2603 (adjoin-axiom-to-module *current-module* cps)
2604 (setq applied (nconc applied (list cps))))
2605 (when applied
2606 (setf (goal-assumptions goal) (nconc (goal-assumptions goal) (nreverse applied)))
2607 (format t "~%[cp] added cp ~a~p to goal ~s: " type (length applied) (goal-name goal))
2608 (let ((*print-indent* (+ 2 *print-indent*)))
2609 (dolist (ax applied)
2610 (print-next)
2611 (print-axiom-brief ax)))
2612 (when *citp-verbose*
2613 (pr-goal goal)))))))
2614
2615 ;;; ===================================================
2616 ;;; {:red | :exec | :bred} [in <goal-name> : ] <term> .
2617 ;;; ===================================================
2618 ;;; reduce-in-goal : List( mode goal-name token-seq)
2619 ;;;
2620 (defun reduce-in-goal (mode goal-name token-seq)
2621 (with-citp-debug ()
2622 (format t "~%~s in ~s : ~s" (string mode) goal-name token-seq))
2623 (let ((next-goal-node (get-target-goal-node goal-name)))
2624 ;; do rewriting
2625 (perform-reduction* token-seq (goal-context (ptree-node-goal next-goal-node)) mode)))
2626
2627 ;;; ===========
2628 ;;; TACTIC: :NF
2629 ;;; explicit application of NF (compute normal form of targets).
2630 ;;;
2631 ;;; ===========
2632 (defun apply-nf (ptree-node &rest ignore)
2633 (declare (type ptree-node ptree-node)
2634 (ignore ignore))
2635 (let ((.cur-goal. (ptree-node-goal ptree-node)))
2636 (when (goal-is-discharged .cur-goal.)
2637 (with-output-chaos-warning ()
2638 (format t "** The goal ~s has already been proved!."
2639 (goal-name .cur-goal.)))
2640 (return-from apply-nf nil))
2641 (with-citp-env ()
2642 (with-spoiler-on ()
2643 (with-in-module ((goal-context .cur-goal.))
2644 (let ((n-targets nil)
2645 (applied nil))
2646 ;; normalize goal sentences
2647 (dolist (sen (goal-targets .cur-goal.))
2648 (multiple-value-bind (ngoal app?)
2649 (normalize-sentence sen *current-module*)
2650 (if app?
2651 (progn (setq applied t) (push ngoal n-targets))
2652 (push sen n-targets))))
2653 (when applied
2654 (let ((next-goal (prepare-next-goal ptree-node 'nf)))
2655 (setf (goal-targets next-goal) (nreverse n-targets))
2656 (return-from apply-nf (values t (list next-goal)))))
2657 (values nil nil)))))))
2658
2659 ;;; ===========
2660 ;;; TACTIC: :CT
2661 ;;; do contradiction check, can dischage the goal
2662 ;;; ===========
2663 (defun apply-ct (ptree-node &rest ignore)
2664 (declare (type ptree-node ptree-node)
2665 (ignore ignore))
2666 (let ((.cur-goal. (ptree-node-goal ptree-node)))
2667 (when (goal-is-discharged .cur-goal.)
2668 (with-output-chaos-warning ()
2669 (format t "** The goal ~s has already been proved!."
2670 (goal-name .cur-goal.)))
2671 (return-from apply-ct nil))
2672 ;;
2673 (with-in-module ((goal-context .cur-goal.))
2674 (with-citp-env ()
2675 (with-spoiler-on ()
2676 (when (check-contradiction .cur-goal. 'ct)
2677 (with-in-module ((goal-context .cur-goal.))
2678 (format t "~%[ct] dischaged:")
2679 (dolist (target (goal-targets .cur-goal.))
2680 (print-next)
2681 (print-axiom-brief target))
2682 (setf (goal-proved .cur-goal.) (goal-targets .cur-goal.))
2683 (setf (goal-targets .cur-goal.) nil)))))
2684 t)))
2685
2686 ;;; ==============
2687 ;;; :ctf or :ctf-
2688 ;;; ==============
2689 (defvar .pvar-names. nil)
2690
2691 (defun make-ctf-constructor-pattern (goal const-op)
2692 (let ((arity (method-arity const-op)))
2693 (cond (arity
2694 (let ((args nil)
2695 (form nil))
2696 (dolist (s arity)
2697 (let ((pn (assoc s .pvar-names. :test #'eq)))
2698 (unless pn
2699 (setq pn (cons s 0))
2700 (push pn .pvar-names.))
2701 (push (intro-fresh-constant goal
2702 (if (zerop (cdr pn))
2703 (progn (incf (cdr pn))
2704 "CTF")
2705 (format nil "CTF-~d" (incf (cdr pn))))
2706 s)
2707 args)))
2708 (setq form (make-applform (method-coarity const-op)
2709 const-op
2710 (nreverse args)))
2711 (with-citp-debug ()
2712 (with-in-module ((goal-context goal))
2713 (format t "~%[ctf consructor] ")
2714 (term-print-with-sort form)))
2715 form))
2716 (t (make-applform (method-coarity const-op) const-op nil)))))
2717
2718 (defun do-apply-ctf-with-constructors (cur-node term tactic)
2719 (let ((cur-goal (ptree-node-goal cur-node))
2720 (sort (term-sort term))
2721 (goals nil))
2722 (let ((constructors (get-constructors-of-sort sort *current-module*))
2723 (.pvar-names. nil))
2724 (declare (special .names.))
2725 (unless constructors
2726 (with-output-chaos-error ('no-constructors)
2727 (format t "Sort ~a has no constructors." (sort-name sort))))
2728 (dolist (const constructors)
2729 (let ((n-goal nil)
2730 (const-pat nil)
2731 (.pvar-names. nil))
2732 (setq n-goal (prepare-next-goal cur-node tactic))
2733 (setq const-pat (make-ctf-constructor-pattern n-goal const))
2734 (with-in-module ((goal-context n-goal))
2735 (multiple-value-bind (lhs rhs type)
2736 (if (sort= (term-sort term) *bool-sort*)
2737 (simplify-boolean-axiom term const-pat)
2738 (values term const-pat :equation))
2739 (when lhs
2740 (let ((ax (make-rule :lhs lhs
2741 :rhs rhs
2742 :condition *bool-true*
2743 :type type
2744 :labels (list (tactic-name tactic))
2745 :behavioural nil)))
2746 (adjoin-axiom-to-module *current-module* ax)
2747 (set-operator-rewrite-rule *current-module* ax)
2748 (compile-module *current-module*)
2749 (push n-goal goals)
2750 (setf (goal-targets n-goal) (goal-targets cur-goal))
2751 (setf (goal-assumptions n-goal)
2752 (append (goal-assumptions cur-goal) (list ax)))))))))
2753 (with-citp-debug ()
2754 (format t "~%[ctf] constructors generated:")
2755 (dolist (g (reverse goals))
2756 (print-next)
2757 (pr-goal g)))
2758 (if goals
2759 (values t (nreverse goals))
2760 (values nil nil)))))
2761
2762 (defun do-apply-ctf-to-equation (cur-node equation tactic)
2763 (let ((cur-goal (ptree-node-goal cur-node)))
2764 (flet ((add-assumption (goal lhs rhs)
2765 (let (n-axiom)
2766 (multiple-value-bind (n-lhs n-rhs type)
2767 (simplify-boolean-axiom lhs rhs)
2768 (cond (n-lhs
2769 (setq n-axiom (make-rule :lhs n-lhs
2770 :rhs n-rhs
2771 :condition *bool-true*
2772 :type type
2773 :behavioural nil
2774 :labels (list(tactic-name tactic))))
2775 (with-in-module ((goal-context goal))
2776 (adjoin-axiom-to-module *current-module* n-axiom)
2777 (set-operator-rewrite-rule *current-module* n-axiom)
2778 (compile-module *current-module*))
2779 (setf (goal-targets goal) (goal-targets cur-goal))
2780 (setf (goal-assumptions goal)
2781 (append (goal-assumptions cur-goal) (list n-axiom))))
2782 (t
2783 (with-output-chaos-warning ()
2784 (format t "[ctf] invalid assumption")
2785 (print-next)
2786 (print-axiom-brief equation)
2787 (print-next)
2788 (format t "...ignored.")
2789 nil)))))))
2790 (let ((t-goal (prepare-next-goal cur-node tactic))
2791 (f-goal (prepare-next-goal cur-node tactic)))
2792 (with-in-module ((goal-context cur-goal))
2793 (let ((lhs (make-applform *bool-sort*
2794 *eql-op*
2795 (list (rule-lhs equation)
2796 (rule-rhs equation)))))
2797 ;; true case
2798 (unless (add-assumption t-goal lhs *bool-true*)
2799 (setq t-goal nil))
2800 ;; false case
2801 (unless (add-assumption f-goal lhs *bool-false*)
2802 (setq f-goal nil))))
2803 (with-citp-debug ()
2804 (format t "~%citp from equation: generated")
2805 (print-next)
2806 (when t-goal
2807 (pr-goal t-goal))
2808 (when f-goal
2809 (pr-goal f-goal)))
2810 (if (and t-goal f-goal)
2811 (values t (list t-goal f-goal))
2812 (if t-goal
2813 (values t (list t-goal))
2814 (if f-goal
2815 (values t (list f-goal))
2816 (values nil nil))))))))
2817
2818 (defun parse-axiom-or-term (form term?)
2819 (let ((*chaos-quiet* nil))
2820 (if term?
2821 (let ((*parse-variables* nil))
2822 (let ((res (simple-parse *current-module* form *cosmos*)))
2823 res))
2824 (parse-axiom-declaration (parse-module-element-1 form)))))
2825
2826 (defun do-apply-ctf (cur-node term-or-equation &optional (tactic .tactic-ctf.))
2827 (with-citp-env ()
2828 (let ((cur-goal (ptree-node-goal cur-node)))
2829 (when (already-proved? cur-goal)
2830 (return-from do-apply-ctf nil))
2831 (if (term? term-or-equation)
2832 (do-apply-ctf-with-constructors cur-node term-or-equation tactic)
2833 (do-apply-ctf-to-equation cur-node term-or-equation tactic)))))
2834
2835 ;;; :ctf(-) toplevel function
2836 ;;;
2837 (defun apply-ctf (s-form term? dash? &optional (verbose *citp-verbose*))
2838 (let ((ptree-node (get-next-proof-context *proof-tree*)))
2839 (with-in-module ((goal-context (ptree-node-goal ptree-node)))
2840 (with-citp-env ()
2841 (let ((form (parse-axiom-or-term s-form term?)))
2842 (multiple-value-bind (applied next-goals)
2843 (do-apply-ctf ptree-node form)
2844 (declare (ignore applied))
2845 (unless next-goals
2846 (return-from apply-ctf nil))
2847 (format t "~%** Generated ~d goal~p" (length next-goals) (length next-goals))
2848 (when *citp-spoiler*
2849 ;; apply implicit rd
2850 (dolist (ngoal next-goals)
2851 (do-apply-rd ngoal nil dash? .tactic-ctf.)))
2852 ;; add generated nodes as children
2853 (add-ptree-children ptree-node next-goals)
2854 (when verbose
2855 (dolist (gn (ptree-node-subnodes ptree-node))
2856 (pr-goal (ptree-node-goal gn))))
2857 (ptree-node-subnodes ptree-node)))))))
2858
2859 ;;;=====================
2860 ;;; :defined :ctf, :ctf-
2861 ;;;=====================
2862 (defun apply-ctf-tactic (ptree-node tactic)
2863 (declare (type ptree-node ptree-node)
2864 (type tactic-ctf tactic))
2865 (let ((goal (ptree-node-goal ptree-node)))
2866 (with-in-module ((goal-context goal))
2867 (multiple-value-bind (applied next-goals)
2868 (do-apply-ctf ptree-node (tactic-ctf-form tactic) tactic)
2869 (declare (ignore applied))
2870 (unless next-goals
2871 (return-from apply-ctf-tactic nil))
2872 (when *citp-spoiler*
2873 ;; apply implicit rd
2874 (dolist (ngoal next-goals)
2875 (do-apply-rd ngoal nil (tactic-ctf-minus tactic) tactic)))
2876
2877 (values t next-goals)))))
2878
2879 ;;;==============
2880 ;;; :csp or :csp-
2881 ;;;==============
2882 (defun do-apply-csp (cur-node axs &optional (tactic .tactic-csp.))
2883 (unless (already-proved? cur-node)
2884 (let ((cur-goal (ptree-node-goal cur-node))
2885 (ngoals nil))
2886 ;; add given assumptions with generating child nodes
2887 (dolist (ax (mapcar #'(lambda (x)
2888 (rule-copy-canonicalized x (goal-context cur-goal) (tactic-name tactic)))
2889 axs))
2890 (let ((n-goal (prepare-next-goal cur-node tactic)))
2891 (with-in-module ((goal-context n-goal))
2892 (adjoin-axiom-to-module *current-module* ax)
2893 (set-operator-rewrite-rule *current-module* ax)
2894 (compile-module *current-module*))
2895 (setf (goal-targets n-goal) (goal-targets cur-goal))
2896 (setf (goal-assumptions n-goal) (append (goal-assumptions cur-goal) (list ax)))
2897 (push n-goal ngoals)))
2898 (with-citp-debug ()
2899 (format t "~%~a generated:" (tactic-name tactic))
2900 (dolist (g (reverse ngoals))
2901 (print-next)
2902 (pr-goal g)))
2903 (values t (nreverse ngoals)))))
2904
2905 (defun apply-csp (ax-forms dash? &optional (verbose *citp-verbose*))
2906 (let ((ptree-node (get-next-proof-context *proof-tree*)))
2907 (with-in-module ((goal-context (ptree-node-goal ptree-node)))
2908 (with-citp-env ()
2909 (let ((axs nil))
2910 (dolist (ax ax-forms)
2911 (let ((*chaos-quiet* nil))
2912 (push (parse-axiom-declaration (parse-module-element-1 ax)) axs)))
2913 (multiple-value-bind (applied next-goals)
2914 (do-apply-csp ptree-node (nreverse axs))
2915 (declare (ignore applied))
2916 (unless next-goals
2917 (return-from apply-csp nil))
2918 (format t "~%** Generated ~d goal~p" (length next-goals) (length next-goals))
2919 (when-spoiler-on ()
2920 ;; apply implicit rd
2921 (dolist (ngoal next-goals)
2922 (do-apply-rd ngoal nil dash? .tactic-csp.)))
2923 ;; add generated node as children
2924 (add-ptree-children ptree-node next-goals)
2925 (when verbose
2926 (dolist (gn (ptree-node-subnodes ptree-node))
2927 (pr-goal (ptree-node-goal gn))))
2928 (ptree-node-subnodes ptree-node)))))))
2929
2930 ;;;=====================
2931 ;;; :defined :csp, :csp-
2932 ;;;=====================
2933 (defun apply-csp-tactic (ptree-node tactic)
2934 (declare (type ptree-node ptree-node)
2935 (type tactic-csp tactic))
2936 (let ((goal (ptree-node-goal ptree-node)))
2937 (with-in-module ((goal-context goal))
2938 (with-citp-env ()
2939 (multiple-value-bind (applied next-goals)
2940 (do-apply-csp ptree-node (tactic-csp-forms tactic) tactic)
2941 (declare (ignore applied))
2942 (unless next-goals
2943 (return-from apply-csp-tactic nil))
2944 (when-spoiler-on ()
2945 ;; apply implicit rd
2946 (dolist (ngoal next-goals)
2947 (do-apply-rd ngoal nil (tactic-csp-minus tactic) tactic)))
2948 (values t next-goals))))))
2949
2950 ;;;
2951 ;;; use discharged sentences as axioms
2952 ;;;
2953 (defun use-discharged-goals (list-labels)
2954 (with-next-context (*proof-tree*)
2955 (let ((goal (ptree-node-goal .context.))
2956 (axs (find-discharged-sentences-with-label list-labels)))
2957 (use-sentences-in-goal goal axs))))
2958
2959 ;;;
2960 ;;; use-theory
2961 ;;;
2962 (defun use-theory (label theory)
2963 (with-next-context (*proof-tree*)
2964 (let ((goal (ptree-node-goal .context.))
2965 (ax (get-discharged-sentence-with-label label)))
2966 (use-theory-in-goal goal ax theory))))
2967
2968 ;;;
2969 ;;; embed-discharged-goals : list-labels module-name -> embeded module
2970 ;;;
2971 (defun embed-discharged-goals (list-labels module-name into)
2972 (let ((axs (find-discharged-sentences-with-label list-labels))
2973 (emod (if into
2974 (eval-modexp module-name)
2975 (make-embed-module module-name (get-context-module)))))
2976 (unless emod
2977 (with-output-chaos-error ()
2978 (format t "No such module ~a" module-name)))
2979 (embed-sentences-in-module emod axs)
2980 emod))
2981
2982 ;;;
2983 ;;; embed-theory
2984 ;;;
2985 (defun embed-theory (label theory module-name into)
2986 (let ((ax (get-discharged-sentence-with-label label))
2987 (emod (if into
2988 (eval-modexp module-name)
2989 (make-embed-module module-name (get-context-module)))))
2990 (unless emod
2991 (with-output-chaos-error ()
2992 (format t "[:embed] No such module ~a" module-name)))
2993 (embed-theory-in-module emod (term-head (axiom-lhs (car ax))) theory)
2994 emod))
2995
2996 ;;; -----------------------------------------------------------
2997 ;;; report-citp-stat
2998 ;;;
2999 (defun report-citp-stat ()
3000 (when *show-stats*
3001 (format t "~%~a" (generate-statistics-form-rewriting-only))))
3002
3003 ;;; ******
3004 ;;; :apply
3005 ;;; ******
3006
3007 ;;; APPLY-TACTIC
3008 ;;; apply-tactic : ptree-node tactic -> List(ptree-node)
3009 ;;; returns the list of generated goal nodes.
3010 ;;;
3011 (defun apply-tactic (ptree-node tactic &optional (verbose nil))
3012 (declare (type ptree-node ptree-node)
3013 (type tactic tactic))
3014 (flet ((prepare-goal-context (module)
3015 (compile-module module)
3016 (reset-term-memo-table module)))
3017 (with-citp-env ()
3018 (when (goal-is-discharged (ptree-node-goal ptree-node))
3019 (with-output-chaos-warning ()
3020 (format t "** The goal ~s has already been proved!." (goal-name (ptree-node-goal ptree-node)))
3021 (return-from apply-tactic nil)))
3022 (format t "~%[~a]=> :goal{~a}" (tactic-name tactic) (goal-name (ptree-node-goal ptree-node)))
3023 (initialize-ptree-node ptree-node)
3024 (prepare-goal-context (goal-context (ptree-node-goal ptree-node)))
3025 (with-citp-debug ()
3026 (let ((exe (tactic-executor tactic)))
3027 (format t "~%Funcalling ~a" exe)))
3028 (multiple-value-bind (applied next-goals)
3029 (funcall (tactic-executor tactic) ptree-node tactic)
3030 (declare (type (or null t) applied)
3031 (type list next-goals))
3032 (unless applied (return-from apply-tactic nil))
3033 (unless next-goals
3034 ;; reset the current ptree-node status,
3035 ;; i.e., cancel side effects
3036 (initialize-ptree-node ptree-node)
3037 (return-from apply-tactic nil))
3038 (format t "~%** Generated ~d goal~p" (length next-goals) (length next-goals))
3039 (add-ptree-children ptree-node next-goals)
3040 (when verbose
3041 (dolist (gn (ptree-node-subnodes ptree-node))
3042 (pr-goal (ptree-node-goal gn))))
3043 (ptree-node-subnodes ptree-node)))))
3044
3045 ;;; apply-tactics-to-node
3046 ;;;
3047 (defun apply-tactics-to-node (target-node tactics &optional (verbose *citp-verbose*))
3048 (declare (type ptree-node target-node))
3049 (unless tactics (return-from apply-tactics-to-node nil))
3050 (let ((subs (apply-tactic target-node (car tactics) verbose)))
3051 (if subs
3052 (dolist (target subs)
3053 (apply-tactics-to-node target (cdr tactics) verbose))
3054 (apply-tactics-to-node target-node (cdr tactics) verbose))))
3055
3056 ;;; apply-tactic-seq
3057 ;;; user defined sequence of tactic
3058 ;;;
3059 (defun apply-tactic-seq (ptree-node tactic &optional (verbose *citp-verbose*))
3060 (declare (type ptree-node ptree-node)
3061 (type tactic-seq tactic))
3062 (apply-tactics-to-node ptree-node (tactic-seq-tactics tactic) verbose))
3063
3064 ;;; apply-tactics
3065 ;;;
3066 (defun flatten-tactic-seq (tactics)
3067 (let ((res nil))
3068 (dolist (tactic tactics)
3069 (if (tactic-seq-p tactic)
3070 (setq res (nconc res (flatten-tactic-seq (tactic-seq-tactics tactic))))
3071 (setq res (nconc res (list tactic)))))
3072 res))
3073
3074 (defun apply-tactics (ptree tactics &optional (verbose *citp-verbose*))
3075 (declare (type ptree ptree)
3076 (type list tactics))
3077 (let ((target (get-next-proof-context ptree)))
3078 (unless target
3079 (format t "~%**> All goals have been proved!")
3080 (return-from apply-tactics nil))
3081 (clear-term-memo-table *term-memo-table*)
3082 (reset-rewrite-counters)
3083 (begin-rewrite)
3084 (apply-tactics-to-node target (flatten-tactic-seq tactics) verbose)
3085 (end-rewrite)
3086 (report-citp-stat)
3087 (check-success ptree)))
3088
3089 ;;; apply-auto
3090 ;;;
3091 (defun apply-auto (ptree &optional (tactics .default-tactics.) (verbose *citp-verbose*))
3092 (with-citp-env ()
3093 (with-spoiler-on ()
3094 (reset-rewrite-counters)
3095 (begin-rewrite)
3096 (if (next-proof-target-is-specified?)
3097 (apply-tactics-to-node (get-next-proof-context ptree) tactics verbose)
3098 (let ((target-nodes (get-unproved-nodes ptree)))
3099 (unless target-nodes
3100 (format t "~%**> All goals have been proved!")
3101 (return-from apply-auto nil))
3102 (dolist (tactic tactics)
3103 (dolist (target-node (get-unproved-nodes ptree))
3104 (apply-tactic target-node tactic verbose)))))
3105 (end-rewrite)
3106 (report-citp-stat)
3107 (check-success ptree))))
3108
3109 ;;; apply-tactics-to-goal
3110 ;;;
3111 (defun apply-tactics-to-goal (ptree name tactics &optional (verbose *citp-verbose*))
3112 (let ((target-node (find-goal-node ptree name)))
3113 (unless target-node
3114 (with-output-chaos-error ('no-named-goal)
3115 (format t "There is no goal with name ~s." name)))
3116 (reset-rewrite-counters)
3117 (begin-rewrite)
3118 (apply-tactics-to-node target-node tactics verbose)
3119 (end-rewrite)
3120 (report-citp-stat)
3121 (check-success ptree)))
3122
3123 ;;; EOF