Presentation is loading. Please wait.

Presentation is loading. Please wait.

PROLOG PROgramming in LOGic

Similar presentations


Presentation on theme: "PROLOG PROgramming in LOGic"— Presentation transcript:

1 PROLOG PROgramming in LOGic
Introductory Lecture

2 Features of PROLOG Logical variables Pattern matching facility
Backtracking strategy to search for proofs

3 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

4 Facts Fact is just what it appears to be
Often a proposition like “It is sunny” represented as ‘It is sunny’.

5 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

6 Rules give Prolog the ability to pursue its decision-making 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

7 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.

8 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)

9 Arithmetic Operators + addition - subtraction * multiplication
/ real division // integer division mod modulus ** power ?- X is 3*4. X = 12 yes

10 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)

11 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).

12 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.

13 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.

14 Prolog Program Write the program in simple text editor & save it as .pl On the terminal, type gprolog. To load a file, “[filename].”

15 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.”

16 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.

17 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) , ……

18 Can read up various online tutorials for detailed study of Prolog.
One good reference is “Programming in Prolog”, by Clocksin & Mellish, Narosa Publishing House.

19 Thank You


Download ppt "PROLOG PROgramming in LOGic"

Similar presentations


Ads by Google