CS440: Assignment #8
Assigned Nov. 7th
Due 12:30, Nov. 21st

Logical Reasoning: A Subset of Prolog in Lisp

For this assignment you will be implementing a subset of Prolog using Lisp. You will end up with a lisp function, named ask, that you can call to prove the validity of a list of clauses given a list of Horn clauses assumed to be true.

This assignment is divided into two parts. For Part 1 you will complete the provided Lisp code to implement unification. Part 2 requires you to use the unification functions to implement additional functions to prove the validity of clauses.

First, retrieve the Lisp code in ~cs440/96/hw8-start.lisp.

Part 1. Unification

Complete the definitions for unifier, and unifier-var. Then test your code on these examples. You do not have to hand in the results of these tests.
(unifier '?x 4)
   ((($VAR X) 4))

(unifier 4 6)
   FAILED

(unify '?x 4)
   4

(unify 4 6)
   4

(unify 6 4)
   6

(unifier '?x '(f ?x))
   FAILED

(unifier '(f ?x) '?x)
   FAILED

(unify '?x '(f ?x))
   ($VAR X)

(unify '(f ?x) '?x)
   (F ($VAR X))

(unifier '?y '?x)
   ((($VAR Y) ($VAR X)))

(unify '?y '(f ?x))
   (F ($VAR X))

(unify '?y '?x)
   ($VAR X)

(unify '(f ?x) '?y)
   (F ($VAR X))

(unifier '(or (g ?x) (p 5 ?z ?t)) '(or ?q (p ?t ?y ?h))) 
   ((($VAR H) 5) (($VAR Z) ($VAR Y)) (($VAR T) 5) (($VAR Q) (G ($VAR X))))

(unify '(or (g ?x) (p 5 ?z ?t)) '(or ?q (p ?t ?y ?h)))  
   (OR (G ($VAR X)) (P 5 ($VAR Y) 5))

(unifier '(p ?x ?y ?z) '(p ?y ?z 4))
   ((($VAR Z) 4) (($VAR Y) ($VAR Z)) (($VAR X) ($VAR Y)))

(unify '(p ?x ?y ?z) '(p ?y ?z 4))
   (P 4 4 4)

(unifier '(p ?x ?y ?z) '(p ?y ?z ?y))
   FAILED
Also, run these examples. Do hand in the results with explanations of why you get the results you did.
(unify '(father tom ?y) '(father ?x mike))
(unify '(father tom ?y) '(father ?x mike))
(unify '(father tom ?x) '(father ?x mike))
(unify '(sons-of ?x tom dick harry) '(sons-of dave ?a ?b ?c))
(unify '(sons-of ?x tom dick harry) '(sons-of dave dick ?a ?b))
(unify '(isa new-world-order ?x) '(isa ?x reality))
(unify '(or (predicate1 a b) (predicate2 c d)) '(predicate1 ?x b))
(unify '(father ?x ?y) '?x)

Part 2. Proof Procedure

The primary function for this part is ask. It accepts as arguments a list of goal clauses and a list of assertions, the logic program. If it succeeds, ask returns Yes and the list of goals with substitutions made. Otherwise, it just returns No.

Another function, prove is used by ask to actually do the proof. prove uses rename-vars, which uses rename-vars-aux, which uses check-gen-new-var.

Complete the incomplete functions in ~cs440/96/hw8-start.lisp. Test your functions on the following examples. I have used the format function at key places in prove to produce the extra debugging output shown. Hand in the results of your functions on these examples. If your functions are not working, hand in as much debugging output as you think will be helpful to me in understanding your partial result.

At the end of this document is one final example for you to run. Do not forget it!


>(ask '((person ?x)) '(((man Marcus))
                       ((man Caesar))
                       ((person ?x) <- (man ?x))
                       ((person Cleo))))))
Yes.
((PERSON MARCUS))
(((VAR X 63) MARCUS) ((VAR X) (VAR X 63)))
Now, the same example with more debugging output:
>(ask '((person ?x)) '(((man Marcus))
                       ((man Caesar))
                       ((person ?x) <- (man ?x))
                       ((person Cleo))))))
Trying to unify goal (PERSON (VAR X)) with assertion consequent 
((MAN MARCUS))
Resulting substitutions = FAILED
Trying to unify goal (PERSON (VAR X)) with assertion consequent 
((MAN CAESAR))
Resulting substitutions = FAILED
Trying to unify goal (PERSON (VAR X)) with assertion consequent 
((PERSON (VAR X 135)) <- (MAN (VAR X 135)))
Resulting substitutions = (((VAR X) (VAR X 135)))
Adding antecedents to goal list and continuing.
Trying to unify goal (MAN (VAR X 135)) with assertion consequent 
((MAN MARCUS))
Resulting substitutions = (((VAR X 135) MARCUS) ((VAR X) (VAR X 135)))
No antecedents. Going on to next goal.

Yes.
((PERSON MARCUS))
(((VAR X 135) MARCUS) ((VAR X) (VAR X 135)))

Here's a second example, the one discussed in class.

>(ask '((predecessor tom pat))
      '(((parent tom bob))
        ((parent tom liz))
        ((parent bob ann))
        ((parent bob pat))
        ((predecessor ?x ?z) <- (parent ?x ?z))
        ((predecessor ?x ?z) <- (parent ?x ?y) (predecessor ?y ?z))))))
Yes.
((PREDECESSOR TOM PAT))
(((VAR Z 83) PAT) ((VAR X 83) BOB) ((VAR Y 77) BOB) ((VAR Z 77) PAT)
 ((VAR X 77) TOM))
