Title: Automated Reasoning
1Automated Reasoning
General Game Playing Lecture 3
Michael Genesereth Spring 2005
2Unification
3Substititions
A substitution is a finite set of pairs of
variables and terms, called replacements. ?
((?x . A) ((?y . (F B)) (?V . ?W)) The result of
applying a substitution ? to an expression ? is
the expression ?? obtained from ? by replacing
every occurrence of every variable in the
substitution by its replacement. (P ?x ?x ?y
?Z)? (P A A (F B) ?Z)
4Unification
A substitution ? is a unifier for an expression ?
and an expression ? if and only if ????. ?
((?x . A) ((?y . (F B)) (?V . ?W)) ? ((?x . A)
((?y . (F B)) (?V . ?W)) (P ?x ?y)? (P A B) (P
?x ?y)? (P A B) If two expressions have a
unifier, they are said to be unifiable.
Otherwise, they are nonunifiable. (P ?x ?y) (P A
B)
5Non-Uniqueness of Unification
Unifier 1 p(x,y)x?a,y?b,v?bp(a,b) p(a,v)x?a,y
?b,v?bp(a,b) Unifier 2 p(x,y)x?a,y?f(w),v?f(w
)p(a,f(w)) p(a,v)x?a,y?f(w),v?f(w)p(a,f(w))
Unifier 3 p(x,y)x?a,y?vp(a,v) p(a,v)x?a,y?v
p(a,v)
6Most General Unifier
A substitution ? is a most general unifier (mgu)
of two expressions if and only if it is as
general as or more general than any other
unifier. Theorem If two expressions are
unifiable, then they have an mgu that is unique
up to variable permutation. p(x,y)x?a,y?vp(a,v
) p(a,v)x?a,y?vp(a,v) p(x,y)x?a,v?yp(a,y) p
(a,v)x?a,v?yp(a,y)
7Most General Unification
(defun mgu (x y al) (cond ((eq x y) al)
((varp x) (mguvar x y al)) ((atom x)
(cond ((varp y) (mguvar y x al))
((atom y) (if (equalp x y) al)))) (t (cond
((varp y) (mguvar y x al)) ((atom y)
nil) ((setq al (mgu (car x) (car y) al))
(mgu (cdr x) (cdr y) al))))))
8Most General Unification (continued)
(defun mguvar (x y al) (let (dum) (cond
((setq dum (assoc x al)) (mgu (cdr
dum) y al)) ((eq x (setq y (mguval y
al))) al) ((mguchkp x y al)) nil)
(t (acons x y al))))) (defun mguval (x al)
(let (dum) (cond ((and (varp x) (setq dum
(assoc x al))) (mguval (cdr dum) al))
(t x))))
9Problem
hates(X,X) hates(Y,f(Y))
10Solution
Before assigning a variable to an expression,
first check that the variable does not occur
within that expression. This is called, oddly
enough, the occur check test. Prolog does not do
the occur check (and is proud of it).
11Most General Unification (concluded)
(defun mguchkp (p q al) (cond ((eq p q))
((varp q) (mguchkp p (cdr (assoc q al)) al))
((atom q) nil) (t (some '(lambda (x)
(mguchkp p x al)) q))))
12Example
(mgu (p ?x b) (p a ?y)) Calling (MGU (P ?x B)
(P A ?y) ((t . t))) Calling (MGU P P ((t .
t))) MGUEXP returned ((t . t)) Calling
(MGU ?x A ((t . t))) MGUEXP returned ((?x .
A) (t . t)) Calling (MGU B ?y ((?x . A) (t .
t))) MGUEXP returned ((?y . B) (?x . A) (t .
t)) MGUEXP returned ((?y . B) (?x . A) (t .
t)) ((?y . B) (?x . A) (t . t))
13Example
Call (mgu (p ?x ?x) (p a b) ((t . t)))
Call (mgu p p ((t . t))) Exit ((t . t))
Call (mgu ?x a ((t . t))) Exit ((?x .
a) (t . t)) Call (mgu ?x b ((?x . a) (t
. t))) Call (mgu a b ((?x . a) (t . t)))
Exit nil Exit ((?y . b) (?x . a) (t .
t)) Exit ((?y . b) (?x . a) (t . t))
14Example
Call (mgu (p (f ?x) (f ?x)) (p ?y (f a))
((t.t))) Call (mgu p p ((t . t))) Exit
((t . t)) Call (mgu (f ?x) ?y ((t . t))
Exit ((?y . (f ?x)) (t . t))) Call (mgu (f
?x) (f a) ((?y . (f ?x)) (t . t))) Call (mgu
f f ((?y . (f ?x)) (t . t))) Exit ((?y . (f
?x)) (t . t))) Call (mgu ?x a ((?y . (f ?x))
(t . t))) Exit ((?x . a) (?y . (f ?x)) (t .
t) Exit ((?x . a) (?y . (f ?x)) (t .
t)) Exit ((?x . a) (?y . (f ?x)) (t . t))
15Example
(mgu (p (f ?x) (f ?x)) (p ?y (f ?y))) Call
(MGU (P ?x ?x) (P ?y (F ?y)) ((t . t))) Call
(MGU P P ((t . t))) Exit ((t . t))
Call (MGU (F ?x) ?y ((t . t)) Exit ((?y .
(F ?x)) (t . t))) Call (MGU (F ?x) (F ?y)
((t . t)) Call (MGU F F ((?y . (F ?x)) (t
. t))) Exit ((?y . (F ?x)) (t . t)))
Call (MGU ?x ?y ((?y . (F ?x)) (t . t)))
Exit NIL Exit NIL Exit NIL NIL
16Evaluate
(defun myevaluate (thing p theory) (let
(answers) (myeval p nil truth)
(nreverse (remove-duplicates answers)))) (defun
eval (p pl al) (cond ((atom p) (evalrs p pl
al)) ((eq 'not (car p)) (evalunprovable p
pl al)) ((eq 'and (car p)) (eval
(cadr p) (append (cddr p) pl) al)) (t
(evalrs p pl al)))) (defun evalexit (pl al)
(cond (pl (eval (car pl) (cdr pl) al)) (t
(setq answers (cons (plug thing
al) answers)))))
17Evaluate (continued)
(defun evalunprovable (p pl al) (unless (eval
(cadr p) (cdr p) al) (evalexit pl
al))) (defun evalrs (p pl al) (do ((l theory
(cdr l)) (rule) (bl)) ((null l))
(setq rule (stdize (car l))) (when (setq bl
(mguexp p rule al)) (evalexit pl bl))))
18Unification
19Substititions
A substitution is a finite set of pairs of
variables and terms, called replacements. X?a,
Y?f(b), V?W The result of applying a
substitution ? to an expression ? is the
expression ?? obtained from ? by replacing every
occurrence of every variable in the substitution
by its replacement. p(X,X,Y,Z)X?a,Y?f(b),V?Wp(
a,a,f(b),Z)
20Unification
A substitution ? is a unifier for an expression ?
and an expression ? if and only if
????. p(X,Y)X?a,Y?b,V?bp(a,b) p(a,V)X?a,Y?b,
V?bp(a,b) If two expressions have a unifier,
they are said to be unifiable. Otherwise, they
are nonunifiable. p(X,X) p(a,b)
21Non-Uniqueness of Unification
Unifier 1 p(X,Y)X?a,Y?b,V?bp(a,b) p(a,V)X?a,Y
?b,V?bp(a,b) Unifier 2 p(X,Y)X?a,Y?f(W),V?f(W
)p(a,f(W)) p(a,V)X?a,Y?f(W),V?f(W)p(a,f(W))
Unifier 3 p(X,Y)X?a,Y?Vp(a,V) p(a,V)X?a,Y?V
p(a,V)
22Most General Unifier
A substitution ? is a most general unifier (mgu)
of two expressions if and only if it is as
general as or more general than any other
unifier. Theorem If two expressions are
unifiable, then they have an mgu that is unique
up to variable permutation. p(X,Y)X?a,Y?Vp(a,V
) p(a,V)X?a,Y?Vp(a,V) p(X,Y)X?a,V?Yp(a,Y) p
(a,V)X?a,V?Yp(a,Y)
23Most General Unification
(defun mgu (x y al) (cond ((eq x y) al)
((varp x) (mguvar x y al)) ((atom x)
(cond ((varp y) (mguvar y x al))
((atom y) (if (equalp x y) al)))) (t (cond
((varp y) (mguvar y x al)) ((atom y)
nil) ((setq al (mgu (car x) (car y) al))
(mgu (cdr x) (cdr y) al))))))
24Most General Unification (continued)
(defun mguvar (x y al) (let (dum) (cond
((setq dum (assoc x al)) (mgu (cdr
dum) y al)) ((eq x (setq y (mguval y
al))) al) ((mguchkp x y al)) nil)
(t (acons x y al))))) (defun mguval (x al)
(let (dum) (cond ((and (varp x) (setq dum
(assoc x al))) (mguval (cdr dum) al))
(t x))))
25Problem
hates(X,X) hates(Y,f(Y))
26Solution
Before assigning a variable to an expression,
first check that the variable does not occur
within that expression. This is called, oddly
enough, the occur check test. Prolog does not do
the occur check (and is proud of it).
27Most General Unification (concluded)
function mguchkp (p,q,al) cond(pq, al
varp(p), mguchkp(p,cdr(assoc(q,al)),al)
atom(q), nil t, some(lambda(x).mguchkp(p,
x,al),q))
28Most General Unification (concluded)
function mguchkp (p,q,al) if (pq) al else
if varp(p) mguchkp(p,cdr(assoc(q,al)),al)
else if atom(q) nil else some(lambda(x).mguchk
p(p,x,al),q)
29Example
Call mgu(p(X,b),p(a,Y),) Call
mgu(p,p,) Exit Call
mgu(X,a,) Exit X?a Call
mgu(b,Y,X?a) Exit Y?b,X?a Exit
Y?b,X?a
30Example
Call mgu(p(X,X),p(a,b),) Call
mgu(p,p,) Exit Call
mgu(X,a,) Exit X lt- a Call
mgu(X,b, X lt- a) Call mgu(a,b,X lt- a)
Exit nil Exit nil Exit nil
31Example
Call mgu(p(f(X),f(X)),p(Y,f(a)),)
Call mgu(p,p,) Exit Call
mgu(f(X),Y,) Exit Y lt- f(X)
Call mgu(f(X),f(a),Y lt- f(X)) Call
mgu(f,f,Y lt- f(X)) Exit Y lt- f(X)
Call mgu(X,a,Y lt- f(X) Exit X lt- a,Y
lt- f(X) Exit X lt- a,Y lt- f(X) Exit
X lt- a,Y lt- f(X)
32Example
Call mgu(p(X,X),p(Y,f(Y)),) Call
mgu(p,p,) Exit Call
mgu(f(X),Y,) Exit Y lt- f(X)
Call mgu(f(X),f(Y),Y lt- f(X)) Call
mgu(f,f,Y lt- f(X)) Exit Y lt- f(X)
Call mgu(X,Y,Y lt- f(X)) Exit nil
Exit nil Exit nil
33Evaluation
34Reasoning Subroutines
Database Call theory Exit m(a,b), m(b,c),
p(X,Y)ltm(X,Y) Subroutines Call
findp(p(X,Y),theory) Exit true Call
findx(X,p(X,Y),theory) Exit A Call
finds(X,p(X,Y),theory) Exit A,B
35Evaluate
(defun myevaluate (thing p theory) (let
(answers) (myeval p nil truth)
(nreverse (remove-duplicates answers)))) (defun
eval (p pl al) (cond ((atom p) (evalrs p pl
al)) ((eq 'not (car p)) (evalunprovable p
pl al)) ((eq 'and (car p)) (eval
(cadr p) (append (cddr p) pl) al)) (t
(evalrs p pl al)))) (defun evalexit (pl al)
(cond (pl (eval (car pl) (cdr pl) al)) (t
(setq answers (cons (plug thing
al) answers)))))
36Evaluate (continued)
(defun evalunprovable (p pl al) (unless (eval
(cadr p) (cdr p) al) (evalexit pl
al))) (defun evalrs (p pl al) (do ((l theory
(cdr l)) (rule) (bl)) ((null l))
(setq rule (stdize (car l))) (when (setq bl
(mguexp p rule al)) (evalexit pl bl))))
37Deduction
38Reasoning Subroutines
Database Call theory Exit m(a,b),
m(b,c),p(X,Y)ltm(X,Y) Subroutines Call
findp(p(X,Y),theory) Exit true Call
findx(X,p(X,Y),theory) Exit A Call
finds(X,p(X,Y),theory) Exit A,B
39Backward
(defun myevaluate (thing p theory) (let
(answers) (myeval p nil truth)
(nreverse (remove-duplicates answers)))) (defun
eval (p pl al) (cond ((atom p) (evalrs p pl
al)) ((eq 'not (car p)) (evalunprovable p
pl al)) ((eq 'and (car p)) (eval
(cadr p) (append (cddr p) pl) al)) (t
(evalrs p pl al)))) (defun evalexit (pl al)
(cond (pl (eval (car pl) (cdr pl) al)) (t
(setq answers (cons (plug thing
al) answers)))))
40Backward (continued)
(defun evalunprovable (p pl al) (unless (eval
(cadr p) (cdr p) al) (evalexit pl
al))) (defun evalrs (p pl al) (do ((l theory
(cdr l)) (rule) (bl)) ((null l))
(setq rule (stdize (car l))) (when (setq bl
(mguexp p rule al)) (evalexit pl bl))))
41(No Transcript)
42Backward Chaining
Backward Chaining is the same as reduction except
that it works on rule form rather than clausal
form.
Reduced literals need be retained only for
non-Horn premises. Cancellation and Dropping
are analogous.
43Example
44Example
- Given q(x) ? p(x) and p(a), find a term ? such
that q(?) is - true.
- q(x) ? p(x) Premise
- p(a) ? Premise
- goal(z) ? q(z) Goal
- goal(z) ? p(z) 1, 3
- goal(a) ? 2, 4
45Example
- Given q(x) ? p(x) and p(a) and p(b), find a term
? such that - q(?) is true.
- q(x) ? p(x) Premise
- p(a) ? Premise
- p(b) ? Premise
- goal(z) ? q(z) Goal
- goal(z) ? p(z) 1, 4
- goal(a) ? 2, 5
- goal(b) ? 3, 5
46Example
- Given q(x) ? p(x) and p(a) ? p(b), find a term ?
such that - q(?) is true.
- q(x) ? p(x) Premise
- p(a) ? p(b) ? Premise
- goal(z) ? q(z) Goal
- goal(z) ? p(z) 1, 3
- goal(a) ? p(b) ? 2, 4
- goal(a) ? goal(b) ? 4, 5