-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnon-linear-planner.lisp
3914 lines (3730 loc) · 218 KB
/
non-linear-planner.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#| This requires OSCAR_3.31. This is based on non-linear planner 43. Lots of little changes. |#
(in-package "OSCAR")
(proclaim '(special *operators* *plan-trace* *initial-state* *goal-state* **premises** *start-time* *inputs*
*empty-inference-queue* *msg* *plans* *plan-node-number* *finish*
*plan-nodes* *start* *display-time* *causal-links* *causal-link-number* *planner*
*planning-problems* *plan-comparison-log* *display-plans* protoplan *plans-decided*
*solutions* *defeater-priority*))
(setf *planner* "Non-Linear-Planner-44")
(setf *test-log* nil)
(setf *time-limit* nil)
(setf *display-plans* nil)
(setf *defeater-priority* 1.0)
(defunction show-plans () (setf *display-plans* (not *display-plans*)))
(defstruct (plan-node (:print-function print-plan-node) (:conc-name nil))
(plan-node-number 0)
(plan-node-action nil))
(defunction print-plan-node (node stream depth)
(declare (ignore depth))
(cond ((eql (plan-node-number node) 0) (princ "*start*" stream))
((eql (plan-node-number node) -1) (princ "*finish*" stream))
(t
(princ "<pn" stream)
(princ (plan-node-number node) stream)
(princ ": " stream)
(princ (plan-node-action node) stream)
(princ ">" stream))))
(defparameter *start* (make-plan-node))
(defparameter *finish* (make-plan-node :plan-node-number -1))
(defstruct (plan (:print-function print-plan) (:conc-name nil))
(plan-number 0)
(plan-steps nil)
(plan-goal nil)
(causal-links nil)
(before-nodes nil)
(not-between nil)
(supporting-inference-nodes nil)
(live-causal-links nil)
(live-links? nil) ;; t if live-causal-links has been computed
(fixed-links nil)
)
;; This lexically-orders before-nodes.
(defunction before-< (x y)
(or (< (plan-node-number (car x)) (plan-node-number (car y)))
(and (eql (plan-node-number (car x)) (plan-node-number (car y)))
(< (plan-node-number (cdr x)) (plan-node-number (cdr y))))))
;; This lexically-orders not-between.
(defunction not-between-< (x y)
(or (< (plan-node-number (car x)) (plan-node-number (car y)))
(and (eql (plan-node-number (car x)) (plan-node-number (car y)))
(< (plan-node-number (cadr x)) (plan-node-number (cadr y))))
(and (eql (plan-node-number (car x)) (plan-node-number (car y)))
(eql (plan-node-number (cadr x)) (plan-node-number (cadr y)))
(< (plan-node-number (cddr x)) (plan-node-number (cddr y))))))
(defunction print-plan (plan stream depth)
(declare (ignore depth))
(princ "<plan " stream)
(princ (plan-number plan) stream)
(princ ">" stream))
(defunction plan-node (n)
(find-if #'(lambda (x) (eql (plan-node-number x) n)) *plan-nodes*))
(defstruct (causal-link (:print-function print-causal-link) (:conc-name nil))
(causal-link-number 0)
(causal-link-root nil)
(causal-link-goal nil)
(causal-link-target nil))
(defunction print-causal-link (link stream depth)
(declare (ignore depth))
(princ "<" stream)
(princ (plan-node-number (causal-link-root link)) stream) (princ " --" stream)
(prinp (causal-link-goal link) stream) (princ "--> " stream)
(if (eq (causal-link-target link) *finish*)
(princ "*finish*" stream)
(princ (plan-node-number (causal-link-target link)) stream))
(princ ">" stream))
(defunction build-causal-link (root goal target)
(let ((link
(find-if
#'(lambda (L)
(and (eq (causal-link-root L) root)
(eq (causal-link-target L) target)
(equal (causal-link-goal L) goal)))
*causal-links*)))
(when (null link)
(setf link
(make-causal-link
:causal-link-number (incf *causal-link-number*)
:causal-link-root root
:causal-link-goal goal
:causal-link-target target))
(push link *causal-links*))
link))
(defunction call-set (node plan)
(subset #'(lambda (L) (equal (causal-link-target L) node)) (causal-links plan)))
(defunction causal-link (n)
(find-if #'(lambda (x) (eql (causal-link-number x) n)) *causal-links*))
(defunction plan (n)
(find-if #'(lambda (p) (eql (plan-number p) n)) *plans*))
(defunction subplan (plan1 plan2)
(and (subsetp (plan-steps plan1) (plan-steps plan2))
(every #'(lambda (x)
(or (eq (cdr x) *finish*)
(mem (car x) (preceding-nodes (cdr x) plan2 (before-nodes plan2)))))
(before-nodes plan1))
(every #'(lambda (x)
(or (not (member (car x) (plan-steps plan1)))
(not (member (cdr x) (plan-steps plan1)))
(eq (cdr x) *finish*)
(mem (car x) (preceding-nodes (cdr x) plan1 (before-nodes plan1)))))
(before-nodes plan2))
(every #'(lambda (x)
(or (eq (cddr x) *finish*)
(mem x (not-between plan2))))
(not-between plan1))
(every #'(lambda (x)
(or (not (member (car x) (plan-steps plan1)))
(not (member (cadr x) (plan-steps plan1)))
(not (member (cddr x) (plan-steps plan1)))
(eq (cddr x) *finish*)
(mem x (not-between plan1))))
(not-between plan2))))
(defunction temporally-projectible (P)
(or (literal P)
(and (conjunctionp P)
(every #'(lambda (Q) (literal Q))
(conjuncts P)))))
(defun print-interest (x stream depth)
(declare (ignore depth))
(princ "#<" stream) (princ "Interest " stream)
(princ (interest-number x) stream)
; (princ ": " stream) (prinp-sequent (interest-sequent x) stream)
(princ ">" stream))
;;=============== substitution into plan formulas =============================
(defunction before-order (x y)
(or (< (inference-number (car x)) (inference-number (car y)))
(and (eql (inference-number (car x)) (inference-number (car y)))
(< (inference-number (cdr x)) (inference-number (cdr y))))))
(defunction not-between-order (x y)
(or (< (inference-number (car x)) (inference-number (car y)))
(and (eql (inference-number (car x)) (inference-number (car y)))
(< (inference-number (cadr x)) (inference-number (cadr y))))
(and (eql (inference-number (car x)) (inference-number (car y)))
(eq (inference-number (cadr x)) (inference-number (cadr y)))
(< (inference-number (cddr x)) (inference-number (cddr y))))))
(defun match-sublis (m x &key (test 'eq))
(cond ((eq m t) x)
(t (plan-sublis m x :test test))))
(defunction plan-sublis (m x &key test)
; (setf m* m x* x) (break)
(cond
((consp x)
(cons (plan-sublis m (car x) :test test) (plan-sublis m (cdr x) :test test)))
((plan-p x)
(let* ((plan-steps (plan-sublis m (plan-steps x) :test test))
(before-nodes (before-nodes x))
(not-between (not-between x))
(causal-links (causal-links x)))
(cond
((equal plan-steps (plan-steps x)) x)
(t
(let ((node-match (mapcar #'(lambda (x y) (cons x y)) (plan-steps x) plan-steps)))
(setf before-nodes (sublis node-match before-nodes))
(setf not-between (sublis node-match not-between))
(setf causal-links (sublis node-match causal-links))
(build-plan plan-steps (plan-goal x) causal-links before-nodes not-between))))))
((plan-node-p x)
(let ((action (plan-sublis m (plan-node-action x) :test test)))
(if (not (equal action (plan-node-action x)))
(let ((node
(make-plan-node
:plan-node-number (plan-node-number x)
:plan-node-action action)))
(draw-conclusion `(plan-node ,node) nil :given nil 1.0 1 nil nil)
node)
x)))
(t (let ((assoc (assoc x m :test test)))
(if assoc (cdr assoc) x)))))
(defunction plan-subst (x y p &key (test 'eq))
(cond
((consp p)
(cons (plan-subst x y (car p) :test test) (plan-subst x y (cdr p) :test test)))
((plan-p p)
(let* ((plan-steps (plan-subst x y (plan-steps p) :test test))
(before-nodes (before-nodes p))
(not-between (not-between p))
(causal-links (causal-links p)))
(cond
((equal plan-steps (plan-steps p)) p)
(t
(let ((node-match (mapcar #'(lambda (x y) (cons x y)) (plan-steps x) plan-steps)))
(setf before-nodes (sublis node-match before-nodes))
(setf not-between (sublis node-match not-between))
(setf causal-links (sublis node-match causal-links))
(build-plan plan-steps (plan-goal p) causal-links before-nodes not-between))))))
((plan-node-p p)
(let ((action (plan-subst x y (plan-node-action p) :test test)))
(if (not (equal action (plan-node-action p)))
(let ((node
(make-plan-node
:plan-node-number (plan-node-number p)
:plan-node-action action)))
(draw-conclusion `(plan-node ,node) nil :given nil 1.0 1 nil nil)
node)
p)))
(t (if (funcall test y p) x p))))
(defunction variable-correspondence (P Q P-vars Q-vars terms)
(cond
((equal P Q) terms)
((member P P-vars)
(let ((quantifier-variables (mem3 terms)))
(cond ((rassoc Q quantifier-variables) (throw 'unifier nil))
(t (list (cons P (mem1 terms)) (cons Q (mem2 terms)) quantifier-variables)))))
((member Q Q-vars)
(cond ((assoc P (mem3 terms)) (throw 'unifier nil))
(t (list (cons P (mem1 terms)) (cons Q (mem2 terms)) (mem3 terms)))))
((null P)
(cond ((null Q) terms)
(t (throw 'unifier nil))))
((null Q) (throw 'unifier nil))
((plan-p P)
(cond ((plan-p Q) (throw 'unifier nil))
((member Q Q-vars)
(cond ((assoc P (mem3 terms)) (throw 'unifier nil))
(t (list (cons P (mem1 terms)) (cons Q (mem2 terms)) (mem3 terms)))))
(t (throw 'unifier nil))))
((plan-node-p P)
(cond ((member Q Q-vars)
(cond ((assoc P (mem3 terms)) (throw 'unifier nil))
(t (list (cons P (mem1 terms)) (cons Q (mem2 terms)) (mem3 terms)))))
(t (throw 'unifier nil))))
((not (listp P))
(cond ((not (listp Q))
(cond ((member Q Q-vars)
(list (cons P (mem1 terms)) (cons Q (mem2 terms)) (mem3 terms)))
((eql P Q) terms)
((eql (cdr (assoc P (mem3 terms))) Q) terms)
(t (throw 'unifier nil))))
(t (throw 'unifier nil))))
((not (listp Q)) (throw 'unifier nil))
((or (eq (car P) 'all) (eq (car P) 'some))
(cond ((eql (car P) (car Q))
(variable-correspondence
(mem3 P) (mem3 Q) P-vars Q-vars
(list (mem1 terms) (mem2 terms)
(cons (cons (mem2 P) (mem2 Q)) (mem3 terms)))))
(t (throw 'unifier nil))))
(t
(variable-correspondence
(cdr P) (cdr Q) P-vars Q-vars
(variable-correspondence (car P) (car Q) P-vars Q-vars terms)))))
(defunction sequential-sublis (m X)
(cond ((eq (length m) 1)
(plan-subst (cdr (mem1 m)) (mem1 (mem1 m)) X))
(t (plan-subst (cdr (mem1 m)) (mem1 (mem1 m)) (sequential-sublis (cdr m) X)))))
;;======================= planning problems =================================
(defmacro make-planning-problem (&rest body)
(let* ((newbody (make-clauses body))
(number (cadr (find-if #'(lambda (x) (eq (car x) :number)) newbody)))
(start-time (cadr (find-if #'(lambda (x) (eq (car x) :start-time)) newbody)))
(msg (cadr (find-if #'(lambda (x) (eq (car x) :message)) newbody)))
(reasons
(mapcar 'eval (cdr (find-if #'(lambda (x) (eq (car x) :reasons)) newbody))))
(forwards-reasons (subset #'forwards-reason-p reasons))
(backwards-reasons (subset #'backwards-reason-p reasons))
(inputs
(mapcar #'(lambda (x) (list (car x) (reform-if-string (mem2 x)) (mem3 x)))
(cdr (find-if #'(lambda (x) (eq (car x) :inputs)) newbody))))
(premises (cdr (find-if #'(lambda (x) (eq (car x) :premises)) newbody)))
(goal (reform (cadr (find-if #'(lambda (x) (eq (car x) :goal)) newbody)))))
(when premises
(setf premises
(mapcar #'(lambda (p) (list (mem1 p) (mem2 p) (mem3 p) (mem4 p))) premises)))
`(progn
(setf *problem-number* ',number)
(setf *msg* ,msg)
(setf *start-time* (or ,start-time 0))
(setf *forwards-substantive-reasons* ',forwards-reasons)
(setf *backwards-substantive-reasons* ',backwards-reasons)
(setf *inputs* ',inputs)
(setf **premises** ',premises)
(setf *goal-state* ',goal)
(setf *fixed-ultimate-epistemic-interests* (list (plan-interest ',goal)))
(dolist (R *forwards-substantive-reasons*) (setf (undercutting-defeaters R) nil))
(dolist (R *backwards-substantive-reasons*) (setf (undercutting-defeaters R) nil))
(dolist (d *backwards-substantive-reasons*)
(dolist (R (reason-defeatees d)) (push d (undercutting-defeaters R))))
)))
(defunction plan-interest (goal)
(make-query
:query-number 1
:query-formula `(? plan (plan-for plan ,goal))
:query-strength 0.1
:positive-query-instructions
(list #'(lambda (c)
(cond ((and (or *display-plans* *display?*) (eql (old-undefeated-degree-of-support c) 0.0))
(reinstate-plan (mem2 (node-formula c))))
((and *display-plans* (not *display?*)) (display-plan (mem2 (node-formula c)))))))
:negative-query-instructions
(list #'(lambda (c)
(when (or *display-plans* *display?*)
(let* ((formula (node-formula c))
(plan (mem2 formula)))
(retract-plan plan)))))
))
(defunction display-planning-settings ()
(terpri)
(princ "(") (terpri)
(princ "======================================================================")
(terpri) (terpri)
(princ " ") (princ *version*) (princ " ")
(let ((time (multiple-value-list (get-decoded-time))))
(princ (mem5 time)) (princ "/") (princ (mem4 time)) (princ "/")
(princ (mem6 time)) (princ " ") (princ (mem3 time))
(princ ":") (if (< (mem2 time) 10) (princ "0")) (princ (mem2 time))
(princ ":") (if (< (mem2 time) 10) (princ "0")) (princ (mem1 time))
(terpri))
(princ " ") (princ *planner*) (terpri) (terpri)
(let ((message
(if *problem-number*
(if (and *msg* (read-from-string *msg* nil))
(cat-list (list "Problem number " (write-to-string *problem-number*) ": " *msg*))
(cat-list (list "Problem number " (write-to-string *problem-number*) ": ")))
*msg*)))
(when message (princ message) (terpri) (terpri)))
(princ "Forwards-substantive-reasons:") (terpri)
(dolist (R *forwards-substantive-reasons*)
(princ " ") (princ R) (terpri))
(terpri)
(princ "Backwards-substantive-reasons:") (terpri)
(dolist (R *backwards-substantive-reasons*)
(princ " ") (princ R) (terpri))
(terpri)
(when (not (zerop *start-time*))
(princ "Start reasoning at cycle ") (princ *start-time*) (terpri) (terpri))
(princ "Goal-state:") (terpri)
(dolist (p (conjuncts *goal-state*))
(princ " ") (princ (pretty p)) (terpri))
(terpri)
(princ "Inputs:") (terpri)
(dolist (x *inputs*)
(princ " ") (prinp (mem2 x)) (princ " : at cycle ") (princ (mem1 x))
(princ " with justification ") (princ (mem3 x)) (terpri))
(terpri)
(when **premises**
(setf **premises** (mapcar #'(lambda (x) (cons (reform-if-string (car x)) (cdr x))) **premises**))
(princ "Given:") (terpri)
(dolist (P **premises**)
(princ " ") (prinp (mem1 P)) (princ " : ")
(when (mem3 P) (princ " at cycle ") (princ (mem3 P)))
(princ " with justification = ") (princ (mem2 P)) (terpri))
(terpri))
; (setf *ultimate-epistemic-interests* *fixed-ultimate-epistemic-interests*)
(setf *query-number* 0)
(princ "======================================================================")
(terpri) (terpri))
(setf *reform-list* nil)
(let ((P (gensym)) (Q (gensym)))
; (pushnew `((,P is a plan for ,Q) (plan-for ,P ,Q) (,P ,Q)) *reform-list* :test 'equal)
(pushnew `((,P => ,Q) (=> ,P ,Q) (,P ,Q)) *reform-list* :test 'equal)
)
#|
(defunction complexity (x)
(cond ((null X) 0)
((stringp x) 1)
((atom x) 1)
((listp x)
(cond ((skolem-function (car x))
(cond ((null (cdr x)) 1)
((and (not (listp (cadr x))) (not (eq (cadr x) '=)))
*skolem-multiplier*)
((and (listp (cadr x)) (skolem-function (caar (cdr x))))
(* *skolem-multiplier* (1+ (complexity (cdr x)))))
(t (apply #'+ (mapcar #'complexity x)))))
((or (u-genp x) (e-genp x)) (* *quantifier-discount* (complexity (q-matrix x))))
((eq (car x) 'plan-for) (+ 2 (complexity (mem3 x))))
((eq (car x) 'protoplan-for) (+ 2 (complexity (mem3 x))))
((eq (car x) 'embellished-plan-for) (+ 2 (complexity (mem3 x))))
((consp (cdr x)) (apply #'+ (mapcar #'complexity x)))
(t (+ (complexity (car x)) (complexity (cdr x))))))))
|#
(defunction complexity (x)
(cond ((null X) 0)
((stringp x) 1)
; ((plan-p x) (length (plan-steps x)))
((plan-p x) (length (plan-steps x)))
((atom x) 1)
((listp x)
(cond ((skolem-function (car x))
(cond ((null (cdr x)) 1)
((and (not (listp (cadr x))) (not (eq (cadr x) '=)))
*skolem-multiplier*)
((and (listp (cadr x)) (skolem-function (caar (cdr x))))
(* *skolem-multiplier* (1+ (complexity (cdr x)))))
(t (apply #'+ (mapcar #'complexity x)))))
((or (u-genp x) (e-genp x)) (* *quantifier-discount* (complexity (q-matrix x))))
; ((eq (car x) 'protoplan-for)
; (+ 1
; (if (plan-p (mem2 x)) (length (plan-steps (mem2 x))) 1)
; (complexity (mem3 x))))
; ((eq (car x) 'plan-for)
; (+ 1
; (if (plan-p (mem2 x)) (length (plan-steps (mem2 x))) 1)
; (complexity (mem3 x))))
((eq (car x) '=>) 1)
((eq (car x) 'protoplan-for)
; (-
(+ 1
(if (plan-p (mem2 x)) (length (plan-steps (mem2 x))) 1)
; (complexity (mem3 x))
(length (mem4 x))))
; (+ (length (mem5 x)) (length (mem6 x)))))
((eq (car x) 'plan-for)
(+ 1
(if (plan-p (mem2 x)) (length (plan-steps (mem2 x))) 1)
))
; (complexity (mem3 x))))
((eq (car x) 'embellished-plan-for)
(+ 2 (length (plan-steps (mem3 x)))))
((eq (car x) 'embellished-protoplan-for)
(+ 2 (length (plan-steps (mem3 x)))))
((eq (car x) 'plan-undermines-causal-link) 1)
((eq (car x) 'plan-undermines-causal-links) 2)
; (1+ (complexity (list (mem2 x) (mem3 x) (mem4 x) (mem5 x)))))
; (+ 4 (if (plan-p (mem2 x)) (length (plan-steps (mem2 x))) 1))
((eq (car x) 'plan-node) .1)
(t (+ (complexity (car x)) (complexity (cdr x))))))
((consp (cdr x)) (apply #'+ (mapcar #'complexity x)))
(t 1)))
(defunction make-new-conclusion
(sequent deductive-only reductio-ancestors non-reductio-supposition)
(let* ((c-vars (formula-node-variables (sequent-formula sequent)))
(sup (sequent-supposition sequent))
(i-vars (supposition-variables sup))
(node
(make-inference-node
:inference-number (incf *inference-number*)
:node-sequent sequent
:node-formula (sequent-formula sequent)
:node-supposition sup
:node-kind :inference
:deductive-only deductive-only
:node-variables c-vars
:node-supposition-variables i-vars
:non-reductio-supposition non-reductio-supposition
:reductio-ancestors reductio-ancestors
)))
(when (and (listp (sequent-formula sequent))
(or (equal (car (sequent-formula sequent)) 'plan-for)
(equal (car (sequent-formula sequent)) 'protoplan-for)
(equal (car (sequent-formula sequent)) 'embellished-protoplan-for)
(equal (car (sequent-formula sequent)) 'embellished-plan-for)))
(push node (supporting-inference-nodes (mem2 (sequent-formula sequent))))
(when *display?*
(display-plan (mem2 (sequent-formula sequent)))))
node))
(defunction display-query (Q)
(princ " Interest in ") (prinp (query-formula Q)) (terpri)
(cond ((null (answered? Q))
(princ " is unsatisfied.")
(when (null (query-answers Q)) (princ " NO ARGUMENT WAS FOUND."))
(terpri))
((or (whether-query-p Q) (?-query-p Q))
(dolist (C (query-answers Q))
; (let ((C (car (query-answers Q))))
(when (>= (compute-undefeated-degree-of-support C) (query-strength Q))
(princ " is answered by node ")
(princ (inference-number C)) (princ ": ")
(princ (pretty (node-formula C))) (terpri)
(when (plan-p (mem2 (node-formula C)))
(show-plan (mem2 (node-formula C)) nil))
(let ((skolem-functions (skolem-functions (node-formula C))))
(when skolem-functions
(let* ((sf (mem1 skolem-functions))
(support-link
(find-if #'(lambda (SL)
(and (eq (support-link-rule SL) EI)
(occur sf (node-formula (support-link-target SL)))
(not (occur sf (node-formula (mem1 (support-link-basis SL)))))))
(ancestral-links C))))
(when support-link
(let* ((node (mem1 (support-link-basis support-link)))
(formula (node-formula node))
(var (q-variable formula)))
(princ " where ") (princ sf) (princ " is any ") (princ var)
(princ " such that ") (princ (q-matrix formula)) (princ ",") (terpri)
(princ " and the existence of such")
(if (equal var "x") (princ " an ") (princ " a ")) (princ var)
(princ " is guaranteed by node ") (princ (inference-number node)) (terpri))))))
)))
(t (dolist (C (query-answers Q))
(when (>= (compute-undefeated-degree-of-support C) (query-strength Q))
(princ " is answered affirmatively by node ")
(princ (inference-number C)) (terpri)))))
(princ "---------------------------------------------------") (terpri))
(defunction display-node
(n proof-nodes nodes-used interests-used interests-discharged nodes-displayed)
; (setf nn n pn proof-nodes nu nodes-used iu interests-used id interests-discharged nd nodes-displayed) ; (break)
;; (step (display-node nn pn nu iu id nd))
(when (eq (node-kind n) :percept)
(princ "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||")
(terpri) (princ "It appears to me that ") (prinp (node-formula n)) (terpri)
(princ "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||")
(terpri))
(princ " # ") (princ (inference-number n))
(when (member n *not-strictly-relevant-nodes*) (princ " NOT STRICTLY RELEVANT"))
(terpri)
(princ " ") (when (eq (node-kind n) :percept) (princ "It appears to me that "))
(prin-pretty (node-formula n))
(when (node-supposition n)
(princ " supposition: ") (set-prinp (node-supposition n)))
(if (zerop (undefeated-degree-of-support n)) (princ " DEFEATED"))
(when (and (member n nodes-used)
(not (member n proof-nodes)))
(princ " -- NOT USED IN PROOF"))
(terpri)
(cond ((keywordp (node-justification n)) (princ " ") (princ (node-justification n)) (terpri))
((support-links n)
(princ " Inferred by:") (terpri)
(dolist (L* (support-links n))
(when (subsetp (support-link-basis L*) nodes-displayed)
(princ " support-link #") (princ (support-link-number L*))
(princ " from ") (princ-set (mapcar #'inference-number (support-link-basis L*)))
(princ " by ") (princ (support-link-rule L*))
(when (support-link-clues L*)
(princ " with clues ")
(princ-set (mapcar #'inference-number (support-link-clues L*))))
(when (support-link-defeaters L*)
(princ " defeaters: ")
(princ-set (mapcar #'inference-number (support-link-defeaters L*))))
(when (defeating-assignment-trees L*) (princ " DEFEATED"))
(terpri)))))
(when (node-defeatees n)
(princ " defeatees: ")
(princ "{ ")
(let ((L (car (node-defeatees n))))
(princ "link ")
(princ (support-link-number L)) (princ " for node ")
(princ (inference-number (support-link-target L))))
(dolist (L (cdr (node-defeatees n)))
(princ " , ")
(princ "link ")
(princ (support-link-number L)) (princ " for node ")
(princ (inference-number (support-link-target L))))
(princ " }") (terpri))
; (princ " by ") (princ (node-justification n))
(let ((generating-interests (intersection (generating-interests n) interests-used)))
(cond ((> (length generating-interests) 1)
(princ " generated by interests ")
(print-list (mapcar #'interest-number generating-interests) 40))
((eq (length generating-interests) 1)
(princ " generated by interest ")
(princ (interest-number (mem1 generating-interests)))))
(when generating-interests (terpri)))
(let ((DI (enabling-interests n)))
(cond
((> (length DI) 1)
(princ " This node is inferred by discharging links to interests ")
(princ (mapcar #'interest-number DI)) (terpri))
(DI
(princ " This node is inferred by discharging a link to interest #")
(princ (interest-number (car DI)))
(let ((SL (find-if #'(lambda (SL) (equal (support-link-rule SL) :reductio)) (support-links n))))
(when SL
(let* ((node* (mem1 (support-link-basis SL)))
(rs (find-if
#'(lambda (sup)
(and
(member (car DI) (generating-interests sup))
;;
(mem sup (a-range (reductio-ancestors node*)))
(not (mem sup (a-range (reductio-ancestors n))))))
(a-range (reductio-ancestors node*)))))
;; (generated-suppositions (car DI)))))
(when rs
(princ " as a result of inferring node #") (princ (inference-number node*))
(princ " from") (terpri) (princ " reductio-supposition #")
(princ (inference-number rs)) (princ ", which was generated by interest #")
(princ (interest-number (car DI)))))))
(terpri)))
(let ((interests
(subset
#'(lambda (in)
(and
(member n (discharging-nodes in))
(or
(some
#'(lambda (dr)
(some
#'(lambda (pn)
(some
#'(lambda (SL)
(and (equal (support-link-rule SL) :reductio)
(member n (support-link-basis SL))
(member (car dr) (support-link-basis SL))))
(support-links pn)))
proof-nodes))
(direct-reductio-interest in))
(some #'(lambda (L)
(and
(discharged-link L)
(or
(equal (link-rule L) :answer)
(some
#'(lambda (pn)
(and
(member (resultant-interest L) (enabling-interests pn))
(some
#'(lambda (SL)
(member n (support-link-basis SL)))
(support-links pn))))
proof-nodes))))
(right-links in)))))
(set-difference interests-used DI))))
(cond ((> (length interests) 1)
(princ " This discharges interests ") (print-list (mapcar #'interest-number interests) 40))
((eq (length interests) 1)
(princ " This discharges interest ") (princ (interest-number (mem1 interests))))
(t
(setf interests
(subset
#'(lambda (in)
(and
(member n (discharging-nodes in))
(not
(some
#'(lambda (dn)
(strongly-discharging-node dn in proof-nodes interests-discharged))
(discharging-nodes in)))))
(set-difference interests-used DI)))
(cond
((> (length interests) 1)
(princ " This discharges interests ") (print-list (mapcar #'interest-number interests) 40)
(princ " but no inference made by discharging these interests is used in the solution."))
((eq (length interests) 1)
(princ " This discharges interest ") (princ (interest-number (mem1 interests)))
(princ " but no inference made by discharging this interest is used in the solution.")))))
(when interests (terpri))))
(when (and (listp (node-formula n))
(or (eq (car (node-formula n)) 'plan-for)
(eq (car (node-formula n)) 'protoplan-for)
(eq (car (node-formula n)) 'embellished-protoplan-for)))
(display-plan (mem2 (node-formula n))))
(when *graphics-pause* (pause-graphics)) ;; wait for a character to be typed in the Listener
(when (and *graph-log* (member n proof-nodes))
(push n *nodes-displayed*)
(draw-n n *og* nodes-displayed)))
(defunction display-used-interest
(interest proof-nodes used-nodes used-interests enabling-interests interests-used-in-proof)
; (when (eq interest (interest 6)) (setf i interest pn proof-nodes un used-nodes ui used-interests ein enabling-interests iun interests-used-in-proof) (break))
;; (step (display-used-interest i pn un ui ein iun))
(princ " # ") (princ (interest-number interest))
(when (not (member interest interests-used-in-proof))
(princ " NOT USED IN PROOF"))
(terpri)
(princ " ")
(when (deductive-interest interest) (princ "deductive "))
(when (reductio-interest interest) (princ "reductio "))
(princ "interest: ") (prin-pretty (interest-formula interest))
(when (interest-supposition interest)
(princ " supposition: ")
(set-prinp (interest-supposition interest)))
(terpri)
(when
(some #'(lambda (L) (query-p (resultant-interest L)))
(right-links interest))
(princ " This is of ultimate interest") (terpri))
(dolist (L (right-links interest))
(when (and (not (query-p (resultant-interest L))) (discharged-link L)
(member (resultant-interest L) used-interests))
(princ " For ")
(when (reductio-interest (resultant-interest L)) (princ "reductio "))
(princ "interest ")
(princ (interest-number (resultant-interest L)))
(princ " by ") (princ (link-rule L))
(let ((nodes (supporting-nodes L)))
(when nodes
(cond ((equal (length nodes) 1)
(princ " using node ")
(princ (inference-number (mem1 nodes))))
(t
(princ " using nodes ")
(print-list (mapcar
#'(lambda (conclusion)
(inference-number conclusion))
nodes) 40)))))
(let ((nodes (link-clues L)))
(when nodes
(cond ((equal (length nodes) 1)
(princ " with clue ")
(princ (inference-number (mem1 nodes))))
(t
(princ " with clues ")
(print-list (mapcar
#'(lambda (conclusion)
(inference-number conclusion))
nodes) 40)))))
(terpri)))
(let ((direct-reductio-interest
(subset #'(lambda (n) (assoc n (direct-reductio-interest interest)))
used-nodes)))
(cond ((> (length direct-reductio-interest) 1)
(princ " Reductio interest generated by nodes ")
(print-list (mapcar #'(lambda (n) (inference-number n))
direct-reductio-interest) 40) (terpri))
((= (length direct-reductio-interest) 1)
(princ " Reductio interest generated by node ")
(princ (inference-number (mem1 direct-reductio-interest))) (terpri))))
(let ((discharging-nodes
(subset
#'(lambda (dn)
(strongly-discharging-node dn interest proof-nodes enabling-interests))
(intersection (discharging-nodes interest) used-nodes)))
(defeatees
(subset #'(lambda (L)
(member (support-link-target L) proof-nodes))
(interest-defeatees interest))))
(when defeatees
(princ " Of interest as a defeater for ")
(cond ((cdr defeatees)
(princ "support-links: ")
(princ "{ ")
(let ((L (car defeatees)))
(princ "link ")
(princ (support-link-number L)) (princ " for node ")
(princ (inference-number (support-link-target L))))
(dolist (L (cdr defeatees))
(princ " , ")
(princ "link ")
(princ (support-link-number L)) (princ " for node ")
(princ (inference-number (support-link-target L))))
(princ " }"))
(t
(princ "support-link ")
(let ((L (car defeatees)))
(princ (support-link-number L)) (princ " for node ")
(princ (inference-number (support-link-target L))))))
(terpri))
(cond
(discharging-nodes
(princ " This interest is discharged by")
(cond ((> (length discharging-nodes) 1) (princ " nodes ")
(princ (mapcar #'inference-number discharging-nodes)))
(t (princ " node ") (princ (inference-number (mem1 discharging-nodes)))))
(terpri))
((not (some #'(lambda (L) (and (query-p (resultant-interest L)) (discharged-link L)))
(right-links interest)))
(setf discharging-nodes
(intersection (discharging-nodes interest) used-nodes))
(cond
(discharging-nodes
(princ " This interest is discharged by")
(cond ((> (length discharging-nodes) 1) (princ " nodes ")
(princ (mapcar #'inference-number discharging-nodes)))
(t (princ " node ") (princ (inference-number (mem1 discharging-nodes)))))
(terpri)
(when
(and (null defeatees) (member interest interests-used-in-proof))
(princ " but no inference made by discharging this interest is used in the solution.")
(terpri)))
((and (null defeatees) (member interest interests-used-in-proof))
(princ " No inference made by discharging this interest is used in the solution.") (terpri)))
)))
(when (and *graph-interests* *graph-log*)
(when *graphics-pause* (pause-graphics)) ;; wait for a character to be typed in the Listener
(draw-i interest *og*)))
(defunction display-belief-changes (links new-beliefs new-retractions)
; (setf l links nb new-beliefs nr new-retractions)
; (when (member (support-link 12) links) (setf l links nb new-beliefs nr new-retractions) (break))
;; (step (display-belief-changes l nb nr))
(when *monitor-assignment-tree* (monitor-assignment-tree links))
; (when (and (not (complete-tree *assignment-tree*)) (null *deductive-only*))
; (princ links) (terpri) (break))
(when (or *display?* *log-on*)
(cond
((and (not *deductive-only*) (or new-beliefs new-retractions))
(when (and *display?*
(some #'(lambda (n) (not (some #'(lambda (L) (eq n (support-link-target L))) links)))
(append new-beliefs new-retractions)))
(princ "---------------------------------------------------------------------------") (terpri)
(princ "Affected-nodes:") (terpri)
(princ *affected-nodes*) (terpri)
(princ "---------------------------------------------------------------------------") (terpri)
(when *discards*
(princ "discarding: ") (dolist (d *discards*) (princ d) (princ " ")) (terpri))
(when *creations*
(princ "creating: ") (dolist (d *creations*) (princ d) (princ " ")) (terpri))
(when (or *discards* *creations*)
(princ "---------------------------------------------------------------------------")
(terpri))
(princ "Recomputed assignment-tree:") (terpri)
(display-assignment-tree))
(when (and *display?* *graphics-on*)
(when *graphics-pause* (pause-graphics))
(dolist (link links) (draw-n (support-link-target link) *og* *nodes-displayed*))
(when
(set-difference (append new-beliefs new-retractions) (mapcar #'support-link-target links))
(flash-nodes *affected-nodes* *og* *yellow-color* 5)))
(when
new-retractions
(when *log-on*
(push " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv" *reasoning-log*))
(when *display?* (princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri)))
(dolist (N new-retractions)
(cond ((or (not (some #'(lambda (L) (eq N (support-link-target L))) links))
(> (length (support-links N)) 1))
(cond
((zerop (undefeated-degree-of-support N))
(when *log-on*
(push (list :defeated N) *reasoning-log*))
(when *display?*
(princ " ") (princ N) (princ " has become defeated.") (terpri)
(princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri))
(when (and *display?* *graphics-on*)
(let ((posi (node-position N *og*)))
(when posi
(when (and (boundp '*speak*) *speak*)
(speak-text "Node ")
(speak-text (write-to-string (inference-number N)))
(speak-text "has become defeated."))
(draw-just-defeated-node posi *og* N)))))
(t
(when *log-on*
(push (list :decreased-support N (undefeated-degree-of-support N))
*reasoning-log*))
(when *display?*
(princ " The undefeated-degree-of-support of ") (princ N)
(princ " has decreased to ") (princ (undefeated-degree-of-support N)) (terpri)
(princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri)))))
((zerop (undefeated-degree-of-support N))
(when *log-on*
(push (list :defeated N) *reasoning-log*))
(when *display?*
(princ " ") (princ N) (princ " is defeated.") (terpri)
(princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri))
(when (and *display?* *graphics-on*)
(when (and (boundp '*speak*) *speak*)
(speak-text "Node ")
(speak-text (write-to-string (inference-number N)))
(speak-text "is defeated."))
(let ((posi (node-position n *og*)))
(cond (posi (draw-just-defeated-node posi *og* n))
(t
(draw-n n *og* *nodes-displayed*)
(push n *nodes-displayed*)
(setf posi (node-position n *og*))
(draw-just-defeated-node posi *og* n)))))))
))
((and *display?* *graphics-on*)
(when *graphics-pause* (pause-graphics))
(dolist (link links) (draw-n (support-link-target link) *og* *nodes-displayed*)))))
(when (and *display?* *graphics-on*)
(setf *nodes-displayed* (union (mapcar #'support-link-target links) *nodes-displayed*))))
(defunction display-peripherals (x boundary nodes-used)
(cond
((equal x " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (setf boundary t))
((listp x)
(cond ((and (equal (mem1 x) :increased-support) (member (mem2 x) nodes-used))
(when (equal boundary t)
(princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri)
(setf boundary nil))
(princ " The undefeated-degree-of-support of ") (princ (mem2 x))
(princ " has increased to ") (princ (mem3 x))
(terpri) (princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri)
(when *graph-log*
(let ((posi (node-position (mem2 x) *og*)))
(when posi
(when (and (boundp '*speak*) *speak*)
(speak-text "The undefeeted-degree-of-support of node ")
(speak-text (write-to-string (inference-number (mem2 x))))
(speak-text "has increased to")
(speak-text (write-to-string (mem3 x))))
(draw-just-undefeated-node posi *og* (mem2 x))))))
((and (equal (mem1 x) :new-support) (member (mem2 x) nodes-used)
(not (eql (mem3 x) 1.0)))
(when (equal boundary t)
(princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri)
(setf boundary nil))
(princ " The undefeated-degree-of-support of ") (princ (mem2 x))
(princ " is ") (princ (mem3 x))
(terpri) (princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri)
(when *graph-log*
(let ((posi (node-position (mem2 x) *og*)))
(when posi
(when (and (boundp '*speak*) *speak*)
(speak-text "The undefeeted-degree-of-support of node ")
(speak-text (write-to-string (inference-number (mem2 x))))
(speak-text "is")
(speak-text (write-to-string (mem3 x))))
(draw-just-undefeated-node posi *og* (mem2 x))))))
((and (equal (mem1 x) :defeated) (member (mem2 x) nodes-used))
(when (equal boundary t)
(princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri)
(setf boundary nil))
(princ " ") (princ (mem2 x)) (princ " has become defeated.") (terpri)
(princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri)
(when *graph-log*
(let ((posi (node-position (mem2 x) *og*)))
(when posi
(when (and (boundp '*speak*) *speak*)
(speak-text "Node ")
(speak-text (write-to-string (inference-number (mem2 x))))
(speak-text "has become defeated."))
(draw-just-defeated-node posi *og* (mem2 x))))))
((and (equal (mem1 x) :decreased-support) (member (mem2 x) nodes-used))
(when (equal boundary t)
(princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri)
(setf boundary nil))
(princ " The undefeated-degree-of-support of ") (princ (mem2 x))
(princ " has decreased to ") (princ (mem3 x))
(terpri) (princ " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv") (terpri))
((and (equal (mem1 x) :answer-query) (member (mem2 x) nodes-used))
(princ " ")
(princ "=========================================") (terpri)
(princ " ") (princ "Justified belief in ")
(prinp (node-formula (mem2 x))) (terpri)
(princ " with undefeated-degree-of-support ") (princ (mem4 x)) (terpri)
(princ " ") (princ "answers ") (princ (mem3 x))
(terpri) (princ " ")
(princ "=========================================") (terpri)
(when (equal (mem1 (node-formula (mem2 x))) 'protoplan-for)
(adopt-plan (mem2 (node-formula (mem2 x)))))
(when (equal (mem1 (node-formula (mem2 x))) 'plan-for)
(adopt-plan (mem2 (node-formula (mem2 x)))))
(when (and (boundp '*speak*) *speak*)
(speak-text "Node ")
(speak-text (write-to-string (inference-number (mem2 x))))
(speak-text "answers query ")
(speak-text (write-to-string (query-number (mem3 x))))))
((and (equal (mem1 x) :retract-answer) (member (mem2 x) nodes-used))
(princ " ")
(princ "=========================================") (terpri)
(princ " ") (princ "Lowering the undefeated-degree-of-support of ")
(prinp (node-formula (mem2 x))) (terpri)
(princ " ") (princ "retracts the previous answer to ")
(princ (mem3 x)) (terpri) (princ " ")
(princ "=========================================") (terpri)
(when (equal (mem1 (node-formula (mem2 x))) 'plan-for)
(retract-plan (mem2 (node-formula (mem2 x)))))
(when (equal (mem1 (node-formula (mem2 x))) 'protoplan-for)
(retract-plan (mem2 (node-formula (mem2 x)))))
(when (and (boundp '*speak*) *speak*)
(speak-text "Node ")
(speak-text (write-to-string (inference-number (mem2 x))))
(speak-text "no longer answers query ")
(speak-text (write-to-string (query-number (mem3 x)))))))))
boundary)
(defunction display-interest (interest)
(if (numberp interest) (setf interest (interest interest)))
(princ " # ") (princ (interest-number interest)) (princ " ")
(when (deductive-interest interest) (princ "deductive "))
(when (reductio-interest interest) (princ "reductio "))
(princ "interest:")