ذكاء-معمل3

advertisement
‫الذكاء االصطناعي‬
Artificial Intelligence ( AI)
comp323
Lab(3)
Matching and Proof Search
• When working with knowledge base KB4 in
the previous chapter, we introduced the term
matching.
• We said, e.g. that Prolog matches woman(X)
with woman(mia), thereby instantiating the
variable X to mia.
• Two terms match, if they are equal or if they
contain variables that can be instantiated in
such a way that the resulting terms are equal.
• If term1 and term2 are constants, then term1
and term2 match if and only if they are the
same atom, or the same number.
• If term1 is a variable and term2 is any type of
term, then term1 and term2 match, and
term1 is instantiated to term2.
• Similarly, if term2 is a variable and term1 is
any type of term, then term1 and term2
match, and term2 is instantiated to term1
• If term1 and term2 are complex terms, then
they match if and only if:
• (a) They have the same functor and arity.
• (b) All their corresponding arguments match
• (c) and the variable instantiations are
compatible
• Two terms match if and only if it follows from
the previous three clauses that they match.
•
•
•
•
•
Examples
=(mia,mia).
=(mia,vincent).
2 = 2.
’mia’ = mia.
let’s look at an example involving
complex terms:
• kill(shoot(gun),Y) = kill(X,stab(knife)).
• kill(shoot(gun), stab(knife)) = kill(X,stab(Y)).
• loves(X,X) = loves(marcellus,mia).
Programming with matching
• The following two line knowledge base
defines two predicates, namely vertical/2 and
horizontal/2, which specify what it means for
a line to be vertical or horizontal respectively.
• vertical(line(point(X,Y),point(X,Z))).
• horizontal(line(point(X,Y),point(Z,Y))).
What can we do with this knowledge
base? Let’s look at some examples:
• vertical(line(point(1,1),point(1,3))).
• vertical(line(point(1,1),point(3,2))).
• horizontal(line(point(1,1),point(2,Y))).
• horizontal(line(point(2,3),P)).
Proof Search
• Suppose we are working with the following
knowledge base
f(a).
f(b).
g(a).
g(b).
h(b).
k(X) :- f(X),g(X),h(X).
Suppose we then pose the query
• k(X).
• we have described: that is, by searching top to
bottom in the knowledge base,
• left to right in goal lists, and
• backtracking to the previous choice point
whenever it fails.
Practical Session 2
?- trace.
• Prolog is now in trace mode, and will evaluate
all queries step by step.
Download