"Hello, Tau Prolog!"

I'm a big fan of Tau Prolog, it provides all the interactive Prolog code on this website, so I thought we'd do a quick introduction. Tau Prolog is a Prolog interpreter written in JavaScript. This means you can write a total of 6 lines of pure boiler-plate JavaScript, of which 2 are for debugging during development, and do all the usual "stuff" in Prolog instead! Let's make three "Hello, world" applications that are typical in JavaScript tutorials, using alert, console.log and by manipulating the DOM.

An index.html Template

Here's a HTML template you can use as a template for all these programs, and others. Besides setting up the DOM, it also contains the boiler-plate JavaScript for calling an init predicate in your Prolog. You can find out more about this boiler-plate in the Tau-Prolog Simple Tutorial.

You'll also need to download tau-prolog.js, in this template it's stored in the same root directory.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Hello, Tau Prolog!</title>
</head>
<body>
    <h1>Hello, Tau Prolog!</h1>
    <div id="greet_answers">
    </div>
<script src="tau-prolog.js"></script>
<script type="text/prolog" id="code.pl">
init :-
    true.
</script>
<script>
const session = pl.create(1000)
const parsed = session.consult("code.pl")
if (parsed !== true) { console.log(pl.format_answer(parsed)) }
const query = session.query("init.")
if (query !== true) { console.log(pl.format_answer(query)) }
// session.answer needs a callback function, here we "mute" the output
session.answer((a) => {}) 
// unmuted: 
// session.answer((a) => console.log(pl.format_answer(a))))
</script>
</body>
</html>

If you need a quick server, here's one in SWI-Prolog that'll get you up and running, query it with swipl -g serve -s server.pl then navigate to localhost:8888:

:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_files)).

:- http_handler(root(.), http_reply_from_files('.', []), [prefix]).

server(Port) :-
    http_server(http_dispatch, [port(Port)]).

serve :-
    server(8888).

Right, now we can make some "Hello, Tau-Prolog!" programs.

alert("Hello, Tau Prolog!")

For this we want to access the JavaScript property called alert, which turns out to be a function. We can then apply this function to a list of arguments, we don't care about what it returns. We're accessing JavaScript here, so we'll need that library. The docs for library(js) are here. So our code.pl in our template becomes:

And because we're running Tau Prolog, you can try it here too!

?-

console.log("Hello, Tau Prolog!")

Using console.log is a little more involved because log is a method of console. It's a similar idea, there's some property called console, which turns out to be an object, there's also a property of this object called log, which turns out to be a function. Once we've got that function, we can apply it again.

Again we can query this here, but you'll need to open your console to see the results. How you open the console varies from browser to platform, but try F12 before searching if you're unfamiliar.

?-

Manipulating the DOM

Now we're getting to the more serious work. We're going to need library(dom), the docs for which are here. To do this we'll need to create a whole new HTML element and we'll make it a child element of <div id="greet_answers">. So that you can run it here and it look quite pretty, we also add a couple of classes.

Now if you query it here you'll get an extra "Hello, Tau Prolog!" answer along with the usual true ; false. answer. We made it white to show it's not a usual successful or false answer.

?-

Conclusion

This is only a "Hello, world!", there's much more goodness to discover! The great thing about Tau Prolog is it all runs client-side, so queries won't hog your server computing power, nor do you need to ensure predicates are safe on your server as you can do all of your reasoning without ever touching the server. Have fun!