Rules
So far we have looked at how to represent facts and to query them. Now we move on to rules. Rules allow us to make conditional statements about our world. Each rule can have several variations, called clauses. These clauses give us different choices about how to perform inference about our world. Let's take an example to make things clearer. 
Consider the following 
'All men are mortal': 
We can express this as the following Prolog rule 

mortal(X) :-

        human(X).

The clause can be read in two ways (called either a declarative or a procedural interpretation). The declarative interpretation is "For a given X, X is mortal if X is human." The procedural interpretation is "To prove the main goal that X is mortal, prove the subgoal that X is human." 
Rules 2:>
To continue our previous example, lets us define the fact 'Socrates is human' so that our program now looks as follows: 
mortal(X) :- 
human(X). 
human(socrates). 
If we now pose the question to Prolog 
?- mortal(socrates). 
The Prolog interpreter would respond as follows: 
yes 
Why was this? Well in order to solve the query ?- mortal(socrates)., we used the rule we saw previously. This said that in order to prove someone mortal, we had to prove them to be human. Thus from the goal Prolog generates the subgoal of showing human(socrates). 
In the above example we were able to match human(socrates) against the database described at the top of this card. In Prolog we say that the subgoal succeeded, and as a result the overall goal succeeded. We know when this happens because Prolog prints yes. 
Rules 3
We can also use variables within queries. For example, we might wish to see if there is somebody who is mortal. This is done by the following line. 

?- mortal(P).

The Prolog interpreter responds. 

P = socrates

yes

This means that Prolog was able to prove the goal by binding the variable P to socrates. This was done by again proving someone was mortal by proving the subgoal that they were human. Prolog thus asked if there was any P that was human. This matches against the clause human(socrates) thereby binding P to socrates. This binding is then passed back to the parent goal, and the results in the printout we saw above. 
Rules 4
Sometimes we may wish to specify alternative ways of proving a particular thing. This we can do by using different rules and facts with the same name. For example, we can represent the sentence 'Something is fun if its a red car or a blue bike or it is ice cream' as follows: 

fun(X) :-                      /* an item is fun if */ 

        red(X),                /* the item is red */

        car(X).                /* and it is a car */

fun(X) :-                      /*  or an item is fun if */

        blue(X),               /* the item is blue */

        bike(X).               /* and it is a bike */

fun(ice_cream).                /* and ice cream is also fun. */

This program says that we have three ways of finding out if something is fun. Something is fun if it is a red and a car or blue and a bike, or if it is ice cream. These three options are represent in Prolog by three clauses of the predicate fun. Just like we saw for pure facts, Prolog will start from the first clause (beit a rule or fact) of fun and try that. If that does not succeed, we try the next clause. We only fail when we run out of rules or facts to try. 
Rules 5
All identically-named variables within a particular rule (e.g. all occurrences of, say, X in the first fun rule below) are constrained to have one and the same instantiation for each solution to a particular query. Identical variable names in separate rules are totally independent, just as if different variable names had been used. Because the consequences of the preceding two sentences are so fundamental to Prolog (and so often misunderstood), we recommend that you read them again. To give an example 
The program 
fun(X) :- 
red(X),
car(X). 
fun(X) :- 
blue(X),
bike(X). 
Looks to Prolog Like 
fun(X_1) :- 
red(X_1),
car(X_1). 
fun(X_2) :- 
blue(X_2),
bike(X_2). 
Thus variable name scoping is per-individual rule (often called a clause). The same variable name may appear in different clauses of a rule, or rules with different names. Each time it is treated as something specific to its context. A variable X may occur in the program many times with many different bindings. It will only have the same bindings when you tell it to. 
Examples of Rules 1
Consider the following program: 
fun(X) :- 
red(X),
car(X). 
fun(X) :- 
blue(X),
bike(X). 
car(vw_beatle).
car(ford_escort).
bike(harley_davidson).
red(vw_beatle).
red(ford_escort).
blue(harley_davidson). 
Above is both our previous program for finding fun items and facts describing some red and blue items and some cars and bikes. Let's now use the above program and see if a harley_davidson is fun. To do this we can ask Prolog the following question. 

?- fun(harley_davidson).                  /* to which Prolog will reply */

