/jsProlog

Prolog interpreter in JS

Primary LanguageJavaScript

Español # English

jsProlog     Buy Me a Coffee at ko-fi.com

Prolog interpreter in JS

First, the interpreter was made by Jan, you can find it in http://ioctl.org/logic/prolog-latest I only encapsulates it and creates a few funtions to access.

Load

Browser

<script src="jsProlog.js"></script>
var program = new jsProlog.program();

Node

var prolog = require("./jsProlog.js");
var program = new prolog.program();

Interface

prolog.getRules();

Return an array with all rules in text (prolog code)

prolog.addRule(rule);

Add a new rule (I don't make diference between facts and rules)

prolog.askQuery(query)

Execute one query return an array of Answers:

Answer(name, value){
  this.name = name;
  this.value = value;
  this.print = function(){...};
}

name: variable name

value: value

When answer is a boolean answer when is true name is '_' and value is true (no answer means false)

Example

program.addRule("human(socrates).");
program.addRule("mortal(X) :- human(X).");
var answer = program.askQuery("mortal(socrates).");
console.log(answer.value);

Demo

Test your prolog code here: demo