automated reasoning

46
Automated Reasoning General Game Playing Lecture 3 Michael Genesereth Spring 2005

Upload: dara

Post on 16-Jan-2016

41 views

Category:

Documents


0 download

DESCRIPTION

General Game PlayingLecture 3. Automated Reasoning. Michael Genesereth Spring 2005. Unification. Substititions. A substitution is a finite set of pairs of variables and terms, called replacements .  = ((?x . A) ((?y . (F B)) (?V . ?W)) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Automated Reasoning

Automated Reasoning

General Game Playing Lecture 3

Michael Genesereth Spring 2005

Page 2: Automated Reasoning

2

Unification

Page 3: Automated Reasoning

3

Substititions

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)

Page 4: Automated Reasoning

4

Unification

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)

Page 5: Automated Reasoning

5

Non-Uniqueness of Unification

Unifier 1:p(x,y){xa,yb,vb}=p(a,b)p(a,v){xa,yb,vb}=p(a,b)

Unifier 2:p(x,y){xa,yf(w),vf(w)}=p(a,f(w))p(a,v){xa,yf(w),vf(w)}=p(a,f(w))

Unifier 3:p(x,y){xa,yv}=p(a,v)p(a,v){xa,yv}=p(a,v)

Page 6: Automated Reasoning

6

Most 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){xa,yv}=p(a,v)p(a,v){xa,yv}=p(a,v)

p(x,y){xa,vy}=p(a,y)p(a,v){xa,vy}=p(a,y)

Page 7: Automated Reasoning

7

Most 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))))))

Page 8: Automated Reasoning

8

Most 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))))

Page 9: Automated Reasoning

9

Problem

~hates(X,X) hates(Y,f(Y))

Page 10: Automated Reasoning

10

Solution

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).

Page 11: Automated Reasoning

11

Most 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))))

Page 12: Automated Reasoning

12

Example

(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))

Page 13: Automated Reasoning

13

Example

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))

Page 14: Automated Reasoning

14

Example

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))

Page 15: Automated Reasoning

15

Example

(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: NILNIL

Page 16: Automated Reasoning

16

Evaluate

(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*)))))

Page 17: Automated Reasoning

17

Evaluate (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))))

Page 18: Automated Reasoning

18

Unification

Page 19: Automated Reasoning

19

Substititions

A substitution is a finite set of pairs of variables and terms, called replacements.

{Xa, Yf(b), VW}

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){Xa,Yf(b),VW}=p(a,a,f(b),Z)

Page 20: Automated Reasoning

20

Unification

A substitution is a unifier for an expression and an expression if and only if =.

p(X,Y){Xa,Yb,Vb}=p(a,b)p(a,V){Xa,Yb,Vb}=p(a,b)

If two expressions have a unifier, they are said to be unifiable. Otherwise, they are nonunifiable.

p(X,X)p(a,b)

Page 21: Automated Reasoning

21

Non-Uniqueness of Unification

Unifier 1:p(X,Y){Xa,Yb,Vb}=p(a,b)p(a,V){Xa,Yb,Vb}=p(a,b)

Unifier 2:p(X,Y){Xa,Yf(W),Vf(W)}=p(a,f(W))p(a,V){Xa,Yf(W),Vf(W)}=p(a,f(W))

Unifier 3:p(X,Y){Xa,YV}=p(a,V)p(a,V){Xa,YV}=p(a,V)

Page 22: Automated Reasoning

22

Most 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){Xa,YV}=p(a,V)p(a,V){Xa,YV}=p(a,V)

p(X,Y){Xa,VY}=p(a,Y)p(a,V){Xa,VY}=p(a,Y)

Page 23: Automated Reasoning

23

Most 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))))))

Page 24: Automated Reasoning

24

Most 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))))

Page 25: Automated Reasoning

25

Problem

~hates(X,X) hates(Y,f(Y))

Page 26: Automated Reasoning

26

Solution

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).

Page 27: Automated Reasoning

27

Most General Unification (concluded)

function mguchkp (p,q,al) {cond(p=q, al; varp(p), mguchkp(p,cdr(assoc(q,al)),al); atom(q), nil; t, some(lambda(x).mguchkp(p,x,al),q))}

Page 28: Automated Reasoning

28

Most General Unification (concluded)

function mguchkp (p,q,al) {if (p=q) al; else if varp(p) mguchkp(p,cdr(assoc(q,al)),al); else if atom(q) nil; else some(lambda(x).mguchkp(p,x,al),q)}

