Introductory Lecture
Logical variables
Pattern matching facility
Backtracking strategy to search for proofs
Facts Rules Query
Variables: must begin with an upper case letter
Constants: must begin with lowercase letter, or enclosed in single quotes
Fact is just what it appears to be Often a proposition like It is sunny
represented as It is sunny .
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
?-
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
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.
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)
+ addition
- subtraction
* multiplication / real division // integer division
mod modulus
** power
?- X is 3*4.
X = 12 yes
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)
% 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).
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.
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.
Write the program in simple text editor &
save it as .pl
On the terminal, type gprolog.
To load a file, [filename].
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.
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.
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.
This document was created with Win2PDF available at http://www.daneprairie.com.
The unregistered version of Win2PDF is for evaluation or non-commercial use only.