0% found this document useful (0 votes)
6 views7 pages

Prolog Example

The document explains the concepts of facts, rules, and queries in Prolog programming. It describes how facts are established in a knowledge base, how rules can deduce new information, and how queries can be used to retrieve information from the knowledge base. Additionally, it covers the use of conjunctions and disjunctions in both rules and queries, illustrating their functionality with various examples.

Uploaded by

abduljawadmunir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Prolog Example

The document explains the concepts of facts, rules, and queries in Prolog programming. It describes how facts are established in a knowledge base, how rules can deduce new information, and how queries can be used to retrieve information from the knowledge base. Additionally, it covers the use of conjunctions and disjunctions in both rules and queries, illustrating their functionality with various examples.

Uploaded by

abduljawadmunir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Facts

Facts are pieces of information available within a Prolog program. A fact is accepted as it is and does
not need to be proven. A fact consists of 2 parts,
first is the predicate name, and the arguments of the predicate. A predicate name is used to signify a
property/relationship that is shared between
the arguments. The arguments are the actual pieces of information that are bound by the
relationship.

In the figure above, the first fact consists of the predicate person/1. The notation "predicate_name/
N" means that a predicate with the name predicate_name has
N arguments. The 2nd fact consists of the predicate gender/2, with the arguments jack and male. The
gender predicate defines a relationship between
arguments, and can be read as "The gender of jack is male". A predicate can have as many
arguments as needed. When presenting the Prolog program with
facts, these facts are added automatically to the knowledge base. The user can then "query" the
available information within the knowledge base. Take for example the following query:

?- gender(X, Y).
X = jack,
Y = male.

Here, we presented a query which consists of the predicate name, and two arguments X and Y. The
X and Y here are called variables. In Prolog, all
names that begin with capital letters are variables. Since Prolog does have the fact "gender(jack,
male)" in the knowledge base, it
matches/unifies the variable X with 'jack' and the variable Y with 'male'. If we ask for information that
is not in the knowledge base, we will get
a "false". For example, the query below will return a "false", since there is no information in the
knowledge base regarding the gender of 'jill'.

?- gender(jill, Y).
false.
About variables and constants
In Prolog, a variable always starts either with a Capital letter or an Underscore. If we want to
represent a constant that starts with a capital letter,
it should be surrounded by the single quote sign, for example : ' My String ', 'Jack', 'maybe', '_2932' .
Other examples of constants are
numbers, lists and complex data structures.

Rules

A Prolog rule is a statement that consists of two parts. The first part is called the head of the rule,
and the second part is called the body of the rule.

The head of the rule is considered true, if and only if the body can also be proven to be true.

The rule above defines the rule "food(F):- fruit(F).", where "food(F)" is the head, and "fruit(F)" is the
body. As we said before, the head is considered

true, if the body is proven to be true, hence, the meaning of the rule is "If F is a fruit, then F is food".

Prolog rules are usually used to deduce new information, from already existing ones. Lets investigate
the following example:

fruit('apple').
fruit('orange').
vegetable('tomato').
vegetable('lettuce').

food(F):- fruit(F).

The code above contains 4 facts. These facts are added to the knowledge base, therefore our
knowledge base knows that apples and oranges are fruits, and tomatoes and lettuce are vegetables.
The rule "food(F):- fruit(F)." allows the program to deduce the fact that fruits are food, and hence
these new

facts are added to the knowledge base.


?- food(apple).
true.

?- food(tomato).
false.
The first query returns true, due to the existence of the rule we discussed earlier. The second query
returns false, since there are no facts or rules in the knowledge base that could be used to deduce
"food(tomato)". Hence, we modify the program as follows.
fruit('apple').
fruit('orange').
vegetable('tomato').
vegetable('lettuce').

food(F):- fruit(F).
food(F):- vegetable(F).

Here, we add a second rule that says if F is a vegetable, then F is food. Note that using the variable
name 'F' doesn't affect the previous rule in anyway. Now, lets run the query again. After adding
the second rule with the same head "food(F)", we effectively changed the rule's meaning to "If F
is a fruit OR if F is a vegetable, then F is food". Note that the order of evaluation of the rules is
from top to bottom, hence, when using the query above, the program will first check if 'tomato' is
a fruit. Since it will fail, it will check the second rule, which will succeed
?- food(tomato).
true.
Queries
In Prolog, a query is a question presented to the knowledge base. A knowledge base is a collection
of facts that could be deduced from one or multiple Prolog programs. If the facts in the query
match/unify with the information in the knowledge base, then the query is considered true. When
a query is true, we are sometimes able to obtain certain information that cause the query presented
to be true. In the previous tutorial, we used the following program:
fruit('apple').
fruit('orange').
vegetable('tomato').
vegetable('lettuce').