yes                                     /* to show the program succeeded */

To execute this query, Prolog will first see if harley_davidson is red, however only vw_beatles and ford_escorts are defined as being red. Hence the query red(harley_davidson) will fail. This means that the first clause of fun will fail. As a result, we now try the second clause of fun. This will mean that we will attempt the subgoals blue(harley_davidson) and bike(harley_davidson). Both these goals match facts in the database. As a result fun succeeds as we saw above. 
Examples of Rules 2
fun(X) :- 
red(X),
car(X). 
fun(X) :- 
blue(X),
bike(X). 
car(vw_beatle).
car(ford_escort).
bike(harley_davidson).
red(vw_beatle).
red(ford_escort).
blue(harley_davidson). 
We can also ask our program to find fun items for us. To do this we can pose the following question. 

?- fun(What).

To which Prolog will reply 

What=vw_beatle

yes

Let's see how Prolog deals with this query. Firstly we will try the first clause of fun. This results in us trying the goal red(What). This succeeds matching the first clause of red with the binding What=vw_beatle. Now we attempt the goal car(vw_beatle). This matches the first clause of car, and, as a result, the fun goal succeeds. 
Rules Exercises

Exercise 4
Indicate whether the following are syntactically correct Rules. 
a :- b, c, d:- e f.  
happy(X):- a , b.  
happy(X):- hasmoney(X) & has_friends(X).  
fun(fish):- blue(betty), bike(yamaha).  

Exercise 5
Given the database below, study the queries underneath it. Indicate whether you think a particular query will succeed or fail by answer yes or no using the buttons. 
likes(john,mary).
likes(john,trains).
likes(peter,fast_cars). 
likes(Person1,Person2):- 
hobby(Person1,Hobby), 
hobby(Person2,Hobby). 
hobby(john,trainspotting). 
hobby(tim,sailing). 
hobby(helen,trainspotting). 
hobby(simon,sailing). 
?- likes(john,trains).  
?- likes(helen,john).  
?- likes(tim,helen).  
?- likes(john,helen).  

This is the end of the rules topic. 

Next Topic 
Return to Introduction Menu 
Rules
So far we have looked at how to represent facts and to query them. Now we move on to rules. Rules allow us to make conditional statements about our world. Each rule can have several variations, called clauses. These clauses give us different choices about how to perform inference about our world. Let's take an example to make things clearer. 
Consider the following 
'All men are mortal': 
We can express this as the following Prolog rule 

mortal(X) :-

        human(X).

The clause can be read in two ways (called either a declarative or a procedural interpretation). The declarative interpretation is "For a given X, X is mortal if X is human." The procedural interpretation is "To prove the main goal that X is mortal, prove the subgoal that X is human." 
Rules 2:>
To continue our previous example, lets us define the fact 'Socrates is human' so that our program now looks as follows: 
mortal(X) :- 
human(X). 
human(socrates). 
If we now pose the question to Prolog 
?- mortal(socrates). 
The Prolog interpreter would respond as follows: 
yes 
Why was this? Well in order to solve the query ?- mortal(socrates)., we used the rule we saw previously. This said that in order to prove someone mortal, we had to prove them to be human. Thus from the goal Prolog generates the subgoal of showing human(socrates). 
In the above example we were able to match human(socrates) against the database described at the top of this card. In Prolog we say that the subgoal succeeded, and as a result the overall goal succeeded. We know when this happens because Prolog prints yes. 
Rules 3
We can also use variables within queries. For example, we might wish to see if there is somebody who is mortal. This is done by the following line. 

?- mortal(P).

The Prolog interpreter responds. 

P = socrates

yes

This means that Prolog was able to prove the goal by binding the variable P to socrates. This was done by again proving someone was mortal by proving the subgoal that they were human. Prolog thus asked if there was any P that was human. This matches against the clause human(socrates) thereby binding P to socrates. This binding is then passed back to the parent goal, and the results in the printout we saw above. 
Rules 4
Sometimes we may wish to specify alternative ways of proving a particular thing. This we can do by using different rules and facts with the same name. For example, we can represent the sentence 'Something is fun if its a red car or a blue bike or it is ice cream' as follows: 

