-
-
Notifications
You must be signed in to change notification settings - Fork 0
solve
Synchronous callback-style solver. The main public entry point of yopl.
import solve from 'yopl'; // or: import solve from 'yopl/solve.js';
solve(rules, name, args, callback): void
| Parameter | Type | Description |
|---|---|---|
rules |
Rules |
Rule database (a {name: rule} object). |
name |
string |
Initial goal name to prove. |
args |
unknown[] |
Argument vector that unifies against the rule head. |
callback |
(env: Env) => void |
Invoked once per solution. |
The callback receives the live unification environment. Use assemble(variable, env) from deep6/traverse/assemble.js to extract bound values.
import {variable} from 'deep6/env.js'; import assemble from 'deep6/traverse/assemble.js'; import solve from 'yopl'; const rules = { one: () => [{args: [1]}] }; const X = variable('X'); solve(rules, 'one', [X], env => { console.log(assemble(X, env)); // 1 });
A rule is a function (or array of functions for a disjunction) that receives a fresh batch of logical variables and returns an array of terms. The first term is the rule head ({args: [...]}); the rest are body goals.
const rules = { member: [(V, X) => [{args: [{value: V, next: X}, V]}], (V, X) => [{args: [{next: X}, V]}, {name: 'member', args: [X, V]}]] };
A body goal can be:
- a structured term:
{name: 'member', args: [X, V]} - a bare string (rule name with no args):
'true' - an inline JS function
(env, goals, stack) => boolean | GoalFramefor predicates implemented directly in JavaScript.
See rules‐system for helpers (head, term, list, cut, call, ...) that build these shapes ergonomically.