IMPLEMENTING PASCAL-LIKE CONSTRUCTS: AN EXERCISE IN PROLONG PROGRAMMING INFORMATICA 2/87 UDK 681.3.06:519.682 PROLOG Bogdan Filipič Department of Computer Science and Informatics »Jožef Štefan« Institute, Ljubljana ABSTRACT. Due to tho declarativo meaning ,of programa. Prolog is a poworful programming language. Howevor, in practice it turna out that numerous tasks within a oertain kind of programs aro quite prooedural. The paper describes a simple implementation of some Pascal-like construots in Prolog as a practical solution to this problem. IMPLEMENTACIJA PASCALSKIH KONSTBUKTOV KOT VAJA IZ PBOGBA- HIKAMJA V PKOL060. Prolog je zaradi deklarativnega pomena programov močan programski jezik, vendar se v praksi izkaSe, da so mnoga opravila znotraj določenih programov povsem proceduralnega znaOaja. V članku je kot praktična reSitev tega problema prikazana implementacija nekaterih pasoalskih konstruktov v Prologu, INTRODUCTION IF-THEH AND IF-THEH-ELSE PROCEDURES Due to the declarative meaning of programs, Prolog is a poverful programming language [1,3]. It is especially well suited for solving non-numerical problems. From the programmer's point of view, programming in Prolog is very effiolont. On the other hand. Prolog implementations suffer from the lack of supporting the prooedural programming approach. In practice it namely turns out that numerous tasks within a certain kind of programs are quite prooedural. One may, for example, wish to perform an aotion conditionally, repeat a procedure until a certain oondition is satisfied or execute a seguence of actions a given number of times. To do this in Prolog, we usually use outs and repeat-fail loops. Unfortunatelly, improving efficienoy through these inechanisms often weakens the clarity and the conciseness of a Prolog program. Some Prolog implementations support condi- tional by default. Remember, for example, P -> Q t R from 0ECsystem-lO Prolog [2], C-Prolog [5] or Suintus Prolog [6]. Arity Prolog [7] even supports ifthen/2 and iftheneIse/3 predicates. Here are common definitions of these procedures: i-f_then(P,Q) if_then(P,Q). calKP), .', calKO). if_then_else(P,Q,R) t- calI(P>, if_then_else(P,Q,R) s-calKR). calKO). Using operators, we may simply define an appropriate predicate if/1 that is determined as a Principal funotor. Its definition includes both above alternativos (see implementation in the appendix). We may now code conditionals of both forms; The problem can be solved neater by using explicit definitions for the oontrol of execution. Including Pascal-like construots into Prolog may be seen as a practical solution to this problem. 2f P then Q. if P then O eise R. where P, Q and R denote single Prolog goals or sequences (i.e. conjunctions or disjunctions) of goals. V=. 28 CAS£ PROCEDURE REPEAT-umiL PROCEDURE Onoe the if predicate has been implemented, we may define čase as a seguenoe of if procedures trying to matoh a certain condition value and to sati8fy related goal(s), We introduce the Prolog čase construct čase X o-f C XlsQl, X7sQ2, ..., XnsQn J. that is interpreted as if X = XI then Ql else if X = X2 then Q2 else if Here we introduce the repeat/l predicate that will have similar effect as the built-in repeat/0. You must keep in mind that proposed repeat O until P. is just a Pascal-like notation for the procedure that will be executed in the Prolog sense, i.e. failure of O will result in backtracking. The execution may be viewed as Pascal-liko when backtracking is not possible, for exainple: repeat ( urite< 'Filename? '), read(F) ) until exists, name( Ansuer, CASCIIJ ). In order to implement the far loop, let us first introduce a utility for managing global counters [4]. Suppose we have certain valuee in our program that can be passed to or modified by any part of the program. Modifying these values may be understood as assigning values to global variables in procedural languages. Suoh variables are particularly suitable as counters. Each counter Is spooified by its name, a key, and the related integer value. The counter managing predicates below siiBply use retract and assert to assure the current value of a counter to be recorded in the database. Based on previously defined repeat procedure and the global counters, the for loop has the following two forms: for CCount,IJ for CCoant,11 II to 12 do a. II donnto 12 do Q. beep s- put<7>. Count identifies the procedure and should be instantiated to a Prolog constant. Further- more, the global counter with the key Count Is activated when executing the procedure. Its current value is instantiated to I. The following example illustrates hov to use the loop: for Ci,l3 f i to 10 do ( nrite(l), nI ). 29 figain, the exeoution is Pascal-like only when the goal(s) appearinfl in tho loop can be satisfied in no more than ono way. REFERENCES [1] Bratko I.: Prolog Programming for Arti- fioial'Intelligence, Addison-Wesley, 1986 CONCLUSION The papar presentB the implementation of some Pascal-like constructs in Prolog. We found them useful for vriting procedural segments of programs, such as input and output procedures. As already stated, our purpose is not to reduoe the Importanoo of standard Prolog concepts, but just to add some convenient procedural foaturos. In our opinion, combination of both declarative and procedural approaches is the right solution • when vondering about hov to code a oomplex task offeotively. And finally, the, reador might have noticod that we said nothing about the repeat procedure when discussing Pascal-like featuros. The reader is invited to imploment it himself as an exercise. [2] Byrd L., Pereira F., Harren D.: A Guide to Version 3 of DEC-10 Prolog, Department of Artificial Intelligence, University of Edinburgh, 1883 [3] Clocksin H.F., Mellish C.S.: Programming in Prolog, Springer-Verlag, 1984 [4] Filipie B., Mozetie I.: A Library of Prolog Utilities, Keport IJS DP-4466, JoZef Štefan Institute, Ljubljana, 1986 [5] Pereira F.: C-Prolog Oser's Manual, Univorsity of Edinburgh, Department of Computer Aided Architectural Design, 1984 [6] Quintus Prolog User's Guide and Reference Manual, Quintus Computer Systems Inc., Palo Alto, 1986 [7] The Arity/Prclog Programming Languago, Arity Corporation, Conoord, 1986 APPENDIX: THE PROGRAM % file PROCED s % Implementing Pascal-like constructs X , .-- op( 900, fx, if ). }- op( 850,. xfx, then ). s- op< 800, xfx, else ). t- op( 900, fx, čase >. t- op( 850, xfx, of J. r- op<'800, xfx, otherMJse ), s- op( 750, xfx, 's' >. s- op( 900, fx, repeat ). ; »- op( 850, xix, until ). s- op( 900, fx, -for ). z- op( 850, xfx', to ). i— op( 850, x-fx, donnto ). s- op( 800, xfx, i3o }. s— op( 750, x1x, 'f' }. CcountsJ, X Counter managemertt. i1 P then Q :~ if_then_else( Q - (R else S), if_ttien_else(P,R,S) , if_then(P,Q) ) . % 11 'else' found Z then if_then_else Z else if then. if_then. if_then_else(P,Q,_> s- calKP), .', calKO). if_then_else(_,_,R) s- calKR). čase X of C XntO othertiise R J s- if X=Xn then Q else R, .'. čase X of C XniQ ] s- 12, .'. for CCount,I]s=Il to 12 do Q :- ctr _set(Count,11), repeat C ctr_inc until 1=12, ctr_remove(Count), /. for C_,_Jt=ll doNnto 12 do _ s~ IK12, /. for CCount,IJs = Il donnto 12 do Q i- ctr_set(Count,Il>, repeat ( ctr_dec(Count,1}, Q ) until 1=12, ctr_reaove(Count>, /. O 30 2 File COUHTS i Z Hanaging global countsrs % Z ctr_set/2 sets a counter to the X desired nuaber. Z ctr_dec/2 decrements a counter ond Z roturns its pravious value. ctr_dec(KeY,a} i- retract( countor(KBy,H} ), m is H - 1, assertC counter(Koy,HI J ), /. ctr_set(Hey,h> s- integar (M) , ctr_raoovo(Key)p assort( counter (Koy,H) ), .'. Z ctr_inc/2 increaents a counter and Z returns its pravious valuo. ctr_inc(KBy,N) M- ratractf countor(Koy,M} ), Ml is » * 1, assartf countor(KaY^MI> )f f> % ctr_is/2 returns the current vaiua Z of a counter. ctr_is(Key,H) t- counter (Key,ti) , /. Z ctr_remova/t resovoa a counter Z froE^ tho databaso, ctr _rBaove(Key) t — retract( counter(Key,_) ), fail. ctr reBove( > .