0% found this document useful (0 votes)
2 views4 pages

Class-2

Prolog is a logic programming language that uses facts and rules to express relationships and perform computations through queries. Facts are unconditional statements about objects, while rules define conditional relationships. A knowledge base in Prolog is a collection of facts and rules that can be queried to derive information about the relationships between objects.

Uploaded by

sajoldebnath07
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)
2 views4 pages

Class-2

Prolog is a logic programming language that uses facts and rules to express relationships and perform computations through queries. Facts are unconditional statements about objects, while rules define conditional relationships. A knowledge base in Prolog is a collection of facts and rules that can be queried to derive information about the relationships between objects.

Uploaded by

sajoldebnath07
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

PROLOG

Prolog is a logic programming language. In prolog, logic is expressed as relations (called as Facts and Rules).
Formulation or Computation is carried out by running a query over these relations.
Facts
We can define fact as an explicit relationship between objects, and properties these objects might have. So facts
are unconditionally true in nature. Suppose we have some facts as given below −
Tom is a cat
Kunal loves to eat Pasta
Hair is black
Nawaz loves to play games
Pratyusha is lazy.

So these are some facts, that are unconditionally true. These are actually statements, that we have to consider as
true.
Following are some guidelines to write facts −
• Names of properties/relationships begin with lower case letters.
• The relationship name appears as the first term.
• Objects appear as comma-separated arguments within parentheses.
• A period "." must end a fact.
• Objects also begin with lower case letters. They also can begin with digits (like 1234), and can be
strings of characters enclosed in quotes e.g. color(penink, ‘red’).
• phoneno(agnibha, 1122334455). is also called a predicate or clause.

Syntax
The syntax for facts is as follows −
relation(object1,object2...).
Example
Following is an example of the above concept −
• cat(tom).
• loves_to_eat(kunal,pasta).
• of_color(hair,black).
• loves_to_play_games(nawaz).
• lazy(pratyusha).
Rules
We can define rule as an implicit relationship between objects. So facts are conditionally true. So when one
associated condition is true, then the predicate is also true. Suppose we have some rules as given below −
• Lili is happy if she dances.
• Tom is hungry if he is searching for food.
• Jack and Bili are friends if both of them love to play cricket.
• will go to play if school is closed, and he is free.

So these are some rules that are conditionally true, so when the right hand side is true, then the left hand side is
also true.
Here the symbol ( :- ) will be pronounced as “If”, or “is implied by”. This is also known as neck symbol, the
LHS of this symbol is called the Head, and right hand side is called Body. Here we can use comma (,) which is
known as conjunction, and we can also use semicolon, that is known as disjunction.
Syntax
rule_name(object1, object2, ...) :- fact/rule(object1,object2, ...)
Suppose a clause is like :P :- Q;R.
This can also be written as
P :- Q.
P :- R.
If one clause is like :
P :- Q,R;S,T,U.
Is understood as
P :- (Q,R);(S,T,U).
Or can also be written as:
P :- Q,R.
P :- S,T,U.

Example
• happy(lili) :- dances(lili).
• hungry(tom) :- search_for_food(tom).
• friends(jack, bili) :- lovesCricket(jack), lovesCricket(bili).
• goToPlay(ryan) :- isClosed(school), free(ryan).

Queries
Queries are some questions on the relationships between objects and object properties. So question can be
anything, as given below −
• Is tom a cat?
• Does Kunal love to eat pasta?
• Is Lili happy?
• Will Ryan go to play?
So according to these queries, Logic programming language can find the answer and return them.
Knowledge Base in Logic Programming
We know there are three main components in logic programming − Facts, Rules and Queries. Among these
three if we collect the facts and rules as a whole then that forms a Knowledge Base. So we can say that the
knowledge base is a collection of facts and rules.
Now, we will see how to write some knowledge bases. Suppose we have our very first knowledge base called
KB1. Here in the KB1, we have some facts. The facts are used to state things, that are unconditionally true of
the domain of interest.
Knowledge Base 1
Suppose we have some knowledge, that Priya, Tiyasha, and Jaya are three girls, among them, Priya can cook.
Let’s try to write these facts in a more generic way as shown below –
girl(priya).
girl(tiyasha).
girl(jaya).
can_cook(priya).
Note − Here we have written the name in lowercase letters, because in Prolog, a string starting with uppercase
letter indicates a variable.
Now we can use this knowledge base by posing some queries. “Is priya a girl?”, it will reply “yes”, “is jamini a
girl?” then it will answer “No”, because it does not know who jamini is. Our next question is “Can Priya cook?”,
it will say “yes”, but if we ask the same question for Jaya, it will say “No”.
Output

| ?- girl(priya)
.

yes
| ?- girl(jamini).

no
| ?- can_cook(priya).

yes
| ?- can_cook(jaya).

no
| ?-

Relations in Prolog

In Prolog programs, it specifies relationship between objects and properties of the objects.
Suppose, there’s a statement, “Amit has a bike”, then we are actually declaring the ownership relationship
between two objects — one is Amit and the other is bike.
If we ask a question, “Does Amit own a bike?”, we are actually trying to find out about one relationship.
There are various kinds of relationships, of which some can be rules as well. A rule can find out about a
relationship even if the relationship is not defined explicitly as a fact.
We can define a brother relationship as follows −
Two person are brothers, if,
• They both are male.
• They have the same parent.

Now consider we have the below phrases −


• parent(sudip, piyus).
• parent(sudip, raj).
• male(piyus).
• male(raj).
• brother(X,Y) :- parent(Z,X), parent(Z,Y),male(X), male(Y)
These clauses can give us the answer that piyus and raj are brothers, but we will get three pairs of output here.
They are: (piyus, piyus), (piyus, raj), (raj, raj). For these pairs, given conditions are true, but for the pairs (piyus,
piyus), (raj, raj), they are not actually brothers, they are the same persons. So we have to create the clauses
properly to form a relationship.
The revised relationship can be as follows −
A and B are brothers if −
• A and B, both are male
• They have same father
• They have same mother
• A and B are not same

In Prolog syntax, we can write −


mother(X,Y) :- parent(X,Y), female(X).
sister(X,Y) :- parent(Z,X), parent(Z,Y), female(X), X \== Y.

You might also like