Upload
lyphuc
View
245
Download
3
Embed Size (px)
Citation preview
Using Prolog as a CAS
Kharkiv Pedagogical UniversityA. Stolyarevska
July 2002
Workshop ACDCA
References
1) Robinson J.A. A machine-oriented logic based on the resolution principle. J. ACM, 12, 23-41.
2) Colmerauer A. Prolog-based theoriques et development actuals. TSI, 2, 4, 1983.
3) Clocksin W.F., Mellish C.S. Programming in Prolog. Springer-Verlag, New York, 1985.
Workshop ACDCA
What is Prolog?� Prolog stands for PROgramming in LOGic. � Prolog programs are interpreted by an Prolog
interpreter, Turbo-Prolog is such an interpreter.
� A Prolog program consists of facts and rules. � A Prolog program describes the problem
instead of the solution for the problem.
Workshop ACDCA
Workshop ACDCA
An example of a small program (1)� %This is a fact which states that john is a parent of bart
� parent(john,bart).
� %This is a fact which states that barbara is a parent of bart� parent(barbara,bart).
� %This is a fact which states that john is a male� male(john).
� %This is a fact which states that bart is a male� male(bart).
� %This is a fact which states that barbara is a female� female(barbara).
� To be continuedWorkshop ACDCA
An example of a small program (2)
� %This is a rule which states that X is a father of Y if� %X is a male and X is the parent of Y
� father(X,Y) :- male(X),parent(X,Y).
� %This is a rule which states that X is a mother of Y if� %X is a female and X is the parent of Y
� mother(X,Y) :- female(X),parent(X,Y).
Workshop ACDCA
An example of a small program (3)
� In the example shown above you see several facts and rules. With these facts and rules in aProlog database you can ask the Prolog interpreter several questions.
� In the table below you can see some questions translated to prolog predicates. If a question has multiple solutions then 'no' indicates that there are no other solutions
Workshop ACDCA
An example of a small program (4)
Workshop ACDCA
� The Prolog system recognizes the type of a term object by its syntax.
� A variable always starts with an uppercase character and all other data types start with a lowercase character.
� There is a picture showing the hierarchy of types in Prolog:
Workshop ACDCA
Principal elements� Terms - for representing data objects� Facts & rules - for specifying relationships
between terms� Queries - for asking questions about how
terms are related with respect to a collection of facts and rules
Workshop ACDCA
Glossary� Variable - a name beginning with a capital letter that can be used to
interpret the value of a certain object� Relation - a name describing the manner in which a collection of
objects belong together� Atom - a relation, possibly involving objects or variables� Domains specify the types of values objects may take in relation� Fact - a relation between objects� Sub-goal - a relation, possibly involving objects or variables, which
Prolog must attempt to satisfy� Rule - a relationship between a fact and a list of sub-goals which must
be for that fact to be true
Workshop ACDCA
Structure of program� A program consists of such sections
� domains� predicates� goal� clauses
� Predicates and clauses sections are necessary
� Let�s consider the simple program -welcome.pro Workshop ACDCA
This the example of thefamous "hello world" program
%Welcome.propredicateshello
goalhello.
clauseshello:-write("Hello, World!"),nl.
Workshop ACDCA
Building the program� Run the file Prolog.exe� The main Prolog window includes system
menu, four sub-windows:� Editor � Dialog� Message� Trace
Workshop ACDCA
Our first exampleCh03ex01.pro
predicateslikes(symbol,symbol)
clauseslikes(ellen, tennis).likes(john, football).likes(tom, baseball).likes(eric, swimming).likes(mark, tennis).likes(bill, Activity):-
likes(tom, Activity).Workshop ACDCA
Examples of dialogue
Workshop ACDCA
Example: Dif.pro program
Run!
How to write the program similar to this?First of all, it is necessary to learn to work
with numbers and lists.It is necessary to understand idea of recursion.
Workshop ACDCA
Workshop ACDCA
Numbers� Numbers are the most important group of symbols in
computing.� Prolog includes facilities for unifyng a variable or
number with a numeric expression.� Examples:
� factorial function� power function
� Individual task:� function ex
Workshop ACDCA
Iteration and recursionLet's consider definition given by Malpas for recursive
procedure.Every recursive procedure should include:1. Non-recursive phrase determines an initial kind of
procedure, i.e. procedure at the moment of termination of recursion,
2. Recursive rule. First sub-aim, settling down in a body of this rule, develops new values of arguments.Further recursive sub-aim is placed, in which the new values of arguments are used.
Workshop ACDCA
Factorial function(Pascal language)
Workshop ACDCA
Factorial function(Prolog language)
Workshop ACDCA
/* Numbers likely to exceed 32767are declared as reals! */
/* Recursive program to compute factorials.Ordinary recursion, not tail recursion. */
predicatesfactorial (integer, real)
clausesfactorial (1, 1):-!.factorial (X, FactX):-
Y = X-1,factorial (Y, FactY), FactX = X*FactY.
predicatesfactorial (integer, real)factorial_aux (integer, real, integer, real)
clausesfactorial (N, FactN):-
factorial_aux (N, FactN, 1, 1).factorial_aux (N, FactN, I, P):-
I < = N,!,NewP = P * I, NewI = I + 1,factorial_aux (N, FactN, NewI, NewP).factorial_aux (N, FactN, I, FactN):- I > N.
Workshop ACDCA
Between.pro
/* iteration */predicatesbetween(integer,integer,
integer)clausesbetween(I,J,I):-I<=J.between(I,J,K):-
I<J,I1=I+1, between(I1,J,K).
Compare them!
Predicates %Recursion!count ( real )
Clausescount(Number) :- write(Number), nl ,
New_number = Number + 1 ,count(New_number) ,write("This added call makes this clause non-tail recursive!").
Goal
count(1). Workshop ACDCA
Function xn
predicates power(real,integer,real)odd(integer)even(integer)clausespower(_,0,1).power(X,N,Xn):-N>0, odd(N),M=N-1,
power(X,M,Xm), Xn=X*Xm.power(X,N,Xn):-N>0,
even(N),X2=X*X,M=N/2,power(X2,M,Xn).
even(X):-0=X mod 2.odd(X):-1=X mod 2.
predicates power(real,integer,real)clausespower(_,0,1).power(X,N,Xn):-
N>0, M=N-1,power(X,M,Xm),Xn=X*Xm.
Workshop ACDCA
Individual task:function ex
...!3!2
132
++++=xxxex
In this program You can use relations power and factorial.
Workshop ACDCA
Workshop ACDCA
Glossary� List - a special sort of object consisting of a
collection of elements enclosed in square brackets and separated by commas
� Head of a list - the first element of a list� Tail of a list - the list that remains the first
element of a given list (and its separating comma) are removed
Workshop ACDCA
Simple programs1) This program determines the length of the list:length_of([], 0).length_of([_|T], L) :-
length_of(T, TailLength), L = TailLength + 1.
2) Finding a sum of the list of integers.sumlist ([I | Is], Sum):- sumlist (Is, IsSum),
Sum=I+IsSum.sumlist ([], 0).
3) Integer element X belongs to the listmember(X, [X|_]).member(X, [_|T]) :- member(X,T).
Workshop ACDCA
ExercisesDefine the next operations under lists: 1)conc - concatenation of two listsconc([],L,L).conc([X|L1],L2,[X|L3]):-conc(L1,L2,L3).
2) append - insert an element to the head of the list:append(X,L,[X|L]).
3) delete - delete an element from the listdomainsilist=integer*predicatesdelete(integer,ilist,ilist)clausesdelete(X,[X|Tail],Tail).delete(X,[Y|Tail],[Y|Tail1]):-
delete(X,Tail,Tail1).Workshop ACDCA
Exercises4) inner productdomainsilist=integer*predicatesinner_product(ilist,ilist,integer)inner_product(ilist,ilist,integer,integer)clausesinner_product(Xs,Ys,IP):-
inner_product(Xs,Ys,0,IP).inner_product([X|Xs],[Y|Ys],Temp,IP):-Temp1=X*Y+Temp,inner_product(Xs,Ys,Temp1,IP).inner_product([],[],IP,IP).
Workshop ACDCA
Examples of questions for exercises 1-4
1) L1=[1,2],L2=[6,12],conc(L1,L2,L3)L3=[1,2,6,12]2)append(1,[2,3,4],K)K=[1,2,3,4]3) delete(3,[2,3,4,6],L)L=[2,4,6]4) inner_product([1,2,3],[2,3,4],P)P=20
Workshop ACDCA
Workshop ACDCA
Tools & Features� inbuilding predicates� unification� backtracking� declarative and procedural semanticsApplication fields� artificial intelligence� interfaces� databases� expert systems
Workshop ACDCA
Examples
Graphics
AI
Interface
Workshop ACDCA
AI - FWGC.proPROBLEM:
A farmer with his goat, wolf and cabbage come to a river that theywish to cross. There is a boat, but it only has room for two, andthe farmer is the only one that can row. If the goat and cabbageget in the boat at the same time, the cabbage gets eaten.Similarly, if the wolf and goat are together without the farmer,the goat is eaten. Devise a series of crossings of the river so that allconcerned make it across safely.
Workshop ACDCA
FWGC.pro
The state of the system is indicated by stating where the farmer,the goat the wolf and the cabbage are located.
state( Farmer, Wolf, Goat, Cabbage )The problem is that a state must only be visited once, and some
states are illegal. This is checked by 'unsafe' and 'member'.The Predicate "go" can be called with a start state and a final statego( state(east,east,east,east), state(west,west,west,west) ).
Workshop ACDCA
References (in Ukraine language)
� Stolyarevska A.L. About experience of teaching of Prolog language in the course of informatics in pedagogical high school // Pedagogics and psychology. The collection of scientific works. Is. 14. Kharkov, 2000. - Pp. 89-96.
� Stolyarevska A.L. Prolog Language and Programming of Expert Systems. The manual. -Kiev. 1997, 86 p.
Workshop ACDCA
TrainingCourse: a course of informatics (computer
science) at Kharkiv State Pedagogical University
Students: of mathematics and physics faculty
Topics: numbers, realization of recurrent and iterative algorithms (calculation of sums, fast degree, Fibonacci numbers), operation with polynomials, other tasks
Workshop ACDCA
Conclusion� PROLOG is a method of declarative programming
(PROgramming in LOGic)� Students use Prolog programming language
while processing knowledge and data represented as symbolical structures
Thank You!
Workshop ACDCA