Professional Documents
Culture Documents
Unit -12
Prolog Programming
• 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
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.
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’).
• guide(Teacher,Student)
• teaches(Teacher,Courseid)
• studies(Student, Courseid)
• 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.
• A program consists of clause these are of three types : facts, rules and questions.
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)
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
** Power
// Integer Division
• 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).
• 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.
– [red,green,blue,white,black]
• 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.
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
• Link:http://www.gprolog.org/
• write('Hello World').