AI Experiment No 1 Study of Prolog
AI Experiment No 1 Study of Prolog
Experiment No – 01
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).
• Pattern-matching mechanism
Applications of Prolog
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.
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
relation(object1,object2...).
Rules − Rules are extinctions of facts that contain conditional clauses. To satisfy a rule these
conditions should be met.
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,
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
Example : For below Prolog Program, we will ask two queries about the facts we have
provided.
Program :
true.
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.
Prolog Syntax
Using the following truth-functional symbols, the Prolog expressions are comprised. These
symbols have the same interpretation as in the predicate 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.
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
| ?- write('hello'),nl,write('world').
hello
world
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
Questions:
(Subject In-charge)
(Prof.S.B.Mehta)