|<< Prev||- Up -||Next >>|
KB3, our third knowledge base, consists of five clauses:
There are two facts, namely
listensToMusic(butch), and three rules.
KB3 defines the same three predicates as KB2 (namely
playsAirGuitar) but it defines them differently. In particular, the three rules that define the
playsAirGuitar predicate introduce some new ideas. First, note that the rule
has two items in its body, or (to use the standard terminology) two goals. What does this rule mean? The important thing to note is the comma
, that separates the goal
listensToMusic(vincent) and the goal
happy(vincent) in the rule's body. This is the way logical conjunction is expressed in Prolog (that is, the comma means and). So this rule says: ``Vincent plays air guitar if he listens to music and he is happy''.
Thus, if we posed the query
Prolog would answer ``no''. This is because while KB3 contains
happy(vincent), it does not explicitly contain the information
listensToMusic(vincent), and this fact cannot be deduced either. So KB3 only fulfils one of the two preconditions needed to establish
playsAirGuitar(vincent), and our query fails.
Incidentally, the spacing used in this rule is irrelevant. For example, we could have written it as
and it would have meant exactly the same thing. Prolog offers us a lot of freedom in the way we set out knowledge bases, and we can take advantage of this to keep our code readable.
Next, note that KB3 contains two rules with exactly the same head, namely:
This is a way of stating that Butch plays air guitar if either he listens to music, or if he is happy. That is, listing multiple rules with the same head is a way of expressing logical disjunction (that is, it is a way of saying or). So if we posed the query
Prolog would answer ``yes''. For although the first of these rules will not help (KB3 does not allow Prolog to conclude that
happy(butch)), KB3 does contain
listensToMusic(butch) and this means Prolog can apply modus ponens using the rule
to conclude that
There is another way of expressing disjunction in Prolog. We could replace the pair of rules given above by the single rule
That is, the semicolon
; is the Prolog symbol for or, so this single rule means exactly the same thing as the previous pair of rules. But Prolog programmers usually write multiple rules, as extensive use of semicolon can make Prolog code hard to read.
It should now be clear that Prolog has something do with logic: after all, the
:- means implication, the
, means conjunction, and the
; means disjunction. (What about negation? That is a whole other story. We'll be discussing it later in the course.) Moreover, we have seen that a standard logical proof rule (modus ponens) plays an important role in Prolog programming. And in fact ``Prolog'' is short for ``Programming in logic''.
|<< Prev||- Up -||Next >>|