fun(X) :-                      /* an item is fun if */ 

        red(X),                /* the item is red */

        car(X).                /* and it is a car */

fun(X) :-                      /*  or an item is fun if */

        blue(X),               /* the item is blue */

        bike(X).               /* and it is a bike */

fun(ice_cream).                /* and ice cream is also fun. */

This program says that we have three ways of finding out if something is fun. Something is fun if it is a red and a car or blue and a bike, or if it is ice cream. These three options are represent in Prolog by three clauses of the predicate fun. Just like we saw for pure facts, Prolog will start from the first clause (beit a rule or fact) of fun and try that. If that does not succeed, we try the next clause. We only fail when we run out of rules or facts to try. 
Rules 5
All identically-named variables within a particular rule (e.g. all occurrences of, say, X in the first fun rule below) are constrained to have one and the same instantiation for each solution to a particular query. Identical variable names in separate rules are totally independent, just as if different variable names had been used. Because the consequences of the preceding two sentences are so fundamental to Prolog (and so often misunderstood), we recommend that you read them again. To give an example 
The program 
fun(X) :- 
red(X),
car(X). 
fun(X) :- 
blue(X),
bike(X). 
Looks to Prolog Like 
fun(X_1) :- 
red(X_1),
car(X_1). 
fun(X_2) :- 
blue(X_2),
bike(X_2). 
Thus variable name scoping is per-individual rule (often called a clause). The same variable name may appear in different clauses of a rule, or rules with different names. Each time it is treated as something specific to its context. A variable X may occur in the program many times with many different bindings. It will only have the same bindings when you tell it to. 
Examples of Rules 1
Consider the following program: 
fun(X) :- 
red(X),
car(X). 
fun(X) :- 
blue(X),
bike(X). 
car(vw_beatle).
car(ford_escort).
bike(harley_davidson).
red(vw_beatle).
red(ford_escort).
blue(harley_davidson). 
Above is both our previous program for finding fun items and facts describing some red and blue items and some cars and bikes. Let's now use the above program and see if a harley_davidson is fun. To do this we can ask Prolog the following question. 

?- fun(harley_davidson).                  /* to which Prolog will reply */

yes                                     /* to show the program succeeded */

To execute this query, Prolog will first see if harley_davidson is red, however only vw_beatles and ford_escorts are defined as being red. Hence the query red(harley_davidson) will fail. This means that the first clause of fun will fail. As a result, we now try the second clause of fun. This will mean that we will attempt the subgoals blue(harley_davidson) and bike(harley_davidson). Both these goals match facts in the database. As a result fun succeeds as we saw above. 
Examples of Rules 2
fun(X) :- 
red(X),
car(X). 
fun(X) :- 
blue(X),
bike(X). 
car(vw_beatle).
car(ford_escort).
bike(harley_davidson).
red(vw_beatle).
red(ford_escort).
blue(harley_davidson). 
We can also ask our program to find fun items for us. To do this we can pose the following question. 

?- fun(What).

To which Prolog will reply 

What=vw_beatle

yes

Let's see how Prolog deals with this query. Firstly we will try the first clause of fun. This results in us trying the goal red(What). This succeeds matching the first clause of red with the binding What=vw_beatle. Now we attempt the goal car(vw_beatle). This matches the first clause of car, and, as a result, the fun goal succeeds. 
Rules Exercises

Exercise 4
Indicate whether the following are syntactically correct Rules. 
a :- b, c, d:- e f.  
happy(X):- a , b.  
happy(X):- hasmoney(X) & has_friends(X).  
fun(fish):- blue(betty), bike(yamaha).  

Exercise 5
Given the database below, study the queries underneath it. Indicate whether you think a particular query will succeed or fail by answer yes or no using the buttons. 
likes(john,mary).
likes(john,trains).
likes(peter,fast_cars). 
likes(Person1,Person2):- 
hobby(Person1,Hobby), 
hobby(Person2,Hobby). 
hobby(john,trainspotting). 
hobby(tim,sailing). 
hobby(helen,trainspotting). 
hobby(simon,sailing). 
?- likes(john,trains).  
?- likes(helen,john).  
?- likes(tim,helen).  
?- likes(john,helen).  

This is the end of the rules topic. 

Next Topic 
Return to Introduction Menu