Tau Prolog: JavaScript Prolog Interpreter

0.3.4 · active · verified Sun Apr 19

Tau Prolog is an open-source, client-side Prolog interpreter implemented entirely in JavaScript. It aims for high compliance with the ISO Prolog Standard, enabling the development and portability of Prolog applications across various systems. The current stable version is 0.3.4, with releases appearing to be infrequent and focused on bug fixes and minor feature additions. A key differentiator is its compatibility with both web browsers (client-side) and Node.js environments, allowing for seamless integration. It uniquely supports DOM manipulation and event handling using Prolog predicates, and incorporates an asynchronous, callback-based execution model to prevent UI blocking in browsers, which contrasts with many other Prolog systems that operate server-side or via WebAssembly.

Common errors

Warnings

Install

Imports

Quickstart

This code demonstrates how to initialize a Tau Prolog session, consult a Prolog program string, formulate a query, and iterate through all possible answers using the library's callback-based API.

const pl = require('tau-prolog');

const session = pl.create();

session.consult(`
    likes(sam, salad).
    likes(dean, pie).
    likes(sam, apples).
    likes(dean, whiskey).
`, {
    success: function() {
        console.log("Program loaded correctly.");
        session.query("likes(sam, X).", {
            success: function(goal) {
                console.log("Goal loaded correctly.");
                // Look for answers
                function findAnswer() {
                    session.answer({
                        success: function(answer) {
                            console.log(session.format_answer(answer));
                            findAnswer(); // Try to find the next answer
                        },
                        fail: function() {
                            console.log("No more answers.");
                        },
                        error: function(err) {
                            console.error("Uncaught exception:", err);
                        },
                        limit: function() {
                            console.warn("Resolution limit exceeded.");
                        }
                    });
                }
                findAnswer();
            },
            error: function(err) {
                console.error("Error parsing goal:", err);
            }
        });
    },
    error: function(err) {
        console.error("Error parsing program:", err);
    }
});

view raw JSON →