{"id":12120,"library":"tau-prolog","title":"Tau Prolog: JavaScript Prolog Interpreter","description":"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.","status":"active","version":"0.3.4","language":"javascript","source_language":"en","source_url":"https://github.com/tau-prolog/tau-prolog","tags":["javascript","tau-prolog","prolog-interpreter","iso-prolog-standard","prolog"],"install":[{"cmd":"npm install tau-prolog","lang":"bash","label":"npm"},{"cmd":"yarn add tau-prolog","lang":"bash","label":"yarn"},{"cmd":"pnpm add tau-prolog","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For Node.js environments, `pl` is the default module export accessible via CommonJS `require()`. ES module `import` syntax is not officially documented or supported by default for this package.","wrong":"import pl from 'tau-prolog';","symbol":"pl","correct":"const pl = require('tau-prolog');"},{"note":"When used in a browser environment via a `<script>` tag, `pl` is exposed as a global variable. Ensure `tau-prolog.js` (or a custom bundle) is loaded.","symbol":"pl","correct":"<script src=\"tau-prolog.js\"></script>\n// pl is then available globally"},{"note":"The core functionality, such as creating a new Prolog session, is accessed through methods on the `pl` object. Direct named imports for `create` are not supported.","wrong":"import { create } from 'tau-prolog';\nconst session = create();","symbol":"pl.create()","correct":"const session = pl.create();"}],"quickstart":{"code":"const pl = require('tau-prolog');\n\nconst session = pl.create();\n\nsession.consult(`\n    likes(sam, salad).\n    likes(dean, pie).\n    likes(sam, apples).\n    likes(dean, whiskey).\n`, {\n    success: function() {\n        console.log(\"Program loaded correctly.\");\n        session.query(\"likes(sam, X).\", {\n            success: function(goal) {\n                console.log(\"Goal loaded correctly.\");\n                // Look for answers\n                function findAnswer() {\n                    session.answer({\n                        success: function(answer) {\n                            console.log(session.format_answer(answer));\n                            findAnswer(); // Try to find the next answer\n                        },\n                        fail: function() {\n                            console.log(\"No more answers.\");\n                        },\n                        error: function(err) {\n                            console.error(\"Uncaught exception:\", err);\n                        },\n                        limit: function() {\n                            console.warn(\"Resolution limit exceeded.\");\n                        }\n                    });\n                }\n                findAnswer();\n            },\n            error: function(err) {\n                console.error(\"Error parsing goal:\", err);\n            }\n        });\n    },\n    error: function(err) {\n        console.error(\"Error parsing program:\", err);\n    }\n});","lang":"javascript","description":"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."},"warnings":[{"fix":"Migrate synchronous `consult` and `query` calls to use the provided callback structure (success, error, fail, limit) as demonstrated in the quickstart example and official documentation.","message":"As of version 0.3.0, the `consult` and `query` methods on `pl.type.Session` and `pl.type.Thread` prototypes became asynchronous, requiring callback functions for success and error handling. Direct synchronous usage from previous versions will no longer work.","severity":"breaking","affected_versions":">=0.3.0"},{"fix":"Structure callbacks carefully, or consider wrapper functions to convert the callback-based API into Promise-based for better readability and maintainability in modern JavaScript codebases. The project mentions that it distributes a package to overcome drawbacks of this interface, but doesn't explicitly name it in the provided context.","message":"Tau Prolog primarily uses a callback-based asynchronous API, especially for `consult`, `query`, and `answer` methods. This can lead to 'callback hell' in complex scenarios. While it supports asynchronous predicates for non-blocking operations, direct Promises or async/await are not part of the core API for resolution flow.","severity":"gotcha","affected_versions":">=0.3.0"},{"fix":"Refer to Tau Prolog's specific documentation for supported built-in predicates and module definitions to ensure compatibility and expected behavior. Test your Prolog programs thoroughly within the Tau Prolog environment.","message":"When comparing Prolog systems, be aware that Tau Prolog aims for ISO Prolog Standard compliance but may have differences in specific built-in predicates or behavior compared to other implementations like SWI-Prolog. For example, `sub_string/5` might not be supported, while `sub_atom/5` is.","severity":"gotcha","affected_versions":"all"},{"fix":"Instead of a traditional trace, the recommended approach involves systematic removal of goals to identify failing fragments or analyzing program generalizations. Logging output in callbacks can also assist.","message":"Tau Prolog does not support a `trace` predicate for debugging in the same way some other Prolog systems (like SWI-Prolog) do. Debugging complex Prolog execution within Tau Prolog requires alternative strategies.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Review the Prolog program for syntax correctness according to the ISO Prolog Standard and Tau Prolog's specific implementation. Check for missing periods, misplaced commas, or incorrect predicate arity. The error callback for `consult` will provide details.","cause":"The Prolog program string passed to `session.consult()` contains syntax errors or uses unsupported predicates/constructs.","error":"Error parsing program"},{"fix":"Examine the goal string for proper Prolog syntax. Ensure variables are correctly capitalized, predicates exist, and terms are well-formed. The error callback for `query` will provide specific error information.","cause":"The Prolog goal string passed to `session.query()` contains syntax errors.","error":"Error parsing goal"},{"fix":"Implement robust error handling in the `session.answer()`'s `error` callback to catch and inspect the exception. Debug the Prolog program's logic to identify the source of the exception. For performance-intensive programs, consider using the `limit` option in `session.answer()` to prevent infinite loops from freezing the application.","cause":"A runtime error occurred during the Prolog inference process (e.g., division by zero, invalid argument type for a predicate, an infinite loop exhausting resources).","error":"Uncaught exception"},{"fix":"Optimize your Prolog program to reduce the search space or prevent infinite recursion. Alternatively, increase the `limit` option in `session.answer()` if the computation is legitimately long, but be mindful of potential browser blocking.","cause":"The Prolog interpreter reached the maximum number of resolution steps (default 1000) while trying to find an answer, often indicating an inefficient or infinitely recursive program.","error":"Resolution limit exceeded"}],"ecosystem":"npm"}