eyeling 1.26.2 → 1.26.3

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.
@@ -5595,6 +5595,10 @@ function __cloneSubst(subst) {
5595
5595
  return out;
5596
5596
  }
5597
5597
 
5598
+ function __defineHiddenWritable(obj, name, value) {
5599
+ Object.defineProperty(obj, name, { value, enumerable: false, writable: true });
5600
+ }
5601
+
5598
5602
  let version = 'dev';
5599
5603
  try {
5600
5604
  // Node: keep package.json version if available
@@ -5906,44 +5910,29 @@ function __logNaturalPriorityFromTerm(t) {
5906
5910
  function __computeMaxScopedClosurePriorityNeeded(forwardRules, backRules) {
5907
5911
  let maxP = 0;
5908
5912
 
5913
+ function bumpMaxPriority(term) {
5914
+ if (term instanceof GraphTerm) return;
5915
+ if (term instanceof Var) {
5916
+ if (maxP < 1) maxP = 1;
5917
+ return;
5918
+ }
5919
+ const p0 = __logNaturalPriorityFromTerm(term);
5920
+ const priority = p0 !== null ? p0 : 1;
5921
+ if (priority > maxP) maxP = priority;
5922
+ }
5923
+
5909
5924
  function scanTriple(tr) {
5910
5925
  if (!(tr && tr.p instanceof Iri)) return;
5911
5926
  const pv = tr.p.value;
5912
5927
 
5913
5928
  // log:collectAllIn / log:forAllIn use the object position for the priority.
5914
5929
  if (pv === LOG_NS + 'collectAllIn' || pv === LOG_NS + 'forAllIn') {
5915
- // Explicit scope graphs are immediate and do not require a closure.
5916
- if (tr.o instanceof GraphTerm) return;
5917
- // Variable or non-numeric object => default priority 1 (if used).
5918
- if (tr.o instanceof Var) {
5919
- if (maxP < 1) maxP = 1;
5920
- return;
5921
- }
5922
- const p0 = __logNaturalPriorityFromTerm(tr.o);
5923
- if (p0 !== null) {
5924
- if (p0 > maxP) maxP = p0;
5925
- } else {
5926
- if (maxP < 1) maxP = 1;
5927
- }
5930
+ bumpMaxPriority(tr.o);
5928
5931
  return;
5929
5932
  }
5930
5933
 
5931
5934
  // log:includes / log:notIncludes use the subject position for the priority.
5932
- if (pv === LOG_NS + 'includes' || pv === LOG_NS + 'notIncludes') {
5933
- // Explicit scope graphs are immediate and do not require a closure.
5934
- if (tr.s instanceof GraphTerm) return;
5935
- // Variable or non-numeric subject => default priority 1 (if used).
5936
- if (tr.s instanceof Var) {
5937
- if (maxP < 1) maxP = 1;
5938
- return;
5939
- }
5940
- const p0 = __logNaturalPriorityFromTerm(tr.s);
5941
- if (p0 !== null) {
5942
- if (p0 > maxP) maxP = p0;
5943
- } else {
5944
- if (maxP < 1) maxP = 1;
5945
- }
5946
- }
5935
+ if (pv === LOG_NS + 'includes' || pv === LOG_NS + 'notIncludes') bumpMaxPriority(tr.s);
5947
5936
  }
5948
5937
 
5949
5938
  for (const r of forwardRules) {
@@ -5980,7 +5969,6 @@ function __varOccursElsewhereInPremise(premise, name, idx, field) {
5980
5969
  return false;
5981
5970
  }
5982
5971
 
5983
-
5984
5972
  function __scopedPriorityForTerm(t) {
5985
5973
  if (t instanceof GraphTerm) return 0;
5986
5974
  const p0 = __logNaturalPriorityFromTerm(t);
@@ -6122,7 +6110,6 @@ function __computeForwardRuleScopedStrata(forwardRules) {
6122
6110
  if (!changed) break;
6123
6111
  }
6124
6112
 
6125
-
6126
6113
  maxLevel = 0;
6127
6114
  for (let i = 0; i < forwardRules.length; i++) {
6128
6115
  __setForwardRuleScopedStratumInfo(forwardRules[i], levels[i]);
@@ -6136,42 +6123,33 @@ function __computeForwardRuleScopedSkipInfo(rule) {
6136
6123
  let needsSnap = false;
6137
6124
  let requiredLevel = 0;
6138
6125
 
6126
+ function addScopedUse(scopeTerm, idx, field) {
6127
+ if (scopeTerm instanceof GraphTerm) return true; // explicit scope
6128
+
6129
+ // If scope term is a Var that appears elsewhere, it might be bound to a GraphTerm.
6130
+ // Be conservative and do not skip in that case.
6131
+ if (scopeTerm instanceof Var) {
6132
+ if (__varOccursElsewhereInPremise(rule.premise, scopeTerm.name, idx, field)) return false;
6133
+ needsSnap = true;
6134
+ requiredLevel = Math.max(requiredLevel, 1);
6135
+ return true;
6136
+ }
6137
+
6138
+ needsSnap = true;
6139
+ const p0 = __logNaturalPriorityFromTerm(scopeTerm);
6140
+ requiredLevel = Math.max(requiredLevel, p0 !== null ? p0 : 1);
6141
+ return true;
6142
+ }
6143
+
6139
6144
  for (let i = 0; i < rule.premise.length; i++) {
6140
6145
  const tr = rule.premise[i];
6141
6146
  if (!(tr && tr.p instanceof Iri)) continue;
6142
6147
  const pv = tr.p.value;
6143
6148
 
6144
6149
  if (pv === LOG_NS + 'collectAllIn' || pv === LOG_NS + 'forAllIn') {
6145
- if (tr.o instanceof GraphTerm) continue; // explicit scope
6146
- // If scope term is a Var that appears elsewhere, it might be bound to a GraphTerm.
6147
- // Be conservative and do not skip in that case.
6148
- if (tr.o instanceof Var) {
6149
- if (__varOccursElsewhereInPremise(rule.premise, tr.o.name, i, 'o')) return null;
6150
- needsSnap = true;
6151
- requiredLevel = Math.max(requiredLevel, 1);
6152
- } else {
6153
- needsSnap = true;
6154
- let prio = 1;
6155
- const p0 = __logNaturalPriorityFromTerm(tr.o);
6156
- if (p0 !== null) prio = p0;
6157
- requiredLevel = Math.max(requiredLevel, prio);
6158
- }
6159
- continue;
6160
- }
6161
-
6162
- if (pv === LOG_NS + 'includes' || pv === LOG_NS + 'notIncludes') {
6163
- if (tr.s instanceof GraphTerm) continue; // explicit scope
6164
- if (tr.s instanceof Var) {
6165
- if (__varOccursElsewhereInPremise(rule.premise, tr.s.name, i, 's')) return null;
6166
- needsSnap = true;
6167
- requiredLevel = Math.max(requiredLevel, 1);
6168
- } else {
6169
- needsSnap = true;
6170
- let prio = 1;
6171
- const p0 = __logNaturalPriorityFromTerm(tr.s);
6172
- if (p0 !== null) prio = p0;
6173
- requiredLevel = Math.max(requiredLevel, prio);
6174
- }
6150
+ if (!addScopedUse(tr.o, i, 'o')) return null;
6151
+ } else if (pv === LOG_NS + 'includes' || pv === LOG_NS + 'notIncludes') {
6152
+ if (!addScopedUse(tr.s, i, 's')) return null;
6175
6153
  }
6176
6154
  }
6177
6155
 
@@ -6354,140 +6332,70 @@ function skolemizeTripleForHeadBlanks(tr, headBlankLabels, mapping, skCounter, f
6354
6332
  // Alpha equivalence helpers
6355
6333
  // ===========================================================================
6356
6334
 
6357
- function termsEqual(a, b) {
6358
- if (a === b) return true;
6359
- if (!a || !b) return false;
6360
- if (a.__tid && b.__tid && a.__tid === b.__tid) return true;
6361
-
6362
- // rdf:nil is equivalent to the empty list ()
6363
- if (a instanceof Iri && a.value === RDF_NIL_IRI && b instanceof ListTerm && b.elems.length === 0) return true;
6364
- if (b instanceof Iri && b.value === RDF_NIL_IRI && a instanceof ListTerm && a.elems.length === 0) return true;
6365
- if (a.constructor !== b.constructor) return false;
6366
-
6367
- if (a instanceof Iri) return a.value === b.value;
6368
-
6369
- if (a instanceof Literal) {
6370
- if (a.value === b.value) return true;
6371
-
6372
- // Plain "abc" == "abc"^^xsd:string (but not language-tagged strings)
6373
- if (literalsEquivalentAsXsdString(a.value, b.value)) return true;
6374
-
6375
- // Keep in sync with unifyTerm(): numeric-value equality, datatype-aware.
6376
- const ai = parseNumericLiteralInfo(a);
6377
- const bi = parseNumericLiteralInfo(b);
6335
+ function __isRdfNilEmptyListPair(a, b) {
6336
+ return a instanceof Iri && a.value === RDF_NIL_IRI && b instanceof ListTerm && b.elems.length === 0;
6337
+ }
6378
6338
 
6379
- if (ai && bi) {
6380
- // Same datatype => compare values
6381
- if (ai.dt === bi.dt) {
6382
- if (ai.kind === 'bigint' && bi.kind === 'bigint') return ai.value === bi.value;
6339
+ function __numericInfosSameDatatypeEqual(ai, bi, exactDecimal) {
6340
+ if (!ai || !bi || ai.dt !== bi.dt) return false;
6341
+ if (ai.kind === 'bigint' && bi.kind === 'bigint') return ai.value === bi.value;
6383
6342
 
6384
- const an = ai.kind === 'bigint' ? Number(ai.value) : ai.value;
6385
- const bn = bi.kind === 'bigint' ? Number(bi.value) : bi.value;
6386
- return !Number.isNaN(an) && !Number.isNaN(bn) && an === bn;
6387
- }
6343
+ if (exactDecimal && ai.dt === XSD_NS + 'decimal') {
6344
+ const da = parseXsdDecimalToBigIntScale(ai.lexStr);
6345
+ const db = parseXsdDecimalToBigIntScale(bi.lexStr);
6346
+ if (da && db) {
6347
+ const scale = Math.max(da.scale, db.scale);
6348
+ return da.num * pow10n(scale - da.scale) === db.num * pow10n(scale - db.scale);
6388
6349
  }
6389
-
6390
- return false;
6391
6350
  }
6392
6351
 
6393
- if (a instanceof Var) return a.name === b.name;
6394
- if (a instanceof Blank) return a.label === b.label;
6395
-
6396
- if (a instanceof ListTerm) {
6397
- if (a.elems.length !== b.elems.length) return false;
6398
- for (let i = 0; i < a.elems.length; i++) {
6399
- if (!termsEqual(a.elems[i], b.elems[i])) return false;
6400
- }
6401
- return true;
6402
- }
6352
+ const an = ai.kind === 'bigint' ? Number(ai.value) : ai.value;
6353
+ const bn = bi.kind === 'bigint' ? Number(bi.value) : bi.value;
6354
+ return !Number.isNaN(an) && !Number.isNaN(bn) && an === bn;
6355
+ }
6403
6356
 
6404
- if (a instanceof OpenListTerm) {
6405
- if (a.tailVar !== b.tailVar) return false;
6406
- if (a.prefix.length !== b.prefix.length) return false;
6407
- for (let i = 0; i < a.prefix.length; i++) {
6408
- if (!termsEqual(a.prefix[i], b.prefix[i])) return false;
6409
- }
6410
- return true;
6411
- }
6357
+ function __literalsEqual(a, b, exactDecimal) {
6358
+ if (a.value === b.value) return true;
6359
+ if (literalsEquivalentAsXsdString(a.value, b.value)) return true;
6360
+ return __numericInfosSameDatatypeEqual(parseNumericLiteralInfo(a), parseNumericLiteralInfo(b), exactDecimal);
6361
+ }
6412
6362
 
6413
- if (a instanceof GraphTerm) {
6414
- return alphaEqGraphTriples(a.triples, b.triples);
6363
+ function __termArraysEqual(xs, ys, eq) {
6364
+ if (xs.length !== ys.length) return false;
6365
+ for (let i = 0; i < xs.length; i++) {
6366
+ if (!eq(xs[i], ys[i])) return false;
6415
6367
  }
6416
-
6417
- return false;
6368
+ return true;
6418
6369
  }
6419
6370
 
6420
- function termsEqualNoIntDecimal(a, b) {
6371
+ function __termsEqual(a, b, exactDecimal) {
6421
6372
  if (a === b) return true;
6422
6373
  if (!a || !b) return false;
6423
6374
  if (a.__tid && b.__tid && a.__tid === b.__tid) return true;
6424
6375
 
6425
6376
  // rdf:nil is equivalent to the empty list ()
6426
- if (a instanceof Iri && a.value === RDF_NIL_IRI && b instanceof ListTerm && b.elems.length === 0) return true;
6427
- if (b instanceof Iri && b.value === RDF_NIL_IRI && a instanceof ListTerm && a.elems.length === 0) return true;
6377
+ if (__isRdfNilEmptyListPair(a, b) || __isRdfNilEmptyListPair(b, a)) return true;
6428
6378
  if (a.constructor !== b.constructor) return false;
6429
6379
 
6430
6380
  if (a instanceof Iri) return a.value === b.value;
6431
-
6432
- if (a instanceof Literal) {
6433
- if (a.value === b.value) return true;
6434
-
6435
- // Plain "abc" == "abc"^^xsd:string (but not language-tagged)
6436
- if (literalsEquivalentAsXsdString(a.value, b.value)) return true;
6437
-
6438
- // Numeric equality ONLY when datatypes agree (no integer<->decimal here)
6439
- const ai = parseNumericLiteralInfo(a);
6440
- const bi = parseNumericLiteralInfo(b);
6441
- if (ai && bi && ai.dt === bi.dt) {
6442
- // integer: exact bigint
6443
- if (ai.kind === 'bigint' && bi.kind === 'bigint') return ai.value === bi.value;
6444
-
6445
- // decimal: compare exactly via num/scale if possible
6446
- if (ai.dt === XSD_NS + 'decimal') {
6447
- const da = parseXsdDecimalToBigIntScale(ai.lexStr);
6448
- const db = parseXsdDecimalToBigIntScale(bi.lexStr);
6449
- if (da && db) {
6450
- const scale = Math.max(da.scale, db.scale);
6451
- const na = da.num * pow10n(scale - da.scale);
6452
- const nb = db.num * pow10n(scale - db.scale);
6453
- return na === nb;
6454
- }
6455
- }
6456
-
6457
- // double/float-ish: JS number (same as your normal same-dt path)
6458
- const an = ai.kind === 'bigint' ? Number(ai.value) : ai.value;
6459
- const bn = bi.kind === 'bigint' ? Number(bi.value) : bi.value;
6460
- return !Number.isNaN(an) && !Number.isNaN(bn) && an === bn;
6461
- }
6462
-
6463
- return false;
6464
- }
6465
-
6381
+ if (a instanceof Literal) return __literalsEqual(a, b, exactDecimal);
6466
6382
  if (a instanceof Var) return a.name === b.name;
6467
6383
  if (a instanceof Blank) return a.label === b.label;
6384
+ if (a instanceof ListTerm) return __termArraysEqual(a.elems, b.elems, (x, y) => __termsEqual(x, y, exactDecimal));
6385
+ if (a instanceof OpenListTerm)
6386
+ return a.tailVar === b.tailVar && __termArraysEqual(a.prefix, b.prefix, (x, y) => __termsEqual(x, y, exactDecimal));
6387
+ if (a instanceof GraphTerm) return alphaEqGraphTriples(a.triples, b.triples);
6388
+ return false;
6389
+ }
6468
6390
 
6469
- if (a instanceof ListTerm) {
6470
- if (a.elems.length !== b.elems.length) return false;
6471
- for (let i = 0; i < a.elems.length; i++) {
6472
- if (!termsEqualNoIntDecimal(a.elems[i], b.elems[i])) return false;
6473
- }
6474
- return true;
6475
- }
6476
-
6477
- if (a instanceof OpenListTerm) {
6478
- if (a.tailVar !== b.tailVar) return false;
6479
- if (a.prefix.length !== b.prefix.length) return false;
6480
- for (let i = 0; i < a.prefix.length; i++) {
6481
- if (!termsEqualNoIntDecimal(a.prefix[i], b.prefix[i])) return false;
6482
- }
6483
- return true;
6484
- }
6485
-
6486
- if (a instanceof GraphTerm) {
6487
- return alphaEqGraphTriples(a.triples, b.triples);
6488
- }
6391
+ function termsEqual(a, b) {
6392
+ // Keep in sync with unifyTerm(): numeric-value equality, datatype-aware.
6393
+ return __termsEqual(a, b, false);
6394
+ }
6489
6395
 
6490
- return false;
6396
+ function termsEqualNoIntDecimal(a, b) {
6397
+ // Numeric equality ONLY when datatypes agree; decimals are compared exactly.
6398
+ return __termsEqual(a, b, true);
6491
6399
  }
6492
6400
 
6493
6401
  function triplesEqual(a, b) {
@@ -6847,66 +6755,18 @@ function ensureFactIndexes(facts) {
6847
6755
  )
6848
6756
  return;
6849
6757
 
6850
- Object.defineProperty(facts, '__byPred', {
6851
- value: new Map(),
6852
- enumerable: false,
6853
- writable: true,
6854
- });
6855
- Object.defineProperty(facts, '__byPS', {
6856
- value: new Map(),
6857
- enumerable: false,
6858
- writable: true,
6859
- });
6860
- Object.defineProperty(facts, '__byPO', {
6861
- value: new Map(),
6862
- enumerable: false,
6863
- writable: true,
6864
- });
6865
- Object.defineProperty(facts, '__byPNonFastS', {
6866
- value: new Map(),
6867
- enumerable: false,
6868
- writable: true,
6869
- });
6870
- Object.defineProperty(facts, '__byPNonFastO', {
6871
- value: new Map(),
6872
- enumerable: false,
6873
- writable: true,
6874
- });
6875
- Object.defineProperty(facts, '__varPred', {
6876
- value: [],
6877
- enumerable: false,
6878
- writable: true,
6879
- });
6880
- Object.defineProperty(facts, '__varPredPS', {
6881
- value: new Map(),
6882
- enumerable: false,
6883
- writable: true,
6884
- });
6885
- Object.defineProperty(facts, '__varPredPO', {
6886
- value: new Map(),
6887
- enumerable: false,
6888
- writable: true,
6889
- });
6890
- Object.defineProperty(facts, '__varPredNonFastS', {
6891
- value: [],
6892
- enumerable: false,
6893
- writable: true,
6894
- });
6895
- Object.defineProperty(facts, '__varPredNonFastO', {
6896
- value: [],
6897
- enumerable: false,
6898
- writable: true,
6899
- });
6900
- Object.defineProperty(facts, '__keySet', {
6901
- value: new Set(),
6902
- enumerable: false,
6903
- writable: true,
6904
- });
6905
- Object.defineProperty(facts, '__keySetComplete', {
6906
- value: false,
6907
- enumerable: false,
6908
- writable: true,
6909
- });
6758
+ __defineHiddenWritable(facts, '__byPred', new Map());
6759
+ __defineHiddenWritable(facts, '__byPS', new Map());
6760
+ __defineHiddenWritable(facts, '__byPO', new Map());
6761
+ __defineHiddenWritable(facts, '__byPNonFastS', new Map());
6762
+ __defineHiddenWritable(facts, '__byPNonFastO', new Map());
6763
+ __defineHiddenWritable(facts, '__varPred', []);
6764
+ __defineHiddenWritable(facts, '__varPredPS', new Map());
6765
+ __defineHiddenWritable(facts, '__varPredPO', new Map());
6766
+ __defineHiddenWritable(facts, '__varPredNonFastS', []);
6767
+ __defineHiddenWritable(facts, '__varPredNonFastO', []);
6768
+ __defineHiddenWritable(facts, '__keySet', new Set());
6769
+ __defineHiddenWritable(facts, '__keySetComplete', false);
6910
6770
 
6911
6771
  // Build lookup indexes eagerly, but do not populate the duplicate-detection
6912
6772
  // string Set for every input fact. The predicate/subject/object indexes are
@@ -6934,45 +6794,21 @@ function cloneFactIndexesForSnapshot(src, dest) {
6934
6794
  return out;
6935
6795
  }
6936
6796
 
6937
- Object.defineProperty(dest, '__byPred', { value: cloneArrayMap(src.__byPred), enumerable: false, writable: true });
6938
- Object.defineProperty(dest, '__byPS', { value: cloneNestedArrayMap(src.__byPS), enumerable: false, writable: true });
6939
- Object.defineProperty(dest, '__byPO', { value: cloneNestedArrayMap(src.__byPO), enumerable: false, writable: true });
6940
- Object.defineProperty(dest, '__byPNonFastS', {
6941
- value: cloneArrayMap(src.__byPNonFastS),
6942
- enumerable: false,
6943
- writable: true,
6944
- });
6945
- Object.defineProperty(dest, '__byPNonFastO', {
6946
- value: cloneArrayMap(src.__byPNonFastO),
6947
- enumerable: false,
6948
- writable: true,
6949
- });
6950
- Object.defineProperty(dest, '__varPred', { value: src.__varPred.slice(), enumerable: false, writable: true });
6951
- Object.defineProperty(dest, '__varPredPS', {
6952
- value: cloneArrayMap(src.__varPredPS),
6953
- enumerable: false,
6954
- writable: true,
6955
- });
6956
- Object.defineProperty(dest, '__varPredPO', {
6957
- value: cloneArrayMap(src.__varPredPO),
6958
- enumerable: false,
6959
- writable: true,
6960
- });
6961
- Object.defineProperty(dest, '__varPredNonFastS', {
6962
- value: src.__varPredNonFastS.slice(),
6963
- enumerable: false,
6964
- writable: true,
6965
- });
6966
- Object.defineProperty(dest, '__varPredNonFastO', {
6967
- value: src.__varPredNonFastO.slice(),
6968
- enumerable: false,
6969
- writable: true,
6970
- });
6971
- Object.defineProperty(dest, '__keySet', { value: new Set(src.__keySet), enumerable: false, writable: true });
6972
- Object.defineProperty(dest, '__keySetComplete', { value: !!src.__keySetComplete, enumerable: false, writable: true });
6797
+ __defineHiddenWritable(dest, '__byPred', cloneArrayMap(src.__byPred));
6798
+ __defineHiddenWritable(dest, '__byPS', cloneNestedArrayMap(src.__byPS));
6799
+ __defineHiddenWritable(dest, '__byPO', cloneNestedArrayMap(src.__byPO));
6800
+ __defineHiddenWritable(dest, '__byPNonFastS', cloneArrayMap(src.__byPNonFastS));
6801
+ __defineHiddenWritable(dest, '__byPNonFastO', cloneArrayMap(src.__byPNonFastO));
6802
+ __defineHiddenWritable(dest, '__varPred', src.__varPred.slice());
6803
+ __defineHiddenWritable(dest, '__varPredPS', cloneArrayMap(src.__varPredPS));
6804
+ __defineHiddenWritable(dest, '__varPredPO', cloneArrayMap(src.__varPredPO));
6805
+ __defineHiddenWritable(dest, '__varPredNonFastS', src.__varPredNonFastS.slice());
6806
+ __defineHiddenWritable(dest, '__varPredNonFastO', src.__varPredNonFastO.slice());
6807
+ __defineHiddenWritable(dest, '__keySet', new Set(src.__keySet));
6808
+ __defineHiddenWritable(dest, '__keySetComplete', !!src.__keySetComplete);
6973
6809
  }
6974
6810
 
6975
- function addToIndexArrayMap(map, key, value) {
6811
+ function pushMapArray(map, key, value) {
6976
6812
  let bucket = map.get(key);
6977
6813
  if (!bucket) {
6978
6814
  bucket = [];
@@ -6981,6 +6817,15 @@ function addToIndexArrayMap(map, key, value) {
6981
6817
  bucket.push(value);
6982
6818
  }
6983
6819
 
6820
+ function getOrCreateMap(map, key) {
6821
+ let inner = map.get(key);
6822
+ if (!inner) {
6823
+ inner = new Map();
6824
+ map.set(key, inner);
6825
+ }
6826
+ return inner;
6827
+ }
6828
+
6984
6829
  function indexFact(facts, tr, idx, addKeySet = true) {
6985
6830
  const sk = termLookupKey(tr.s);
6986
6831
  const ok = termLookupKey(tr.o);
@@ -6991,45 +6836,30 @@ function indexFact(facts, tr, idx, addKeySet = true) {
6991
6836
  const pk = tr.p.__tid;
6992
6837
  pkForKey = pk;
6993
6838
 
6994
- let pb = facts.__byPred.get(pk);
6995
- if (!pb) {
6996
- pb = [];
6997
- facts.__byPred.set(pk, pb);
6998
- }
6999
- pb.push(idx);
6839
+ pushMapArray(facts.__byPred, pk, idx);
7000
6840
 
7001
6841
  if (sk !== null) {
7002
- let ps = facts.__byPS.get(pk);
7003
- if (!ps) {
7004
- ps = new Map();
7005
- facts.__byPS.set(pk, ps);
7006
- }
7007
- addToIndexArrayMap(ps, sk, idx);
6842
+ pushMapArray(getOrCreateMap(facts.__byPS, pk), sk, idx);
7008
6843
  } else {
7009
- addToIndexArrayMap(facts.__byPNonFastS, pk, idx);
6844
+ pushMapArray(facts.__byPNonFastS, pk, idx);
7010
6845
  }
7011
6846
 
7012
6847
  if (ok !== null) {
7013
- let po = facts.__byPO.get(pk);
7014
- if (!po) {
7015
- po = new Map();
7016
- facts.__byPO.set(pk, po);
7017
- }
7018
- addToIndexArrayMap(po, ok, idx);
6848
+ pushMapArray(getOrCreateMap(facts.__byPO, pk), ok, idx);
7019
6849
  } else {
7020
- addToIndexArrayMap(facts.__byPNonFastO, pk, idx);
6850
+ pushMapArray(facts.__byPNonFastO, pk, idx);
7021
6851
  }
7022
6852
  } else if (tr.p instanceof Var) {
7023
6853
  facts.__varPred.push(idx);
7024
6854
 
7025
6855
  if (sk !== null) {
7026
- addToIndexArrayMap(facts.__varPredPS, sk, idx);
6856
+ pushMapArray(facts.__varPredPS, sk, idx);
7027
6857
  } else {
7028
6858
  facts.__varPredNonFastS.push(idx);
7029
6859
  }
7030
6860
 
7031
6861
  if (ok !== null) {
7032
- addToIndexArrayMap(facts.__varPredPO, ok, idx);
6862
+ pushMapArray(facts.__varPredPO, ok, idx);
7033
6863
  } else {
7034
6864
  facts.__varPredNonFastO.push(idx);
7035
6865
  }
@@ -7183,16 +7013,8 @@ function makeDerivedRecord(fact, rule, premises, subst, captureExplanations) {
7183
7013
  function ensureBackRuleIndexes(backRules) {
7184
7014
  if (backRules.__byHeadPred && backRules.__wildHeadPred) return;
7185
7015
 
7186
- Object.defineProperty(backRules, '__byHeadPred', {
7187
- value: new Map(),
7188
- enumerable: false,
7189
- writable: true,
7190
- });
7191
- Object.defineProperty(backRules, '__wildHeadPred', {
7192
- value: [],
7193
- enumerable: false,
7194
- writable: true,
7195
- });
7016
+ __defineHiddenWritable(backRules, '__byHeadPred', new Map());
7017
+ __defineHiddenWritable(backRules, '__wildHeadPred', []);
7196
7018
 
7197
7019
  for (const r of backRules) indexBackRule(backRules, r);
7198
7020
  }
@@ -7201,13 +7023,7 @@ function indexBackRule(backRules, r) {
7201
7023
  if (!r || !r.conclusion || r.conclusion.length !== 1) return;
7202
7024
  const head = r.conclusion[0];
7203
7025
  if (head && head.p instanceof Iri) {
7204
- const k = head.p.__tid;
7205
- let bucket = backRules.__byHeadPred.get(k);
7206
- if (!bucket) {
7207
- bucket = [];
7208
- backRules.__byHeadPred.set(k, bucket);
7209
- }
7210
- bucket.push(r);
7026
+ pushMapArray(backRules.__byHeadPred, head.p.__tid, r);
7211
7027
  } else {
7212
7028
  backRules.__wildHeadPred.push(r);
7213
7029
  }
@@ -7242,12 +7058,11 @@ function isSinglePremiseAgendaRuleSafe(r, backRules) {
7242
7058
  return backRules.__wildHeadPred.length === 0;
7243
7059
  }
7244
7060
 
7245
- function mergeSinglePremiseAgendaBuckets() {
7061
+ function mergeSinglePremiseAgendaBuckets(...buckets) {
7246
7062
  let out = null;
7247
7063
  let seen = null;
7248
7064
 
7249
- for (let i = 0; i < arguments.length; i++) {
7250
- const bucket = arguments[i];
7065
+ for (const bucket of buckets) {
7251
7066
  if (!bucket || bucket.length === 0) continue;
7252
7067
 
7253
7068
  if (out === null) {
@@ -7257,8 +7072,7 @@ function mergeSinglePremiseAgendaBuckets() {
7257
7072
  }
7258
7073
 
7259
7074
  if (!seen) seen = new Set(out);
7260
- for (let j = 0; j < bucket.length; j++) {
7261
- const entry = bucket[j];
7075
+ for (const entry of bucket) {
7262
7076
  if (seen.has(entry)) continue;
7263
7077
  seen.add(entry);
7264
7078
  out.push(entry);
@@ -7268,14 +7082,14 @@ function mergeSinglePremiseAgendaBuckets() {
7268
7082
  return out;
7269
7083
  }
7270
7084
 
7271
- function termContainsVarForAgenda(t) {
7085
+ function termContainsVar(t) {
7272
7086
  if (t instanceof Var) return true;
7273
- if (t instanceof ListTerm) return t.elems.some(termContainsVarForAgenda);
7087
+ if (t instanceof ListTerm) return t.elems.some(termContainsVar);
7274
7088
  if (t instanceof OpenListTerm) return true;
7275
7089
  if (t instanceof GraphTerm)
7276
7090
  return t.triples.some(
7277
7091
  (tr) =>
7278
- termContainsVarForAgenda(tr.s) || termContainsVarForAgenda(tr.p) || termContainsVarForAgenda(tr.o),
7092
+ termContainsVar(tr.s) || termContainsVar(tr.p) || termContainsVar(tr.o),
7279
7093
  );
7280
7094
  return false;
7281
7095
  }
@@ -7294,15 +7108,6 @@ function makeSinglePremiseAgendaIndex(forwardRules, backRules) {
7294
7108
  size: 0,
7295
7109
  };
7296
7110
 
7297
- function addToMapArray(m, k, v) {
7298
- let bucket = m.get(k);
7299
- if (!bucket) {
7300
- bucket = [];
7301
- m.set(k, bucket);
7302
- }
7303
- bucket.push(v);
7304
- }
7305
-
7306
7111
  for (let i = 0; i < forwardRules.length; i++) {
7307
7112
  const r = forwardRules[i];
7308
7113
  if (!isSinglePremiseAgendaRuleSafe(r, backRules)) continue;
@@ -7328,29 +7133,15 @@ function makeSinglePremiseAgendaIndex(forwardRules, backRules) {
7328
7133
  index.size += 1;
7329
7134
 
7330
7135
  if (entry.goalPredTid !== null) {
7331
- addToMapArray(index.byPredAll, entry.goalPredTid, entry);
7136
+ pushMapArray(index.byPredAll, entry.goalPredTid, entry);
7332
7137
  index.allIriPred.push(entry);
7333
- if (entry.goalSKey === null && entry.goalOKey === null) addToMapArray(index.byPred, entry.goalPredTid, entry);
7334
- if (entry.goalSKey !== null) {
7335
- let ps = index.byPS.get(entry.goalPredTid);
7336
- if (!ps) {
7337
- ps = new Map();
7338
- index.byPS.set(entry.goalPredTid, ps);
7339
- }
7340
- addToMapArray(ps, entry.goalSKey, entry);
7341
- }
7342
- if (entry.goalOKey !== null) {
7343
- let po = index.byPO.get(entry.goalPredTid);
7344
- if (!po) {
7345
- po = new Map();
7346
- index.byPO.set(entry.goalPredTid, po);
7347
- }
7348
- addToMapArray(po, entry.goalOKey, entry);
7349
- }
7138
+ if (entry.goalSKey === null && entry.goalOKey === null) pushMapArray(index.byPred, entry.goalPredTid, entry);
7139
+ if (entry.goalSKey !== null) pushMapArray(getOrCreateMap(index.byPS, entry.goalPredTid), entry.goalSKey, entry);
7140
+ if (entry.goalOKey !== null) pushMapArray(getOrCreateMap(index.byPO, entry.goalPredTid), entry.goalOKey, entry);
7350
7141
  } else {
7351
7142
  if (entry.goalSKey === null && entry.goalOKey === null) index.wildPred.push(entry);
7352
- if (entry.goalSKey !== null) addToMapArray(index.wildPS, entry.goalSKey, entry);
7353
- if (entry.goalOKey !== null) addToMapArray(index.wildPO, entry.goalOKey, entry);
7143
+ if (entry.goalSKey !== null) pushMapArray(index.wildPS, entry.goalSKey, entry);
7144
+ if (entry.goalOKey !== null) pushMapArray(index.wildPO, entry.goalOKey, entry);
7354
7145
  }
7355
7146
  }
7356
7147
 
@@ -7366,7 +7157,7 @@ function getSinglePremiseAgendaCandidates(index, fact) {
7366
7157
  let exact = null;
7367
7158
  if (fact.p instanceof Iri) {
7368
7159
  const pk = fact.p.__tid;
7369
- if ((sk === null && termContainsVarForAgenda(fact.s)) || (ok === null && termContainsVarForAgenda(fact.o))) {
7160
+ if ((sk === null && termContainsVar(fact.s)) || (ok === null && termContainsVar(fact.o))) {
7370
7161
  // A fact with a variable-bearing subject/object (most importantly a
7371
7162
  // top-level variable fact such as `?S :p ?O.`) can match rules whose
7372
7163
  // premise is fixed in that position. The ordinary `(p,s)` / `(p,o)` lookup
@@ -7721,16 +7512,8 @@ function unifyOpenWithList(prefix, tailv, ys, subst) {
7721
7512
  }
7722
7513
 
7723
7514
  function graphTriplesContainVars(triples) {
7724
- function termHasVar(t) {
7725
- if (t instanceof Var) return true;
7726
- if (t instanceof ListTerm) return t.elems.some(termHasVar);
7727
- if (t instanceof OpenListTerm) return t.prefix.some(termHasVar) || true;
7728
- if (t instanceof GraphTerm) return t.triples.some((tr) => termHasVar(tr.s) || termHasVar(tr.p) || termHasVar(tr.o));
7729
- return false;
7730
- }
7731
-
7732
7515
  for (const tr of triples) {
7733
- if (termHasVar(tr.s) || termHasVar(tr.p) || termHasVar(tr.o)) return true;
7516
+ if (termContainsVar(tr.s) || termContainsVar(tr.p) || termContainsVar(tr.o)) return true;
7734
7517
  }
7735
7518
  return false;
7736
7519
  }