Transcript document

PROLOG
PROgramming in LOGic
Introductory Lecture
Features of PROLOG
Logical variables
Pattern matching facility
Backtracking strategy to search for proofs
Structure of PROLOG Programs
Facts
Rules
Query
Variables: must begin with an upper case
letter
Constants: must begin with lowercase
letter, or enclosed in single quotes
Facts
Fact is just what it appears to be
Often a proposition like “It is sunny”
 represented as ‘It is sunny’.
Queries
 Action of asking the program about information
contained within its data base
 After a program is loaded, you will receive the
query prompt,
? you can ask the program a question such as
?- 'It is sunny'.
and it will respond with the answer
Yes
?-
Rules
 give Prolog the ability to pursue its decisionmaking process
 'It is sunny'.
'It is summer'.
‘It is hot' :- 'It is summer', 'It is sunny'.
'It is cold' :- 'It is winter', 'It is snowing'.
 The query,
?- 'It is hot'.
Yes
Predicate Logic
 female(amy).
 female(johnette).
 male(anthony).
 male(bruce).
 male(ogden).






parentof(amy,johnette).
parentof(amy,anthony).
parentof(amy,bruce).
parentof(ogden,johnette).
parentof(ogden,anthony).
parentof(ogden,bruce).
 siblingof(X,Y) :- parentof(Z,X) , parentof(Z,Y) , X ≠ Y.
Lists & Records
 List is designated by square brackets. Ex:
[dog,cat,mouse]
 Records or tuples are represented as patterns.
 The elements of a tuple are accessed by pattern
matching.
book(Title,Author,Publisher,Date).
author(LastName,FirstName,MI).
publisher(Company,City).
book(T,A,publisher(C,rome),Date)
Arithmetic Operators







+
*
/
//
mod
**
addition
subtraction
multiplication
real division
integer division
modulus
power
 ?- X is 3*4.
X = 12
yes
Logical Operators
a :- b.
% a if b
a :- b,c.
% a if b and c.
a :- b;c.
% a if b or c.
a :- \++ b.
% a if b is not provable
a :- not b.
% a if b fails
a :- b -> c;d. % a if (if b then c else d)
Unification and Pattern Matching
% deriv(Polynomial, variable, derivative)
% dc/dx = 0
deriv(C,X,0) :- number(C).
% dx/dx = 1
deriv(X,X,1).
% d(cv)/dx = c(dv/dx)
deriv(C*U,X,C*DU) :- number(C), deriv(U,X,DU).
% d(u v)/dx = u(dv/dx) + v(du/dx)
deriv(U*V,X,U*DV + V*DU) :- deriv(U,X,DU),
deriv(V,X,DV).
Functions
Prolog does not provide for a function type
so, functions must be defined as relations.
fac(0,1).
fac(N,F) :- N > 0, M is N - 1,
fac(M,Fm), F is N * Fm.
minimum(M,N,M) :- M =< N.
minimum(M,N,N) :- N =< M.
Lists
 List --> [ ]
List --> [Element|List]
 Example:
length([ ],0).
length([H|T],N) :- length(T,M), N is M+1.
sum([ ],0).
sum([X|L],Sum) :- sum(L,SL), Sum is X + SL.
Prolog Program
Write the program in simple text editor &
save it as .pl
On the terminal, type gprolog.
To load a file, “[filename].”
Tracing
Allows to watch Prolog's reasoning as it
happens
Helpful for debugging
Type “trace.” on the ? prompt & then type
any query.
To turn off tracing type “notrace.”
Cut
 It allows you to tell Prolog, which previous choices it
need not consider again when it backtracks through the
chain of satisfied goals.
 Example:
facility(Pers,Fac) :book_overdue(Pers,Book), !,
basic_facility(Fac).
Facility(Pers,Fac) :- general_facility(Fac)
?- client(X) , facility(X,Y).
If a client is found to have an overdue book, then only allow
the client the basic facilities of the library. Don’t bother
going through all the client’s overdue books, and don’t
consider any other rule about facilities.
FAIL – Built-in Predicate
Has no arguments
When a fail is encountered after a cut, the
normal backtracking behavior is altered by
the effect of cut.
 average_taxpayer :- foreigner(X) , ! , fail.
average_taxpayer :- spouse(X,Y) , ……
Can read up various online tutorials for
detailed study of Prolog.
One good reference is “Programming in
Prolog”, by Clocksin & Mellish, Narosa
Publishing House.
Thank You