History on the implementation and compilation of Prolog

History on the implementation and compilation of Prolog Zhenjie Chen December 4, 2012 Abstract Prolog, abbreviation for PROgrammation en LOGique, was ...
Author: Ashlynn Carroll
11 downloads 1 Views 91KB Size
History on the implementation and compilation of Prolog Zhenjie Chen December 4, 2012 Abstract Prolog, abbreviation for PROgrammation en LOGique, was designed in 1972 by Philippe Roussel, Alain Colmerauer at the University of AixMarseille and Robert Kowalski at the University of Edinburgh [1]. Since 1972, Prolog has been one of the most popular logical programming language based on Horn clause logic. Intrigued by the interesting history of Prolog, I decided to learn the nature of the design and implementation of Prolog interpreter/compiler. Warren Abstract Machine (WAM) has become the de facto standard for designing Prolog system since 1983 when it was first invented by Warren. GNU Prolog is a mordern free Prolog system based on WAM and is the first Prolog system which really complicant to ISO starndard for Prolog by 2000. Besides GNU Prolog, there have been lots of other free and comercial Prolog systems since the brith of Prolog in 1972.

1

Introduction

Prolog, abbreviation for PROgrammation en LOGique, was born of a project aimed not at producing a programming language but at processing natural languages, ie. French. The project gave rise to a preliminary version of Prolog at the end of 1971 and a more definitive version at the end of 1972 [2]. Back in 1970, Robert Pasero and Philippe had been invited by Alain who was leading the automatic translation project, TAUM (Traduction Automatique de l’Universit´ e de Montr´ eal). They familiarized themselves with the natural languages processing and wrote several nondeterministic context-free analyzers in Algol 60 and a French paraphrase generator using Q-systems, the programming language developed by Alain for the automatic translation project. Meanwhile, a doctoral student of Alain’s, Jean Trudel had chosen to work on automated theorem-proving based on Alan Robinson’s article [3]. Jean was really the person who filled the gap between Robinson’s paper and the automatic translation project and Robert Pasero is the person who was always involed on the natural language processing side of Prolog. In June 1971, they invited Robert Kowalski from the University of Edinburgh, one of the inventors of SL-resolution, to visit them for a week. Robert 1

Kwalski turned out to be the direct contributor of the theoretical framework on which Prolog is founded. During the fall of 1972, the first Prolog system was implemented by Philippe in Niklaus Wirt’s language Algol-W. For those who are interested on the early history of Prolog and people who directly or indirectly contributed to Prolog, please refer to [2]. The rest of this paper is organized as follows. Section 2 introduces the de facto standard execution model for Prolog: Warren Abstract Machine (WAM), the extension of WAM such as clp(FD) and other execution models. Section 3 describes a modern Prolog system based on WAM, GNU Prolog, including the design and implementation as well as an example in details. The remainder of this paper will briefly describe some other Prolog systems since the birth of Prolog in 1972, including free and commercial Prolog systems.

2

WAM: Warren Abstract Machine

2.1

Before WAM (1983)

Due to Boyer, Moore, and Bruynooghe’s work, all the Prolog systems are using either structure-sharing or structure-copying to represente compound terms. Early Prolog systems are mostlly structure-sharing while modern systems are mostly structure-copying. After the first implementation of Prolog system(interpreter) by Philippe in 1972, G´ erard Battani, Henri Meloni and Ren´ e Bazzoli written a structure-sharing interpreter in Fortran in 1973. Inspired by the idea of building a compiler for Prolog and the fact that existing Prolog interpreters are too slow which leads to users’ complain, David Warren was thinking about building a Prolog compiler for his Ph.D. dissertation topic. By 1977, Warren had developed the first Prolog compiler, DEC-10 Prolog [4]. There are several compiled systems that bridged the gap between the DEC10 compiler and the WAM before 1983, such as Prolog-X and NIP. The golden age of Prolog started after Warren developed the WAM in 1983, a structurecopying execution model for Prolog which has become the de facto standard implementation technique [5].

