-
Notifications
You must be signed in to change notification settings - Fork 2
/
oscar-core.lisp
9810 lines (9338 loc) · 552 KB
/
oscar-core.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
;;;; OSCAR_3.31 (the beginning part has been moved to base.lisp)
(in-package "OSCAR")
(eval-when (:load-toplevel :execute)
(export '(trace-on trace-off trace-from display-on display-off display-from
proof-on proof-off logic-on logic-off reductio-on reductio-off
log-on log-off IQ-on IQ-off graph-log-on graph-log-off
graphics-pause-on graphics-pause-off
test think)))
;======================================================
; -------------------------------------- CONCLUSIONS --------------------------------------
(defstruct (d-node (:conc-name nil) (:print-function print-d-node))
d-node-number
(d-node-description nil)
(discrimination-tests nil)
(d-node-c-lists nil)
(d-node-i-lists nil)
(parent-d-node nil)
(d-node-forwards-reasons nil) ;; a list of partially-instantiated-premises
(d-node-backwards-reasons nil) ;; a list of non-degenerate backwards-reasons
(d-node-interest-schemes nil) ;; a list of partially-instantiated-premises
(d-node-degenerate-backwards-reasons nil)
)
(defunction print-d-node (x stream depth)
(declare (ignore depth))
(princ "#<" stream) (princ "d-node: " stream)
(princ (d-node-number x) stream) (princ ">" stream))
(defunction d-node (n)
(find-if #'(lambda (dn) (eql (d-node-number dn) n)) *discrimination-net*))
(defunction display-d-node (dn depth test)
; (setf d dn de depth te test)
;; (step (display-d-node d de te))
(let ((pp *print-pretty*))
(setf *print-pretty* nil)
(line-indent depth)
(princ "--")
(if test (prinp-test test) (princ dn))
(princ " ")
(terpri)
(dolist (cl (d-node-c-lists dn)) (line-indent depth) (princ " ") (princ cl) (terpri))
(dolist (il (d-node-i-lists dn)) (line-indent depth) (princ " ") (princ il) (terpri))
(dolist (ip (d-node-forwards-reasons dn))
(line-indent depth)
(cond ((ip-basis ip) (princ " instantiated-premise ") (princ (ip-number ip)) (princ " for "))
(t (princ " first premise for ")))
(princ (ip-reason ip))
(princ ": ")
(let ((p (match-sublis (ip-binding ip) (fp-formula (ip-premise ip)))))
(if (and (negationp p) (negationp (negand p))) (setf p (negand (negand p))))
(prinp p))
(terpri))
(dolist (br (d-node-backwards-reasons dn)) (line-indent depth) (princ " conclusion for ")
(princ br) (terpri))
(dolist (br (d-node-degenerate-backwards-reasons dn)) (line-indent depth) (princ " conclusion for ")
(princ br) (terpri))
(dolist (is (d-node-interest-schemes dn))
(line-indent depth)
(princ " interest-scheme ") (princ (is-number is)) (princ " for interest ")
(princ (interest-number (is-target-interest is)))
(princ " by ")
(princ (is-reason is))
(princ ": ")
(let ((p (match-sublis (is-binding is) (fp-formula (is-premise is)))))
(if (and (negationp p) (negationp (negand p))) (setf p (negand (negand p))))
(prinp p))
(terpri))
(setf *print-pretty* pp)))
(defunction formula-code (P)
(setf *quantifier-number* 0)
(multiple-value-bind (code term-list) (formula-code* P nil)
(values (reverse code) term-list)))
(defunction formula-code* (P descriptor)
(cond ((listp P)
(let ((description nil) (elt-num 1) (term-list nil))
(cond
;; This handles notational variants.
((or (eq (car p) 'all) (eq (car P) 'some))
(setf P
(cons (car P) (subst (list 'q-var (incf *quantifier-number*)) (mem2 P) (cddr P)))))
((eq (car P) 'at)
(setf term-list (cddr P))
(setf P (list (car P) (cadr P))))
((eq (car P) 'throughout)
(setf term-list (cdr (mem3 P)))
(setf P (list (car P) (cadr P) (list (car (mem3 P))))))
((and (symbolp (car P))
(not (member (car P) *operators*))
(not (member (car P) '(~ & v -> <-> all some ? @))))
(setf term-list (cdr P))
(setf P (list (car P)))))
(dolist (Q P)
(let ((Q-descriptor (cons elt-num descriptor)))
(cond ((not (listp Q))
(push (cons (reverse Q-descriptor) Q) description))
(t
(multiple-value-bind (d tl) (formula-code* Q Q-descriptor)
(setf term-list (append tl term-list))
(setf description (append d description))
)))
(incf elt-num)))
(values description term-list)))
(t (values (list (cons descriptor P)) nil))))
(defun display-discrimination-net (&optional (nodes *discrimination-net*))
(setf *callees* nil)
(setf *blank-line* nil)
(setf *line-columns* nil)
(display-discrimination-node *top-d-node* nil 0 t nodes)
nil)
(defun ddn (&optional (nodes *discrimination-net*)) (display-discrimination-net nodes))
(defun display-discrimination-node (d-node listees depth last? nodes &optional test)
(when (member d-node nodes)
(when (null depth)
(setf depth 0) (setf listees nil))
(cond ((or (mem d-node listees) (mem d-node *callees*))
(line-indent depth)
(when (not (mem depth *line-columns*)) (princ "|"))
(princ "--") (princ d-node) (princ " .....") (terpri)
(setf *blank-line* nil)
(cond (last? (pull depth *line-columns*))
(t (pushnew depth *line-columns* :test 'eql))))
(t
(let* ((DC (discrimination-tests d-node))
(number (length (discrimination-tests d-node)))
(number* (round (/ number 2)))
(draw-line?
(or (mem d-node listees)
(mem d-node *callees*)
(some #'(lambda (C) (not (mem c listees))) (discrimination-tests d-node)))))
(pushnew d-node listees :test 'equal)
(push d-node *callees*)
(when (and (not *blank-line*) (> number* 0))
(line-indent depth) (terpri) (setf *blank-line* t))
(dotimes (n number*)
(let ((test (mem1 DC)))
(cond
((zerop n)
(display-discrimination-node (cdr test) listees (1+ depth) nil nodes test))
((cdr DC) (display-discrimination-node (cdr test) listees (1+ depth) nil nodes test))
(t (display-discrimination-node (cdr test) listees (1+ depth) t nodes test))))
(setf DC (cdr DC)))
(pushnew depth *line-columns* :test 'eql)
(display-d-node d-node depth test)
(setf *blank-line* nil)
(when last? (pull depth *line-columns*))
(when (> number 0) (pushnew (1+ depth) *line-columns* :test 'eql))
(dolist (test DC)
(cond
((cdr DC)
(display-discrimination-node (cdr test) listees (1+ depth) nil nodes test))
(t (display-discrimination-node (cdr test) listees (1+ depth) t nodes test)))
(setf DC (cdr DC)))
(when
(and (not *blank-line*) draw-line?)
(line-indent depth) (terpri) (setf *blank-line* t))
)))))
#| The list of instantiated-premises for a forwards-reason. |#
(defunction reason-ips (reason)
(let ((ips nil))
(dolist (dn *discrimination-net*)
(dolist (ip (d-node-forwards-reasons dn))
(when (equal (ip-reason ip) reason) (push ip ips))))
ips))
#| The list of interest-schemes for a backwards-reason. |#
(defunction reason-iss (reason)
(let ((iss nil))
(dolist (dn *discrimination-net*)
(dolist (is (d-node-interest-schemes dn))
(when (equal (ip-reason is) reason) (push is iss))))
iss))
(defunction d-node-ancestors (dn)
(let ((pn (parent-d-node dn)))
(when pn (cons pn (d-node-ancestors pn)))))
(defunction d-node-descendants (dn)
(when (discrimination-tests dn)
(let ((nodes (a-range (discrimination-tests dn))))
(append nodes (unionmapcar #'d-node-descendants nodes)))))
#| Display the part of the discrimination-net that contains d-node number n. |#
(defunction show-d-node (n)
(let* ((dn (if (numberp n) (d-node n) n))
(nodes (cons dn (append (d-node-ancestors dn) (d-node-descendants dn)))))
(display-discrimination-net nodes)))
(defunction show-interest (n)
(let* ((in (if (numberp n) (interest n) n))
(dn (i-list-d-node (interest-i-list in)))
(nodes (cons dn (append (d-node-ancestors dn) (d-node-descendants dn)))))
(display-discrimination-net nodes)))
(defunction show-node (n)
(let* ((node (if (numberp n) (node n) n))
(dn (c-list-d-node (node-c-list node)))
(nodes (cons dn (append (d-node-ancestors dn) (d-node-descendants dn)))))
(display-discrimination-net nodes)))
#| This displays all d-nodes directly relevant to the reason. |#
(defunction show-reason (reason)
(let ((nodes nil))
(cond ((forwards-reason-p reason)
(dolist (dn *discrimination-net*)
(when (some #'(lambda (ip) (equal (ip-reason ip) reason)) (d-node-forwards-reasons dn))
(push dn nodes))))
((backwards-reason-p reason)
(dolist (dn *discrimination-net*)
(when
(or (member reason (d-node-backwards-reasons dn))
(member reason (d-node-degenerate-backwards-reasons dn))
(some #'(lambda (is) (equal (is-reason is) reason)) (d-node-interest-schemes dn)))
(push dn nodes)))))
(setf nodes
(unionmapcar+
#'(lambda (dn) (cons dn (append (d-node-ancestors dn) (d-node-descendants dn))))
nodes))
(display-discrimination-net nodes)))
(defunction prinp-test (test)
(princ "(") (princ (caar test)) (princ " . ") (prinp (cdar test)) (princ ") : ") (princ (cdr test)))
(defstruct (c-list (:print-function print-c-list)
(:conc-name nil))
(c-list-formula nil)
(corresponding-i-lists nil)
(c-list-nodes nil)
(c-list-processed-nodes nil)
(link-defeatees nil)
(reductio-interests nil)
(c-list-variables nil)
(c-list-contradictors nil)
(c-list-term-list nil)
(c-list-d-node nil)
(generated-instantiated-premises nil)
(supported-interest-schemes nil))
(defun print-c-list (x stream depth)
(declare (ignore depth))
(princ "#<c-list for " stream)
(prinp (c-list-formula x) stream) (princ ">" stream))
(defunction processed-c-list-for (formula)
(cdr (find-if #'(lambda (cl) (notational-variant formula (car cl))) *processed-conclusions*)))
(defunction notational-variant (p q &optional vars)
(cond ((null p) (null q))
((listp p)
(and (listp q)
(cond ((and (or (eq (car p) 'some) (eq (car p) 'all))
(eq (car p) (car q)))
(notational-variant (cdr p) (cdr q)
(cons (cons (cadr p) (cadr q)) vars)))
((listp (car q)) (and (notational-variant (car p) (car q) vars)
(notational-variant (cdr p) (cdr q) vars)))
((or (eql (car p) (car q))
(and vars
(mem (cons (car p) (car q)) vars)))
(notational-variant (cdr p) (cdr q) vars)
))))
(t (and (not (listp q))
(or (eql p q)
(mem (cons p q) vars))))))
(defunction nodes-for (formula)
(let ((c-list (c-list-for formula)))
(if c-list (c-list-nodes c-list))))
(defunction processed-nodes-for (formula)
(let ((c-list (processed-c-list-for formula)))
(if c-list (c-list-nodes c-list))))
(defunction display-conclusions ()
(princ "(") (terpri)
(princ "================== CONCLUSIONS ===================")
(let* ((**conclusions**
(unionmapcar
#'(lambda (dn)
(unionmapcar
#'(lambda (cl) (c-list-nodes cl)) (d-node-c-lists dn)))
*discrimination-net*))
(conclusions
(order **conclusions**
#'(lambda (c1 c2)
(< (inference-number c1) (inference-number c2))))))
(dolist (conclusion conclusions)
(display-conclusion conclusion)
(terpri) (princ "---------------------------------------------------")))
(princ ")") (terpri))
(defunction display-conclusion (n)
(terpri) (princ n)
(when (not (equal (node-kind n) :inference))
(terpri) (princ " kind: ") (princ (node-kind n)))
; (terpri) (princ " maximal-degree-of-support: ") (princ (compute-maximal-degree-of-support n))
(terpri) (princ " undefeated-degree-of-support: ") (princ (compute-undefeated-degree-of-support n))
(dolist (Q (answered-queries n))
(terpri) (princ " This answers ") (princ Q)))
(defunction display-conclusions-by-supposition ()
(princ "(") (terpri)
(let ((suppositions nil))
(dolist (dn *discrimination-net*)
(dolist (cl (d-node-c-lists dn))
(dolist (c (c-list-nodes cl))
(pushnew (node-supposition c) suppositions :test '==)
(setf suppositions
(order suppositions
#'(lambda (s1 s2)
(or (< (length s1) (length s2))
(and (= (length s1) (length s2))
(lessp s1 s2)))))))))
(let* ((**conclusions**
(unionmapcar
#'(lambda (dn)
(unionmapcar
#'(lambda (cl) (c-list-nodes cl)) (d-node-c-lists dn)))
*discrimination-net*)))
(dolist (sup suppositions)
(princ "==========================================") (terpri)
(princ "Conclusions with supposition ") (set-prinp sup) (princ ":") (terpri)
(let* ((sup-conclusions
(subset #'(lambda (c) (== (node-supposition c) sup))
**conclusions**))
(conclusions
(order sup-conclusions
#'(lambda (c1 c2)
(< (inference-number c1) (inference-number c2))))))
(dolist (c conclusions)
(when (== (node-supposition c) sup)
(princ " #") (princ (inference-number c)) (princ " ")
(prinp (node-formula c)) (terpri)))))))
(princ ")") (terpri))
(defunction display-c-lists ()
(princ "(") (terpri)
(dolist (dn *discrimination-net*)
(dolist (cl (d-node-c-lists dn))
(princ "==========================================") (terpri)
(princ "c-list-formula: ") (prinp (c-list-formula cl)) (terpri)
(let ((conclusions
(order (c-list-nodes cl)
#'(lambda (c1 c2)
(let ((s1 (node-supposition c1))
(s2 (node-supposition c2)))
(or (< (length s1) (length s2))
(and (= (length s1) (length s2))
(lessp s1 s2))))))))
(dolist (c conclusions)
(princ " #") (princ (inference-number c))
(princ " sup = ") (set-prinp (node-supposition c))
(terpri)))))
(princ ")") (terpri))
(defunction display-processed-c-lists ()
(princ "(") (terpri)
(dolist (cl *processed-conclusions*)
(princ "==========================================") (terpri)
(princ "c-list-formula: ") (prinp (car cl)) (terpri)
(let ((conclusions
(order (c-list-nodes (cdr cl))
#'(lambda (c1 c2)
(let ((s1 (node-supposition c1))
(s2 (node-supposition c2)))
(or (< (length s1) (length s2))
(and (= (length s1) (length s2))
(lessp s1 s2))))))))
(dolist (c conclusions)
(princ " #") (princ (inference-number c))
(princ " sup = ") (set-prinp (node-supposition c))
(terpri))))
(princ ")") (terpri))
(defunction ?-variables (formula)
(cond ((and formula (listp formula))
(union (?-variables (car formula)) (?-variables (cdr formula))))
((atom formula)
(if (equal (car (explode (write-to-string formula))) "?") (list formula)))))
#| (? formula), where formula can contain variables of the form "?x", returns a list of all
known conclusions matching the formula. |#
(defunction ? (formula)
(when (stringp formula) (setf formula (reform formula)))
(let* ((d-node (d-node-for formula))
(nodes (search-d-nodes formula d-node)))
(cond (nodes
(terpri)
(princ "The following answers are known for the query (? ") (prinp formula) (princ "):") (terpri)
(princ "------------------------------------------------------------------------------------------------------------------------------------------------------------")
(terpri)
(dolist (node nodes)
(princ " ") (princ (node-formula node)) (princ " by node #")
(princ (inference-number node)) (terpri))
(terpri))
(t
(terpri) (princ "No answers are known for the query (? ") (prinp formula) (princ ").") (terpri)
(princ "------------------------------------------------------------------------------------------------------------------------------------------------------------")
(terpri)
(terpri)))
nodes))
(defunction search-d-nodes (formula d-node)
(let ((nodes nil)
(?-vars (?-variables formula)))
(dolist (c-list (d-node-c-lists d-node))
(dolist (node (c-list-nodes c-list))
(when (and (null (node-supposition node))
(match formula (node-formula node) ?-vars))
(push node nodes))))
(append nodes
(unionmapcar #'(lambda (dt) (search-d-nodes formula (cdr dt)))
(discrimination-tests d-node)))))
(defunction ?interests (formula)
(when (stringp formula) (setf formula (reform formula)))
(let* ((d-node (d-node-for formula))
(interests (search-d-node-interests formula d-node)))
(cond (interests
(terpri)
(princ "The following interests were adopted for the query (? ") (prinp formula) (princ "):") (terpri)
(princ "------------------------------------------------------------------------------------------------------------------------------------------------------------")
(terpri)
(dolist (interest interests)
(princ " ") (princ (interest-formula interest)) (princ " by interest #")
(princ (interest-number interest)) (terpri))
(terpri))
(t
(terpri) (princ "No interests were adopted for the query (? ") (prinp formula) (princ ").") (terpri)
(princ "------------------------------------------------------------------------------------------------------------------------------------------------------------")
(terpri)
(terpri)))
interests))
(defunction search-d-node-interests (formula d-node)
(let ((interests nil)
(?-vars (?-variables formula)))
(dolist (i-list (d-node-i-lists d-node))
(dolist (interest (i-list-interests i-list))
(when (and (null (node-supposition interest))
(match formula (interest-formula interest) ?-vars))
(push interest interests))))
(append interests
(unionmapcar #'(lambda (dt) (search-d-node-interests formula (cdr dt)))
(discrimination-tests d-node)))))
; ---------------------------- ULTIMATE-EPISTEMIC-INTERESTS -----------------------------
(defstruct (query (:print-function print-query)
(:conc-name nil))
(query-number 0)
(query-formula nil)
(query-strength 0)
(query-queue-node nil)
(deductive-query nil) ;; t if the query is whether the query formula is deductively provable
(positive-query-instructions nil) ;; a list of functions applicable to an inference-node
(negative-query-instructions nil) ;; a list of functions applicable to an inference-node
(query-answers nil) ;;a list of inference-nodes
(answered? nil) ;; t if some answer is justified to degree greater than or equal
;; to the degree of interest, nil otherwise
(query-interest nil) ;; the interest recording the query
(negative-query-interest nil) ;; the negative-interest for a whether-query
(?-constraint nil)) ;; a function which when applied to the ?-vars yields a discharge-condition
;; for the query-interest, constraining the instantiating terms.
(defun print-query (x stream depth)
(declare (ignore depth))
(princ "#<" stream) (princ "Query " stream) (princ (query-number x) stream)
(princ ": " stream) (princ (pretty (query-formula x)) stream)
(princ ">" stream))
(defunction ?-query-p (Q)
(and (query-p Q) (?-genp (query-formula Q))))
#| This returns two values: the matrix, and the list of ?-variables. |#
(defunction ?-matrix (p &optional vars)
(push (q-variable p) vars)
(let ((formula (q-matrix p)))
(cond ((?-genp formula) (?-matrix formula vars))
(t (values formula vars)))))
(defunction whether-query-p (Q)
(and (query-p Q) (whether-formula (query-formula Q))))
(defunction query (n)
(find-if #'(lambda (c) (equal (query-number c) n))
*ultimate-epistemic-interests*))
(defunction show-query (Q)
(if (numberp Q) (setf Q (query Q)))
(princ Q) (terpri))
(defunction show-queries ()
(terpri)
(princ
"================== ULTIMATE EPISTEMIC INTERESTS ===================")
(terpri)
(dolist (Q *ultimate-epistemic-interests*)
(show-query Q)))
(defunction inclusive-node-ancestors (node)
(cons node (node-ancestors node)))
(defunction ancestral-links (node)
(unionmapcar+ #'support-links (inclusive-node-ancestors 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))
(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)
(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-queries ()
(terpri)
(princ
"================== ULTIMATE EPISTEMIC INTERESTS ===================")
(terpri)
(dolist (Q *ultimate-epistemic-interests*)
(display-query Q)))
(defunction answers (formula query)
(let ((query-formula (query-formula query)))
(if (?-genp query-formula)
(instance-of formula query-formula)
(equal formula query-formula))))
#| This assumes that formula2 is indefinite. |#
(defunction instance-of (formula1 formula2)
(match (mem2 formula2) formula1 (list (mem2 (mem1 formula2)))))
(defunction in-interest (sequent)
(let ((interests (interests-for (sequent-formula sequent) nil)))
(when interests
(some #'(lambda (interest) (null (interest-supposition interest)))
interests))))
(defunction adopt-ultimate-interest (query)
(push query *ultimate-epistemic-interests*)
(when (not (in-interest (list nil (query-formula query))))
(queue-query-for-interest query)))
(defunction queue-query-for-interest (query)
(let ((node
(make-inference-queue-node
:queue-number (incf *queue-number*)
:enqued-item query
:item-kind :query
:item-complexity (complexity (query-formula query))
:discounted-strength (query-strength query)
:degree-of-preference (query-preference query))))
(setf (query-queue-node query) node)
(setf *inference-queue* (ordered-insert node *inference-queue* #'i-preferred))))
; -------------------------------------- INTERESTS --------------------------------------
(defstruct (instantiated-premise (:print-function print-instantiated-premise) (:conc-name ip-))
(reason nil)
(binding nil) ;; cumulative binding prior to this premise
(basis nil)
(premise nil)
(remaining-premises nil) ;; premises left to be instantiated
(instantiations nil) ;; instantiations of node-variables in previous premises
(used-premise-variables nil) ;; premise-variables bound in earlier premises
(used-variables nil) ;; conclusion-variables occurring in basis
(derived-premises nil) ;; instantiated-premises immediately following this one
(d-node nil)
(number 0)
(clues nil)
(initial? nil)) ;; records whether the premise is the initial premise of the reason
(defun print-instantiated-premise (x stream depth)
(declare (ignore depth))
(princ "<instantiated-premise " stream) (princ (ip-number x) stream) (princ " for " stream)
(princ (ip-reason x) stream)
(princ ">" stream))
(defstruct (interest-scheme (:include instantiated-premise)
(:print-function print-interest-scheme) (:conc-name is-))
(target-interest nil)
(supposition nil)
(supposition-variables nil)
(instance-function nil)
(generating-node nil))
(defun print-interest-scheme (x stream depth)
(declare (ignore depth))
(princ "<<interest-scheme " stream) (princ (is-number x) stream) (princ " for " stream)
(princ (is-target-interest x) stream) (princ " by " stream) (princ (ip-reason x) stream) (princ ">>" stream))
#| This finds the interest-scheme with is-number n. |#
(defunction interest-scheme (n)
(let ((is nil))
(some #'(lambda (dn)
(find-if #'(lambda (i)
(and (equal (is-number i) n)
(setf is i)))
(d-node-interest-schemes dn)))
*discrimination-net*)
is))
#| This finds the instantiated-premise with ip-number n. |#
(defunction instantiated-premise (n)
(let ((ip nil))
(some #'(lambda (dn)
(find-if #'(lambda (i)
(and (equal (ip-number i) n)
(setf ip i)))
(d-node-forwards-reasons dn)))
*discrimination-net*)
ip))
(defmacro is-derived-interest-schemes (is) `(is-derived-premises ,is))
(defstruct (interest-link (:print-function print-interest-link)
(:conc-name nil)) ; "An interest-graph-link"
(link-number 0)
(resultant-interest nil)
(link-interest nil)
(link-interest-formula nil)
(link-interest-condition nil)
(link-binding nil)
(link-rule nil)
(remaining-premises nil)
(supporting-nodes nil)
(link-instantiations nil)
(link-supposition nil)
(link-defeaters nil)
(link-defeat-status nil)
(link-strength 0) ; maximum-degree-of-interest conveyed
(link-generating-node nil)
(discharged-link nil)
(interest-match nil)
(interest-reverse-match nil)
(generating-link nil)
(link-premise nil)
(link-clues nil)
)
(defun print-interest-link (x stream depth)
(declare (ignore depth))
(princ "#<" stream) (princ "Link " stream)
(princ (link-number x) stream)
(when (resultant-interest x)
(princ ": for interest #" stream) (princ (interest-number (resultant-interest x)) stream))
(princ " by " stream) (princ (link-rule x) stream)
(princ ">" stream))
(defunction link (n)
(find-if #'(lambda (node) (equal (link-number node) n)) *interest-links*))
(defunction display-links ()
(dolist (L *interest-links*) (princ L) (terpri)))
(defunction display-link (L)
(princ "INTEREST-LINK #") (princ (link-number L)) (terpri)
(princ " resultant-interest: ") (princ (resultant-interest L)) (terpri)
(princ " supporting-nodes: ") (princ (supporting-nodes L)) (terpri)
(princ " link-interest: ") (princ (link-interest L)) (terpri)
(princ " remaining-premises: ") (princ (remaining-premises L)) (terpri)
(princ " reason: ") (princ (link-rule L)) (terpri)
(princ " link-interest: ") (princ (link-interest L)) (terpri)
)
(defstruct (interest (:print-function print-interest)
(:conc-name nil)) ; "An interest-graph-node"
(interest-number 0)
(interest-sequent nil)
(interest-formula nil)
(interest-supposition nil)
(right-links nil)
(left-links nil)
(degree-of-interest *base-priority*)
(last-processed-degree-of-interest nil)
(interest-defeat-status nil)
(discharged-degree nil) ;; used in computing priorities
(deductive-interest nil)
(cancelled-interest nil)
(interest-queue-node nil)
(interest-i-list nil)
(maximum-degree-of-interest 0)
(interest-defeatees nil)
(reductio-interest nil) ;; is the interest partly for reductio?
(direct-reductio-interest nil)
(generated-suppositions nil)
(generating-nodes nil)
(interest-priority 0)
(interest-variables nil)
(discharge-condition nil) ;;a function of node, unifier, and interest-link
(interest-supposition-variables nil)
(cancelling-node nil)
(discharging-nodes nil)
(interest-supposition-nodes nil)
(generated-interest-schemes nil)
(defeater-binding nil)
(generating-defeat-nodes nil)
(cancelled-left-links nil)
(non-reductio-interest t) ;; is the interest partly for non-reductio?
(anchored-nodes nil)
(text-discharge-condition nil) ;; a text statement of the discharge condition
(enabled-nodes nil) ;; the nodes for which this is an enabling-interest
)
#|
(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))
|#
(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))
(defunction ifm (n)
(when (numberp n) (setf n (interest n)))
(prinp (interest-formula n))
(interest-formula n))
(defstruct (i-list (:print-function print-i-list)
(:conc-name nil))
(i-list-formula nil)
(corresponding-c-lists nil)
(i-list-interests nil)
(i-list-queries nil)
(reductio-trigger nil)
(i-list-reductio-supposition nil)
(i-list-variables)
(i-list-term-list nil)
(i-list-d-node nil))
(defun print-i-list (x stream depth)
(declare (ignore depth))
(princ "#<i-list for " stream)
(prinp (i-list-formula x) stream) (princ ">" stream))
#| This returns three values -- the i-list and the match and its reverse. |#
(defunction i-list-for (formula i-vars)
(multiple-value-bind (profile term-list) (formula-code formula)
(let ((d-node (pursue-d-node-for profile *top-d-node*)))
(when d-node
(some
#'(lambda (il)
(multiple-value-bind
(match match*)
(one-one-match term-list (i-list-term-list il) i-vars (i-list-variables il))
(when match
(return-from i-list-for (values il match match*)))))
(d-node-i-lists d-node))))))
#| If p and q match one-one, this returns the match and its reverse-match. |#
(defunction one-one-match (p q p-vars q-vars)
(let* ((match (match p q p-vars))
(match* (reverse-match match)))
(when
(and match
(or (eq match t)
(and
(subsetp (a-range match) q-vars)
(equal (match-sublis match* q) p))))
(values match match*))))
#| This returns two values -- the list of interests, and the match |#
(defunction interests-for (formula i-vars)
(multiple-value-bind
(i-list match)
(i-list-for formula i-vars)
(if i-list (values (i-list-interests i-list) match))))
#| c-variables is the list of node-variables. |#
(defunction matching-i-lists-for (term-list c-variables d-node)
(let ((i-lists nil))
(dolist (il (d-node-i-lists d-node))
(let ((unifier (unifier term-list (i-list-term-list il) c-variables (i-list-variables il))))
(if unifier (push (list il unifier) i-lists))))
i-lists))
#| c-variables is the list of node-variables. |#
(defunction matching-c-lists-for (term-list i-variables d-node)
(let ((c-lists nil))
(dolist (cl (d-node-c-lists d-node))
(let ((unifier (unifier (c-list-term-list cl) term-list (c-list-variables cl) i-variables)))
(if unifier (push (list cl unifier) c-lists))))
c-lists))
(defunction store-interest (interest &optional i-list)
; (when (eq (interest-number interest) 11) (setf i interest il i-list) (break))
;; (step (store-interest i il))
(push interest *interests*)
(cond (i-list
(push interest (i-list-interests i-list))
(let ((reductio-sup (i-list-reductio-supposition i-list)))
(when reductio-sup
(push interest (generating-interests reductio-sup))
(push reductio-sup (generated-suppositions interest))))
(setf (interest-i-list interest) i-list))
(t
(multiple-value-bind (profile term-list) (formula-code (interest-formula interest))
(index-interest interest profile term-list *top-d-node*)))))
#| (descrimination-tests d-node) is an a-list of pairs (test . dn), where test has the form of the
car of a formula-code, and dn is a d-node. |#
(defunction index-interest (interest profile term-list d-node)
(let ((dn (e-assoc (car profile) (discrimination-tests d-node)))
(new-profile (cdr profile)))
(cond (dn
(if new-profile (index-interest interest new-profile term-list dn)
(store-interest-at-d-node interest term-list dn)))
(new-profile
(index-interest-at-new-nodes interest term-list d-node new-profile (car profile)))
(t (store-interest-at-new-d-node
interest term-list d-node (car profile))))))
(defunction fetch-I-list-for (term-list d-node)
(find-if #'(lambda (il)
(equal term-list (i-list-term-list il)))
(d-node-i-lists d-node)))
(defunction store-interest-at-d-node (interest term-list dn)
; (when (eq interest (interest 11)) (setf i interest tl term-list d dn) (break))
;; (step (store-interest-at-d-node i tl d))
(let* ((formula (interest-formula interest))
(i-variables (interest-variables interest))
(i-list (fetch-i-list-for term-list dn)))
(cond (i-list
(push interest (i-list-interests i-list))
(let ((reductio-sup (i-list-reductio-supposition i-list)))
(when reductio-sup
(push interest (generating-interests reductio-sup))
(push reductio-sup (generated-suppositions interest)))))
(t (let ((c-lists (matching-c-lists-for term-list i-variables dn)))
(setf i-list (make-i-list
:i-list-formula formula
:corresponding-c-lists c-lists
:i-list-interests (list interest)
:reductio-trigger
(appropriate-for-reductio-supposition formula)
:i-list-variables i-variables
:i-list-term-list term-list
:i-list-d-node dn
))
(push i-list (d-node-i-lists dn))
(dolist (cl c-lists)
(push (cons i-list (cdr cl)) (corresponding-i-lists (mem1 cl)))))))
(setf (interest-i-list interest) i-list)))
#| Test is the final member of the formula-profile for the node-formula. |#
(defunction store-interest-at-new-d-node (interest term-list d-node test)
; (when (eq interest (interest 7)) (setf i interest tl term-list d d-node ts test) (break))
;; (step (store-interest-at-new-node i tl d ts))
(let* ((i-variables (interest-variables interest))
(formula (interest-formula interest))
(dn (make-d-node
:d-node-number (incf *d-node-number*)
:d-node-description (cons test (d-node-description d-node))
:parent-d-node d-node))
(i-list (make-i-list
:i-list-formula formula
:i-list-interests (list interest)
:reductio-trigger
(appropriate-for-reductio-supposition formula)
:i-list-variables i-variables
:i-list-term-list term-list
:i-list-d-node dn
)))
(push dn *discrimination-net*)
(push (cons test dn) (discrimination-tests d-node))
(setf (d-node-i-lists dn) (list i-list))
(setf (interest-i-list interest) i-list)))
(defunction find-matching-i-lists-for (formula variables)
(multiple-value-bind (profile term-list) (formula-code formula)
(pursue-i-lists-for formula profile term-list variables *top-d-node*)))
(defunction pursue-i-lists-for (formula profile term-list variables d-node)
(let ((dn (e-assoc (car profile) (discrimination-tests d-node))))
(when dn
(let ((new-profile (cdr profile)))
(cond
(new-profile (pursue-i-lists-for formula new-profile term-list variables dn))
(t (matching-i-lists-for term-list variables dn)))))))
(defunction index-interest-at-new-nodes (interest term-list d-node profile test)
(let ((dn (make-d-node
:d-node-number (incf *d-node-number*)
:d-node-description (cons test (d-node-description d-node))
:parent-d-node d-node)))
(push (cons test dn) (discrimination-tests d-node))
(push dn *discrimination-net*)
(let ((desc (cdr profile)))
(cond (desc (index-interest-at-new-nodes interest term-list dn desc (car profile)))
(t (store-interest-at-new-d-node interest term-list dn (car profile)))))))
(defunction pursue-d-node-for (profile d-node)
(let ((dn (e-assoc (car profile) (discrimination-tests d-node))))
(when dn
(let ((new-profile (cdr profile)))
(cond
(new-profile (pursue-d-node-for new-profile dn))
(t dn))))))
(defunction store-interest-with-c-lists (interest c-lists)
; (when (eq (interest-number interest) 25) (setf i interest cl c-lists) (break))
;; (step (store-interest-with-c-lists i cl))
(multiple-value-bind (profile term-list) (formula-code (interest-formula interest))
(declare (ignore profile))
(cond
(c-lists
(push interest *interests*)
(let* ((formula (interest-formula interest))
(dn (c-list-d-node (caar c-lists)))
(i-list (fetch-i-list-for term-list dn))
(i-variables (interest-variables interest)))
(cond (i-list
(push interest (i-list-interests i-list))
(let ((reductio-sup (i-list-reductio-supposition i-list)))
(when reductio-sup
(push interest (generating-interests reductio-sup))
(push reductio-sup (generated-suppositions interest)))))
(t (setf i-list (make-i-list
:i-list-formula formula
:corresponding-c-lists c-lists
:i-list-interests (list interest)
:reductio-trigger
(appropriate-for-reductio-supposition formula)
:i-list-variables i-variables
:i-list-term-list term-list
:i-list-d-node dn
))
(push i-list (d-node-i-lists dn))
(dolist (cl c-lists)
(push (cons i-list (cdr cl)) (corresponding-i-lists (mem1 cl))))))
(setf (interest-i-list interest) i-list)))
(t (store-interest interest)))))
(defunction appropriate-for-reductio-supposition (formula)
(and
(not (conjunctionp formula))
(not (conditionalp formula))
(not (biconditionalp formula))
(not (disjunctionp formula))
(not (u-genp formula))
(not (undercutterp formula))
(or (not (negationp formula))
(atomic-formula (mem2 formula)))))
(defunction store-inference-node (node formula)
; (when (eql (inference-number node) 14) (setf n node f formula) (break))
;; (step (store-inference-node n f))
(multiple-value-bind (profile term-list) (formula-code formula)
(index-inference-node node profile term-list *top-d-node*)))
#| (descrimination-tests d-node) is an a-list of pairs (test . dn), where test has the form of the
car of a formula-code, and dn is a d-node. |#
(defunction index-inference-node (node profile term-list d-node)