Writing CGI Applications with Prolog
Using CGI is one, and a relatively straightforward, way to access Prolog programs via the web. Although (understandably) you probably have not heard much about Prolog programming for the web, those familiar with Prolog will be aware that there are problems (which may happen to involve a web based solution) for which Prolog is a good choice. I will here focus on creation of the Prolog (I am using SWI Prolog) files and how to collect header information from submitted HTML forms. The simple example program I will use takes two 3-arity tuples consisting of numbers, adds those numbers together for the first tuple and multiples those numbers together for the second tuple, then outputs the results. Here is a run-down of the Prolog code. You will have the opportunity to download this code at the bottom of the tutorial. We begin with the Prolog code for the core program code:
add_multiply(Tuple1, Tuple2, Result1, Result2) :-
Tuple1 = (X1,Y1,Z1),
Tuple2 = (X2,Y2,Z2),
add(X1,Y1,Z1, Result1),
multiply(X2,Y2,Z2, Result2).
add(X,Y,Z, Result) :- Result is X+Y+Z.
multiply(X,Y,Z, Result) :- Result is X*Y*Z.
As an example, a call to add_multiply((10,20,30),(2,3,4),Result1,Result2) would give Result1 = 60, Result2 = 24. Save this code in a file named 'addmultiply.pl'.
Before continuing with the Prolog code, we will create the HTML frontend, which calls the CGI Prolog script. In the case of this example, it will look something like this.
<html>
<head>
<title>CGI and Prolog</title>
</head>
<body>
<form name="frmCGIProlog" method="POST" action="http://yourdomain/cgi-bin/addmultiply">
<!-- It is important that form field names are in lowercase, becaue they will later be converted to Prolog terms -->
Add Arguments: <select name="addarguments">
<option>(22,65,89)</option>
<option>(56,43,32)</option>
<option>(123,98,76)</option>
</select><br />
Multiply Arguments: <select name="multiplyarguments">
<option>(3,4,5)</option>
<option>(12,11,34)</option>
<option>(31,44,55)</option>
</select><br />
<input type="submit" value="Submit Form" >
</form>
</body>
</html>
Next thing to do is add the Prolog code to process the CGI form information and output HTML. SWI Prolog comes with a neat little package aptly called called 'cgi'. So we include this package and add the Prolog code.
:- use_module(library('cgi')). %import the library
main :-
%collect GET/POST information
cgi_get_form(Arguments),
%return the value from 'add' form field
get_argument_value(Arguments, addarguments, AddArgument),
%important! values collected from forms are treated as strings. In this case, we want to convert the strings to Prolog terms.
string_to_term(AddArgument, AddArgumentAsTerm),
%do the same for 'multiply'
get_argument_value(Arguments, multiplyarguments, MultiplyArgument),
string_to_term(MultiplyArgument, MultiplyArgumentAsTerm),
add_multiply(AddArgumentAsTerm, MultiplyArgumentAsTerm, ResultAdd, ResultMultiply),
%Output HTML
write('Content-type: text/html\n\n'),
write('<html>\n'),
write('<head>\n'),
write('<title>Simple SWI-Prolog CGI script output</title>\n'),
write('</head>\n\n'),
write('<body>\n'),
write(ResultAdd),write('<br />\n'),
write(ResultMultiply),
write('</body>\n'),
write('</html>'),
halt.
%Prolog functions called by 'main'
add_multiply(Tuple1, Tuple2, Result1, Result2) :-
Tuple1 = (X1,Y1,Z1),
Tuple2 = (X2,Y2,Z2),
add(X1,Y1,Z1, Result1),
multiply(X2,Y2,Z2, Result2).
add(X,Y,Z, Result) :- Result is X+Y+Z.
multiply(X,Y,Z, Result) :- Result is X*Y*Z.
get_argument_value([], _, _) :- fail.
get_argument_value([Head|_], ArgumentName, ArgumentValue) :-
Head =.. [Name, Value],
Name = ArgumentName,
Value = ArgumentValue.
get_argument_value([Head|Tail], ArgumentName, ArgumentValue) :-
Head =.. [Name, _],
Name \= ArgumentName,
get_argument_value(Tail, ArgumentName, ArgumentValue).
string_to_term(String, Term) :-
string_to_atom(String, Atom),
atom_to_term(Atom, Term, []).
Once you have created the code, you can load it in Prolog to check that it is OK. If you get an error message along the lines of source_sink `library(cgi)' does not exist, then that means the required package does not exist. At the time of writing, I am using Prolog SWI-Prolog 5.6.30 under Linux Mandriva Free 2007 and the package files are located at '/usr/lib/pl-5.6.30/library'. My default installation of Prolog did not contain this file, so I uninstalled it and downloaded the latest stable release from the SWI Prolog site and re-installed it. Alternatively, you can download the library file 'cgi.pl' from the SWI Prolog online CVS browser. The documentation from this library can be found at http://www.swi-prolog.org/packages/clib.html. The package cgi.pl is quite minimal and actually calls an accompanying C library which does the work.
After writing the Prolog source code, it is time to compile it into a self-contained executable. This is possible with SWI Prolog, but is platform dependent. Go here to see how this can be achived for different platforms. For the Linux platform, go into the Prolog shell and to create a self-contained executable, type
?- qsave_program(addmultiply,[stand_alone(true),goal(main)]).
This will create an executable named 'addmultiply'. Once you have set the appropriate permissions, you can place it in the CGI-directory. To learn more about CGI, here are some useful links. Most CGI stuff is about Perl, but the basics apply.
- http://www.cgi101.com/book/
- http://www.lies.com/begperl/
- http://www.comp.leeds.ac.uk/Perl/Cgi/start.html
And there you go. Happy Prolog web programming!