2.2

WAM (1983)

The Warren Abstract Machine(WAM) is very similar to the abstract machine based on Warren’s early work on Prolog compiler, DEC-10 Prolog [4]. It was modified to incorporate tail recursion optimization. The main difference are described as follows [5]: 1. Using structure-copying instead of structure-sharing for compound terms; 2. Choice points are seperated from environments and are created only when needed rather than at every procedure call;

2

3. Environments are trimmed during execution, by discarding variables no longer needed; 4. Potentially unsafe variables in the final goal of a clause are made global only if needed at runtime, rather than by default at compile time. The WAM defines a high level instruction set maping closely to Prolog source code which makes it straightforward to compile Prolog into WAM. The Prolog is considered to be a natural generalization of the imperative languages plus unification and backtracking. The WAM instructions has a close maping of these features: Sequntial control call/return/jump instructions unification get/put/unify instructions backtracking try/retry/trust instructions optimizations minimize the useage of memory

2.3

Extend WAM for new logic language

There are several significant extension of WAM for new logic languages, such as CHIP(Constraint Handling In Prolog), clp(FD) and SLG WAM. clp(FD) is a Constraint Logic Programming language over Finite Domains built by Daniel Diaz and Philippe Codognet at INRIA [6]. The GNU Prolog (section 3) is based on two compilers, clp(FD) and wamcc(a compiler translating Prolog to C), both of which are no longer maintained 1 .

2.4

New execution models

Though WAM is the de facto standard execution model for Prolog system, even in modern systems like GNU Prolog which will be covered in section 3 , there are several radically different execution models. The Vienna Abstract Machine (VAM) is based on partial evaluation of each call while the BinProlog system is based on the explicit passing of success continuations [7]. 2.4.1

VAM: Vienna Abstract Machine

The Vienna Abstract Machine (VAM) is a Prolog machine developed at the TU Wien [8]. Different from WAM, an inference in VAM is performed by unifying the goal and head immediately, instead of bypassing arguments through a register interface. The VAM instructions can be understood only by their combination at runtime which is different from WAM instructions. There are two implementations for VAM: the V AM1p and V AM2p . The difference is in how the argument traversal is done. Two pointers are used in V AM2p , one for the goal code and one for the head code. Therefore, V AM2p is well 1 http://cri-dist.univ-paris1.fr/diaz/

3

suited for intermediate code emulator while V AM1p is designed for native code compilation. 2.4.2

BinProlog

BinProlog has been developed by Paul Tarau mostly at the University of Moncton, Canada, and is based on his BinWAM abstract machine, a specialization of the WAM for the efficient execution of binary logic programs [9], i.e., a simplified subset of the WAM. It has two key ideas: transforming clauses to binary clauses and passing success continuations [7].

3

GNU Prolog

3.1

Introduction

GNU Prolog is a free Prolog compiler supported by the GNU organization. The development of GNU Prolog started in January 1996 under the name Calypso. It released as a GNU product, GNU Prolog, in April 1999 after a discussion with GNU organization in late 1998 [10]. It is based on two compilers, wamcc and clp(FD), by Daniel Diaz and Philippe Codognet. Wamcc can translate Prolog to C and also support translating a WAM branching into a native code jump, more details are here [11]. In contract to wamcc, the GNU Prolog compilation scheme is to translate a WAM file into a mini-assembly (MA) file. The MA has been specifically designed for GNU Prolog and used as a machine-independent intermediate language. GNU Prolog supports floating point numbers, streams, dynamic code, DCG, operating system interface,socket, a Prolog debugger, etc. and is the only free Prolog system really compliant to ISO standard for Prolog by 2000. It also includes a very efficient constraint solver over finite arithmetic constraints, boolean constraints, symbolic constraints, etc. The solver is highly extensible by the user [10].

3.2

Design and implementation

