As we have mentioned earlier, Prolog programs consists of two building blocks which are rules and facts.
Rules are general implications, where facts are just facts. It should be intuitive so examples help here, I am going to give some.
Rule #1: People who have more than one million dollars in their bank account are rich.
Fact #1: Joe is rich.
Fact #2: Joe has more than one million dollars in his bank account.
Fact #3: John is not rich.
Here we have a rule and three facts. We could combine Fact and Rules to produce some new Facts. For example if we use Rule #1 and Fact #3, we learn that John has less than one million dollars in his bank account.
Prolog is about defining Rules, feeding Facts and running queries over them.
Let’s leave the Rules for now and continue with facts, you can state a Fact in prolog like this:
If you downloaded the IDE you can write these facts to the upper part and press F9 to consult. After you consult go to the lower part which is terminal and run queries like this:
the output will be true. You can also query
and the result will be false. Because Prolog will search the facts if test has been assigned as a website and when it couldn’t find it will output false.
Notice we have been writing constants starting with lower case letters, this is because terms starting with upper case letters have special meaning in Prolog. They are variables. You can ask Prolog for all the websites it knows like this:
The output will be
101 ?- isWebsite(X).
X = google ;
X = facebook ;
X = blogger.
When X has more than one result Prolog will give the results one by one, so you need to press “;” to get the next result.
Also notice that the same sentence we write to the upper screen (which is source file) and to lower screen (which is terminal) has different meanings. If you write “isWebsite(google).” to source file, you will give the information that google is a website to Prolog, if you write “isWebsite(google).” to terminal, you will be asking Prolog “Is google a website?”.
So that we are familiar with facts, we can start defining some rules.
Rules without variables are a little bit complicated if you didn’t take a logic course. But you will understand when we introduce variables because they make much more sense that way. I will first show Rules without variables.
Assume we want to state that if b and c are true, a must be true. We show this as
a -> (b \and \!\, c).
in Prolog we would define it like this.
and after this rule we need to give two facts about b and c which you already know can be given as:
which implies b and c are true. Then consult this file with F9 and query “a.” , Prolog will tell you true. Because we know the fact b and c, and a :- b,c. implies that when b and c are true, a is true. a is a rule without parameters.
Before moving on with the parameters let me explain the syntax.
function :- statement1, statement2, … , statementn.
or can equally be written as
when we call the function function, statements will be checked one by one in order, if any of them are wrong function does not continue and returns false.
Rules with parameter(s)
Let’s define our first Rule with variables, notice the parameter starts with upper case letter.
isFood(X) :- isEdible(X).
This is a redundant Rule since we could’ve just used isEdible instead of isFood. But as we learn this it will be easier to write Rules with multiple statements. They will be very useful, this is just an example. Now if we had a database of Facts which contains Edible objects like this:
with this rule we can now ask Prolog about foods with the query isFood(X). The output will be:
108 ?- isFood(X).
X = apple ;
X = tomato ;
X = potato.
Rules with multiple statements
Continuing with the food example, let’s define our rule as following:
This tells Prolog if anything is Fruit and Red, that’s an apple. Now assume we find some data(facts) about two objects which are called object1 and object2 and these facts are defined as following:
Combine the rule and these facts in the source file and consult with the key F9.
Now when you write isApple(object1). to terminal you will get this:
3 ?- isApple(object1).
We could easily see this without defining a rule by just looking at the facts but imagine you have gigabytes of data about people and you create some rules which will distinguish between people, this is called data-mining and Prolog comes in very handy in data-mining. Another thing we could do with this source file is to ask Prolog about the Apples by writing isApple(X). and the output will be:
6 ?- isApple(X).
X = object1.
Notice that the X we used here and X we use when defining the rule are just for convention, you can use A or Test or Var, anything starting with an upper-case letter.
You can also ask if object2 is an apple by the query isApple(object2). which will output:
7 ?- isApple(object2).
Since object2 satisfies only one condition of two which are required to be an Apple, the result is obviously false.
Rules with “return” values
I wrote the return in quotes because Prolog handles return values a little bit different, but I will explain analogically with C++. Every Prolog Rule is a bool function in C++. What you can do to return variables is to pass by reference, so does the Prolog pass the variables we give by references and inside a Rule you can change that variable.
Remember our isApple(X). query. What we do here is to send the variable X by reference to the Rule isApple, then the isApple Rule changes the value of X.
It will be more clear with this addition example.
Sum is A + B.
This Rule takes three variables and sets the value of third variable to the sum of first two variables. Let’s try this out, write the Rule add to the source file and consult.
Now try to run the query add(10,5,X). Output will be:
12 ?- add(10,5,X).
X = 15.
You can also try to run add(10,5,15). or add(10,5,10). First one will give you true while the second one gives to false. This means besides computation you can check Rules to see if the values satisfy a condition.
Returning more than one value is also possible in a situation like this:
X is A*2,
Y is A*3,
Z is A*4.
as you might expect by now the query and the result will be:
31 ?- multiply(5,X,Y,Z).
X = 10,
Y = 15,
Z = 20.