-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling Ciao Prolog from Elixir #96
Comments
I am sandboxing by starting an engine and using stdin/stdout. Maybe this is enough for an initial version of our library. I am curious about the way the promt "?- " since I cannot read it from the stdout of the engine. Is the prompt emited through the stdout, is it directly emited in the tty, do you have other approach? |
Hi @aherranz! Yes, starting a new Ciao process and interacting via stdin/stdout is a good approach for simple query/answer solutions. Some example from bash: $ query='append([1,2],[3,4],_X),writeq(_X).'; answer=$(echo "$query" | ciaosh -q); printf "query:%s answer:%s\n" "$query" "$answer"
query:append([1,2],[3,4],_X),writeq(_X). answer:[1,2,3,4] Obviously this is really weak (see that we use More or less, this is the approach that was taken in the Ciao Java interface, but using a more involved communication protocol. We did an experimental port of the same approach for a Python interface and it works fine, if serializing data for each query is not an issue. Another approach would be a more tight interaction using the C interface (useful if you can preserve references to previously created terms and do a lot of queries). I have no experience with Erlang/Elixir but I guess that it'd possible to interface Elixir->C->Prolog in this way. I'd take this approach only when performance becomes a problem. Please let us know if the server approach (using stdin/stdout) fits your application and we can send you some pointers to similar code in other interfaces. |
Thank you! As a prototype we will follow the most simple approach for us: we will start a Ciao engine from Elixir and we will feed it with queries and we will parse the answers directly reading and parsing the stdout of the engine. I'll keep you informed. |
Let me show you our first session, I am using the low level functions that will not be public in the first protype. It seems that the stdin/out approach is promising: iex(1)> alias Prolixir.Engine
Prolixir.Engine
iex(2)> engine = Engine.start "ciao", nil
%Porcelain.Process{pid: #PID<0.189.0>, out: {:send, #PID<0.188.0>}, err: :out}
iex(3)> Engine.read engine
{:ok, :out, "Ciao 1.23.0 [LINUXx86_64]\n"}
iex(4)> Engine.read engine
{:error, :empty}
iex(5)> Engine.write engine, "append(A,B,[1,2,3])"
{:input, "append(A,B,[1,2,3])"}
iex(6)> Engine.read engine
{:error, :empty}
iex(7)> Engine.write engine, ".\n"
{:input, ".\n"}
iex(8)> Engine.read engine
{:ok, :out, "\nA = [],\nB = [1,2,3] ? "}
iex(9)> Engine.write engine, ";\n"
{:input, ";\n"}
iex(10)> Engine.read engine
{:ok, :out, "\nA = [1],\nB = [2,3] ? "}
iex(11)> Engine.read engine
{:error, :empty}
iex(12)> Engine.write engine, ";\n"
{:input, ";\n"}
iex(13)> Engine.read engine
{:ok, :out, "\nA = [1,2],\nB = "}
iex(14)> Engine.write engine, ";\n"
{:input, ";\n"}
iex(15)> Engine.read engine
{:ok, :out, "[3] ? "}
iex(16)> Engine.write engine, ";\n"
{:input, ";\n"}
iex(17)> Engine.read engine
{:ok, :out, "\nA = [1,2,3],\nB = [] ? "}
iex(18)> Engine.read engine
{:ok, :out, "\nno"}
iex(19)> Engine.read engine
{:error, :empty}
iex(20)> Engine.write engine, "halt.\n"
{:input, "halt.\n"}
iex(21)> Engine.read engine
{:ok, :out, "\n"}
iex(22)> Engine.read engine
{:error, %Porcelain.Result{status: 0, out: {:send, #PID<0.188.0>}, err: :out}}
iex(23)> |
Dear all we are planning to implement an Elixir library to interface Ciao Prolog programs. We need to load a Prolog/CLP program and (lazily) get every solution (including variable substitutions and/or constraints on them).
The text was updated successfully, but these errors were encountered: