First Prolog program
First Prolog program
What is Prolog?
Prolog is a logic programming language that is used in artificial intelligence. It
is a declarative programming language expressing logic as relations, called facts
and rules. A Prolog program consists of a collection of facts and rules; a query
is a theorem to be proved. Here are some basic elements of Prolog:
Facts: These are statements that are true. They are written as a predicate
followed by a period. For example, `likes(john, mango).` is a fact that states that
John likes mango.
Rules: These are statements that define relationships between facts. They are
written as a predicate followed by a body, which is a list of one or more
predicates separated by commas and enclosed in parentheses, followed by a
period. For example,
Grandfather(X, Y) :-father(X,Z),parent(Z,Y)
This implies that for X to be the grandfather of Y, Z should be a parent of Y and
X should be the father of Z.
Queries: These are questions that we ask Prolog. They are written as a
predicate followed by a question mark. For example, `?- likes(john, mango).` is
a query that asks if John likes mango.
Key features of Prolog:
Backtracking: When a task fails, Prolog traces backward and tries to satisfy
the previous task. This backtracking feature allows for flexible problem-
solving.
Symbols in Prolog
If --> :-
Not ~ Not
Or V ;
and ^ ,
Two types of prolog programming are there based on the features of:
Facts, rules and queries
Knowledge based
Facts
We can define fact as an explicit relationship between objects, and properties
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.
Facts are statements that are assumed to be true. In Prolog, facts are written using a
predicate name followed by a list of arguments enclosed in parentheses. For
example: man(john) . Rules are logical statements that describe the relationships
between different facts.
Example:
Apple is a fruit.
Orange is a fruit.
Rule:
In Prolog, rules are logical statements that describe the relationships between facts.
They are used to specify conditions that must be met for a fact to be true. Rules are
written in the form: Head :- Body and Left-hand side :- right-hand side.
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.
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.
Queries:
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
In this section, we will see what knowledge base in logic programming is.
− Facts, Rules and Queries. Among these three if we collect the facts and rules
Well, as we know there are three main components in logic programming
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,
as shown below −
among them, Priya can cook. Let’s try to write these facts in a more generic way
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”.
Conclusion: Successfully execute the program in prolog of simple fact for the
statement using prolog.