Prolog Server Pages

Server-Side Prolog Scripting

Prolog Server Pages (PSP) is a server-side scripting language based on Prolog. In the current implementation is released for usage under Windows 10/8/7/Vista/XP only. PSP is added to the handler mappings of the Microsoft Internet Information Services (IIS) webserver as a Common Gateway Interface (CGI) module. Support for operating systems other than Windows and webservers other than IIS as well as utilization of FastCGI instead of CGI are all planned enhancements for future releases. Nonetheless, the software can be recompiled using the provided source code at needs. Support can be asked through the project website (http://www.prologwebservices.com/).

Simple and elegant Prolog-HTML integration

The current implementation provides a unique attribute named prolog that is valid for all HTML tags (Notice that PSP removes prolog attributes before sending output, thus the documents always remain HTML-valid). The underlying execution model is that the prolog goal contained as attribute to an HTML tag enwraps the HTML code contained within the tag and backtracks successively.

In addition to readability, another advantage is the incentive to put long pieces of Prolog code into separate files, which also facilitates code re-use. Let us assume, for example, that a standard Prolog file named database.pl contains the following code:

% database.pl
  :- dynamic p/1, q/1.

  p(a).
  p(b).
  p(c).
  p(d).
  q(c).

Now consider the following PSP page:

<!DOCTYPE html>
<html xmlns:psp="http://www.prologwebservices.org/psp">
<body psp:prolog="ensure_loaded( database.pl )">
  <div psp:prolog="p(X)">
  Now X is <b psp:prolog="write(X)"> </b><br>
  <span style="text-decoration: underline;" psp:prolog="q(X)">
    Do something when X=c here!<br>
    </span>
  </div>
</body>
</html>

The resulting code is more compact, elegant and easy to manage than chunk-based code. The generated output will be:

Now X is a
Now X is b
Now X is c
Do something when X=c here!
Now X is d

The above example also illustrated the fact that standard HTML tag attributes are untouched by the PSP parser. One limitation of the present syntax scheme is that sometimes it is necessary to use seemingly redundant HTML tags to incorporate the required prolog code, as we did above using span. However, as a side-effect this requirement helps maintaining appropriate context for the Prolog code. Importantly, variables are local to the HTML tag where Prolog code appears.

In order to perform actions before HTTP headers are sent, PSP requires that Prolog code is contained in the <html> tag. Below is an example to implement server-side redirection:

<!DOCTYPE html>
<html xmlns:psp="http://www.prologwebservices.org/psp" psp:prolog="
http_current_request(Request),
http_redirect(moved, 'http://www.prologwebservices.com/', Request)
">
</html>

Note that having a prolog attribute in the <html> tag is also the only way to have variables that are global to the entire page. The SWI Prolog HTTP library provides several predicates that can be used to inspect and manipulate HTTP headers.

Continue reading with Remote Procedure Call
Last update: Mon Nov 02 00:39:25 2015