All predicatesShow sourcepwp.pl -- Prolog Well-formed Pages

PWP is an approach to server-side scripting using Prolog which is based on a simple key principle:

Especially when generating XML rather than HTML, this is such an obvious thing to do. We have many kinds of XML checking tools.

Having decided that the input should be well formed, that means NO NEW SYNTAX

None of the weird and horrible <% ... %> or whatever not-quite-XML stuff you see in other template systems, making checking so very hard (and therefore, making errors so distressingly common).

That in turns means that PWP "markup" must be based on special elements or special attributes. The fact that an XML parser must allow undeclared attributes on any element even when validating, but must not allow undeclared elements, suggests doing this through attributes. In particular, one should be able to take an existing DTD, such as an XHTML DTD, and just use that without modification. So the design reduces to

This description uses the following name space:

xmlns:pwp='http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl'

The attributes are

Here's what they mean. Each element is expanded in the context of a set of variable bindings. After expansion, if the tag is not mapped to '-', all attributes in the pwp: namespace are removed and the children elements are recursively expanded.

Examples:

  1. A "Hello World" like example
    <html
      xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl"
      pwp:ask = "ensure_loaded(msg), once(msg(Greeting))">
      <head>
        <title pwp:use="Greeting"/>
      </head>
      <body>
        <p><span pwp:use="Greeting" pwp:tag='-'/></p>
      </body>
    </html>

    where msg.pl contains

    msg('Hello, World!').

    This example illustrates an important point. Prolog Well-Formed Pages provide NO way to physically incorporate Prolog clauses into a page template. Prolog clauses must be put in separate files which can be checked by a Prolog syntax checker, compiler, cross-referencer, &c WITHOUT the Prolog tool in question needing to know anything whatsoever about PWP. You load the files using pwp:ask on the root element.

  2. Binding some variables and using them
    <html
      xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl">
      <head><title>Example 2</title></head>
      <body pwp:ask="Hello = 'Hello world', A = 20, B = 22">
        <h1 pwp:use="Hello"/>
        <p>The answer is <span pwp:use="C" pwp:ask="C is A+B"/>.</p>
      </body>
    </html>
  3. Making a table We are given a Prolog database staff.pl defining staff(NickName, FullName, Office, Phone, E_Mail_Address). status(NickName, full_time | part_time). We want to make a phone list of full time staff.
    <html
      xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl"
      pwp:ask='ensure_loaded(staff)'>
      <head>
        <title>Phone list for Full-Time staff.</title>
      </head>
      <body>
        <h1>Phone list for Full-Time staff.</h1>
        <table
          pwp:ask = "setof(FullName-Phone,
                           N^O^E^(
                             status(N, full_time),
                             staff(N, FullName, O, Phone, E)
                           ),
                           Staff_List)">
          <tr><th>Name</th><th>Phone</th></tr>
          <tr pwp:ask="member(FullName-Phone, Staff_List)">
            <td pwp:use="FullName"/>
            <td pwp:use="Phone"/>
          </tr>
        </table>
      </body>
    </html>
  4. Substituting into an attribute Same data base as before, but now we want to make a mailing list page.
    <html
      xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl"
      pwp:ask='ensure_loaded(staff)'>
      <head>
        <title>Phone list for Full-Time staff.</title>
      </head>
      <body>
        <h1>Phone list for Full-Time staff.</h1>
        <table
          pwp:ask = "setof(FullName-E_Mail,
                           N^O^P^staff(N, FullName, O, P, E_Mail),
                           Staff_List)">
          <tr><th>Name</th><th>Address</th></tr>
          <tr pwp:ask="member(FullName-E_Mail, Staff_List)">
            <td pwp:use="FullName"/>
            <td><a pwp:use="E_Mail"
                   pwp:att='$' href="mailto:$(E_Mail)$"/></td>
          </tr>
        </table>
      </body>
    </html>
  5. If-then-else effect A page that displays the value of the 'SHELL' environment variable if it has one, otherwise displays 'There is no default shell.'
    <html
      xmlns:pwp="http://www.cs.otago.ac.nz/staffpriv/ok/pwp.pl">
      <head><title>$SHELL</title></head>
      <body>
        <p pwp:ask="getenv('SHELL', Shell)"
        >The default shell is <span pwp:tag="-" pwp:use="Shell"/>.</p>
        <p pwp:ask="\+getenv('SHELL',_)">There is no default shell.</p>
      </body>
    </html>

    There is one other criterion for a good server-side template language:

    It should be possible to compile templates so as to eliminate most if not all interpretation overhead.

    This particular notation satisfies that criterion with the limitation that the conversion of a term to character data requires run-time traversal of terms (because the terms are not known until run time).