Page 29: Automated Reasoning

29

Example

Call: mgu(p(X,b),p(a,Y),{}) | | Call: mgu(p,p,{}) | Exit: {} | | Call: mgu(X,a,{}) | Exit: {Xa} | | Call: mgu(b,Y,{Xa}) | Exit: {Yb,Xa} | Exit: {Yb,Xa}

Page 30: Automated Reasoning

30

Example

Call: mgu(p(X,X),p(a,b),{}) | | Call: mgu(p,p,{}) | Exit: {} | | Call: mgu(X,a,{}) | Exit: {X <- a} | | Call: mgu(X,b, {X <- a}) | Call: mgu(a,b,{X <- a}) | Exit: nil | Exit: nil | Exit: nil

Page 31: Automated Reasoning

31

Example

Call: mgu(p(f(X),f(X)),p(Y,f(a)),{}) | | Call: mgu(p,p,{}) | Exit: {} | | Call: mgu(f(X),Y,{}) | Exit: {Y <- f(X)} | | Call: mgu(f(X),f(a),{Y <- f(X)}) | Call: mgu(f,f,{Y <- f(X)}) | Exit: {Y <- f(X)} | Call: mgu(X,a,{Y <- f(X)} | Exit: {X <- a,Y <- f(X)} | Exit: {X <- a,Y <- f(X)} | Exit: {X <- a,Y <- f(X)}

Page 32: Automated Reasoning

32

Example

Call: mgu(p(X,X),p(Y,f(Y)),{}) | | Call: mgu(p,p,{}) | Exit: {} | | Call: mgu(f(X),Y,{}) | Exit: {Y <- f(X)} | | Call: mgu(f(X),f(Y),{Y <- f(X)}) | Call: mgu(f,f,{Y <- f(X)}) | Exit: {Y <- f(X)} | Call: mgu(X,Y,{Y <- f(X)}) | Exit: nil | Exit: nil | Exit: nil

Page 33: Automated Reasoning

33

Evaluation

Page 34: Automated Reasoning

34

Reasoning Subroutines

Database: Call: theory Exit: {m(a,b), m(b,c), p(X,Y)<=m(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}

Page 35: Automated Reasoning

35

Evaluate

(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*)))))

Page 36: Automated Reasoning

36

Evaluate (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))))

Page 37: Automated Reasoning

37

Deduction

Page 38: Automated Reasoning

38

Reasoning Subroutines

Database: Call: theory Exit: {m(a,b), m(b,c),p(X,Y)<=m(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}

Page 39: Automated Reasoning

39

Backward

(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*)))))

Page 40: Automated Reasoning

40

Backward (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))))

Page 41: Automated Reasoning

41

Page 42: Automated Reasoning

42

Backward Chaining

Backward Chaining is the same as reduction except that it works on rule form rather than clausal form.

ϕ⇐ ϕ1 ∧...∧ϕm

ψ ∧ψ1 ∧...∧ψ n?

ϕ1 ∧...∧ϕm∧[ψ ]∧ψ1 ∧...∧ψ n?σ

where σ =mgu(ϕ,ψ )

Reduced literals need be retained only for non-Horn premises.

Cancellation and Dropping are analogous.

Page 43: Automated Reasoning

43

Example

1. m⇐ Premise

2. p⇐ m Premise

3. q⇐ m Premise

4. r⇐ p∧q Premise

5. r? Goal

6. p∧q? 4,5

7. m∧q? 2,6

8. q? 1,7

9. m? 3,8

10. ? 1,9

Page 44: Automated Reasoning

44

Example

Given q(x) p(x) and p(a), find a term such that q() is true.

1. q(x) p(x) Premise2. p(a) Premise3. goal(z) q(z) Goal4. goal(z) p(z) 1, 35. goal(a) 2, 4

Page 45: Automated Reasoning

45

Example

Given q(x) p(x) and p(a) and p(b), find a term such that q() is true.

1. q(x) p(x) Premise2. p(a) Premise3. p(b) Premise4. goal(z) q(z) Goal5. goal(z) p(z) 1, 46. goal(a) 2, 57. goal(b) 3, 5

Page 46: Automated Reasoning

46

Example

Given q(x) p(x) and p(a) p(b), find a term such that q() is true.

1. q(x) p(x) Premise2. p(a) p(b) Premise3. goal(z) q(z) Goal4. goal(z) p(z) 1, 35. goal(a) p(b) 2, 46. goal(a) goal(b) 4, 5