-
-
Notifications
You must be signed in to change notification settings - Fork 0
Home
🔍 Search this wiki — ranked, deep-linked search via wiki-search; install the bookmarklet to search in place. Fallback: GitHub wiki search.
yopl is an ES6 mini-library that implements a Prolog-style logic solver in JavaScript. It provides:
- A small core solver with multiple driver styles (callback, generator, async callback, async generator).
- A built-in rule library: system helpers, comparisons, arithmetic, bitwise, boolean logic, and JS-native bridges (Array / Map / Set / Date).
- A rule compiler with two tagged-template front-ends — a per-clause DSL (
clause\...`) and a strict-Prolog whole-program parser (prolog`...``) — write rules declaratively, catch arity drift at compile time. - Unification powered by
deep6— yopl's only runtime dependency, itself a zero-dependency library.
npm install --save yoplimport {variable} from 'deep6/env.js';
import assemble from 'deep6/traverse/assemble.js';
import solve from 'yopl';
const rules = {
member: [(V, X) => [{args: [{value: V, next: X}, V]}], (V, X) => [{args: [{next: X}, V]}, {name: 'member', args: [X, V]}]]
};
const list = {value: 1, next: {value: 2, next: {value: 3, next: null}}};
const X = variable('X');
solve(rules, 'member', [list, X], env => {
console.log('X =', assemble(X, env));
});
// X = 1
// X = 2
// X = 3-
compile-overview — the rule compiler IR + lowering + per-clause
clause\...`` DSL. -
compile-prolog — strict-Prolog tagged-template parser (
prolog\...`,prologClause`...`,prologFile`). - Writing rules — the older hand-written encoding (still supported, still useful for low-level work).
- Using deep6 — pattern catalog for custom predicates that call deep6 directly: Env lifecycle, variable inspection, unify entry, sentinels.
- Search feasibility — where naïve backtracking hits the wall (empirical limits on yopl 1.4.0) and how to mitigate.
All four solvers share the same proof engine; they differ only in how they deliver solutions and whether they can await. Pick along two axes — sync vs async, push (callback) vs pull (generator):
| Push (callback) | Pull (generator) | |
|---|---|---|
| Sync | solve (main entry) | solvers-gen |
| Async | solvers-async | solvers-asyncGen |
- Use an async solver when a goal predicate or your result handler must
await. - Use a generator solver when you want lazy enumeration, early termination via
break, or to compose solutions through iterator utilities. - Otherwise prefer solve — it has the lowest overhead.
See solve for the full motivation and shared rule shape.
-
rules-system — generic logic primitives:
head,term,list,cut,call,not,eq,unifyOpts,true,fail, generic type predicates, higher-order (map,filter,foldl, …). -
rules-native — JS-native bridges:
arrayList/arrayGet/arraySet/arrayLength,mapEntries/mapGet/mapHas,setItems/setHas,dateTimestamp/dateComponents/dateComponentsUTC, plus type testsisArray/isMap/isSet/isDate. -
rules-comp — comparisons:
lt,le,gt,ge,nz. -
rules-math — arithmetic:
add,sub,mul,div,neg(each reversible);is/2arithmetic-expression evaluator;=:=/=\=arithmetic comparison. -
rules-bits — bitwise:
bitAnd,bitOr,bitXor,bitNot. -
rules-logic — boolean logic:
logicalAnd,logicalOr,logicalXor,logicalNot.
-
compile-overview — IR, lowering, validation, the per-clause
clause\...`DSL, and theLit`-walker. -
compile-prolog — strict-Prolog tagged-template parsers (
prolog\...`,prologClause`...`), filesystem loaders (prologFile,prologFileAsync`), body operators, op directives, source-map opt-in.
- Release-notes — long-form per-release changelog with internal-change context.
yopl is the next generation of heya-unify, based on an unpublished project named yoctoProlog. It implements Prolog-like logic-programming primitives without a dedicated language; the goal is an embedded JavaScript-native unifier and solver for tricky logic algorithms.
heya-unify was documented in a blog series: