eyeprolog 1.3.91 → 1.3.93
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/book/README.md +1 -0
- package/examples/book/chapter-40/02-program.pl +5 -4
- package/examples/book/chapter-40/03-program-2.pl +4 -4
- package/examples/book/chapter-40/04-program-3.pl +5 -0
- package/index.d.ts +6 -1
- package/package.json +1 -1
- package/src/iso.js +198 -64
- package/src/quads.js +23 -1
- package/src/scryer-compat.js +22 -12
- package/src/solver.js +100 -82
- package/src/standard-library.js +15 -3
- package/src/term.js +135 -13
- package/test/run-regression.mjs +123 -27
- package/the-art-of-eyeprolog.md +27 -10
package/src/solver.js
CHANGED
|
@@ -1371,7 +1371,14 @@ function groupNeedsActiveFrame(group) {
|
|
|
1371
1371
|
return group.cutReachable !== false || (group.recursive && !group.linearNumeric);
|
|
1372
1372
|
}
|
|
1373
1373
|
|
|
1374
|
-
function
|
|
1374
|
+
function datalogAnswerSolutions(solver, relation, goal, env) {
|
|
1375
|
+
const state = { pending: false };
|
|
1376
|
+
const iterator = datalogAnswerGenerator(solver, relation, goal, env, state);
|
|
1377
|
+
iterator.hasPendingAlternatives = () => state.pending;
|
|
1378
|
+
return iterator;
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
function* datalogAnswerGenerator(solver, relation, goal, env, state) {
|
|
1375
1382
|
if (!relation) return;
|
|
1376
1383
|
const candidates = datalogCandidateIndexes(
|
|
1377
1384
|
relation,
|
|
@@ -1380,20 +1387,26 @@ function* datalogAnswerSolutions(solver, relation, goal, env) {
|
|
|
1380
1387
|
derefForLocal,
|
|
1381
1388
|
memoAnswerScalarKey,
|
|
1382
1389
|
);
|
|
1383
|
-
const
|
|
1390
|
+
const indexes = candidates == null
|
|
1391
|
+
? { length: relation.rows.length, at: (index) => index }
|
|
1392
|
+
: { length: candidates.length, at: (index) => candidates[index] };
|
|
1393
|
+
for (let position = 0; position < indexes.length; position++) {
|
|
1394
|
+
const rowIndex = indexes.at(position);
|
|
1384
1395
|
const row = relation.rows[rowIndex];
|
|
1385
1396
|
const next = env.clone();
|
|
1397
|
+
let matches = true;
|
|
1386
1398
|
for (let i = 0; i < goal.arity; i++) {
|
|
1387
1399
|
solver.stats.unify_calls++;
|
|
1388
|
-
if (!unify(goal.args[i], row[i], next))
|
|
1400
|
+
if (!unify(goal.args[i], row[i], next)) {
|
|
1401
|
+
matches = false;
|
|
1402
|
+
break;
|
|
1403
|
+
}
|
|
1389
1404
|
}
|
|
1405
|
+
if (!matches) continue;
|
|
1406
|
+
state.pending = position + 1 < indexes.length;
|
|
1390
1407
|
yield next;
|
|
1391
|
-
};
|
|
1392
|
-
if (candidates == null) {
|
|
1393
|
-
for (let rowIndex = 0; rowIndex < relation.rows.length; rowIndex++) yield* visit(rowIndex);
|
|
1394
|
-
return;
|
|
1395
1408
|
}
|
|
1396
|
-
|
|
1409
|
+
state.pending = false;
|
|
1397
1410
|
}
|
|
1398
1411
|
|
|
1399
1412
|
function pushWfsAnswerFrames(stack, model, group, goal, rest, env, depth, active, solver) {
|
|
@@ -1428,7 +1441,10 @@ function bundledBetweenIterator(solver, group, goal, env) {
|
|
|
1428
1441
|
// the generated value in the caller repeatedly revisits all earlier frames.
|
|
1429
1442
|
// Enumerate the canonical bundled relation directly while leaving user
|
|
1430
1443
|
// definitions and non-EyeProlog registries on the ordinary Prolog path.
|
|
1431
|
-
|
|
1444
|
+
const state = { pending: false };
|
|
1445
|
+
const iterator = bundledBetweenSolutions(solver, goal, env, state);
|
|
1446
|
+
iterator.hasPendingAlternatives = () => state.pending;
|
|
1447
|
+
return iterator;
|
|
1432
1448
|
}
|
|
1433
1449
|
|
|
1434
1450
|
function requireBetweenInteger(term, env) {
|
|
@@ -1440,7 +1456,7 @@ function requireBetweenInteger(term, env) {
|
|
|
1440
1456
|
return BigInt(value.name);
|
|
1441
1457
|
}
|
|
1442
1458
|
|
|
1443
|
-
function* bundledBetweenSolutions(solver, goal, env) {
|
|
1459
|
+
function* bundledBetweenSolutions(solver, goal, env, state) {
|
|
1444
1460
|
const lower = requireBetweenInteger(goal.args[0], env);
|
|
1445
1461
|
const upper = requireBetweenInteger(goal.args[1], env);
|
|
1446
1462
|
const requested = deref(goal.args[2], env);
|
|
@@ -1457,8 +1473,12 @@ function* bundledBetweenSolutions(solver, goal, env) {
|
|
|
1457
1473
|
for (let value = lower; value <= upper; value++) {
|
|
1458
1474
|
const next = env.clone();
|
|
1459
1475
|
solver.stats.unify_calls++;
|
|
1460
|
-
if (unify(goal.args[2], numberTerm(value), next))
|
|
1476
|
+
if (unify(goal.args[2], numberTerm(value), next)) {
|
|
1477
|
+
state.pending = value < upper;
|
|
1478
|
+
yield next;
|
|
1479
|
+
}
|
|
1461
1480
|
}
|
|
1481
|
+
state.pending = false;
|
|
1462
1482
|
}
|
|
1463
1483
|
|
|
1464
1484
|
function bundledMemberIterator(solver, group, goal, env) {
|
|
@@ -1468,25 +1488,48 @@ function bundledMemberIterator(solver, group, goal, env) {
|
|
|
1468
1488
|
return null;
|
|
1469
1489
|
}
|
|
1470
1490
|
|
|
1471
|
-
//
|
|
1472
|
-
//
|
|
1473
|
-
//
|
|
1474
|
-
//
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1491
|
+
// Attributed and delayed list variables retain the ordinary clause path so
|
|
1492
|
+
// their hooks and wake-up boundaries remain source-defined. Plain proper,
|
|
1493
|
+
// partial, and improper lists can all follow the canonical two-clause
|
|
1494
|
+
// relation lazily without first traversing the spine.
|
|
1495
|
+
if (env._prologAttributes != null || env._delays != null) return null;
|
|
1496
|
+
const state = { pending: false };
|
|
1497
|
+
const iterator = bundledMemberSolutions(solver, goal, env, state);
|
|
1498
|
+
iterator.hasPendingAlternatives = () => state.pending;
|
|
1499
|
+
return iterator;
|
|
1479
1500
|
}
|
|
1480
1501
|
|
|
1481
|
-
function* bundledMemberSolutions(solver, goal, env) {
|
|
1502
|
+
function* bundledMemberSolutions(solver, goal, env, state) {
|
|
1482
1503
|
let cursor = deref(goal.args[1], env);
|
|
1483
1504
|
while (isCons(cursor)) {
|
|
1484
1505
|
const item = cursor.args[0];
|
|
1485
1506
|
cursor = deref(cursor.args[1], env);
|
|
1486
1507
|
const next = env.clone();
|
|
1487
1508
|
solver.stats.unify_calls++;
|
|
1488
|
-
if (unify(goal.args[0], item, next))
|
|
1509
|
+
if (unify(goal.args[0], item, next)) {
|
|
1510
|
+
// The tail itself is the pending search state. Do not inspect it for a
|
|
1511
|
+
// future successful unification: an untried list position remains a
|
|
1512
|
+
// choicepoint even when resuming it will eventually fail.
|
|
1513
|
+
state.pending = isCons(cursor) || cursor.type === VAR;
|
|
1514
|
+
yield next;
|
|
1515
|
+
}
|
|
1489
1516
|
}
|
|
1517
|
+
if (cursor.type === VAR) {
|
|
1518
|
+
const openTail = cursor;
|
|
1519
|
+
const id = nextFreshId();
|
|
1520
|
+
let candidate = cons(goal.args[0], variable(`__member${id}_tail`));
|
|
1521
|
+
for (let before = 0n; ; before++) {
|
|
1522
|
+
const next = env.clone();
|
|
1523
|
+
solver.stats.unify_calls++;
|
|
1524
|
+
if (unify(openTail, candidate, next)) {
|
|
1525
|
+
state.pending = true;
|
|
1526
|
+
yield next;
|
|
1527
|
+
}
|
|
1528
|
+
candidate = cons(variable(`__member${id}_head_${before}`), candidate);
|
|
1529
|
+
generatedLengthAllocationCheckpoint(solver, before + 1n);
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
state.pending = false;
|
|
1490
1533
|
}
|
|
1491
1534
|
|
|
1492
1535
|
function bundledEllipsisPlan(solver, group, goal, rest, env) {
|
|
@@ -1520,7 +1563,7 @@ function bundledEllipsisPlan(solver, group, goal, rest, env) {
|
|
|
1520
1563
|
if (output.type === VAR && continuationInput.type === VAR && output.name === continuationInput.name &&
|
|
1521
1564
|
output.name.startsWith('\u0000dcg')) {
|
|
1522
1565
|
return {
|
|
1523
|
-
iterator:
|
|
1566
|
+
iterator: bundledEllipsisIterator(solver, continuation.args[1], input, env),
|
|
1524
1567
|
rest: rest.slice(1),
|
|
1525
1568
|
};
|
|
1526
1569
|
}
|
|
@@ -1528,7 +1571,14 @@ function bundledEllipsisPlan(solver, group, goal, rest, env) {
|
|
|
1528
1571
|
}
|
|
1529
1572
|
}
|
|
1530
1573
|
|
|
1531
|
-
return { iterator:
|
|
1574
|
+
return { iterator: bundledEllipsisIterator(solver, goal.args[1], input, env), rest };
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
function bundledEllipsisIterator(solver, output, input, env) {
|
|
1578
|
+
const state = { pending: false };
|
|
1579
|
+
const iterator = bundledEllipsisSolutions(solver, output, input, env, state);
|
|
1580
|
+
iterator.hasPendingAlternatives = () => state.pending;
|
|
1581
|
+
return iterator;
|
|
1532
1582
|
}
|
|
1533
1583
|
|
|
1534
1584
|
function isBinaryIdentityGroup(group) {
|
|
@@ -1548,7 +1598,7 @@ function isBinaryIdentityGroup(group) {
|
|
|
1548
1598
|
(a.name === right.name && b.name === left.name);
|
|
1549
1599
|
}
|
|
1550
1600
|
|
|
1551
|
-
function* bundledEllipsisSolutions(solver, output, input, env) {
|
|
1601
|
+
function* bundledEllipsisSolutions(solver, output, input, env, state) {
|
|
1552
1602
|
let cursor = input;
|
|
1553
1603
|
const requestedOutput = deref(output, env);
|
|
1554
1604
|
|
|
@@ -1568,7 +1618,10 @@ function* bundledEllipsisSolutions(solver, output, input, env) {
|
|
|
1568
1618
|
while (true) {
|
|
1569
1619
|
const next = env.clone();
|
|
1570
1620
|
solver.stats.unify_calls++;
|
|
1571
|
-
if (unify(output, cursor, next))
|
|
1621
|
+
if (unify(output, cursor, next)) {
|
|
1622
|
+
state.pending = isCons(cursor);
|
|
1623
|
+
yield next;
|
|
1624
|
+
}
|
|
1572
1625
|
if (isEmptyList(cursor)) return;
|
|
1573
1626
|
if (!isCons(cursor)) return;
|
|
1574
1627
|
cursor = deref(cursor.args[1], env);
|
|
@@ -1601,7 +1654,14 @@ function bundledLengthIterator(solver, group, goal, env) {
|
|
|
1601
1654
|
if (length.type === VAR && cursor.name === length.name) return null;
|
|
1602
1655
|
}
|
|
1603
1656
|
}
|
|
1604
|
-
|
|
1657
|
+
const state = {
|
|
1658
|
+
// length(OpenList, Length) has an unbounded succession of longer lists.
|
|
1659
|
+
// All other accepted fast-path modes are semidet.
|
|
1660
|
+
pending: length.type === VAR && cursor.type === VAR,
|
|
1661
|
+
};
|
|
1662
|
+
const iterator = bundledLengthSolutions(solver, goal, env);
|
|
1663
|
+
iterator.hasPendingAlternatives = () => state.pending;
|
|
1664
|
+
return iterator;
|
|
1605
1665
|
}
|
|
1606
1666
|
|
|
1607
1667
|
function* bundledLengthSolutions(solver, goal, env) {
|
|
@@ -2185,7 +2245,14 @@ function pushStreamingScalarFactRun(stack, solver, runGoals, groups, rest, env,
|
|
|
2185
2245
|
});
|
|
2186
2246
|
}
|
|
2187
2247
|
|
|
2188
|
-
function
|
|
2248
|
+
function scalarFactRunSolutions(solver, goals, groups, env, depth, active) {
|
|
2249
|
+
const state = { pending: false };
|
|
2250
|
+
const iterator = scalarFactRunGenerator(solver, goals, groups, env, depth, active, state);
|
|
2251
|
+
iterator.hasPendingAlternatives = () => state.pending;
|
|
2252
|
+
return iterator;
|
|
2253
|
+
}
|
|
2254
|
+
|
|
2255
|
+
function* scalarFactRunGenerator(solver, goals, groups, env, depth, active, pendingState) {
|
|
2189
2256
|
const localStack = [{ index: 0, names: [], values: [] }];
|
|
2190
2257
|
while (localStack.length) {
|
|
2191
2258
|
const state = localStack.pop();
|
|
@@ -2194,6 +2261,7 @@ function* scalarFactRunSolutions(solver, goals, groups, env, depth, active) {
|
|
|
2194
2261
|
const next = env.clone();
|
|
2195
2262
|
for (let i = 0; i < state.names.length; i++) next.bind(state.names[i], state.values[i]);
|
|
2196
2263
|
if (next._variableConstraints != null && !next.validateVariableConstraints()) continue;
|
|
2264
|
+
pendingState.pending = localStack.length !== 0;
|
|
2197
2265
|
yield next;
|
|
2198
2266
|
continue;
|
|
2199
2267
|
}
|
|
@@ -2218,6 +2286,7 @@ function* scalarFactRunSolutions(solver, goals, groups, env, depth, active) {
|
|
|
2218
2286
|
for (let i = nextStates.length - 1; i >= 0; i--) localStack.push(nextStates[i]);
|
|
2219
2287
|
if (solver.solutionsSeen >= solver.solutionLimit) return;
|
|
2220
2288
|
}
|
|
2289
|
+
pendingState.pending = false;
|
|
2221
2290
|
}
|
|
2222
2291
|
|
|
2223
2292
|
|
|
@@ -2813,64 +2882,13 @@ function builtinIsReadyOrAuthoritative(def, solver, goal, env) {
|
|
|
2813
2882
|
|
|
2814
2883
|
function iteratorMayHavePendingAlternatives(iterator) {
|
|
2815
2884
|
const predicate = iterator?.hasPendingAlternatives;
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
function
|
|
2820
|
-
let buffered = null;
|
|
2821
|
-
let bufferedReady = false;
|
|
2822
|
-
let closed = false;
|
|
2823
|
-
|
|
2824
|
-
const fill = () => {
|
|
2825
|
-
if (closed || bufferedReady) return;
|
|
2826
|
-
buffered = iterator.next();
|
|
2827
|
-
bufferedReady = true;
|
|
2828
|
-
if (buffered.done) closed = true;
|
|
2829
|
-
};
|
|
2830
|
-
|
|
2831
|
-
return {
|
|
2832
|
-
next() {
|
|
2833
|
-
fill();
|
|
2834
|
-
if (buffered?.done !== false) return buffered ?? { done: true, value: undefined };
|
|
2835
|
-
const result = buffered;
|
|
2836
|
-
buffered = null;
|
|
2837
|
-
bufferedReady = false;
|
|
2838
|
-
// Ordinary relational iterators are advanced by one answer so their
|
|
2839
|
-
// exhausted state is known before this answer reaches the caller.
|
|
2840
|
-
fill();
|
|
2841
|
-
return result;
|
|
2842
|
-
},
|
|
2843
|
-
return(value) {
|
|
2844
|
-
if (!closed) iterator.return?.();
|
|
2845
|
-
closed = true;
|
|
2846
|
-
buffered = { done: true, value };
|
|
2847
|
-
bufferedReady = true;
|
|
2848
|
-
return buffered;
|
|
2849
|
-
},
|
|
2850
|
-
throw(error) {
|
|
2851
|
-
closed = true;
|
|
2852
|
-
if (typeof iterator.throw === 'function') return iterator.throw(error);
|
|
2853
|
-
throw error;
|
|
2854
|
-
},
|
|
2855
|
-
prepareClose(reason) {
|
|
2856
|
-
iterator.prepareClose?.(reason);
|
|
2857
|
-
},
|
|
2858
|
-
hasPendingAlternatives() {
|
|
2859
|
-
fill();
|
|
2860
|
-
return buffered?.done === false;
|
|
2861
|
-
},
|
|
2862
|
-
[Symbol.iterator]() { return this; },
|
|
2863
|
-
};
|
|
2864
|
-
}
|
|
2865
|
-
|
|
2866
|
-
function choicepointAwareIterator(iterator) {
|
|
2867
|
-
return typeof iterator?.hasPendingAlternatives === 'function'
|
|
2868
|
-
? iterator
|
|
2869
|
-
: lookaheadChoiceIterator(iterator);
|
|
2885
|
+
// An unannotated suspended iterator is itself an untried continuation.
|
|
2886
|
+
// Advancing it here would speculatively execute Prolog search (and possibly
|
|
2887
|
+
// effects) merely to discover whether a later answer succeeds.
|
|
2888
|
+
return typeof predicate !== 'function' || predicate.call(iterator) !== false;
|
|
2870
2889
|
}
|
|
2871
2890
|
|
|
2872
2891
|
function pushResumeBuiltinFrame(stack, iterator, goals, depth, active) {
|
|
2873
|
-
iterator = choicepointAwareIterator(iterator);
|
|
2874
2892
|
if (!iteratorMayHavePendingAlternatives(iterator)) {
|
|
2875
2893
|
// Finish the suspended generator now so its finally blocks release child
|
|
2876
2894
|
// solvers, aggregate statistics, and perform predicate-local cleanup.
|
package/src/standard-library.js
CHANGED
|
@@ -274,7 +274,14 @@ function* statisticsBuiltin({ solver, env }) {
|
|
|
274
274
|
yield env;
|
|
275
275
|
}
|
|
276
276
|
|
|
277
|
-
function
|
|
277
|
+
function statisticsValueBuiltin(context) {
|
|
278
|
+
const state = { pending: false };
|
|
279
|
+
const iterator = statisticsValueSolutions(context, state);
|
|
280
|
+
iterator.hasPendingAlternatives = () => state.pending;
|
|
281
|
+
return iterator;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function* statisticsValueSolutions({ solver, goal, env }, state) {
|
|
278
285
|
const snapshot = runtimeStatistics(solver);
|
|
279
286
|
const key = deref(goal.args[0], env);
|
|
280
287
|
const entries = key.type === VAR
|
|
@@ -288,10 +295,15 @@ function* statisticsValueBuiltin({ solver, goal, env }) {
|
|
|
288
295
|
throw new PrologError('domain_error(statistics_key)', key);
|
|
289
296
|
}
|
|
290
297
|
|
|
291
|
-
for (
|
|
298
|
+
for (let index = 0; index < entries.length; index++) {
|
|
299
|
+
const [name, value] = entries[index];
|
|
292
300
|
const next = env.clone();
|
|
293
|
-
if (unify(goal.args[0], atom(name), next) && unify(goal.args[1], numberTerm(value), next))
|
|
301
|
+
if (unify(goal.args[0], atom(name), next) && unify(goal.args[1], numberTerm(value), next)) {
|
|
302
|
+
state.pending = index + 1 < entries.length;
|
|
303
|
+
yield next;
|
|
304
|
+
}
|
|
294
305
|
}
|
|
306
|
+
state.pending = false;
|
|
295
307
|
}
|
|
296
308
|
|
|
297
309
|
export function createEyePrologRegistry() {
|
package/src/term.js
CHANGED
|
@@ -65,7 +65,7 @@ export class CompactListTerm {
|
|
|
65
65
|
}
|
|
66
66
|
return this._args;
|
|
67
67
|
}
|
|
68
|
-
mayContainVariable(name) {
|
|
68
|
+
mayContainVariable(name, env = null) {
|
|
69
69
|
if (String(name).startsWith(this._variablePrefix)) {
|
|
70
70
|
const indexText = String(name).slice(this._variablePrefix.length);
|
|
71
71
|
if (/^\d+$/.test(indexText)) {
|
|
@@ -73,9 +73,12 @@ export class CompactListTerm {
|
|
|
73
73
|
if (index >= this._offset && index < this._offset + this._compactLength) return true;
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
|
-
//
|
|
77
|
-
// an
|
|
78
|
-
//
|
|
76
|
+
// An Env keeps the exact backtrackable set of cells whose current values
|
|
77
|
+
// may reach an external variable. Without one, retain the shared
|
|
78
|
+
// high-water mark as a conservative public-API fallback.
|
|
79
|
+
if (env?.compactListMayReachExternalVariable != null) {
|
|
80
|
+
return env.compactListMayReachExternalVariable(this._compactState, this._offset);
|
|
81
|
+
}
|
|
79
82
|
return this._compactState.maxPossiblyBoundIndex >= this._offset;
|
|
80
83
|
}
|
|
81
84
|
}
|
|
@@ -124,6 +127,11 @@ export class Env {
|
|
|
124
127
|
this._backtrackableBlackboard = null;
|
|
125
128
|
this._occursCheckHandler = null;
|
|
126
129
|
this._localVariables = null;
|
|
130
|
+
// Origins carried by variables that temporarily represent compact-list
|
|
131
|
+
// cells. Indexed by logical variable name because callers may reconstruct
|
|
132
|
+
// an equivalent Term object while environments retain name identity.
|
|
133
|
+
this._compactVariableOrigins = null;
|
|
134
|
+
this._compactVariableRisks = null;
|
|
127
135
|
}
|
|
128
136
|
clone() {
|
|
129
137
|
// Most speculative environments are either rejected without a binding or
|
|
@@ -142,6 +150,8 @@ export class Env {
|
|
|
142
150
|
clone._backtrackableBlackboard = this._backtrackableBlackboard;
|
|
143
151
|
clone._occursCheckHandler = this._occursCheckHandler;
|
|
144
152
|
clone._localVariables = this._localVariables;
|
|
153
|
+
clone._compactVariableOrigins = this._compactVariableOrigins;
|
|
154
|
+
clone._compactVariableRisks = this._compactVariableRisks;
|
|
145
155
|
return clone;
|
|
146
156
|
}
|
|
147
157
|
setOccursCheckHandler(handler) {
|
|
@@ -164,8 +174,80 @@ export class Env {
|
|
|
164
174
|
// Execution callbacks belong to the Solver driving this Env, not to the
|
|
165
175
|
// logical branch being adopted from an inner attribute-hook call.
|
|
166
176
|
this._localVariables = other._localVariables;
|
|
177
|
+
this._compactVariableOrigins = other._compactVariableOrigins;
|
|
178
|
+
this._compactVariableRisks = other._compactVariableRisks;
|
|
167
179
|
return this;
|
|
168
180
|
}
|
|
181
|
+
compactVariableOrigins(name) {
|
|
182
|
+
return this._compactVariableOrigins?.get(name) ?? null;
|
|
183
|
+
}
|
|
184
|
+
transferCompactVariableOrigins(sourceName, targetName, origins) {
|
|
185
|
+
const next = new Map(this._compactVariableOrigins ?? []);
|
|
186
|
+
if (sourceName !== targetName) next.delete(sourceName);
|
|
187
|
+
next.set(targetName, origins);
|
|
188
|
+
this._compactVariableOrigins = next.size === 0 ? null : next;
|
|
189
|
+
}
|
|
190
|
+
removeCompactVariableOrigins(name) {
|
|
191
|
+
if (this._compactVariableOrigins?.has(name) !== true) return;
|
|
192
|
+
const next = new Map(this._compactVariableOrigins);
|
|
193
|
+
next.delete(name);
|
|
194
|
+
this._compactVariableOrigins = next.size === 0 ? null : next;
|
|
195
|
+
}
|
|
196
|
+
addCompactVariableRisks(origins) {
|
|
197
|
+
if (origins.length === 0) return;
|
|
198
|
+
const next = new Map(this._compactVariableRisks ?? []);
|
|
199
|
+
for (const { state, index } of origins) {
|
|
200
|
+
const current = next.get(state) ?? null;
|
|
201
|
+
let top = current?.top ?? null;
|
|
202
|
+
const removed = current?.removed ?? null;
|
|
203
|
+
while (top != null && removed?.has(top)) top = top.parent;
|
|
204
|
+
// Immediate variable-to-variable handoffs carry the same origin. Avoid
|
|
205
|
+
// stacking duplicate risk entries along that common alias chain.
|
|
206
|
+
if (top?.index === index) continue;
|
|
207
|
+
next.set(state, { top: { index, parent: top }, removed });
|
|
208
|
+
}
|
|
209
|
+
this._compactVariableRisks = next;
|
|
210
|
+
}
|
|
211
|
+
removeCompactVariableRisks(origins) {
|
|
212
|
+
if (origins.length === 0 || this._compactVariableRisks == null) return;
|
|
213
|
+
const next = new Map(this._compactVariableRisks);
|
|
214
|
+
for (const { state, index } of origins) {
|
|
215
|
+
const current = next.get(state);
|
|
216
|
+
if (current == null) continue;
|
|
217
|
+
let top = current.top;
|
|
218
|
+
let removed = current.removed;
|
|
219
|
+
while (top != null && removed?.has(top)) top = top.parent;
|
|
220
|
+
if (top?.index === index) {
|
|
221
|
+
top = top.parent;
|
|
222
|
+
while (top != null && removed?.has(top)) top = top.parent;
|
|
223
|
+
} else {
|
|
224
|
+
// Out-of-order handoffs are uncommon, but remain exact: tombstone
|
|
225
|
+
// every active entry for this origin without rewriting the persistent
|
|
226
|
+
// stack shared by sibling environments.
|
|
227
|
+
let scan = top;
|
|
228
|
+
let changed = false;
|
|
229
|
+
while (scan != null) {
|
|
230
|
+
if (scan.index === index && removed?.has(scan) !== true) {
|
|
231
|
+
if (!changed) removed = new Set(removed ?? []);
|
|
232
|
+
removed.add(scan);
|
|
233
|
+
changed = true;
|
|
234
|
+
}
|
|
235
|
+
scan = scan.parent;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (top == null) next.delete(state);
|
|
239
|
+
else next.set(state, { top, removed });
|
|
240
|
+
}
|
|
241
|
+
this._compactVariableRisks = next.size === 0 ? null : next;
|
|
242
|
+
}
|
|
243
|
+
compactListMayReachExternalVariable(state, offset) {
|
|
244
|
+
const current = this._compactVariableRisks?.get(state);
|
|
245
|
+
if (current == null) return false;
|
|
246
|
+
for (let entry = current.top; entry != null; entry = entry.parent) {
|
|
247
|
+
if (current.removed?.has(entry) !== true && entry.index >= offset) return true;
|
|
248
|
+
}
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
169
251
|
getBacktrackableBlackboard(key) {
|
|
170
252
|
return this._backtrackableBlackboard?.get(key);
|
|
171
253
|
}
|
|
@@ -605,7 +687,7 @@ function occursUncached(variableName, term, env) {
|
|
|
605
687
|
if (binding !== undefined) stack.push(binding);
|
|
606
688
|
continue;
|
|
607
689
|
}
|
|
608
|
-
if (isCompactList(current) && !current.mayContainVariable(variableName)) continue;
|
|
690
|
+
if (isCompactList(current) && !current.mayContainVariable(variableName, env)) continue;
|
|
609
691
|
if (current?.type !== COMPOUND || seenTerms.has(current)) continue;
|
|
610
692
|
seenTerms.add(current);
|
|
611
693
|
for (let i = 0; i < current.arity; i++) stack.push(current.args[i]);
|
|
@@ -693,11 +775,11 @@ export function unify(left, right, env, options = {}) {
|
|
|
693
775
|
const aLocalDcg = env?.isLocalVariable(a.name) === true &&
|
|
694
776
|
a.name.startsWith('\u0000dcg') && b.name.startsWith('\u0000dcg');
|
|
695
777
|
if (aLocalDcg) {
|
|
696
|
-
|
|
778
|
+
transferCompactVariableOrigins(b, a, env);
|
|
697
779
|
env?.finalizePrologAttributeAlias?.(b.name, a.name);
|
|
698
780
|
env.bind(b.name, a);
|
|
699
781
|
} else {
|
|
700
|
-
|
|
782
|
+
transferCompactVariableOrigins(a, b, env);
|
|
701
783
|
env?.finalizePrologAttributeAlias?.(a.name, b.name);
|
|
702
784
|
env.bind(a.name, b);
|
|
703
785
|
}
|
|
@@ -715,7 +797,7 @@ export function unify(left, right, env, options = {}) {
|
|
|
715
797
|
b = deref(b, env);
|
|
716
798
|
if (a.type !== VAR) { stack.push([a, b]); continue; }
|
|
717
799
|
}
|
|
718
|
-
markCompactVariableBound(a);
|
|
800
|
+
markCompactVariableBound(a, b, env);
|
|
719
801
|
env?.dropPrologAttributes?.(a.name);
|
|
720
802
|
env.bind(a.name, b);
|
|
721
803
|
if (aLocal) env.forgetLocalVariable(a.name);
|
|
@@ -733,7 +815,7 @@ export function unify(left, right, env, options = {}) {
|
|
|
733
815
|
a = deref(a, env);
|
|
734
816
|
if (b.type !== VAR) { stack.push([a, b]); continue; }
|
|
735
817
|
}
|
|
736
|
-
markCompactVariableBound(b);
|
|
818
|
+
markCompactVariableBound(b, a, env);
|
|
737
819
|
env?.dropPrologAttributes?.(b.name);
|
|
738
820
|
env.bind(b.name, a);
|
|
739
821
|
if (bLocal) env.forgetLocalVariable(b.name);
|
|
@@ -761,10 +843,50 @@ export function unify(left, right, env, options = {}) {
|
|
|
761
843
|
return true;
|
|
762
844
|
}
|
|
763
845
|
|
|
764
|
-
function
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
846
|
+
function compactVariableOrigins(term, env) {
|
|
847
|
+
const remembered = term?.type === VAR ? env?.compactVariableOrigins(term.name) : null;
|
|
848
|
+
const origins = remembered == null ? [] : [...remembered];
|
|
849
|
+
if (term?._compactState != null && term._compactIndex != null &&
|
|
850
|
+
!origins.some(({ state, index }) => state === term._compactState && index === term._compactIndex)) {
|
|
851
|
+
origins.push({ state: term._compactState, index: term._compactIndex });
|
|
852
|
+
}
|
|
853
|
+
return origins;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
function transferCompactVariableOrigins(source, target, env) {
|
|
857
|
+
const sourceOrigins = compactVariableOrigins(source, env);
|
|
858
|
+
if (sourceOrigins.length === 0 || target?.type !== VAR) return;
|
|
859
|
+
const targetOrigins = compactVariableOrigins(target, env);
|
|
860
|
+
for (const origin of sourceOrigins) {
|
|
861
|
+
if (!targetOrigins.some(({ state, index }) => state === origin.state && index === origin.index)) {
|
|
862
|
+
targetOrigins.push(origin);
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
env?.addCompactVariableRisks(sourceOrigins);
|
|
866
|
+
env?.transferCompactVariableOrigins(source.name, target.name, targetOrigins);
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
function markCompactVariableBound(term, value, env) {
|
|
870
|
+
const origins = compactVariableOrigins(term, env);
|
|
871
|
+
if (origins.length === 0) return;
|
|
872
|
+
// A compact cell commonly passes through a fresh clause variable before it
|
|
873
|
+
// receives its actual value. Carry the provenance along that alias. Only a
|
|
874
|
+
// non-scalar value can make an unrelated logical variable reachable from
|
|
875
|
+
// the compact list, so ground atoms and numbers need not poison the whole
|
|
876
|
+
// suffix for subsequent finite-tree occurs checks.
|
|
877
|
+
if (value?.type === VAR) {
|
|
878
|
+
transferCompactVariableOrigins(term, value, env);
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
if (isScalar(value)) {
|
|
882
|
+
env?.removeCompactVariableRisks(origins);
|
|
883
|
+
env?.removeCompactVariableOrigins(term.name);
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
env?.addCompactVariableRisks(origins);
|
|
887
|
+
env?.removeCompactVariableOrigins(term.name);
|
|
888
|
+
for (const { state, index } of origins) {
|
|
889
|
+
if (index > state.maxPossiblyBoundIndex) state.maxPossiblyBoundIndex = index;
|
|
768
890
|
}
|
|
769
891
|
}
|
|
770
892
|
|