1.1.3 Knowledge Base 3

KB3, our third knowledge base, consists of five clauses:

happy(vincent).
listensToMusic(butch).
playsAirGuitar(vincent):-  
   listensToMusic(vincent),  
   happy(vincent).
playsAirGuitar(butch):-  
   happy(butch).
playsAirGuitar(butch):-  
   listensToMusic(butch).

There are two facts, namely happy(vincent) and listensToMusic(butch), and three rules.

KB3 defines the same three predicates as KB2 (namely happy, listensToMusic, and 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

playsAirGuitar(vincent):-  
   listensToMusic(vincent),  
   happy(vincent).

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

?- playsAirGuitar(vincent).

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

playsAirGuitar(vincent):- happy(vincent),listensToMusic(vincent). 

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:

playsAirGuitar(butch):-  
   happy(butch).
playsAirGuitar(butch):-  
   listensToMusic(butch).

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

?- playsAirGuitar(butch).

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

playsAirGuitar(butch):-  
   listensToMusic(butch).

to conclude that playsAirGuitar(butch).

There is another way of expressing disjunction in Prolog. We could replace the pair of rules given above by the single rule

playsAirGuitar(butch):-  
   happy(butch);
   listensToMusic(butch). 

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


Patrick Blackburn, Johan Bos and Kristina Striegnitz
Version 1.2.5 (20030212)