Prev Next Up Home Keys Figs Search New

C++ Interface to Prolog

Appeared in Volume 7/1, February 1994

Keywords: C++.

mlevy@csr.uvic.ca
Michael Levy
27th August 1993

Nigel Horspool and I have designed a high-level C++ interface to Prolog as part of the TOPIC project. It is described in a paper entitled "Translator Based Multi-Paradigm Programming" which is about to appear in the Journal of Systems and Software.

It allows you to write C++ code like this:

Variable X,Y,Z;
Goal g(append(X,Y,L) & cut() & append(Y,Z,L2)).
In TOPIC you get the answers by using the method "next":

while(g.next()){
 	cout << "X=" << g.Eval(X) << "\n";
 }
My C++ is a bit rusty, so forgive any C++ errors in the above example. There are more examples in the paper.

litsios@iis.ee.ethz.ch
James Litsios
28th August 1993 (revised 22th Sept. 1992)

I have implemented a Prolog type inference engine in C++ and the resulting code looks very much like the previous post on the TOPIC project. Although I would still consider my code prototypal, my aim is to have a viable alternative to using Prolog. The main reason being portability because the main code in my programs is numerical (C++ and FORTRAN).

I use reference counting for garbage collection. My current code suffers from a large copy overhead at each recursive call of a rule but I am slowly working on removing this limitation. When this is done is will consider making the code public.

A typical test program is given below (_k implies that the type is a class):

 main(){
 	LOGIC_k_atom a("a"),b("b"),c("c"),d("d"),nl("\n");
 	LOGIC_k_variable X,Y,Z,ZZ;
 	LOGIC_k x = L(a,b); // make a list
 	LOGIC_k y = L(c,d);
 	print(x); print(nl); print(y); print(nl);
 	for(LOGIC_k_rule ap3 = LOGIC_append(x, y, X) 
                           && LOGIC_pick(Y, X, Z);
 		ap3.active();
 		ap3.next()){
 			print(Y); print(nl); print(Z); print(nl);
 	}
 }
I had originally overloaded the comma operator to create lists by writing them as (a,b,c) but this can cause tricky programming errors so I now use a list creation function L.

Prev Next Up Home Keys Figs Search New