food(F):- fruit(F).
food(V):- vegetable(V).
We then wrote a simple query to find out weather 'tomato' is food or not.
?- food(tomato).
true.
What if we would like to know a single kind of food. Instead of presenting the query with a string,
we present it with a variable.
?- food(X).
X = apple .

Here, the program tries to find out if there exists X, such that food(X) is true, hence the Prolog
replies by telling us that the query is true if X is equal to apple. You might notice that the program
did not terminate after we received the reply "X = apple". This is because there exists more than
one solution to our query. To find out subsequent solutions, we use/press the ' ; ' operator, also
called an "OR" in Prolog.
?- food(X).
X = apple ;
X = orange ;
X = tomato ;
X = lettuce.
Notice here how we get a different solution every time we press ' ; '. This is because Prolog
backtracks to find different solutions. We will discuss backtracking in a later tutorial. In the query
above, we pressed ' ; ' until there were no more solutions available. If you do not want to find any
more alternatives, simply press "Enter" or a dot ' . ' .
Let us make things more complicated. We would like to find out all food items that are also
vegetables. To do so, we use the comma ' , ' sign, also called
an "AND" in Prolog.

As in the diagram above, the query consists of 2 parts. The first part is "food(F)", and the second
part is "vegetable(F)", with the comma in between. Prolog will first try to unify/match the variable
F with a food item. If that food item F is also a vegetable as per the predicate "vegetable(F)", then
the query succeeds. If F is not a vegetable, the program backtracks, i.e., it goes back to "food(F)",
trying to find another item to match with F, and then checking if F is a vegetable or not. Long
story short, Prolog tries to match queries left to right. Running the following query produces the
following result:
?- food(F), vegetable(F).
F = tomato ;
F = lettuce.

Conjunctions & Disjunctions in Prolog


In logic programming, a logical expression is usually build from smaller, less complex logical terms.
The way these terms relate to each other within an expression is defined by Conjunctions and
Disjunctions. Conjunctions and Disjunctions in Prolog and other logic programming languages
are equivalent to ANDs and ORs (respectively) in imperative and object oriented programming
languages such as C#, C++, Java and Python.
In the following two tutorials we will discuss both Conjunctions and Disjunctions using examples,
and examine the behaviour of both logical operators within rules and queries.
Conjunctions in Prolog
In this tutorial, we will discuss Conjunctions in logic programming, and how they are used in
Prolog. To start with, lets present the definition of a Conjunction. A Conjunction is a logical
operator that connects two logical terms. A conjunction between the two terms will result in the
whole expression to evaluate to true if both terms evaluate to true. If either or both terms in the
expression evaluate to false, the whole expression evaluates to false. (The word "conjunction" is
used mainly in the context of logic and logic programming, it is equivalent to an "AND" in Java,
C++, etc..)
Conjunction - Example 1 (conjunction within a rule body) In Prolog, we are allowed to write rule
bodies that contain conjunctions. Remember when we said that a rule head is true if the body is
true; if we use a conjunction in the body, the head of the rule is considered true, if and only if all
terms in the body are also true. Lets consider the following
example:
pet(maggie).
pet(bounty).
pet(ginger).

fast(maggie).
fast(ginger).

lazy(bounty).

big(maggie).
big(ginger).

friendly(ginger).

cat(X):- pet(X), lazy(X).


dog(X):- pet(X), fast(X), big(X), friendly(X).

The example above contains 2 rules. The first describes a cat, and the second describes a dog.
(Regardless of how true the following statements are) The first rule says that X is a cat, if X is a
pet and X is lazy. A conjunction in Prolog is represented by the comma symbol ' , '. The second
rule says that X is a dog, if X is a pet and X is fast and X is big and X is friendly, which shows that
we can use conjunctions with any number of terms. Note that if any of the four conditions fail,
the whole condition fails, and hence dog(X) fails.
?- cat(X).
X = bounty ;
false.

?- dog(ginger).
true.

?- dog(maggie).
false.

The first query, "cat(X)" lets prolog attempt to unify X with something that satisfies both "pet(X)"
and "lazy(X)". Since "bounty" is the only constant that satisfies both conditions, Prolog replies
with the answer "X = bounty". The second query asks the knowledge base if "ginger" is a dog.
Since "ginger" satisfies all 4 conditions of dog(X), Prolog replies with true. The last query asks if
"maggie" is a dog. Even though maggie satisfies the conditions "pet(X)", "fast(X)" and "big(X)",
it doesn't satisfy "friendly(X)", since there is no information in the knowledge base to indicate that,
hence the whole expression fails, and Prolog replies with false.
One final note about conjunctions within rules, conjunctions can only occur at the body of the
rule. No conjunctions are allowed at the head of the rule. For example, we cannot have a rule as
follows:
smart(X),dog(X) :- pet(X), ............ % WRONG
Conjunction - Example 2 (conjunction within a query)
Conjunctions are not just used within rule bodies, they are also used within queries. For example,
we can ask Prolog to find both a Cat and a Dog from the knowledge base at the same time.
?- dog(X),cat(Y).
X = ginger,
Y = bounty .