The traditional compilation of a programming language is to decompose the compilation process into several steps. For example, Prolog might be compiled into WAM, then translated to C using wamcc, and compile the generated C code by Gcc. Compilation of Prolog to the WAM is a de facto standard and well-known process. There are three classical ways to process the WAM code: 1. Executing the WAM code with an emulator written in C; like in SICStus Prolog; 2. Assemble the code into executable file; like in Quintus Prolog; 3. Compiling to native code; like in Prolog by BIM, latest version of SICStus Prolog or Aquarius Prolog. 4

GNU Prolog uses a different compilation scheme to process the WAM code: translate the WAM file into a mini-assembly (MA) language which is machineindependent. The independent MA code is then mapped to the assembly language of the target machine. The MA language is simple and based only on 11 instructions most of which handle the control of Prolog and the invocation of C functions [10].

3.3

Understanding mini-assembly language: MA

Mini-assemble language is designed as a machine independent language for WAM code to be translated in. The MA only contains 11 instructions [12] which makes it trivial to translate from MA code to target machine code. They are listed on table 1, details of these instructions can be found in Diza, Daniel and Codognet, Philippe’s paper [12]. pl jump pl label jump label move ret target

pl call pl label call c f ct name(arg, ...) c ret

pl ret fail ret move reg1, reg2

pl fail jump ret

Table 1: Mini-assemble language instruction set The MA language is simple enough, however, we can still minimize the instruction set. The move instruction, used to perform a copy of WAM X or Y register, can be replaced by call c instruction. Unfortunately, the C function call is more costly compared to adding move instruction. A trade off must be made between performance and minimality of the instruction set. With the current MA instruction set, translation from WAM code to MA instructions can be performed in linear time w.r.t the size of the WAM file [12].

3.4

Example

In this subsection, we will compile a real Prolog program into WAM instructions and then WAM instructions codes to MA instruction using GNU Prolog, or more specificly pl2wam and wam2ma, and analysis what happend in these processes. Note that all these compilation are without optimization. Here is the Prolog program(this program can be rewrited and contains tail recursion which can greatly improve the performance) f ac.pl, 1 2

fac (0 , 1). f a c (N, R) :− N > 0 , N1 i s N−1 , f a c (N1 , R1 ) , R i s N∗R1 .

3.4.1

Prolog to WAM

To compile the Prolog program to WAM instructions, simply run, gplc -W –noreorder –no-reg-opt fac.pl. The compiler will generate a file named f ac.wam,

5

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

% % % %

compiler : file : date : t im e :

GNU P r o l o g 1 . 3 . 0 ( Jan 4 2007) fac . pl 12 3 2012 15:44:56

file name ( ’ / nfs / student /z/ zhenjie / fac . pl ’ ) . p r e d i c a t e ( f a c /2 ,1 , s t a t i c , private , user , [ try me else (1) , get integer (0 ,0) , get integer (1 ,1) , proceed , label (1) , trust me else fail , allocate (3) , get variable (y(0) ,0) , get variable (y(1) ,1) , put atom ( > , 0 ) , put integer (2 ,1) , c a l l c ( ’ Set Bip Name 2 ’ , [ ] , [ x ( 0 ) , x ( 1 ) ] ) , math load value (y (0) ,0) , put integer (0 ,1) , c a l l c ( ’ Blt Gt ’ , [ f a s t c a l l , boolean ] , [ x ( 0 ) , x ( 1 ) ] ) , put atom ( i s , 0 ) , put integer (2 ,1) , c a l l c ( ’ Set Bip Name 2 ’ , [ ] , [ x ( 0 ) , x ( 1 ) ] ) , math load value (y (0) ,0) , c a l l c ( ’ Fct Dec ’ , [ f a s t c a l l , x ( 0 ) ] , [ x ( 0 ) ] ) , get variable (x(1) ,0) , put value (x (1) ,0) , put variable (y (2) ,1) , c a l l ( fac /2) , put atom ( i s , 0 ) , put integer (2 ,1) , c a l l c ( ’ Set Bip Name 2 ’ , [ ] , [ x ( 0 ) , x ( 1 ) ] ) , math load value (y (0) ,0) , math load value (y (2) ,1) , c a l l c ( ’ Fct Mul ’ , [ f a s t c a l l , x ( 0 ) ] , [ x ( 0 ) , x ( 1 ) ] ) , get value (y (1) ,0) , deallocate , proceed ] ) .

