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.
<script src="jsProlog.js"></script>
var program = new jsProlog.program();
var prolog = require("./jsProlog.js");
var program = new prolog.program();
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)
program.addRule("human(socrates).");
program.addRule("mortal(X) :- human(X).");
var answer = program.askQuery("mortal(socrates).");
console.log(answer.value);
Test your prolog code here: demo