Automated Reasoning General Game PlayingLecture 3 Michael Genesereth Spring 2005.

Slides:



Advertisements
Similar presentations
09 Examples Functional Programming. Tower of Hanoi AB C.
Advertisements

Resolution.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
1 Programming Languages and Paradigms Lisp Programming.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
Incomplete Information General Game PlayingLecture 5 Michael Genesereth Spring 2005.
Computational Logic General Game PlayingLecture 4 Michael Genesereth / Nat Love Spring 2006.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
CSE (c) S. Tanimoto, 2007 Unification 1 Unification Predicate calculus rules are sometimes so general that we need to create specializations of.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
First Order Predicate Logic
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Unification We can represent any substitution by a set of ordered pairs: s = { t 1 /v 1, t 2 /v 2, …, t n /v n } where: t i /v i means that the term i.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
Dr. Philip Cannata 1 Programming Languages Chapter 14 – Functional Programming – Lisp.
Resolution Preliminaries Computational LogicLecture 9 By Prof. Michael Genesereth, Computer Science, Stanford Univ Spring 2005 Modified by Charles Ling.
Functional Programming: Lisp MacLennan Chapter 10.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
State Space Search and Planning General Game PlayingLecture 4 Michael Genesereth Spring 2005.
Comparative Programming Languages Functional programming with Lisp/Scheme.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Converting arbitrary wffs to CNF A wff is in prenex form iff it consists of a string of quantifiers (called a prefix) followed by a quantifier free formula.
1 Section 7.1 First-Order Predicate Calculus Predicate calculus studies the internal structure of sentences where subjects are applied to predicates existentially.
1 Introduction to Artificial Intelligence LECTURE 9: Resolution in FOL Theorem Proving in First Order Logic Unification Resolution.
Additional Scheme examples
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Chapter 15 – Functional Programming Languages
Horn Clauses and Unification
LISP A brief overview.
First Lecture on Introductory Lisp
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Horn Clauses and Unification
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE (c) S. Tanimoto, 2004 Unification
CSE S. Tanimoto Unification
Horn Clauses and Unification
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
topics mutable data structures
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
CSE (c) S. Tanimoto, 2002 Unification
Defining Functions with DEFUN
Horn Clauses and Unification
Functional Programming: Lisp
Lisp.
Resolution Preliminaries
Presentation transcript:

Automated Reasoning General Game PlayingLecture 3 Michael Genesereth Spring 2005

2 Unification

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)

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)

5 Non-Uniqueness of Unification Unifier 1: p(x,y){x  a,y  b,v  b}=p(a,b) p(a,v){x  a,y  b,v  b}=p(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  v}=p(a,v) p(a,v){x  a,y  v}=p(a,v)

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){x  a,y  v}=p(a,v) p(a,v){x  a,y  v}=p(a,v) p(x,y){x  a,v  y}=p(a,y) p(a,v){x  a,v  y}=p(a,y)

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

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

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

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

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

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)) ((?y. B) (?x. A) (t. t))

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

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

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: NIL NIL

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

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

18 Unification

19 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){X  a,Y  f(b),V  W}=p(a,a,f(b),Z)

20 Unification A substitution  is a unifier for an expression  and an expression  if and only if  = . p(X,Y){X  a,Y  b,V  b}=p(a,b) p(a,V){X  a,Y  b,V  b}=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)

21 Non-Uniqueness of Unification Unifier 1: p(X,Y){X  a,Y  b,V  b}=p(a,b) p(a,V){X  a,Y  b,V  b}=p(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  V}=p(a,V) p(a,V){X  a,Y  V}=p(a,V)

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){X  a,Y  V}=p(a,V) p(a,V){X  a,Y  V}=p(a,V) p(X,Y){X  a,V  Y}=p(a,Y) p(a,V){X  a,V  Y}=p(a,Y)

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

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

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

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

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

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

29 Example 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}

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

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

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

33 Evaluation

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}

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

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

37 Deduction

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}

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

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

41

42 Backward 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.

43 Example

44 Example Given q(x)  p(x) and p(a), find a term  such that q(  ) is true. 1. q(x)  p(x)Premise 2. p(a)  Premise 3. goal(z)  q(z)Goal 4. goal(z)  p(z) 1, 3 5. goal(a)  2, 4

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)Premise 2. p(a)  Premise 3. p(b)  Premise 4. goal(z)  q(z)Goal 5. goal(z)  p(z) 1, 4 6. goal(a)  2, 5 7. goal(b)  3, 5

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)Premise 2. p(a)  p(b)  Premise 3. goal(z)  q(z)Goal 4. goal(z)  p(z) 1, 3 5. goal(a)  p(b)  2, 4 6. goal(a)  goal(b)  4, 5