In the above query, we asked Prolog to unify X with a variable such that dog(X) is true and to
unify Y with a constant such that cat(Y) is true. The knowledge base was successful, hence, it
returned "X = ginger, Y = bounty". We can also ask for certain facts in conjunction, for example:
?- big(maggie),big(ginger).
true.

?- big(maggie),lazy(ginger).
false.

?- fast(maggie), pet(bounty), cat(X).


X = bounty .

For the first query, we asked the knowledge base if "maggie is big and ginger is big". Since both
conditions are true, we received true.For the second query, we asked if "maggie is big and ginger
is lazy". Since there is no information in the knowledge base that says ginger is lazy,
Prolog replies with false. The last query asks if "maggie is fast and bounty is a bet and if there exists
a constant in the knowledge base that could unify with X such that cat(X) is true". Since all three
conditions are true, Prolog replies with "X = bounty.". If one of the conditions were false, we X
would not be assigned any value, and we would get a false right away.
Disjunctions in Prolog
In this tutorial, we will discuss Disjunctions and how they are used in Prolog through a couple of
examples. A disjunction in logic programming is equivalent to an OR, where an expression is
considered true if, one or more terms connected with the disjunction is true. A disjunction in
Prolog is represented by the semicolon sign ' ; '.
Disjunction - Example 1 (Disjunction within a rule body).
We will begin by writing a Prolog program that describes the quality "lazy". We say that something
is lazy if it is a cat or if it is sleepy. We can describe "lazy" by the following program.

cat(bounty).
person(jack).
dog(ginger).

sleepy(jack).
sleepy(bounty).

lazy(X):- cat(X) ; sleepy(X).


Here, the rule says that "lazy(X)" is true if "cat(X) is true or sleepy(X) is true", i.e., only one of the
two conditions should be true for the head to be true. (Note that, similar to conjunctions,
disjunctions in Prolog can also be between two or more terms.). Lets observe the following query.
?- lazy(bounty).
true .

?- lazy(jack).
true.
?- lazy(ginger).
false.

Here, the first query "lazy(bounty)" is true, since the constant "bounty" satisfies both "cat(X)" and
"sleepy(X)" , where X is unified with "bounty". The second query is also true, since "jack" satisfies
"sleepy(X)" when X is unified with "jack". Note that even though "jack" doesn't satisfy "cat(X)",
the rule is still true since we used a disjunction, and only one of the conditions in the body has to
be true. The third query "lazy(ginger)" returns false since both conditions "cat(X)" and "sleepy(X)"
are false (and a disjunction needs at least one condition to be true).
Disjunction - Example 2 (Disjunctions within a query)
Just like conjunctions, in Prolog, a query is also allowed to contain disjunctions. In this case, only
one of the statements in the query that are connected with disjunctions need to be true, in order
to receive a true response. Take the following query for example:
?- cat(bounty) ; dog(bounty).
true .

?- cat(bounty) ; dog(bounty).
true ;
false.

The queries above are the same query but with different responses. The first query returns true
since "cat(bounty)" is true. In the first query, we pressed "enter", hence, the query terminated. In
the second case, we pressed " ; " to get other alternative responses. The query first responds with
"true" since the first condition is true, when we press " ; ", it tries to satisfy the second condition
"dog(bounty)" and fails, hence, we receive the false response.
?- dog(X) ; cat(Y) ; sleepy(Z).
X = ginger ;
Y = bounty ;
Z = jack ;
Z = bounty.

In the query above, we used disjunctions between the three terms. Since we used disjunctions,
Prolog has to satisfy only one of the three terms, hence when finding a solutions, we get a solutions
for X individually, then after running out of solutions for X, Prolog tries to unify Y, and later Z.
If we had used a conjunction, Prolog would have tried to unify all 3 together, since all three are
required to satisfy the query.
?- dog(X) , cat(Y) , sleepy(Z).
X = ginger,
Y = bounty,
Z = jack ;
X = ginger,
Y = Z, Z = bounty

More examples and references


https://cse.sc.edu/~ahein/330/example.html

http://www.cs.toronto.edu/~sheila/384/w10/simple-prolog-examples.html

You might also like