40
Using Prolog as a CAS Kharkiv Pedagogical University A. Stolyarevska July 2002 Workshop ACDCA

Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

  • Upload
    lyphuc

  • View
    245

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Using Prolog as a CAS

Kharkiv Pedagogical UniversityA. Stolyarevska

July 2002

Workshop ACDCA

Page 2: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 3: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 4: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Workshop ACDCA

Page 5: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 6: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 7: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 8: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

An example of a small program (4)

Workshop ACDCA

Page 9: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

� 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

Page 10: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 11: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 12: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 13: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

This the example of thefamous "hello world" program

%Welcome.propredicateshello

goalhello.

clauseshello:-write("Hello, World!"),nl.

Workshop ACDCA

Page 14: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Building the program� Run the file Prolog.exe� The main Prolog window includes system

menu, four sub-windows:� Editor � Dialog� Message� Trace

Workshop ACDCA

Page 15: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 16: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Examples of dialogue

Workshop ACDCA

Page 17: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 18: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Workshop ACDCA

Page 19: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 20: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 21: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Factorial function(Pascal language)

Workshop ACDCA

Page 22: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Factorial function(Prolog language)

Workshop ACDCA

Page 23: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

/* 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

Page 24: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 25: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 26: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Individual task:function ex

...!3!2

132

++++=xxxex

In this program You can use relations power and factorial.

Workshop ACDCA

Page 27: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Workshop ACDCA

Page 28: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 29: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 30: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 31: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 32: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 33: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Workshop ACDCA

Page 34: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Tools & Features� inbuilding predicates� unification� backtracking� declarative and procedural semanticsApplication fields� artificial intelligence� interfaces� databases� expert systems

Workshop ACDCA

Page 35: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

Examples

Graphics

AI

Interface

Workshop ACDCA

Page 36: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 37: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 38: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 39: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

Page 40: Using Prolog as a CAS - RFDZ :: HOMErfdz.ph-noe.ac.at/fileadmin/Mathematik_Uploads/ACDCA/VISITME2002/... · Using Prolog as a CAS ... Ł The Prolog system recognizes the type of a

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

[email protected]

Thank You!

Workshop ACDCA