-
Notifications
You must be signed in to change notification settings - Fork 2
/
pttp-1i.lisp
2447 lines (2221 loc) · 88.1 KB
/
pttp-1i.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
;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: PTTP; Base: 10. -*-
;;; Copyright (c) 1986 Mark E. Stickel, SRI International, Menlo Park, CA 94025 USA
;;;
;;; Permission is hereby granted, free of charge, to any person obtaining a
;;; copy of this software and associated documentation files (the "Software"),
;;; to deal in the Software without restriction, including without limitation
;;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
;;; and/or sell copies of the Software, and to permit persons to whom the
;;; Software is furnished to do so, subject to the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be included
;;; in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
;;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
;;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
;;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(in-package :pttp)
(eval-when (:compile-toplevel :load-toplevel :execute)
(setq *print-radix* nil))
(defvar float-internal-time-units-per-second (float internal-time-units-per-second))
(defvar and-connective '|,/2|)
(defvar or-connective '\;/2)
;; dynamic variables used during compilation
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar name nil)) ; name of procedure being compiled
(defvar arity) ; arity of procedure being compiled
(defvar clause-numbers) ; association list of clauses and their numbers
(defvar first-argument-type) ; the type of the first argument to the procedure
(defvar traceable) ; compile-procedure option to compile code
; for tracing
(defvar unbounded-search) ; compile-procedure option to not compile code
; for depth-bounded search
(defvar unsafe-unification) ; compile-procedure option to not use the
; occurs check during unification
(defvar incomplete-inference) ; compile-procedure option to compile
; ME reduction operations
(defvar allow-repeated-goals) ; compile-procedure option to compile
; ME pruning operations
(defvar trace-calls t) ; if nil, tracing will not be compiled
; regardless of :traceable option
(defvar count-calls t) ; compile code to count successful unifications?
(defvar recompile nil) ; recompile procedure even if clauses and
; parameters are the same
(defvar print-clauses t) ; print input clauses
(defvar print-compile-names t) ; print names of functions as they are compiled
(defvar print-compile-times t) ; print compilation time
;; when variable names are included in the variable,
;; variables are represented by
;; (variable-level pointer-to-value . variable-name) for bound variables
;; and (variable-level nil . variable-name) for unbound variables
;;
;; when variable names are not included in the variable,
;; variables are represented by
;; (variable-level . pointer-to-value) for bound variables
;; and (variable-level . nil ) for unbound variables
;; this encoding assumes that integers will not be used as functors
(eval-when (:compile-toplevel :load-toplevel :execute)
(pushnew :include-name-in-variable *features*))
(defun new-variable (var-name var-level)
#-include-name-in-variable (declare (ignore var-name))
(list* var-level nil #+include-name-in-variable var-name))
(defmacro variable-p (x)
;; x nonatomic
`(integerp (car ,x)))
(defmacro variable-level (x)
`(car ,x))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmacro variable-value (x)
#+include-name-in-variable `(cadr ,x)
#-include-name-in-variable `(cdr ,x)))
(defmacro variable-name (x)
#-include-name-in-variable (declare (ignore x))
#+include-name-in-variable `(cddr ,x)
#-include-name-in-variable `'_)
(defmacro dereference (x &key (if-constant t) (if-variable nil) (if-compound nil))
;; dereferences x leaving result in x, returns t if result is atomic, nil if not
(assert (symbolp x))
(let ((y (gensym)))
`(do nil (nil)
(cond ((atom ,x) (return ,if-constant))
((variable-p ,x)
(unless (let ((,y (variable-value ,x)))
(when ,y (setq ,x ,y)))
(return ,if-variable)))
(t (return ,if-compound))))))
(defvar *trail-array* (make-array 10000))
(defvar *trail* 0)
(defmacro trail-variable-p (var)
`(< (variable-level ,var) !level!))
(defmacro bind-variable-to-term (var term trail?)
;; returns non-nil value (term is never nil)
(assert (symbolp var))
(assert (member trail? '(:trail :dont-trail :maybe-trail)))
(cond ((eq trail? :dont-trail)
`(progn (setf (variable-value ,var) ,term)
t))
((eq trail? :trail)
`(progn (setf (svref *trail-array* (incf *trail*)) ,var)
(setf (variable-value ,var) ,term)
t))
(t `(progn (if (trail-variable-p ,var)
(setf (svref *trail-array* (incf *trail*)) ,var))
(setf (variable-value ,var) ,term)
t))))
(defmacro undo-bindings nil
;; MUST RETURN NIL
`(let ((trail *trail*))
(when (> trail !old-trail!)
(do ((trail-array *trail-array*))
(nil)
(setf (variable-value (svref trail-array trail)) nil)
(when (= (decf trail) !old-trail!)
(setq *trail* !old-trail!)
(return nil))))))
(defmacro safe-bind-variable-to-compound (var term trail? undo?)
;; returns non-nil value if binding is successful,
;; undoes bindings on trail and returns nil if not successful
(assert (symbolp term))
`(if (variable-occurs-in-terms-p ,var (cdr ,term))
,(if undo? `(undo-bindings) nil)
(bind-variable-to-term ,var ,term ,trail?)))
(defmacro pcall (term (vars unbound) level continuation)
(stack-list-new-variables
unbound
(let* ((formals (head-locs (cdr term) 'a))
(actuals (mapcar #'(lambda (x) (term-constructor x vars)) (cdr term)))
(decls (mapcan #'(lambda (x y)
(if (or (atom y) (eq (car y) 'quote))
nil
(list (list x y))))
formals actuals))
(args (mapcar #'(lambda (x y)
(if (or (atom y) (eq (car y) 'quote))
y x))
formals actuals)))
(if decls
`(let ,decls (,(car term) ,@args ,level ,continuation))
`(,(car term) ,@args ,level ,continuation)))))
(defvar *ncalls* 0) ; counter for number of inferences
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun wrap-count-calls (form)
(if (and count-calls (not (eq name 'query/0)))
`(progn (incf *ncalls*) ,form)
form)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun head-locs (terms &optional (name 'arg) nth)
(let* ((prop (if nth 'nth-arguments 'arguments))
(n (if (numberp terms) terms (length terms)))
(l (get name prop)))
(or (cdr (assoc n l))
(let (w)
(dotimes (i n)
(push (if nth
`(nth ,(1+ i) ,name)
(intern (concatenate 'string "!" (symbol-name name)
(princ-to-string (1+ i)) "!")
'pttp))
w))
(setq w (nreverse w))
(setf (get name prop) (cons (cons n w) l))
w)))))
;; UNIFICATION
(defun ground-term-p (term)
(dereference term
:if-constant t
:if-variable nil
:if-compound (ground-terms-p (cdr term))))
(defun ground-terms-p (terms)
(do ((l terms (cdr l)) (term))
((null l) t)
(setq term (car l))
(dereference term
:if-constant nil
:if-variable (return-from ground-terms-p nil)
:if-compound (cond ((null (cdr l)) (setq l term))
((not (ground-terms-p (cdr term)))
(return-from ground-terms-p nil))))))
(defun variable-occurs-in-term-p (var term)
;; works for both external (e.g., X) and internal (i.e., (level
;; pointer-or-nil . name)) variable representations
;; i.e., we check (eq var term) in the constant as well as variable case
;; it would be slightly more efficient to handle only the internal
;; representation when running Prolog programs
(dereference term
:if-constant (eq var term)
:if-variable (eq var term)
:if-compound (variable-occurs-in-terms-p var (cdr term))))
(defun variable-occurs-in-terms-p (var terms)
;; works for both external (e.g., X) and internal (i.e., (level
;; pointer-or-nil . name)) variable representations
;; i.e., we check (eq var term) in the constant as well as variable case
;; it would be slightly more efficient to handle only the internal
;; representation when running Prolog programs
(do ((l terms (cdr l)) (term))
((null l) nil)
(setq term (car l))
(dereference term
:if-constant (when (eq var term)
(return-from variable-occurs-in-terms-p t))
:if-variable (when (eq var term)
(return-from variable-occurs-in-terms-p t))
:if-compound (cond ((null (cdr l)) (setq l term))
((variable-occurs-in-terms-p var (cdr term))
(return-from variable-occurs-in-terms-p t))))))
(defmacro unify-macro (unify-list-fun trail? safely?)
;; undoes bindings on trail and returns nil if not successful
`(dereference t1
:if-constant
(dereference t2
:if-constant (or (eql t1 t2) (undo-bindings))
:if-variable (bind-variable-to-term t2 t1 ,trail?)
:if-compound (undo-bindings))
:if-variable
(dereference t2
:if-constant (bind-variable-to-term t1 t2 ,trail?)
:if-variable (or (eq t1 t2)
(if (<= (variable-level t1)
(variable-level t2))
(bind-variable-to-term t2 t1 ,trail?)
(bind-variable-to-term t1 t2 ,trail?)))
:if-compound ,(if safely?
`(safe-bind-variable-to-compound
t1 t2 ,trail? t)
`(bind-variable-to-term t1 t2 ,trail?)))
:if-compound
(dereference t2
:if-constant (undo-bindings)
:if-variable ,(if safely?
`(safe-bind-variable-to-compound
t2 t1 ,trail? t)
`(bind-variable-to-term t2 t1 ,trail?))
:if-compound (or (eq t1 t2)
(if (eq (car t1) (car t2))
(let ((l1 (cdr t1)) (l2 (cdr t2)))
(unify-list-macro ,unify-list-fun
,trail? ,safely?))
(undo-bindings))))))
(defmacro unify-list-macro (unify-list-fun trail? safely?)
;; undoes bindings on trail and returns nil if not successful
`(block unify-list
(do ((t1) (t2))
((null l1) (return t))
(setq t1 (car l1)) (setq l1 (cdr l1))
(setq t2 (car l2)) (setq l2 (cdr l2))
(dereference t1
:if-constant
(dereference t2
:if-constant (unless (eql t1 t2)
(return-from unify-list (undo-bindings)))
:if-variable (bind-variable-to-term t2 t1 ,trail?)
:if-compound (return-from unify-list (undo-bindings)))
:if-variable
(dereference t2
:if-constant (bind-variable-to-term t1 t2 ,trail?)
:if-variable
(or (eq t1 t2)
(if (<= (variable-level t1)
(variable-level t2))
(bind-variable-to-term t2 t1 ,trail?)
(bind-variable-to-term t1 t2 ,trail?)))
:if-compound
,(if safely?
`(unless (safe-bind-variable-to-compound
t1 t2 ,trail? t)
(return-from unify-list nil))
`(bind-variable-to-term t1 t2 ,trail?)))
:if-compound
(dereference t2
:if-constant (return-from unify-list (undo-bindings))
:if-variable
,(if safely?
`(unless (safe-bind-variable-to-compound
t2 t1 ,trail? t)
(return-from unify-list nil))
`(bind-variable-to-term t2 t1 ,trail?))
:if-compound
(cond ((eq t1 t2))
((not (eq (car t1) (car t2)))
(return-from unify-list
(undo-bindings)))
((null l1)
(setq l1 (cdr t1)) (setq l2 (cdr t2)))
((not ,(if (eq trail? :maybe-trail)
`(,unify-list-fun (cdr t1) (cdr t2)
!old-trail! !level!)
`(,unify-list-fun (cdr t1) (cdr t2)
!old-trail!)))
(return-from unify-list nil))))))))
(defun always-trails-unify (t1 t2 !old-trail!)
(unify-macro always-trails-unify-list :trail t))
(defun always-trails-unify-list (l1 l2 !old-trail!)
(unify-list-macro always-trails-unify-list :trail t))
(defun maybe-trails-unify (t1 t2 !old-trail! !level!)
(unify-macro maybe-trails-unify-list :maybe-trail t))
(defun maybe-trails-unify-list (l1 l2 !old-trail! !level!)
(unify-list-macro maybe-trails-unify-list :maybe-trail t))
(defun unsafe-always-trails-unify (t1 t2 !old-trail!)
(unify-macro unsafe-always-trails-unify-list :trail nil))
(defun unsafe-always-trails-unify-list (l1 l2 !old-trail!)
(unify-list-macro unsafe-always-trails-unify-list :trail nil))
(defun unsafe-maybe-trails-unify (t1 t2 !old-trail! !level!)
(unify-macro unsafe-maybe-trails-unify-list :maybe-trail nil))
(defun unsafe-maybe-trails-unify-list (l1 l2 !old-trail! !level!)
(unify-list-macro unsafe-maybe-trails-unify-list :maybe-trail nil))
(defmacro unify-argument-with-constant (actual formal &key trail-is-nil)
(assert (or (atom formal) (eq (car formal) 'quote) (eq (car formal) 'nth)))
`(let ((!temp! ,actual))
(dereference !temp!
:if-constant ,(if trail-is-nil
`(eql !temp! ,formal)
`(or (eql !temp! ,formal) (undo-bindings)))
:if-variable (bind-variable-to-term !temp! ,formal :trail)
:if-compound ,(if trail-is-nil nil `(undo-bindings)))))
(defmacro unify-argument-with-compound (actual formal &key trail-is-nil unsafe)
(assert (symbolp formal))
`(let ((!temp! ,actual))
(dereference !temp!
:if-constant ,(if trail-is-nil nil `(undo-bindings))
:if-variable ,(if unsafe
`(bind-variable-to-term !temp! ,formal :trail)
`(safe-bind-variable-to-compound
!temp! ,formal :trail ,(not trail-is-nil)))
:if-compound (or (eq !temp! ,formal)
(if (eq (car !temp!) (car ,formal))
,(if unsafe
`(unsafe-maybe-trails-unify-list
(cdr !temp!) (cdr ,formal)
!old-trail! !level!)
`(maybe-trails-unify-list
(cdr !temp!) (cdr ,formal)
!old-trail! !level!))
,(if trail-is-nil nil `(undo-bindings)))))))
(defmacro identical-to-constant (term constant)
(assert (symbolp constant))
(if (symbolp term)
`(dereference ,term
:if-constant (eql ,term ,constant)
:if-variable nil
:if-compound nil)
(let ((temp (gensym)))
`(let ((,temp ,term))
(identical-to-constant ,temp ,constant)))))
(defmacro identical-to-variable (term variable)
(assert (symbolp variable))
(if (symbolp term)
`(dereference ,term
:if-constant nil
:if-variable (eq ,term ,variable)
:if-compound nil)
(let ((temp (gensym)))
`(let ((,temp ,term))
(identical-to-variable ,temp ,variable)))))
(defmacro identical-to-compound (term compound)
(assert (symbolp compound))
(if (symbolp term)
`(dereference ,term
:if-constant nil
:if-variable nil
:if-compound (or (eq ,term ,compound)
(and (eq (car ,term) (car ,compound))
(identical-list (cdr ,term) (cdr ,compound)))))
(let ((temp (gensym)))
`(let ((,temp ,term))
(identical-to-compound ,temp ,compound)))))
(defmacro identical (t1 t2)
(if (symbolp t2)
`(dereference ,t2
:if-constant (identical-to-constant ,t1 ,t2)
:if-variable (identical-to-variable ,t1 ,t2)
:if-compound (identical-to-compound ,t1 ,t2))
(let ((temp (gensym)))
`(let ((,temp ,t2))
(identical ,t1 ,temp)))))
(defmacro identical-list (l1 l2)
`(block identical-list
(do ((l1 ,l1) (l2 ,l2) (t1) (t2))
((null l1) (return t))
(setq t1 (car l1)) (setq l1 (cdr l1))
(setq t2 (car l2)) (setq l2 (cdr l2))
(dereference t2
:if-constant (unless (identical-to-constant t1 t2)
(return-from identical-list nil))
:if-variable (unless (identical-to-variable t1 t2)
(return-from identical-list nil))
:if-compound
(dereference t1
:if-constant (return-from identical-list nil)
:if-variable (return-from identical-list nil)
:if-compound
(cond ((eq t1 t2))
((not (eq (car t1) (car t2)))
(return-from identical-list nil))
((null l1)
(setq l1 (cdr t1))
(setq l2 (cdr t2)))
((not (identical-list-fun (cdr t1) (cdr t2)))
(return-from identical-list nil))))))))
(defun identical-list-fun (l1 l2)
(identical-list l1 l2))
;; SUPPORT FOR OUTPUT AND TRACING
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmacro specifier (x)
`(get ,x 'specifier))
(defmacro precedence (x)
`(get ,x 'precedence)))
(defvar *writing* t) ; use prefix, postfix, infix operators during printing
(defun function-case (x)
(string-downcase x))
(defun constant-case (x)
(string-downcase x))
(defun variable-case (x)
(string-capitalize x))
(defun display-term (term)
(let ((*writing* nil))
(write-term term)))
(defun write-term (term)
(dereference term)
(cond ((atom term) (princ (if (symbolp term) (constant-case term) term)))
((variable-p term) (princ (variable-case (variable-name term))) (princ "_") (princ (variable-level term)))
(t (write-functor-and-arguments (car term) (cdr term))))
term)
(defun parenthesize-argument (arg prec rel)
(dereference arg
:if-constant nil
:if-variable nil
:if-compound (let ((argprec (precedence (car arg))))
(and argprec (funcall rel prec argprec)))))
(defun write-functor-and-arguments (fn args &aux spec prec)
(cond ((eq fn 'cons/2)
(princ "[")
(write-term (car args))
(do ((x (cadr args) (caddr x)))
(nil)
(dereference x)
(cond ((eq x '|[]|) (princ "]") (return))
((and (not (atom x)) (eq (car x) 'cons/2))
(princ ",")
(write-term (cadr x)))
(t
(princ "|")
(write-term x)
(princ "]")
(return)))))
((and *writing* (setq spec (specifier fn)))
(setq prec (precedence fn))
(case spec
((fx fy)
(princ (function-case (functor-name fn)))
(princ " ")
(cond ((parenthesize-argument (car args) prec (if (eq spec 'fx) #'<= #'<))
(princ "(") (write-term (car args)) (princ ")"))
(t (write-term (car args)))))
((xf yf)
(cond ((parenthesize-argument (car args) prec (if (eq spec 'fx) #'<= #'<))
(princ "(") (write-term (car args)) (princ ")"))
(t (write-term (car args))))
(princ " ")
(princ (function-case (functor-name fn))))
((xfx xfy yfx yfy)
(cond ((parenthesize-argument (car args) prec (if (member spec '(xfx xfy))
#'<= #'<))
(princ "(") (write-term (car args)) (princ ")"))
(t (write-term (car args))))
(princ " ")
(princ (function-case (functor-name fn)))
(princ " ")
(cond ((parenthesize-argument (cadr args) prec (if (member spec '(xfx yfx))
#'<= #'<))
(princ "(") (write-term (cadr args)) (princ ")"))
(t (write-term (cadr args)))))))
(t (princ (function-case (functor-name fn)))
(when args
(princ "(")
(cond ((parenthesize-argument (car args) 0 #'<)
(princ "(") (write-term (car args)) (princ ")"))
(t (write-term (car args))))
(dolist (term (cdr args))
(princ ",")
(cond ((parenthesize-argument term 0 #'<)
(princ "(") (write-term term) (princ ")"))
(t (write-term term))))
(princ ")"))))
nil)
(defun write-functor-and-arguments* (fn &rest args)
(write-functor-and-arguments fn args))
(defun write-clause (clause &optional number variables)
(let ((goalp (and (eq (car clause) '<-/2) (equal (cadr clause) '(query/0)))))
(fresh-line)
(when goalp (princ " ----------------") (terpri))
(when number
(if (consp number)
(format t "~3D~A. " (car number) (cdr number))
(format t "~3D. " number)))
(if goalp
(let (quantified)
(write-term (cadr clause))
(princ " <- ")
(dolist (v variables)
(when (variable-occurs-in-term-p v (caddr clause))
(princ "(") (write-term v) (princ ")") (setq quantified t)))
(when quantified (princ " "))
(write-term (caddr clause)))
(let (quantified)
(dolist (v variables)
(when (variable-occurs-in-term-p v clause)
(princ "(") (write-term v) (princ ")") (setq quantified t)))
(when quantified (princ " "))
(write-term clause)))
(princ ".")))
(defvar *tracing* nil)
(defvar *spy-points* nil)
(defmacro trace-call-fail (nm form)
(let ((args (head-locs (get nm 'arity))))
`(progn
(when (or *tracing* (member ',nm *spy-points*))
(format t "~&~VT~4D CALL " (+ !level! !level! -1) !level!)
(write-functor-and-arguments* ',nm . ,args))
,form
(when (or *tracing* (member ',nm *spy-points*))
(format t "~&~VT~4D FAIL " (+ !level! !level! -1) !level!)
(write-functor-and-arguments* ',nm . ,args)))))
(defmacro trace-exit-redo (nm form)
(let ((args (head-locs (get nm 'arity))))
`(progn (when (or *tracing* (member ',nm *spy-points*))
(format t "~&~VT~4D EXIT " (+ !level! !level! -1) !level!)
(write-functor-and-arguments* ',nm . ,args))
,form
(when (or *tracing* (member ',nm *spy-points*))
(format t "~&~VT~4D REDO " (+ !level! !level! -1) !level!)
(write-functor-and-arguments* ',nm . ,args)))))
(defun wrap-call-fail-trace (form)
(if traceable `(trace-call-fail ,name ,form) form))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun wrap-exit-redo-trace (form)
(if traceable `(trace-exit-redo ,name ,form) form)))
;; CONSECUTIVELY BOUNDED DEPTH-FIRST SEARCH
(defvar *remaining-depth* 1000000) ; effectively infinite values so that depth-
(defvar *prev-depth-increment* 1000001) ; bounded code will run outside of search calls
(defvar *minus-next-depth-increment* -1000)
(defvar *old-remaining-depths* nil)
(defvar *old-prev-depth-increments* nil)
(defvar *old-minus-next-depth-increments* nil)
;; search is a Prolog predicate which takes
;; goal argument so that the user does not have to insert
;; end-search calls between the goals and additional
;; code to do something with, e.g., print, each solution
(defvar *trace-search* t) ; print a message when starting search on each level, etc.
(defvar *trace-search-calls* t) ; include number of inferences in the message
(defvar *trace-search-time*) ; time spent printing search messages to be excluded
; from execution time
(defmacro trace-search (&rest args)
`(when *trace-search*
(let ((start-time (get-internal-run-time)))
(fresh-line)
(when *trace-search-calls*
(format t "~11:D inferences so far. " *ncalls*))
(format t ,@args)
(incf *trace-search-time* (- (get-internal-run-time) start-time)))))
(defun begin-search (maximum-depth minimum-depth default-depth-increment
!level! !continuation!)
(let ((*old-remaining-depths* (cons *remaining-depth* *old-remaining-depths*))
(*old-prev-depth-increments* (cons *prev-depth-increment*
*old-prev-depth-increments*))
(*old-minus-next-depth-increments* (cons *minus-next-depth-increment*
*old-minus-next-depth-increments*)))
(let (*remaining-depth* *prev-depth-increment* *minus-next-depth-increment* cut)
(dereference maximum-depth) (if (null maximum-depth) (setq maximum-depth 1000000))
(dereference minimum-depth) (if (null minimum-depth) (setq minimum-depth 0))
(dereference default-depth-increment) (if (null default-depth-increment)
(setq default-depth-increment 1))
(setq *remaining-depth* minimum-depth)
(setq *prev-depth-increment* (1+ minimum-depth))
(do nil (nil)
(when (> *remaining-depth* maximum-depth)
(trace-search "Search ended, maximum depth reached. ") (return nil))
(trace-search "Start searching with ~:[no subgoals~;at most ~2D subgoal~:P~]. "
(> *remaining-depth* 0) *remaining-depth*)
(setq *minus-next-depth-increment* -1000)
(setq cut t)
(unwind-protect
(progn (funcall !continuation! !level!) (setq cut nil))
(when cut (trace-search "Search ended by cut. ")))
(let ((next-depth-increment (- *minus-next-depth-increment*)))
(when (= next-depth-increment 1000)
(trace-search "Search ended, no more inferences possible. ")
(return nil))
(setq next-depth-increment (max next-depth-increment default-depth-increment))
(incf *remaining-depth* next-depth-increment)
(setq *prev-depth-increment* next-depth-increment))))))
(defmacro end-search (form)
;; executes form only for solutions that were not discovered in a previous
;; search with lower depth bound
`(if (< *remaining-depth* *prev-depth-increment*)
(let* ((*remaining-depth* (car *old-remaining-depths*))
(*prev-depth-increment* (car *old-prev-depth-increments*))
(*minus-next-depth-increment* (car *old-minus-next-depth-increments*))
(*old-remaining-depths* (cdr *old-remaining-depths*))
(*old-prev-depth-increments* (cdr *old-prev-depth-increments*))
(*old-minus-next-depth-increments* (cdr *old-minus-next-depth-increments*)))
,form)))
(defmacro with-n-subgoals (n form)
`(let ((*remaining-depth* (- *remaining-depth* ,n)))
(cond ((minusp *remaining-depth*)
(if (> *remaining-depth* *minus-next-depth-increment*)
(setq *minus-next-depth-increment* *remaining-depth*))
nil)
(t ,form))))
(defun wrap-depth-test (form clause-body)
(if unbounded-search
form
(let ((n (clause-body-length clause-body T)))
(if (> n 0)
`(with-n-subgoals ,n ,form)
form))))
(defun not-solvable (!arg1! !arg2! !level! !continuation! &aux (!old-trail! *trail*))
;; !arg2! specifies depth of search
(incf !level!)
(trace-call-fail
not/2
(dereference !arg1!
:if-variable (error "NOT was given non-compound argument ~A" !arg1!)
:if-constant (error "NOT was given non-compound argument ~A" !arg1!)
:if-compound
(if (ground-term-p !arg1!)
(let ((*trace-search* (and *tracing* *trace-search*)))
(begin-search
!arg2! !arg2! nil !level!
#'(lambda (lev)
#+LispM (declare (sys:downward-function))
(apply (car !arg1!)
(append (cdr !arg1!) ; inefficient
(list lev
#'(lambda (lev)
#+LispM
(declare (sys:downward-function))
(declare (ignore lev))
(undo-bindings)
(return-from not-solvable nil)))))))
(trace-exit-redo not/2 (funcall !continuation! !level!)))
(error "NOT was given non-ground argument ~A" !arg1!)))))
;; MODEL-ELIMINATION REDUCTION RULE
(defun ancestors-name (nm)
(or (get nm 'ancestors)
(let ((w (intern (concatenate 'string "*" (symbol-name nm) "-ANCESTORS*") 'pttp)))
(setf (get nm 'ancestors) w)
w)))
(defun wrap-push-ancestor (form)
(if (or (not allow-repeated-goals) (not incomplete-inference))
(let ((nname (ancestors-name name)))
(if (= arity 0)
`(let ((,nname t))
,form)
`(let ((,nname (cons ,(cond ((= arity 0) t)
((= arity 1) '!arg1!)
(t '!args!))
,nname)))
,form)))
form))
(defun wrap-pop-ancestor (form)
(if (or (not allow-repeated-goals) (not incomplete-inference))
(let ((nname (ancestors-name name)))
(if (= arity 0)
`(let ((,nname nil))
,form)
`(let ((,nname (cdr ,nname)))
,form)))
form))
(defmacro reduce-by-ancestor (arity type)
;; must recompile this and its calls to change counting and tracing
(let ((count-calls t) (traceable nil))
`(dolist (!ancestor! !ancestors!)
(when ,(cond ((eq type :constant-first-argument)
(if (= arity 1)
`(unify-argument-with-constant !ancestor! !arg1! :trail-is-nil t)
`(and (unify-argument-with-constant
(car !ancestor!) !arg1! :trail-is-nil t)
,(if (= arity 2)
`(always-trails-unify-list
(cdr !ancestor!) (cdr !args!) !old-trail!)
`(always-trails-unify-list
(cdr !ancestor!) (cdr !args!) !old-trail!)))))
((eq type :variable-first-argument)
(if (= arity 1)
`(always-trails-unify !ancestor! !arg1! !old-trail!)
`(always-trails-unify-list !ancestor! !args! !old-trail!)))
((eq type :compound-first-argument)
(if (= arity 1)
`(unify-argument-with-compound !ancestor! !arg1! :trail-is-nil t)
`(and (unify-argument-with-compound
(car !ancestor!) !arg1! :trail-is-nil t)
,(if (= arity 2)
`(always-trails-unify-list
(cdr !ancestor!) (cdr !args!) !old-trail!)
`(always-trails-unify-list
(cdr !ancestor!) (cdr !args!) !old-trail!)))))
(t (error "Unrecognized first argument type ~A" type)))
,(wrap-count-calls (wrap-exit-redo-trace `(funcall !continuation! !level!)))
(if (= *trail* !old-trail!)
(return t)
(undo-bindings))))))
(defun reduce-by-ancestor-for-constant-first-argument/1 (!ancestors! !arg1! !old-trail!
!level! !continuation!)
(reduce-by-ancestor 1 :constant-first-argument))
(defun reduce-by-ancestor-for-variable-first-argument/1 (!ancestors! !arg1! !old-trail!
!level! !continuation!)
(reduce-by-ancestor 1 :variable-first-argument))
(defun reduce-by-ancestor-for-compound-first-argument/1 (!ancestors! !arg1! !old-trail!
!level! !continuation!)
(reduce-by-ancestor 1 :compound-first-argument))
(defun reduce-by-ancestor-for-constant-first-argument/2 (!ancestors! !arg1! !args!
!old-trail! !level!
!continuation!)
(reduce-by-ancestor 2 :constant-first-argument))
(defun reduce-by-ancestor-for-variable-first-argument/2 (!ancestors! !args! !old-trail!
!level! !continuation!)
(reduce-by-ancestor 2 :variable-first-argument))
(defun reduce-by-ancestor-for-compound-first-argument/2 (!ancestors! !arg1! !args!
!old-trail! !level!
!continuation!)
(reduce-by-ancestor 2 :compound-first-argument))
(defun reduce-by-ancestor-for-constant-first-argument/3 (!ancestors! !arg1! !args!
!old-trail! !level!
!continuation!)
(reduce-by-ancestor 3 :constant-first-argument))
(defun reduce-by-ancestor-for-variable-first-argument/3 (!ancestors! !args! !old-trail!
!level! !continuation!)
(reduce-by-ancestor 3 :variable-first-argument))
(defun reduce-by-ancestor-for-compound-first-argument/3 (!ancestors! !arg1! !args!
!old-trail! !level!
!continuation!)
(reduce-by-ancestor 3 :compound-first-argument))
(defun reduce-by-ancestor-for-constant-first-argument/4+ (!ancestors! !arg1! !args!
!old-trail! !level!
!continuation!)
(reduce-by-ancestor 4 :constant-first-argument))
(defun reduce-by-ancestor-for-variable-first-argument/4+ (!ancestors! !args! !old-trail!
!level! !continuation!)
(reduce-by-ancestor 4 :variable-first-argument))
(defun reduce-by-ancestor-for-compound-first-argument/4+ (!ancestors! !arg1! !args!
!old-trail! !level!
!continuation!)
(reduce-by-ancestor 4 :compound-first-argument))
(defun reduce-by-ancestor-call-fun (arity type)
(case arity
(1 (case type
(:constant-first-argument 'reduce-by-ancestor-for-constant-first-argument/1)
(:variable-first-argument 'reduce-by-ancestor-for-variable-first-argument/1)
(:compound-first-argument 'reduce-by-ancestor-for-compound-first-argument/1)))
(2 (case type
(:constant-first-argument 'reduce-by-ancestor-for-constant-first-argument/2)
(:variable-first-argument 'reduce-by-ancestor-for-variable-first-argument/2)
(:compound-first-argument 'reduce-by-ancestor-for-compound-first-argument/2)))
(3 (case type
(:constant-first-argument 'reduce-by-ancestor-for-constant-first-argument/3)
(:variable-first-argument 'reduce-by-ancestor-for-variable-first-argument/3)
(:compound-first-argument 'reduce-by-ancestor-for-compound-first-argument/3)))
(t (case type
(:constant-first-argument 'reduce-by-ancestor-for-constant-first-argument/4+)
(:variable-first-argument 'reduce-by-ancestor-for-variable-first-argument/4+)
(:compound-first-argument 'reduce-by-ancestor-for-compound-first-argument/4+)))))
(defun reduce-by-ancestor-call (name arity type)
(let ((ancestors (ancestors-name (negated-functor name))))
(if (= arity 0)
`(when ,ancestors
,(wrap-count-calls (wrap-exit-redo-trace `(progn
(funcall !continuation! !level!)
(return-from ,name nil)))))
`(when ,ancestors
(,(reduce-by-ancestor-call-fun arity type)
,ancestors
,@(cond ((= arity 0) nil)
((= arity 1) `(!arg1!))
(t (if (eq type :variable-first-argument)
`(!args!) `(!arg1! !args!))))
!old-trail!
!level!
!continuation!)))))
;; MODEL ELIMINATION PRUNING
(defmacro identical-to-ancestor (arity type)
(let ((temp (cond ((= arity 2) `(identical (cadr !ancestor!) !arg2!))
((= arity 3) `(and (identical (cadr !ancestor!) !arg2!)
(identical (caddr !ancestor!) !arg3!)))
((>= arity 4) `(identical-list (cdr !ancestor!) (cdr !args!))))))
`(dolist (!ancestor! !ancestors!)
(when ,(cond ((eq type :constant-first-argument)
(if (= arity 1)
`(identical-to-constant !ancestor! !arg1!)
`(and (identical-to-constant (car !ancestor!) !arg1!) ,temp)))
((eq type :variable-first-argument)
(if (= arity 1)
`(identical-to-variable !ancestor! !arg1!)
`(and (identical-to-variable (car !ancestor!) !arg1!) ,temp)))
((eq type :compound-first-argument)
(if (= arity 1)
`(identical-to-compound !ancestor! !arg1!)
`(and (identical-to-compound (car !ancestor!) !arg1!) ,temp)))
(t (error "Unrecognized first argument type ~A" type)))
(return t)))))
(defun identical-to-ancestor-for-constant-first-argument/1 (!ancestors! !arg1!)
(identical-to-ancestor 1 :constant-first-argument))
(defun identical-to-ancestor-for-variable-first-argument/1 (!ancestors! !arg1!)
(identical-to-ancestor 1 :variable-first-argument))
(defun identical-to-ancestor-for-compound-first-argument/1 (!ancestors! !arg1!)
(identical-to-ancestor 1 :compound-first-argument))
(defun identical-to-ancestor-for-constant-first-argument/2 (!ancestors! !arg1! !arg2!)
(identical-to-ancestor 2 :constant-first-argument))
(defun identical-to-ancestor-for-variable-first-argument/2 (!ancestors! !arg1! !arg2!)
(identical-to-ancestor 2 :variable-first-argument))
(defun identical-to-ancestor-for-compound-first-argument/2 (!ancestors! !arg1! !arg2!)
(identical-to-ancestor 2 :compound-first-argument))
(defun identical-to-ancestor-for-constant-first-argument/3 (!ancestors! !arg1! !arg2!
!arg3!)
(identical-to-ancestor 3 :constant-first-argument))
(defun identical-to-ancestor-for-variable-first-argument/3 (!ancestors! !arg1! !arg2!
!arg3!)
(identical-to-ancestor 3 :variable-first-argument))
(defun identical-to-ancestor-for-compound-first-argument/3 (!ancestors! !arg1! !arg2!
!arg3!)
(identical-to-ancestor 3 :compound-first-argument))
(defun identical-to-ancestor-for-constant-first-argument/4+ (!ancestors! !arg1! !args!)
(identical-to-ancestor 4 :constant-first-argument))
(defun identical-to-ancestor-for-variable-first-argument/4+ (!ancestors! !arg1! !args!)
(identical-to-ancestor 4 :variable-first-argument))
(defun identical-to-ancestor-for-compound-first-argument/4+ (!ancestors! !arg1! !args!)
(identical-to-ancestor 4 :compound-first-argument))
(defun identical-to-ancestor-call-fun (arity type)
(case arity
(1 (case type
(:constant-first-argument 'identical-to-ancestor-for-constant-first-argument/1)
(:variable-first-argument 'identical-to-ancestor-for-variable-first-argument/1)
(:compound-first-argument 'identical-to-ancestor-for-compound-first-argument/1)))
(2 (case type
(:constant-first-argument 'identical-to-ancestor-for-constant-first-argument/2)
(:variable-first-argument 'identical-to-ancestor-for-variable-first-argument/2)
(:compound-first-argument 'identical-to-ancestor-for-compound-first-argument/2)))
(3 (case type
(:constant-first-argument 'identical-to-ancestor-for-constant-first-argument/3)
(:variable-first-argument 'identical-to-ancestor-for-variable-first-argument/3)
(:compound-first-argument 'identical-to-ancestor-for-compound-first-argument/3)))
(t (case type
(:constant-first-argument 'identical-to-ancestor-for-constant-first-argument/4+)
(:variable-first-argument 'identical-to-ancestor-for-variable-first-argument/4+)
(:compound-first-argument 'identical-to-ancestor-for-compound-first-argument/4+)))))
(defun identical-to-ancestor-call (name arity type)
(let ((ancestors (ancestors-name name)))
(if (= arity 0)
ancestors
`(when ,ancestors
(,(identical-to-ancestor-call-fun arity type)
,ancestors
,@(cond ((= arity 0) nil)
((= arity 1) `(!arg1!))
((= arity 2) `(!arg1! !arg2!))
((= arity 3) `(!arg1! !arg2! !arg3!))
(t `(!arg1! !args!))))))))
;; COMPILER
(defun clause-head (clause)
(cond
((or (eq (car clause) '<-/2) (eq (car clause) '<-))
(cadr clause))
((or (eq (car clause) '->/2) (eq (car clause) '->))
(caddr clause))
(t
clause)))
(defun clause-pred (clause)
(car (clause-head clause)))
(defun clause-args (clause)
(cdr (clause-head clause)))
(defun clause-body (clause)
(cond
((or (eq (car clause) '<-/2) (eq (car clause) '<-))
(caddr clause))
((or (eq (car clause) '->/2) (eq (car clause) '->))
(cadr clause))
(t
'(true/0))))
(defun replace-variable-in-term (var value term)
(cond ((eq var term) value)