Weblog Banner

Input and Backtracking

So far, you've seen that a simple Weblog application comprises two files: an HTML web page, and a Prolog program. The HTML file contains special labels, which are replaced with output from the Prolog program, using the submit/2 predicate.

The "Hello World!" application on the previous page, is an example of a "one-shot" program, which simply runs, generates output, and is done. But Weblog can do much more than this; in particular, it can continue execution after the call to submit/2, process further input from displayed web page, even backtracking to provide alternative answers.

Bonjour le Monde

Let's look at a slightly more interesting program, which returns the message, "Hello World!", in several different languages. Here is the Prolog code:

weblog :-
   (  greeting( Tongue, Phrase ),
      submit( 'hello_2.htm', [(language,Tongue),(hello,Phrase)] ),
      getarg( choose, Next ),
      Next = `done`
   -> submit( 'hello_21.htm', [(hello,`You pressed "DONE": Goodbye!`)] )
   ;  submit( 'hello_21.htm', [(hello,`No More Answers: Goodbye!`)] )
   ).

greeting( english, `Hello World!` ).
greeting( french, `Bonjour le Monde!` ).
greeting( german, `Hallo Welt!` ).
greeting( italian, `Ciao Mondo!` ).
greeting( spanish, `Hola Mundo!` ).
Multiple Languages

As before, your program is called "weblog"; this time, however, rather than simply call submit/2 with a hardwired response, "Hello World!", it picks up a language and a phrase a program you have written, called "greeting/2". On the first call, greeting/2 returns the results, "english" and "`Hello World!", and both these values are passed into the call to submit/2.

Notice how, unike the previous example, things don't finish with the call to submit/2, but continue. In effect, your program is waiting for the user to view the web page, and then respond to it, by selecting or editing one or more items on a displayed form. But how does execution continue, and how does your Weblog application know what options the user has chosen? The answer, is in another predicate, "getarg/2".

The "getarg/2" Predicate

Let's take a look at this example's HTML web page, "hello_2.pl":

<html>

  <head>
    <title>Hello World Example with Backtracking</title>
  </head>

  <body>

    <h2>In {:language:}, you say, "{:hello:}"</h2>

      <p>Please choose your option:</p>

    <form name="" method="get" action="/weblog/hello_2.exe">
      <input type="submit" name="choose" value="more"/>
      <input type="submit" name="choose" value="done"/>
    </form>

  </body>

</html>

This time, you will notice there is an additional label, "language", alongside the original "hello" label, but more importantly, there is also a "<form>" element: this is entry which enables your Weblog application to continue after it calls submit/2. In this case, the form contains just two buttons (input type="submit"), both named "choose", but with differnet values, of "more" and "done" respecitvely.

When you click on either button, your Weblog application picks up where it left off, and you can find out which button was pressed, by calling getarg/2. If you click "done", the Weblog application succeeds, and uses submit/2 to display the message, "You pressed "DONE": Goodbye!", and stops. However, if you selected "more", something more interesting happens ...

Backtracking

Backtracking

One of Prolog's special features, is its ability to "backtrack" in order to find subsequent solutions, and of course, this ability is fully supported in Weblog. In this example, if you selected "more", the simple test, "Next = done" will fail, causing the program to backtrack to the next choice point, which will be the next case of "greeting/2". This time, "Toungue" and "Phrase" will be bound to "french" and "`Bonjour le Monde!`" respectively

Once again submit/2 will display the "hello_2.htm" file, with the latest result, and once again you will be able to select "more" or "done", and the process repeats.

If you press "more" too many times, so that you run out of cases for greeting/2, the program backtracks into the "else" branch, and displays the message, "No More Answers: Goodbye!", at which point the program stops.

Rather than simply submit an HTML file with a fixed response, "Hello World!", this version of the program has a backtrackable table of languages and their corresponding translations of the greeting. As always, the "weblog/0" program is called automatically, and it picks up a greeting, and calls "submit/2" to combine the HTML file, "hello_2.pl", with values for both the labels, "{:language:}" and "{:hello:"}".

One of Prolog's special features, is its ability to "backtrack" in order to find other solutions, and of course, this ability is fully supported in Weblog. Let's add a "<form>" element to our HTML example, and call the file "hello_2.htm":

Continued Execution

So far, so good, but that's not all. Notice how our second example carries on executing after calling "submit/2" to show a language and greeting. In fact, it is effectively waiting for you to choose one of the two "<form>" options on the "hello_2.htm" page, "more" or "done".

The call to "getarg/2" is all you need to pick up the value of a form field, in this case the one called "choose": if you chose "done", the program prints a polite message which is displayed in a copy of the the old, "hello_1.htm" page, renamed "hello_21.htm"; if you chose "more", the program attempts to backtrack to the next solution for "greeting/2", and execution continues. If there are no further solutions, the program backtracks to the "else" branch, and displays an appropriate goodbye message, again with "hello_21.htm".

The Back Button

The Browser Back Button

One very useful way in which Weblog actually exceeds the features of regular Prolog, is that it responds intelligently to the browser's "back" button. If you make a selection on a form, and then change your mind, simply press "back" and select something else. Or step several levels back, right up to the very start of your application, if that's what you want.

In fact, you are not limited to moving back: you can move both back and forwards as much as you like, using your browser buttons, and take your time deciding where to start interacting with your application, from whatever point you want.

Interacting with Weblog

Now you have seen the essence of how to interact with Weblog: creating an application which displays multiple pages, waiting for your input in between, and controlling its function accordingly. Next, you'll see how to store information and even modify your application while it is running.

Click here to run the "Bonjour Le Monde" example

Click on the buttons to find out more, or jump directly to any of these links:

Weblog Previous Next
Direct LinkExample Name
One Shot Program"Hello World"
Input and Backtracking"Bonjour Le Monde"
Learning and Storage"Teach Me"
Flexible Output"John Likes Mary"
Tables and Forms"Family Tree"
Photoshop and Images"Lunar Phase"
Illustrator and Prolog"Big Ben Time Check"
DreamWeaver and VisiRule 2"Lawns"