60 likes | 142 Views
Ch. 9 – FOL Inference. Supplemental slides for CSE 327 Prof. Jeff Heflin. EBNF Grammar for Prolog. <program> <clause> { <clause> } <clause> <fact> | <rule> <fact> <compound> . <compound> <atom> ( <termlist> ) <rule> <head> :- <body> . <head> <compound>
E N D
Ch. 9 – FOL Inference Supplemental slides for CSE 327 Prof. Jeff Heflin
EBNF Grammar for Prolog <program> <clause> { <clause> }<clause> <fact> | <rule><fact> <compound> .<compound> <atom> ( <termlist> )<rule> <head> :- <body> .<head> <compound> <body> <compound> { , <compound> } <termlist> <term> { , <term> } <term> <atom> | <num> | <var> | … • Notes: • { <a> } means 0, 1 or more <a>’s • This grammar is slightly more restrictive than the actual language
Backward Chaining functionFOL-BC-Ask (KB, goals, ) returns a set of substitutionslocalanswers, a set of substitutions, initially empty if goals is empty then return { }q’ Subst( ,First(goals))for each sentence rin KB whereStandardize-Apart(r) = (p1… pn q) and ’ Unify(q,q’) succeedsnew_goals Prepend([p1,…,pn],Rest(goals))answers FOL-BC-Ask(KB, new_goals, Compose(’, )) answersreturnanswers Alternative description of Figure 9.6, p. 338
Backward Chaining Example Set of sentences: S1: x1,y1 child(x1,y1) parent(y1,x1) S2: x2,y2 parent(x2,y2) female(x2) mother(x2,y2) S3: child(Lisa,Homer) S4: child(Lisa,Marge) S5: female(Marge) Note: variables have already been standardized apart using subscripts Query: x mother(x,Lisa)
Backward Chaining Search Tree mother(x0,Lisa) match rule S2’={x2/x0, y2/Lisa} parent(x0,Lisa), female(x0) match rule S1’={y1/x0, x1/Lisa} child(Lisa,x0), female(x0) match S4 ’={x0/Marge} match S3 ’={x0/Homer} female(Homer) female(Marge) matches S5 ’={ x0/Marge} no matches answers={} (FAIL!) answers={ x0/Marge}
Forward Chaining functionFOL-FC-Ask (KB,) returns a substitution or falselocalnew, the new sentences inferred on each iteration repeat until new is emptynew {}for each sentence rin KBdo (p1… pn q ) Standardize-Apart (r)for each such thatSubst( , p1… pn)=Subst( , p1’ … pn’) for some p1’ ,…, pn’ in KBq’ Subst( ,q)if q’ does not unify with some sentence already in KB or newthen do add q’ to new Unify(q’, ) if is not failthen return add new to KBreturnanswers From Figure 9.3, p. 332