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

AI Experiment No 1 Study of Prolog

The document provides an overview of Prolog, a logic programming language. It discusses that Prolog is a declarative language based on logic and consists of facts and rules. Facts are unconditional true statements, while rules describe logical relationships. Prolog finds solutions to queries by searching through the database of facts and rules. The document also outlines some key features of Prolog like pattern matching, backtracking, and uniform data structures. It describes the basic elements of a Prolog program - facts, rules, and queries to run the program. Finally, it covers Prolog syntax and how to handle input/output using predicates like write() and read().

Uploaded by

B 14 Tejas Nun
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)
354 views7 pages

AI Experiment No 1 Study of Prolog

The document provides an overview of Prolog, a logic programming language. It discusses that Prolog is a declarative language based on logic and consists of facts and rules. Facts are unconditional true statements, while rules describe logical relationships. Prolog finds solutions to queries by searching through the database of facts and rules. The document also outlines some key features of Prolog like pattern matching, backtracking, and uniform data structures. It describes the basic elements of a Prolog program - facts, rules, and queries to run the program. Finally, it covers Prolog syntax and how to handle input/output using predicates like write() and read().

Uploaded by

B 14 Tejas Nun
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

NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S

NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)


Department of B-Tech Final Year Computer Science and Engineering
---------------------------------------------------------------------------------------------------------------------
Artificial Intelligence Lab (BTCOL 706)

Experiment No – 01

Aim: Study of PROLOG.

Theory:

1. Prolog stands for programming in logic. In the logic programming paradigm, prolog
language is most widely available. Prolog is a declarative language, which means that a
program consists of data based on the facts and rules (Logical relationship) rather than
computing how to find a solution. A logical relationship describes the relationships which
hold for the given application.
2. To obtain the solution, the user asks a question rather than running a program. When a user
asks a question, then to determine the answer, the run time system searches through the
database of facts and rules.
3. Prolog - PROgramming in LOGic, was first developed by Alain Colmerauer and Philippe
Roussel in 1972 - A Logic Programming language based on the predicate logic.
4. In prolog, clauses are actually descriptive statements that specify what is true about the
problem and because of that Prolog is also known as declarative language or rule-based
language.

Basically, in prolog program we write the program statements in terms of facts and rules. The
system reads in the program and stores it. Upon asking the questions (known as queries) the
system gives the answer by searching through the possible solution(s).

Some basic features of Prolog include:

• Pattern-matching mechanism

• Backtracking strategy that searches for possible solutions

• Uniform data structures from which programs are built

Artificial Intelligence 1 BTCOL706


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
Department of B-Tech Final Year Computer Science and Engineering
---------------------------------------------------------------------------------------------------------------------
Prolog is also widely used for AI programs especially experts systems. For Prolog programming
SWI-Prolog provides comprehensive prolog environment and it is also free.

Applications of Prolog

The applications of prolog are as follows:

 Specification Language
 Robot Planning
 Natural language understanding
 Machine Learning
 Problem Solving
 Intelligent Database retrieval
 Expert System
 Automated Reasoning

In Prolog, we need not mention the way how one problem can be solved, we just need to mention
what the problem is, so that Prolog automatically solves it. However, in Prolog we are supposed
to give clues as the solution method.

Prolog language basically has three different elements −

Facts − The fact is predicate that is true, for example, if we say, “Tom is the son of Jack”, then
this is a fact. Facts are those statements that state the objects or describe the relationship between
objects. For an instance when we say john likes piano, we are showing the 'like' relationship
between two objects 'john and piano' and in prolog this fact can be written as likes(john,piano).
Facts are also known as "unconditional horn clauses" and they are always true.

Syntax

The syntax for facts is as follows −

relation(object1,object2...).

Artificial Intelligence 2 BTCOL706


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
Department of B-Tech Final Year Computer Science and Engineering
---------------------------------------------------------------------------------------------------------------------
Examples:

logic_programming. // Read as : logic programming

music_student(john). // Read as : john is a music student

likes('John', car(bmw)) // Read as : john likes bmw car

gives(john, chocolate, jane). // Read as : john gives chocolate to jane

Rules − Rules are extinctions of facts that contain conditional clauses. To satisfy a rule these
conditions should be met.

For example, if we define a rule as −

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
father of Z.

Examples: If we want to say that john and jane are friends if john likes jane and jane likes john.
Then in prolog this friends rule can be written as,

friends(john,jane) :- likes(john,jane), likes(jane,john).

Examples: Rules with variables.

likes(john, X) :- car(X). // Read as : john likes X if X is a car.

friends(X, Y) :- likes(X, Y), likes(Y, X). // Read as : X and Y are friends if X


likes Y and Y likes X. OR Two people are friends if they like each other.

