eyeling 1.5.33 → 1.5.34
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/eyeling.js +27 -112
- package/package.json +1 -1
package/eyeling.js
CHANGED
|
@@ -1867,91 +1867,6 @@ function termToJsStringDecoded(t) {
|
|
|
1867
1867
|
return stripQuotes(lex);
|
|
1868
1868
|
}
|
|
1869
1869
|
|
|
1870
|
-
function _jsonPointerUnescape(seg) {
|
|
1871
|
-
// RFC6901: ~1 -> '/', ~0 -> '~'
|
|
1872
|
-
// Any other '~' escape is invalid.
|
|
1873
|
-
let out = "";
|
|
1874
|
-
for (let i = 0; i < seg.length; i++) {
|
|
1875
|
-
const c = seg[i];
|
|
1876
|
-
if (c !== "~") { out += c; continue; }
|
|
1877
|
-
if (i + 1 >= seg.length) return null;
|
|
1878
|
-
const n = seg[i + 1];
|
|
1879
|
-
if (n === "0") out += "~";
|
|
1880
|
-
else if (n === "1") out += "/";
|
|
1881
|
-
else return null;
|
|
1882
|
-
i++;
|
|
1883
|
-
}
|
|
1884
|
-
return out;
|
|
1885
|
-
}
|
|
1886
|
-
|
|
1887
|
-
function _jsonToTerm(v) {
|
|
1888
|
-
if (v === null) return makeStringLiteral("null");
|
|
1889
|
-
if (typeof v === "string") return makeStringLiteral(v);
|
|
1890
|
-
if (typeof v === "number") return new Literal(String(v));
|
|
1891
|
-
if (typeof v === "boolean") return new Literal(v ? "true" : "false");
|
|
1892
|
-
if (Array.isArray(v)) return new ListTerm(v.map(_jsonToTerm));
|
|
1893
|
-
if (typeof v === "object") return makeStringLiteral(JSON.stringify(v));
|
|
1894
|
-
return null;
|
|
1895
|
-
}
|
|
1896
|
-
|
|
1897
|
-
function _jsonPointerLookup(jsonText, pointer) {
|
|
1898
|
-
// Support URI fragment form "#/a/b" (percent-decoded) as well.
|
|
1899
|
-
let ptr = pointer;
|
|
1900
|
-
if (ptr.startsWith("#")) {
|
|
1901
|
-
try { ptr = decodeURIComponent(ptr.slice(1)); } catch (e) { return null; }
|
|
1902
|
-
}
|
|
1903
|
-
|
|
1904
|
-
// Cache per JSON document
|
|
1905
|
-
let entry = jsonPointerCache.get(jsonText);
|
|
1906
|
-
if (!entry) {
|
|
1907
|
-
let parsed = null;
|
|
1908
|
-
try { parsed = JSON.parse(jsonText); } catch (e) { parsed = null; }
|
|
1909
|
-
entry = { parsed, ptrCache: new Map() };
|
|
1910
|
-
jsonPointerCache.set(jsonText, entry);
|
|
1911
|
-
}
|
|
1912
|
-
if (entry.parsed === null) return null;
|
|
1913
|
-
|
|
1914
|
-
// Cache per pointer within this doc
|
|
1915
|
-
if (entry.ptrCache.has(ptr)) return entry.ptrCache.get(ptr);
|
|
1916
|
-
|
|
1917
|
-
let cur = entry.parsed;
|
|
1918
|
-
|
|
1919
|
-
if (ptr === "") {
|
|
1920
|
-
const t = _jsonToTerm(cur);
|
|
1921
|
-
entry.ptrCache.set(ptr, t);
|
|
1922
|
-
return t;
|
|
1923
|
-
}
|
|
1924
|
-
if (!ptr.startsWith("/")) { entry.ptrCache.set(ptr, null); return null; }
|
|
1925
|
-
|
|
1926
|
-
const parts = ptr.split("/").slice(1);
|
|
1927
|
-
for (const raw of parts) {
|
|
1928
|
-
const seg = _jsonPointerUnescape(raw);
|
|
1929
|
-
if (seg === null) { entry.ptrCache.set(ptr, null); return null; }
|
|
1930
|
-
|
|
1931
|
-
if (Array.isArray(cur)) {
|
|
1932
|
-
// JSON Pointer uses array indices as decimal strings
|
|
1933
|
-
if (!/^(0|[1-9]\d*)$/.test(seg)) { entry.ptrCache.set(ptr, null); return null; }
|
|
1934
|
-
const idx = Number(seg);
|
|
1935
|
-
if (!Number.isFinite(idx) || idx < 0 || idx >= cur.length) { entry.ptrCache.set(ptr, null); return null; }
|
|
1936
|
-
cur = cur[idx];
|
|
1937
|
-
continue;
|
|
1938
|
-
}
|
|
1939
|
-
|
|
1940
|
-
if (cur !== null && typeof cur === "object") {
|
|
1941
|
-
if (!Object.prototype.hasOwnProperty.call(cur, seg)) { entry.ptrCache.set(ptr, null); return null; }
|
|
1942
|
-
cur = cur[seg];
|
|
1943
|
-
continue;
|
|
1944
|
-
}
|
|
1945
|
-
|
|
1946
|
-
entry.ptrCache.set(ptr, null);
|
|
1947
|
-
return null;
|
|
1948
|
-
}
|
|
1949
|
-
|
|
1950
|
-
const out = _jsonToTerm(cur);
|
|
1951
|
-
entry.ptrCache.set(ptr, out);
|
|
1952
|
-
return out;
|
|
1953
|
-
}
|
|
1954
|
-
|
|
1955
1870
|
function jsonPointerUnescape(seg) {
|
|
1956
1871
|
// RFC6901: ~1 -> '/', ~0 -> '~'
|
|
1957
1872
|
let out = "";
|
|
@@ -3840,28 +3755,28 @@ function listHasTriple(list, tr) {
|
|
|
3840
3755
|
//
|
|
3841
3756
|
// This is semantics-preserving for the ongoing proof state.
|
|
3842
3757
|
|
|
3843
|
-
function
|
|
3758
|
+
function gcCollectVarsInTerm(t, out) {
|
|
3844
3759
|
if (t instanceof Var) { out.add(t.name); return; }
|
|
3845
|
-
if (t instanceof ListTerm) { for (const e of t.elems)
|
|
3760
|
+
if (t instanceof ListTerm) { for (const e of t.elems) gcCollectVarsInTerm(e, out); return; }
|
|
3846
3761
|
if (t instanceof OpenListTerm) {
|
|
3847
|
-
for (const e of t.prefix)
|
|
3762
|
+
for (const e of t.prefix) gcCollectVarsInTerm(e, out);
|
|
3848
3763
|
out.add(t.tailVar);
|
|
3849
3764
|
return;
|
|
3850
3765
|
}
|
|
3851
|
-
if (t instanceof FormulaTerm) { for (const tr of t.triples)
|
|
3766
|
+
if (t instanceof FormulaTerm) { for (const tr of t.triples) gcCollectVarsInTriple(tr, out); return; }
|
|
3852
3767
|
}
|
|
3853
3768
|
|
|
3854
|
-
function
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
3769
|
+
function gcCollectVarsInTriple(tr, out) {
|
|
3770
|
+
gcCollectVarsInTerm(tr.s, out);
|
|
3771
|
+
gcCollectVarsInTerm(tr.p, out);
|
|
3772
|
+
gcCollectVarsInTerm(tr.o, out);
|
|
3858
3773
|
}
|
|
3859
3774
|
|
|
3860
|
-
function
|
|
3861
|
-
for (const g of goals)
|
|
3775
|
+
function gcCollectVarsInGoals(goals, out) {
|
|
3776
|
+
for (const g of goals) gcCollectVarsInTriple(g, out);
|
|
3862
3777
|
}
|
|
3863
3778
|
|
|
3864
|
-
function
|
|
3779
|
+
function substSizeOver(subst, limit) {
|
|
3865
3780
|
let c = 0;
|
|
3866
3781
|
for (const _k in subst) {
|
|
3867
3782
|
if (++c > limit) return true;
|
|
@@ -3869,9 +3784,9 @@ function _substSizeOver(subst, limit) {
|
|
|
3869
3784
|
return false;
|
|
3870
3785
|
}
|
|
3871
3786
|
|
|
3872
|
-
function
|
|
3787
|
+
function gcCompactForGoals(subst, goals, answerVars) {
|
|
3873
3788
|
const keep = new Set(answerVars);
|
|
3874
|
-
|
|
3789
|
+
gcCollectVarsInGoals(goals, keep);
|
|
3875
3790
|
|
|
3876
3791
|
const expanded = new Set();
|
|
3877
3792
|
const queue = Array.from(keep);
|
|
@@ -3885,7 +3800,7 @@ function _gcCompactForGoals(subst, goals, answerVars) {
|
|
|
3885
3800
|
if (bound === undefined) continue;
|
|
3886
3801
|
|
|
3887
3802
|
const before = keep.size;
|
|
3888
|
-
|
|
3803
|
+
gcCollectVarsInTerm(bound, keep);
|
|
3889
3804
|
if (keep.size !== before) {
|
|
3890
3805
|
for (const nv of keep) {
|
|
3891
3806
|
if (!expanded.has(nv)) queue.push(nv);
|
|
@@ -3900,12 +3815,12 @@ function _gcCompactForGoals(subst, goals, answerVars) {
|
|
|
3900
3815
|
return out;
|
|
3901
3816
|
}
|
|
3902
3817
|
|
|
3903
|
-
function
|
|
3818
|
+
function maybeCompactSubst(subst, goals, answerVars, depth) {
|
|
3904
3819
|
// Keep the fast path fast.
|
|
3905
3820
|
// Only compact when the substitution is clearly getting large, or
|
|
3906
3821
|
// we are in a deep chain (where the quadratic behavior shows up).
|
|
3907
|
-
if (depth < 128 && !
|
|
3908
|
-
return
|
|
3822
|
+
if (depth < 128 && !substSizeOver(subst, 256)) return subst;
|
|
3823
|
+
return gcCompactForGoals(subst, goals, answerVars);
|
|
3909
3824
|
}
|
|
3910
3825
|
|
|
3911
3826
|
|
|
@@ -3921,9 +3836,9 @@ function proveGoals( goals, subst, facts, backRules, depth, visited, varGen ) {
|
|
|
3921
3836
|
|
|
3922
3837
|
// Variables from the original goal list (needed by the caller to instantiate conclusions)
|
|
3923
3838
|
const answerVars = new Set();
|
|
3924
|
-
|
|
3839
|
+
gcCollectVarsInGoals(initialGoals, answerVars);
|
|
3925
3840
|
if (!initialGoals.length) {
|
|
3926
|
-
results.push(
|
|
3841
|
+
results.push(gcCompactForGoals(initialSubst, [], answerVars));
|
|
3927
3842
|
return results;
|
|
3928
3843
|
}
|
|
3929
3844
|
|
|
@@ -3935,7 +3850,7 @@ function proveGoals( goals, subst, facts, backRules, depth, visited, varGen ) {
|
|
|
3935
3850
|
const state = stack.pop();
|
|
3936
3851
|
|
|
3937
3852
|
if (!state.goals.length) {
|
|
3938
|
-
results.push(
|
|
3853
|
+
results.push(gcCompactForGoals(state.subst, [], answerVars));
|
|
3939
3854
|
continue;
|
|
3940
3855
|
}
|
|
3941
3856
|
|
|
@@ -3951,9 +3866,9 @@ function proveGoals( goals, subst, facts, backRules, depth, visited, varGen ) {
|
|
|
3951
3866
|
if (composed === null) continue;
|
|
3952
3867
|
|
|
3953
3868
|
if (!restGoals.length) {
|
|
3954
|
-
results.push(
|
|
3869
|
+
results.push(gcCompactForGoals(composed, [], answerVars));
|
|
3955
3870
|
} else {
|
|
3956
|
-
const nextSubst =
|
|
3871
|
+
const nextSubst = maybeCompactSubst(composed, restGoals, answerVars, state.depth + 1);
|
|
3957
3872
|
stack.push({
|
|
3958
3873
|
goals: restGoals,
|
|
3959
3874
|
subst: nextSubst,
|
|
@@ -3980,9 +3895,9 @@ function proveGoals( goals, subst, facts, backRules, depth, visited, varGen ) {
|
|
|
3980
3895
|
if (composed === null) continue;
|
|
3981
3896
|
|
|
3982
3897
|
if (!restGoals.length) {
|
|
3983
|
-
results.push(
|
|
3898
|
+
results.push(gcCompactForGoals(composed, [], answerVars));
|
|
3984
3899
|
} else {
|
|
3985
|
-
const nextSubst =
|
|
3900
|
+
const nextSubst = maybeCompactSubst(composed, restGoals, answerVars, state.depth + 1);
|
|
3986
3901
|
stack.push({
|
|
3987
3902
|
goals: restGoals,
|
|
3988
3903
|
subst: nextSubst,
|
|
@@ -4001,9 +3916,9 @@ function proveGoals( goals, subst, facts, backRules, depth, visited, varGen ) {
|
|
|
4001
3916
|
if (composed === null) continue;
|
|
4002
3917
|
|
|
4003
3918
|
if (!restGoals.length) {
|
|
4004
|
-
results.push(
|
|
3919
|
+
results.push(gcCompactForGoals(composed, [], answerVars));
|
|
4005
3920
|
} else {
|
|
4006
|
-
const nextSubst =
|
|
3921
|
+
const nextSubst = maybeCompactSubst(composed, restGoals, answerVars, state.depth + 1);
|
|
4007
3922
|
stack.push({
|
|
4008
3923
|
goals: restGoals,
|
|
4009
3924
|
subst: nextSubst,
|
|
@@ -4036,7 +3951,7 @@ function proveGoals( goals, subst, facts, backRules, depth, visited, varGen ) {
|
|
|
4036
3951
|
if (composed === null) continue;
|
|
4037
3952
|
|
|
4038
3953
|
const newGoals = body.concat(restGoals);
|
|
4039
|
-
const nextSubst =
|
|
3954
|
+
const nextSubst = maybeCompactSubst(composed, newGoals, answerVars, state.depth + 1);
|
|
4040
3955
|
stack.push({
|
|
4041
3956
|
goals: newGoals,
|
|
4042
3957
|
subst: nextSubst,
|