Since I cannot stop nagging people about Web Prolog, I’d like to take this opportunity to show how the program above can be written in it:
main :-
pengine_spawn(Pid, [
node('http://localhost:3060'),
src_text("
q(X) :- p(X).
p(a). p(b). p(c).
")
]),
pengine_ask(Pid, q(_X)),
collect_and_print(Pid).
collect_and_print(Pid) :-
receive({
success(Pid, [X], false) ->
writeln(X);
success(Pid, [X], true) ->
writeln(X),
pengine_next(Pid),
collect_and_print(Pid)
}).
Now, let’s compare this program with the program using library(pengines)
:
-
Web Prolog’s Erlang-ish style is much easier to understand (and especially easy for Erlangers).
-
Using library(pengines)
, one has to wait for a create
message before doing anything else. In Web Prolog, pengine_spawn/1-2
doesn’t return until the Pid has a value. Just like in Erlang.
-
In the Web Prolog program, we are checking that the success
messages really are coming from the actor named Pid. (After all, in another context, they could have come from anywhere.) This is not done in the program using library(pengines)
. (It can be done, but it isn’t. The handler would need one more argument.)
-
Suppose that instead of printing the value of each X, we want to collect them in a list. This would be a bit tricky using library(pengines)
, but is easy using Web Prolog:
main(List) :-
pengine_spawn(Pid, [
node('http://localhost:3060'),
src_text("
q(X) :- p(X).
p(a). p(b). p(c).
")
]),
pengine_ask(Pid, q(_X)),
collect_in_list(Pid, List).
collect_in_list(Pid, List) :-
receive({
success(Pid, [X], false) ->
List = [X];
success(Pid, [X], true) ->
List = [X|Rest],
pengine_next(Pid),
collect_in_list(Pid, Rest)
}).
It runs like this in the PoC demonstrator:
Welcome to SWI Web Prolog!
?- main(List).
List = [q(a),q(b),q(c)].
?-
- There are other problems with
library(pengines)
, but nothing more (I think) that shows up in this example.
Ergo, due to DNA from Erlang, Web Prolog has a much better design. I’m allowed to say this, because I designed library(pengines)
. Well, Jan helped too, but his design ideas were good, mine were not, and I feel bad about them. Naturally, I want you all to have the best!
Of course, Web Prolog is under development, and the PoC demonstrator is not secure, so if you need something that is stable and secure, you have to stay with library(pengines)
.