-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch-approach.lisp
52 lines (39 loc) · 1.55 KB
/
batch-approach.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
(in-package #:cl-prolog)
;; Prove of 11.2
(defun prove (goal bindings)
"Return a list of possible solutions to goal."
(mapcan #'(lambda (clause)
(let ((new-clause (rename-variables clause)))
(prove-all (clause-body new-clause)
(unify goal (clause-head new-clause) bindings))))
(get-clauses (predicate goal))))
(defun prove-all (goals bindings)
"Return a list of solutions to the conjunction of goals."
(cond ((eq bindings fail) fail)
((null goals) (list bindings))
(t (mapcan #'(lambda (goal1-solution)
(prove-all (rest goals) goal1-solution))
(prove (first goals) bindings)))))
;; (defmacro ?- (&rest goals) `(prove-all ',goals no-bindings))
;; (defmacro ?- (&rest goals) `(top-level-prove ',goals))
(defmacro ?- (&rest goals) `(top-level-prove ',(replace-?-vars goals)))
(defun top-level-prove (goals)
"Prove the goals, and print variables readably."
(show-prolog-solutions
(variables-in goals)
(prove-all goals no-bindings)))
(defun show-prolog-solutions (vars solutions)
"Print the variables in each of the solutions."
(if (null solutions)
(format t "~&No.")
(mapc #'(lambda (solution) (show-prolog-vars vars solution))
solutions))
(values))
(defun show-prolog-vars (vars bindings)
"Print each variable with its binding."
(if (null vars)
(format t "~&Yes")
(dolist (var vars)
(format t "~&~a = ~a" var
(subst-bindings bindings var))))
(princ ";"))