Questions − And to run a prolog program, we need some questions, and those questions can be
answered by the given facts and rules. Prolog queries are the questions asked by the user to
prolog interpreter about facts and rules stored in it's database. For the particular program, upon
asking the query, Prolog interpreter trace through the facts and rules it has been given in the top-
down manner and try to find a match for a given query. If it finds the matches it will report

Artificial Intelligence 3 BTCOL706


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
Department of B-Tech Final Year Computer Science and Engineering
---------------------------------------------------------------------------------------------------------------------
yes/true and if it doesn't then it will report no/false. Query can be made up of multiple subgoals -
for example ?- student(john, music), student(jane,music) - so here in this sentence we have query
with two subgoals and subgoals must be separated by commas. But all subgoals must be satisfied
otherwise whole query would result in failure. One more thing is - if you use any variables to
define rules in prolog program, it's not necessary to use the same variable name when you ask
queries.

Example : For below Prolog Program, we will ask two queries about the facts we have
provided.

Program :

likes(alice,john). // Read as : alice likes john

likes(john,mary). // Read as : john likes mary

Queries : ?- likes(alice,john). // Read as :- Does alice like john?

true.

?- likes(alice,mary). // Read as :- Does alice like mary?

false.

In above example, text in red color is what user types and queries are terminated
by the full stop at the end.

So, when we enter ?- likes(alice,john). interpreter will answer yes or true since it
can find the match for the given query.

But when we ask ?- likes(alice,mary). then it would say no or false.

Prolog Syntax

The syntax of Prolog is as follows:

Artificial Intelligence 4 BTCOL706


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
Department of B-Tech Final Year Computer Science and Engineering
---------------------------------------------------------------------------------------------------------------------
Symbols

Using the following truth-functional symbols, the Prolog expressions are comprised. These
symbols have the same interpretation as in the predicate calculus.

English Predicate Prolog


Calculus

If --> :-

Not ~ Not

Or V ;

and ^ ,
Variable

Variable is a string. The string can be a combination of lower case or upper case letters. The
string can also contain underscore characters that begin with an underscore or an upper-case
letter. Rules for forming names and predicate calculus are the same.

Handling input and output

The write() Predicate

To write the output we can use the write() predicate. This predicate takes the parameter as input,
and writes the content into the console by default. write() can also write in files. Let us see some
examples of write() function.

Example:

?- write(56).

56

yes

| ?- write('hello').

hello

Artificial Intelligence 5 BTCOL706


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
Department of B-Tech Final Year Computer Science and Engineering
---------------------------------------------------------------------------------------------------------------------
yes

| ?- write('hello'),nl,write('world').

hello

world

The read() Predicate

The read() predicate is used to read from console. User can write something in the console, that
can be taken as input and process it. The read() is generally used to read from console, but this
can also be used to read from files. Now let us see one example to see how read() works.

Program
cube :-
write('Write a number: '),
read(Number),
process(Number).
process(stop) :- !.
process(Number) :-
C is Number * Number * Number,
write('Cube of '),write(Number),write(': '),write(C),nl, cube.
Output
| ?- [read_write].
compiling D:/TP Prolog/Sample_Codes/read_write.pl for byte code...
D:/TP Prolog/Sample_Codes/read_write.pl compiled, 9 lines read - 1226 bytes written, 12 ms

(15 ms) yes


| ?- cube.
Write a number: 2.
Cube of 2: 8
Write a number: 10.
Cube of 10: 1000
Write a number: 12.
Cube of 12: 1728
Write a number: 8.
Cube of 8: 512
Write a number: stop
.

Program: Write a following program and take print and attached

1. Write a Prolog in Prolog calculate addition of two no.

Artificial Intelligence 6 BTCOL706


NUTAN MAHARASHTRA VIDYA PRASARAK MANDAL’S
NUTAN COLLEGE OF ENGINEERING & RESEARCH (NCER)
Department of B-Tech Final Year Computer Science and Engineering
---------------------------------------------------------------------------------------------------------------------
2. Write a Prolog in Prolog to find Maximum of two no .
3. Write a Prolog in Prolog that take number N from the user and count from N to 10.
4. Write a Prolog in Prolog that take number N from the user and count from N to 1.
5. Write a Prolog in Prolog that take number N from the user calculate factorial of no.
6. Write a Prolog in Prolog that take number N from the user calculate square of no from N
to 20 and display it .

Questions:

1. What is Prolog. Explain it .


2. Explain Loop and condition, decision making statement with example.
3. Explain List and its operation with example.
4. What is backtracking explain with example.

(Subject In-charge)

(Prof.S.B.Mehta)

Artificial Intelligence 7 BTCOL706

You might also like