LogicJS ES6
This is a fork of the wonderful LogicJS library. All credits go to the original authors.
It has been carefully adapted to satisfy JavaScript strict mode
and to support ES6 modules. As a result, this version runs beautifully in your Rollup and Webpack enabled applications, for example your Svelte component or virtually any other framework that relies on import
/export
for bundling, lazy loading, and code splitting.
LogicJS
LogicJS adds logic programming to JavaScript.
Logic programming is typically known by the language Prolog.
Installation
Install using npm:
npm install logicjs-es6
Import as an ES6 module:
Introduction
var or = logicor and = logicand eq = logiceq run = logicrun lvar = logiclvar between = logicbetween //creates two unique logic variablesvar x = y = //creates a 'goal'g1 = //runs goal asking for the possible values of x and y //[2, 'dog'] //[3, 'dog'] //[ [2, 3], ['dog', 'dog'] ] //a goal is a sequence of assertions//here, we assert that x is a value from 1 to 3//and that y is either 1 or 2g2 = //get only the first 2 answers //[ [1, 1], [1, 2] ]//get all answers //[ [1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 2] ]
Goals
Programmers may create their own goals by combining primitive goals such as or, and and eq.
{ //mcbob is father of bob //bob is father of bill return } { var z = //dummy variable return } //who is father of bob? //['mcbob']//who is grandfather of who? //[ ['mcbob', 'bill'] ]
The win and fail goals simply succeed or not succeed. They are analogous to the true and false constants in logic.
//[ undefined ] //[]
Note that failure means there are no answers (empty array), while success means there is at least one answer (in this case the answer is undefined, since we still don't know the value of x).
Constraints
In pure logic programming, it doesn't matter which arguments of a goal have been instantiated.
This works for the arithmetic relations.
var add = logicadd sub = logicsub mul = logicmul div = logicdiv //[ 4 ] //[ -4 ] //[ 12 ] //[ 12 ]
When not enough arguments are instantied, some goals will propagate a constraint (such as "x+1=y" or "x is less or equal to 2"). When it's still not possible to find a value for the variable, it'll return a domain with the possible values of that variable.
var less_equal = logicless_equalvar write = consolelog v = 0 //2 v = 0 //undefinedd = 0 //-inf, 2d = 0 //2, infd = 0 //-inf, inf
Unification (done by the goal logic.eq) is the most basic kind of constraint. Constraint logic programming adds further constraints.
An example of an impure goal included in LogicJS is between, which requires the first two arguments to be numbers.
Implementation
The implementation of LogicJS is based on MiniKanren/SICP.
- Bindings associate a variable to a value (e.g. X=2).
- Streams are similar to lists, but they are evaluated on the fly and thus are potentially infinite.
- Packages contain a list of bindings (sometimes called a frame) and a list of constraints. Logic programming without support for constraints might only use frames instead of packages.
- Goals take a package as input and return a stream of packages (since a goal can have zero or infinite answers).