Again, with more output:
>(ask '((predecessor tom pat))
      '(((parent tom bob))
        ((parent tom liz))
        ((parent bob ann))
        ((parent bob pat))
        ((predecessor ?x ?z) <- (parent ?x ?z))
        ((predecessor ?x ?z) <- (parent ?x ?y) (predecessor ?y ?z))))))
Trying to unify goal (PREDECESSOR TOM PAT) with assertion consequent 
((PARENT TOM BOB))
Resulting substitutions = FAILED
Trying to unify goal (PREDECESSOR TOM PAT) with assertion consequent 
((PARENT TOM LIZ))
Resulting substitutions = FAILED
Trying to unify goal (PREDECESSOR TOM PAT) with assertion consequent 
((PARENT BOB ANN))
Resulting substitutions = FAILED
Trying to unify goal (PREDECESSOR TOM PAT) with assertion consequent 
((PARENT BOB PAT))
Resulting substitutions = FAILED
Trying to unify goal (PREDECESSOR TOM PAT) with assertion consequent 
((PREDECESSOR (VAR X 114) (VAR Z 114)) <-
 (PARENT (VAR X 114) (VAR Z 114)))
Resulting substitutions = (((VAR Z 114) PAT) ((VAR X 114) TOM))
Adding antecedents to goal list and continuing.
Trying to unify goal (PARENT (VAR X 114) (VAR Z 114)) with assertion consequent 
((PARENT TOM BOB))
Resulting substitutions = FAILED
Trying to unify goal (PARENT (VAR X 114) (VAR Z 114)) with assertion consequent 
((PARENT TOM LIZ))
Resulting substitutions = FAILED
Trying to unify goal (PARENT (VAR X 114) (VAR Z 114)) with assertion consequent 
((PARENT BOB ANN))
Resulting substitutions = FAILED
Trying to unify goal (PARENT (VAR X 114) (VAR Z 114)) with assertion consequent 
((PARENT BOB PAT))
Resulting substitutions = FAILED
Trying to unify goal (PARENT (VAR X 114) (VAR Z 114)) with assertion consequent 
((PREDECESSOR (VAR X 119) (VAR Z 119)) <-
 (PARENT (VAR X 119) (VAR Z 119)))
Resulting substitutions = FAILED
Trying to unify goal (PARENT (VAR X 114) (VAR Z 114)) with assertion consequent 
((PREDECESSOR (VAR X 120) (VAR Z 120)) <-
 (PARENT (VAR X 120) (VAR Y 120))
 (PREDECESSOR (VAR Y 120) (VAR Z 120)))
Resulting substitutions = FAILED
Trying to unify goal (PREDECESSOR TOM PAT) with assertion consequent 
((PREDECESSOR (VAR X 122) (VAR Z 122)) <-
 (PARENT (VAR X 122) (VAR Y 122))
 (PREDECESSOR (VAR Y 122) (VAR Z 122)))
Resulting substitutions = (((VAR Z 122) PAT) ((VAR X 122) TOM))
Adding antecedents to goal list and continuing.
Trying to unify goal (PARENT (VAR X 122) (VAR Y 122)) with assertion consequent 
((PARENT TOM BOB))
Resulting substitutions = (((VAR Y 122) BOB) ((VAR Z 122) PAT)
                      ((VAR X 122) TOM))
No antecedents. Going on to next goal.
Trying to unify goal (PREDECESSOR (VAR Y 122) (VAR Z 122)) with assertion consequent 
((PARENT TOM BOB))
Resulting substitutions = FAILED
Trying to unify goal (PREDECESSOR (VAR Y 122) (VAR Z 122)) with assertion consequent 
((PARENT TOM LIZ))
Resulting substitutions = FAILED
Trying to unify goal (PREDECESSOR (VAR Y 122) (VAR Z 122)) with assertion consequent 
((PARENT BOB ANN))
Resulting substitutions = FAILED
Trying to unify goal (PREDECESSOR (VAR Y 122) (VAR Z 122)) with assertion consequent 
((PARENT BOB PAT))
Resulting substitutions = FAILED
Trying to unify goal (PREDECESSOR (VAR Y 122) (VAR Z 122)) with assertion consequent 
((PREDECESSOR (VAR X 128) (VAR Z 128)) <-
 (PARENT (VAR X 128) (VAR Z 128)))
Resulting substitutions = (((VAR Z 128) PAT) ((VAR X 128) BOB)
                      ((VAR Y 122) BOB) ((VAR Z 122) PAT)
                      ((VAR X 122) TOM))
Adding antecedents to goal list and continuing.
Trying to unify goal (PARENT (VAR X 128) (VAR Z 128)) with assertion consequent 
((PARENT TOM BOB))
Resulting substitutions = FAILED
Trying to unify goal (PARENT (VAR X 128) (VAR Z 128)) with assertion consequent 
((PARENT TOM LIZ))
Resulting substitutions = FAILED
Trying to unify goal (PARENT (VAR X 128) (VAR Z 128)) with assertion consequent 
((PARENT BOB ANN))
Resulting substitutions = FAILED
Trying to unify goal (PARENT (VAR X 128) (VAR Z 128)) with assertion consequent 
((PARENT BOB PAT))
Resulting substitutions = (((VAR Z 128) PAT) ((VAR X 128) BOB)
                      ((VAR Y 122) BOB) ((VAR Z 122) PAT)
                      ((VAR X 122) TOM))
No antecedents. Going on to next goal.
Yes.
((PREDECESSOR TOM PAT))
(((VAR Z 128) PAT) ((VAR X 128) BOB) ((VAR Y 122) BOB)
 ((VAR Z 122) PAT) ((VAR X 122) TOM))
Given the assertions, what is the answer to "Is C1 a mammal?" Show your call to ask and the results.