Advanced Features in Prolog
Advanced Features in Prolog
Objects in Prolog
• Object is a collection of attributes. Treating related
information as a single object is similar to records
in conventional programming languages.
• For instance, an object for address can be defined
as address(Number, City, Zipcode, Country).
• The entire address is treated as a single object and
can be used as an argument of a predicate. Here
address is called functor and Number, Street, City,
Zipcode and Country are called components which
hold actual values.
• Consider an example of storing data about a
course in two different representations as:
• course (logic, monday, 9, 11, saroj,
kaushik, block3, room21).
(1)
• course (logic, time(monday,9,11),
lecturer(saroj, kaushik), location(block3,
room21)).
(2)
• These are two representations of a fact “a lecture
course on 'logic', given on Monday from 9 to 11 in
the morning by saroj kaushik in block 3, room 21”
• In representation (1), there is a relationship
between eight items.
• In (2) there is a relationship between four objects
– a course name, a time, a lecturer and a location.
• The four-argument version of course enables more
concise rules to be written by abstracting the
details that are relevant to the query.
• Let us define few useful predicates using
representation (2) for course facts.
• teacher_course(L, C) - teacher L teaches a
course C.
• teacher_on_day(L, Day, C) - teacher L teaches a
course C on day Day.
• duration(C, D) - course C of D duration.
• These predicates are defined as follows:
teacher_course(L, C) :- course(C,_, lecturer(L,_),_).
teacher_on_day(L, Day, C) :- course( C , time (
Day, _ , _ ), L , _).
duration(C, D):- course(C, time( -, Start,
Finish),_,_), D is Finish – Start.
• Note that we have hidden the details which are not
required in particular rule formation by putting
underscore ( _ ).
• This is called Data abstraction. We don't have
definite rules to decide when to use structured data
or not.
Query: Who teaches logic course?
Goal: ?- teacher_course(L, logic).
?- teacher_course(L, logic).
?- course(logic, _, lecturer(L,_), _ ).
{L = saroj}
succeeds Answer: L = saroj
Query: Which course does saroj teach?
Goal: ?- teacher_course(saroj, C).
X-Y Y
Y-Z
Z
X-Z
The System predicate "cut"
• Prolog is non deterministic in nature because even
when a goal has been achieved, the interpreter
backtracks to achieve the goal.
• Non deterministic system is one which involves
choice points more than one of which lead to a
successful conclusion.
• Prolog provides a system defined predicate called
cut (denoted by !) for affecting the procedural
behavior of program and to limit the non
determinism by preventing interpreter from
finding alternative solutions.
• When interpreter comes across a 'cut', the
effect is that all alternative solutions of the
goal waiting to be tried are abandoned thereby
reducing the size of search tree.
• There are many applications, where the very
first solution is of interest, if it exists.
• Cut prunes the search tree and hence shortens
the path traversed by Prolog interpreter.
• It reduces the computation time and also saves
storage space.
• Semantically 'cut' always succeeds.
Example: Write a program that lists the users and
library facilities open to them according to the
following scheme.
facilities
basic additional
Reference Enquiry
Borrowing Inter library loan
Types of Cut
• There are two types of cuts viz., green cut and red
cut.
• Green cut : It does not affect the solution but
affects the efficiency of the Prolog program.
• Removal of such cut does not change the meaning
of the program.
Example: Write Prolog program for merging two
ordered lists.
/* merge(X, Y, Z) - Z is obtained by merging
ordered lists X and Y. */
merge( [X|X1], [Y|Y1], [X|Z] ) :- X < Y, !,
merge(X1, [Y|Y1], Z).
merge( [X|X1], [Y|Y1], [X, Y|Z] ):- X = Y, !,
merge(X1, Y1, Z).
merge( [X|X1], [Y|Y1], [Y|Z] ) :- X > Y,
merge( [X|X1], Y1, Z).
merge(X, [ ], X).
merge( [ ], Y, Y).
• If we remove cuts from the rules, then the
solutions are not affected and program does not
change.
Red cut: The cut whose removal from the program
changes the meaning of the program.
• Consider two versions of the programs for finding
maximum of two numbers. Version I
% max(X, Y, Z) – Z is unified with maximum of X and Y.
max(X, Y, Z) :- X ≥ Y, !, Z = X . (1)
max(X, Y, Y) . (2)
Goals: ?- max(5, 4, 4). Answer: No
?- max (5, 4, 5). Answer: Yes
• If the cut is deleted from the rule, then we will
not get correct answer.