In additional to the original 33 WAM instructions, some extensions have been made. The call c instruction is used to call a C function to do the comparision(1921), multiplication(37-39), evaluation(25-26, 34-35), etc. The WAM instructions is acutally a Prolog list with labels and jumps with declaration such as static, private, user and file name. WAM instructions are good for optimization, however, it depends on the original Prolog program. The example presented here is not a tail recursion code which means gplc can not perform tail recursion 6

optimization. 3.4.2

WAM to MA

By running gplc -M fac.pl, we can get a MA file named fac.ma as follows, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

p l c o d e g l o b a l X666163 2 call c C r e a t e C h o i c e P o i n t 2 (& Lpred1 1 ) call c G e t I n t e g e r T a g g e d ( 7 ,X( 0 ) ) fail ret call c G e t I n t e g e r T a g g e d ( 1 5 ,X( 1 ) ) fail ret pl ret Lpred 1 1 : call c Delete Choice Point2 () call c Allocate (3) move X( 0 ) ,Y( 0 ) move X( 1 ) ,Y( 1 ) call c Put Atom Tagged ( t a ( 0 ) ) move ret X( 0 ) call c Put Integer Tagged (23) move ret X( 1 ) call c Set Bip Name 2 (X( 0 ) ,X( 1 ) ) call c Math Load Value (Y(0) ,&X( 0 ) ) call c Put Integer Tagged (7) move ret X( 1 ) call c B l t G t (X( 0 ) ,X( 1 ) ) fail ret call c Put Atom Tagged ( t a ( 1 ) ) move ret X( 0 ) . . . . . . more MA i n s t r u c t i o n s

Limited by the size of this paper, we only list top 25 lines of MA instructions. As we can see in these two files, fac.wam and fac.ma, the translation from WAM instruction to MA instruction is straitforward. Table 2 shows some of the translation between WAM instructions and MA instructions. It justifies the statement that translation from WAM code to MA instructions can be performed in linear time w.r.t the size fo WAM file [12].

3.5

Miscellaneous

To complete the compiling process, we should map the MA instructions to a traget machine code. It has been proved that the mapping from MA instructions to native assembly code is executed in linear time w.r.t the size of the MA file. This is important since the original output of wamcc can potencially exceed the size limit for the input of gcc. With this new abstract model, we can finally decrease the compilation time for Prolog.

7

try me else(1) get integer(0, 0) put integer(0, 1) proceed label(1) trust me else fail allocate(3) get variable(y(0), 0) get value(y(1), 0) call c(’Set Bip Name 2’, [], [x(0), x(1)]) deallocate

call c Create Choice Point2(&Lpred1 1) call c Get Integer Tagged(7, X(1)) fail ret call c Put Integer Tagged(7) move ret X(1) pl ret Lpred1 1 call c Delete Choice Point2() call c Allocate(3) move X(0), Y(0) call c Unify(Y(1), X(0)) call c Set Bip Name 2(X(0), X(1)) call c Deallocate()

Table 2: Relation between WAM instruction and MA instruction

4

Other systems

According to the Resource Guide avaliable in http://www.cs.cmu.edu/Groups/ AI/html/faqs/lang/prolog/prg/part2/faq.html, there are more than 80 Prolog systems by 1997 2 . Most of the Prolog systems are sequencial and free of access. Parallel Prolog systems are not required or popular until much more powerful computer systems have been designed. Most of the free Prolog implementations are available from the CMU AI Repository, in ftp://ftp.cs.cmu.edu:/user/ai/lang/prolog/ impl/.

