0% found this document useful (0 votes)
2 views

First Prolog program

The document provides an overview of the Prolog programming language, highlighting its use in artificial intelligence as a declarative logic programming language that utilizes facts, rules, and queries. It explains key features such as unification, backtracking, and the handling of lists and recursion, along with guidelines for writing facts and rules. Additionally, it illustrates how to create a knowledge base in Prolog with examples of facts and queries.

Uploaded by

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

First Prolog program

The document provides an overview of the Prolog programming language, highlighting its use in artificial intelligence as a declarative logic programming language that utilizes facts, rules, and queries. It explains key features such as unification, backtracking, and the handling of lists and recursion, along with guidelines for writing facts and rules. Additionally, it illustrates how to create a knowledge base in Prolog with examples of facts and queries.

Uploaded by

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

Aim: Study of Prolog programming language & its function to write simple

fact for the statement using prolog.

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:

The key features of Prolog are as follows:

 Declarative Language: Prolog is a declarative programming language,


meaning that programs are written as sets of logical statements. This
makes Prolog programs concise and easy to read.

 Predicate Calculus: Prolog uses the language of predicate calculus,


which allows for the representation of relationships between facts and rules.

 Handling Lists and Recursion: Prolog naturally handles lists and


recursion, making it well-suited for tasks that involve these concepts.

 Unification: Prolog uses unification, which is the process of determining


if given terms can represent the same structure. Unification is a fundamental
concept in Prolog.

 Backtracking: When a task fails, Prolog traces backward and tries to satisfy
the previous task. This backtracking feature allows for flexible problem-
solving.

 Efficiency: Prolog is known for its efficiency in solving problems that


would be difficult in other programming languages.

Symbols in Prolog

Using the following truth-functional symbols, the Prolog expressions are


English Predicate Calculus Prolog

If --> :-

Not ~ Not

Or V ;

and ^ ,

comprised. These symbols have the same interpretation as in the predicate


calculus.

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

have some facts as given below −


these objects might have. So facts are unconditionally true in nature. Suppose we

 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:

Fact is a statement that are true.

 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.

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.

Queries:

properties. So question can be anything, as given below −


Queries are some questions on the relationships between objects and object

 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.

You might also like