You are on page 1of 20

Sardar Patel College of Engineering,Bakrol

Unit -12
Prolog Programming

Artificial Intelligence (3170716)


Introduction to Prolog
• Prolog or PROgramming in LOGics is a logical and declarative programming language.

• This is particularly suitable for programs that involve symbolic or non-numeric computation.
• Prolog features include interface to other language and database product, stand alone
application generator and more recently support for techniques such as object-oriented and
constraint based programming.

• Application : Expert System, ML, Robot Planning, automated Reasoning, Problem Solving

Artificial Intelligence (3170716)


Relation in Prolog
• Prolog programming specifies relationship among objects and properties of object.
– Ex: Amit has a bike
– Ex: A & B both are brother, than we must satisfy all the condition’s whatever we write.
» A and B are brothers if A and B are both male and
they have the same father and they have the same mother and a is not same as B.

Artificial Intelligence (3170716)


Facts, Rules, Queries
• In prolog program we declare facts describing explicit relationship between object and properties of objects might
have:
Example: Tulika likes ice-cream
Hair is black

Mini is a cat

• We define rules defining implicit relationship between objects & rules defining implicit object properties.
Example: A is a child of B if B is parent of A.

• One then use the system to generate queries by asking questions above relationship between objects or about
object properties.

Example: Tulika like ice-cream?

Artificial Intelligence (3170716)


Facts
• Facts are properties of object or relationship between objects.
– Example: Rima has phone number 2121214
phoneno(Rima,2121214)

• Rules to define Facts:


1. Names of properties/ relationship begin with lower case letters.

2. The relationship name appears as the first term.


3. Object appears as comma- separated argument within parentheses.

4. A period “ .” must end a fact.

5. Objects also begin with lower case letters, they ca also begin with digit(like 1234) and can be string of
characters enclosed in quote.
Example: color(penink ,’red’).

Artificial Intelligence (3170716)


Rules
• Example:one teacher will guide student if that student studies that very course id on which the teacher
teaches.
• Facts are unit clauses and rules are non-unit clauses
• Variable name start with a capital letter.
– In prolog

• guide(Teacher,Student)
• teaches(Teacher,Courseid)

• studies(Student, Courseid)

Artificial Intelligence (3170716)


Goal or Query
• Queries will be based on facts and rules. We can ask questions based on stored information.

• Suppose we want to know if sudhir lecture in course002 or not then we can ask:
– ?- teaches(sudhir,course001).
– yes
• In GNU Prolog queries are terminated by a fullstop.

– We can ask

– ?- teaches(Sudhir,X).

– X= course001.

Artificial Intelligence (3170716)


Clause Syntax
• :- means “if” or “is implied by”
• Left part is (head) & Right part is(body)
• (,) conjuction ----(And)

• (;) disjunction ---(OR)

• A program consists of clause these are of three types : facts, rules and questions.

• A procedure is a set of clause about the same relation.

• CLAUSE : P :- Q;R also written as


– P:- Q.
– P:- R.

Artificial Intelligence (3170716)


Data Object in Prolog
Data object

Simple object
Structure

Constants Variable

atoms Numbers(int,real)

• Example:Atoms: tom,pat,x100,x_45
Numbers: 100, 1234
Variable: x, y xval , _x
Structure: day(9,jun,2017),point(10,25)

Artificial Intelligence (3170716)


Comparison Operator
Operator Meaning
X>Y X is greater than Y
X<Y X is less than Y
X >= Y X is greater than or equal to Y
X =< Y X is less than or equal to Y
X =:= Y the X and Y values are equal
X =\= Y the X and Y values are not equal

Artificial Intelligence (3170716)


Arithmetic Operators

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
** Power
// Integer Division

Artificial Intelligence (3170716)


Example of Arithmetic

• Example:
– X= 1+2
– X is 1 + 2.
– X is 5/2, is 5//2, Z is 5 mod 2, W is mod(10,3).

Artificial Intelligence (3170716)


Control Structure
• Conjunction (AND logic) can be implemented using the comma (,) operator. So two predicates separated by
comma are joined with AND statement.
• Disjunction (OR logic) can be implemented using the semi-colon (;) operator. So two predicates separated by
semi-colon are joined with OR statement.

• Cut (remove choice points): Sometimes it is desirable to prevent Prolog from backtracking into alternative
solutions. The basic tool available to the programmer to stop prolog from continuing futher in its backtrack is the
cut operator.

– Syntxa: !

– To stop prolog from searching for more solutions after the first is found you would use the cut operator, like
so.

Artificial Intelligence (3170716)


List
• It is simple data structure widely used in non-numeric programming list consisit of any number of
items such as red,green,blue,white,dark.
• A list can be either empty or non-empty.
• List can be viewed as consisting two things,
1. The first item, called the head of the list.

2. The remaining part of the list called tail.


• In prolog it will be represented as:

– [red,green,blue,white,black]

Artificial Intelligence (3170716)


Backtracking
• Backtracking is a procedure, in which prolog searches the truth value of different predicates by
checking whether they are correct or not. The backtracking term is quite common in algorithm
designing, and in different programming environments.

• Note − While we are running some prolog code, during backtracking there may be multiple answers,
we can press semicolon (;) to get next answers one by one, that helps to backtrack. Otherwise when
we get one result, it will stop.

Artificial Intelligence (3170716)


Recursion
• Recursion is a technique in which one predicate uses itself (may be with some other
predicates) to find the truth value.

• So this predicate is recursive in nature. Suppose we say that just_ate(deer, grass), it


means is_digesting(deer, grass) is true. Now if we say is_digesting(tiger, grass), this
will be true if is_digesting(tiger, grass) :- just_ate(tiger, deer), is_digesting(deer,
grass), then the statement is_digesting(tiger, grass) is also true.
Artificial Intelligence (3170716)
Tower of Hanoi
• Program:
(M, Source, target, auxiliary)
move(1,X,Y,_) :-
write('Move top disk from '), write(X), write(' to '), write(Y), nl. move(N,X,Y,Z) :-
N>1,

M is N-1,

move(M,X,Z,Y),

move(1,X,Y,_),

move(M,Z,Y,X).

Artificial Intelligence (3170716)


Download Prolog

• Link:http://www.gprolog.org/

Artificial Intelligence (3170716)


Hello World in Prolog

• write('Hello World').

Artificial Intelligence (3170716)


Thank
You!

Artificial Intelligence (3170716)

You might also like