Prolog Web Services

Prolog Programming Across The Internet Made Easy

All you need to realize a Web Service in Prolog is to use the following HTTP versions of the all-solutions predicates.

http_call(+URL:Goal)

Invoke Goal as a goal on the web services located at URL, which must be a PSP page. Note that making messages traveling back and forth between client and server machines would be highly inefficient. Therefore, http_call/1 determines all solutions (see http_findall/3 below) to Goal and then backtracks successively.

http_findall(+Template, +URL:Goal, -Bag)

The http_findall/3 predicate creates a list of all instantiations, as defined in Template, which satisfy Goal on successive backtracking, and unifies the result with Bag. If there are no solutions to Goal, then Bag is unified with the empty list []. The recipient HTTP address of the call is specified using module qualification. The following example illustrates how to send and receive data using PSP (assuming that the address where the web service resides is http://localhost/ws.psp):

% Note that URL appears as the leftmost term in module qualification
% Parentheses could have been omitted as (:)/2 is left-associative

my_webservice(Bag) :-
http_findall(X, 'http://localhost/ws.psp':(lists:member(X, [1,2,2,3])), Bag).

% Bag = [1,2,2,3]

http_bagof(+Template, +URL:Goal, -Bag)

The http_bagof/3 predicate unifies Bag with all alternatives of Template. The construct +Var^Goal can be used to neglect binding of Var in Goal. Like the built-in bagof/3 Prolog predicate, http_bagof/3 fails if Goal has no solutions. The behavior of http_bagof/3 together with the use of the ^ operator is illustrated in the following example:

my_webservice(Bag) :-
http_bagof(
    Y,
    'http://localhost/ws.psp':(X^(member(X, [1,2,2,3]), Y is 2*X)),
    Bag   ).

% Bag = [2,4,4,6] (binding of X has been ignored)

http_setof(+Template, +URL:Goal, -Bag)

The http_setof/3 predicate is equivalent to http_bagof/3, but Bag contains no duplicates and is sorted using the sort/2 Prolog predicate. Consider the following example:

my_webservice(Bag) :-
http_setof(
    Y,
    'http://localhost/ws.psp':(X^(member(X, [1,2,2,3]), Y is 2*X)),
    Bag
  ).

% Bag = [2,4,6] (duplicate 4 has been removed)
Full compliance with SWI Prolog HTTP server infrastructure

PSP version 0.4.1 is developed under SWI Prolog version 7.3 (http://www.swi-prolog.org/) and complies with the handler definition of the SWI Prolog HTTP library (i.e. the http_dispatch library), according to the directive:

:- http_handler(root(.), psp_handler, [prefix]).

As standard HTTP library does not support CGI-based server, minor changes were necessary to maintain functionality (e.g., for session management), which however are completely transparent to the end-user. This means that no extra predicates are defined and users can refer to the existing documentation sources. Therefore, there is native support for all standard HTTP library predicates (for details, please see the SWI Prolog HTTP server package documentation at http://www.swi-prolog.org/pldoc/package/http.html).

Continue reading with Prolog Server Pages
Last update: Sun Oct 25 16:46:47 2015