How to use Prolog with PHP?

2019-08-28 22:32发布

问题:

I want to use Prolog with PHP. Is it possible?

回答1:

There are always the exec-familiy functions to execute/spawn another process.



回答2:

As suggested above, you can execute a Prolog interpreter or binary. However, most Prolog implementations also export a C API that can be used to call the Prolog interpreter.

You could create a small PHP module to start an interpreter and execute queries. For instance, the SICStus documentation describes using Prolog from C in detail:

  • 6 Mixing C/C++ and Prolog


回答3:

Most Prologs allow for prolog code to be compiled into a binary. You could try calling this binary from within PHP using some kind of process call as already mentioned.



回答4:

I wrote a translator that can convert some simple PHP programs into Prolog and vice-versa.

This is a possible input program:

function is_greater_than($a,$b){
    return $a > $b;
}
function is_an_animal($a){
    return in_array($a,["dog","cat"]);
}

...and this is the output program:

is_greater_than(A,B) :- 
    A>B. 
is_an_animal(A) :- 
    member(A,["dog","cat"]).

This translator is only a proof-of-concept, but it may still make it easier to convert some simple PHP programs into Prolog.



标签: php prolog