author
- Richard O'Keefe
To be done
- Support compilation of PWP input files
Source pwp_files(:In:atom, +Out:atom) is det
loads an Xml document from the file named In, transforms it using the PWP attributes, and writes the transformed version to the new file named Out.
Source pwp_stream(:Input:input_stream, +Output:output_stream, +Context:list) is det
Loads an Xml document from the given Input stream, transforms it using the PWP attributes, and writes the transformed version to the given Output stream. Context provides initial contextual variables and is a list of Name=Value.
Source pwp_xml(:In:list(xml), -Out:list(xml), +Context)
maps down a list of XML items, acting specially on elements and copying everything else unchanged, including white space. The Context is a list of 'VariableName'=CurrentValue bindings.
Source pwp_attributes(+Atts0:list(atom=atom), -Ask:atom, -Use:atom, -How:atom, -Att:atom, -Tag:atom, -Atts1:list(atom=atom))[private]
Walks down a list of AttributeName=ItsValue pairs, stripping out those whose AttributeName begins with the 'pwp:' prefix, and copying the rest to Atts1. Along the way, Ask/Use/How/Att/Tag are bound to the values of the pwp:ask/pwp:use/pwp:how/pwp:att/pwp:tag attributes, if any. At the end, any of these variables that are still unbound REMAIN unbound; they are not bound to default values.
Source pwp_attr(+XMLAttr, -PWPLocal) is semidet[private]
True if PWPLocal is the local name of a pwp:Local expression in XML. This predicate deals with the three different XML representations: the form is returned of XML namespace processing is not enabled. The second if it is enabled and the namespace is properly defined and the last if the namespace is not defined.
Source pwp_unite(+Bindings, +Context0, -Context:list(atom=any))[private]
merges the new Bindings with the bindings in the outer Context0, constructing a new list of VariableName=CurrentValue bindings in Context1. This is only used when the CurrentValue parts of the new Bindings are known to be distinct new variables, so the Bindings cannot possibly conflict with any existing binding in Context0. This is O(|Bindings|.|Context0|), which is not that efficient, but since we do not expect there to be very many variables it doesn't matter much.
Source pwp_unite(+Bindings, +Context0:list(atom=any))[private]
looks up the bindings in Bindings in the outer Context0. This is only used for 'pwp:use' terms (and the related terms in $(...)$ attribute value substitutions), so that we have no interest in forming a new context. (If we did, we'd use pwp_unite/3 instead.) This is only used when the CurrentValue parts of the new Bindings are known to be distinct new variables, so the Bindings cannot possibly conflict with any existing binding in Context0. However, there could be new variables in Bindings, and that would cause problems. An XML term may not contain variables, and a term we want to convert to a list of character codes had better not contain variables either. One approach would be to just bind such variables to something, another is to throw some kind of exception. For the moment we call functor/3 so as to get an instantiation error.
Source pwp_attach(+Tree, ?Ys0:list(xml), ?Ys:list(xml))[private]
is a combination of "flatten" and "append". It unifies Ys0\Ys with the result of flattening Tree.
Source pwp_is_codes(+String:any)[private]
is true when String is a list of integers and each of those integers is a possible Unicode value (in the range U+0000..U+10FFFF). Back in the days of ISO Latin 1 we would have checked for 0..255, and way back in the days of ASCII for 0..127. Yes, there are more than a million possible characters in Unicode; currently about 100 000 of them are in use.