Calling Python from Prolog
Updated 08 Oct 2002
Upper levels:
Amzi Python Interface 0.2...
4. Manual

 4.5. Calling Python from Prolog

 [ Prev - Up - Next ] 

The interface defines two extended predicates, python/2 and python/1, the former if you want to collect the output, the latter if you don't.

From Test/test.py, for example:

arithm(X,Y) :-
  python(byfive(X),Z),
  Y is Z+1.
Argument type conversions are performed as follows:
PythonProlog
string string
integer integer
list list
tuple list
AmziTerm Amzi TERM
other ADDR
Note that in these conversions, Prolog terms are neither produced nor recognized, and Python tuples convert to lists. This is because terms are not a native Python type. However AmziTerms can be passed from Python to Prolog and back without modification.

The merger of the list and tuple types into lists might be seen as a problem, but Python functions are usually reasonably consistent in intended interpretation of what they return, as to whether a tuple is supposed to be a unitary value that something is going with, or a way of returning multiple results from a process (such as quotient and remainder from division). So I think it will be OK.

Calls like that of byfive require the function called to be defined in the main module of the program. It is possible to call functions in other modules by separating the module name from the function name with #, and enclosing the whole thing in quotes, e.g. 'string#split'. But if you don't also import the module in Python, there's usually a crash when the program closes, so it's probably better not to use this until I've learned whatever it is that I don't know about calling modules from C!

Errors from undefined functions and modules also aren't properly caught (standard Prolog behavior, where undefined stuff causes silent failure), except for a few where I'm experimenting with ways to do it.


 [ Prev - Top - Next ]