5

Conclusion

To summarize, Prolog is the first Logic Programming Language and one of the most popular Logic Programming Language. Since its born on the begining of 70s, researcher have been working on designing the implementation model, optimization method, concurrency, finite domain constraints, higer-order logic support, etc. The WAM invented by Warren at 1983 has become the de facto standard for implementating Prolog sysmtem since then. Based on the WAM abstract model, GNU Prolog contains an intermediate level abstract(MA), which is machine independent, between WAM and target machine. Thus GNU Prolog changes the traditional compile scheme for Prolog program by indroduction this new abstract model, MA. There are so many topics to be covered that we can only present several of them. Other topics such as optimization for mapping instructions, optimization for register allocation, parallelism, higher order logic, nonmonotonic reseasoning(negation as failure support), etc. cannot be covered in this paper. It is obvious that the modern Prolog systems, most of which 2 The

Resource Guide has not been updated since 1997.

8

are still based on the WAM, are more focus on the functionality, such as concurrency3 , object-orientation, web support, etc. rather than the basic design. One reason may be the WAM model is good enough since it supports lots of optimization the other reason may be the functionality of this Prolog system are more important then redesign the basic implementation technology of compiling Prolog program.

Acknowledgment I would like to thank the instructor of An Introduction to Artificial Intelligence at UNM, Prof. Lydia Tapia, who encouraged me on the topic and gave me usefull suggestions on the outline of this paper. Thanks also extended to the authors of the book AI Algorithms, Data Structures and Idioms in Prolog, Lisp, and Java, especially Prof. George F. Luger for his clear explaination of Prolog programming in the class as well as in the book.

References [1] A. Colmerauer, “The birth of prolog,” in III, CACM Vol.33, No7, 1993, pp. 37–52. [2] A. Colmerauer and P. Roussel, “History of programming languages—ii,” T. J. Bergin, Jr. and R. G. Gibson, Jr., Eds. New York, NY, USA: ACM, 1996, ch. The birth of Prolog, pp. 331–367. [Online]. Available: http://doi.acm.org/10.1145/234286.1057820 [3] J. A. Robinson, “A machine-oriented logic based on the resolution principle,” J. ACM, vol. 12, no. 1, pp. 23–41, Jan. 1965. [Online]. Available: http://doi.acm.org/10.1145/321250.321253 [4] D. Warren, Applied Logic-its use and implementation as programming tool. Stanford Research Institute, 1983. [5] D. Warren and A. Center, An abstract Prolog instruction set. national, 1983, vol. 309.

SRI Inter-

[6] D. Diaz and P. Codognet, “A minimal extension of the wam for clp (fd),” in Proceedings of the Tenth International Conference on Logic Programming, 1993, pp. 774–790. [7] P. V. Roy, “1983–1993: The wonder years of sequential prolog implementation,” 1993. [8] A. Krall and U. Neumerkel, “The vienna abstract machine,” in Programming Language Implementation and Logic Programming. Springer, 1990, pp. 121–135. 3 Prolog-MPI

for SWI-Prolog.

9

[9] P. Tarau, “BinProlog 7.0 Professional Edition User Guide,” 1998. [Online]. Available: http://europepmc.org/abstract/CIT/71678 [10] D. Diaz and P. Codognet, “Gnu prolog: beyond compiling prolog to c,” Practical Aspects of Declarative Languages, pp. 81–92, 2000. [11] P. Codognet, D. Diaz et al., “wamcc: Compiling prolog to c,” in 12th International Conference on Logic Programming, Tokyo, Japan, MIT Press, 1995. [12] D. Diaz, P. Codognet et al., “Design and implementation of the gnu prolog system,” Journal of Functional and Logic Programming, vol. 6, no. 2001, p. 542, 2001.

10

Suggest Documents