@hviana/sema 0.1.5 → 0.1.6
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/AGENTS.md +6 -5
- package/CITATION.cff +49 -0
- package/HOW_IT_WORKS.md +11 -12
- package/README.md +7 -5
- package/dist/example/demo.js +25 -25
- package/dist/example/train_base.d.ts +19 -21
- package/dist/example/train_base.js +1687 -1952
- package/dist/src/alphabet.d.ts +3 -3
- package/dist/src/alphabet.js +24 -27
- package/dist/src/alu/src/alu.d.ts +169 -194
- package/dist/src/alu/src/alu.js +374 -414
- package/dist/src/alu/src/expr.d.ts +36 -43
- package/dist/src/alu/src/expr.js +252 -278
- package/dist/src/alu/src/index.d.ts +9 -98
- package/dist/src/alu/src/index.js +10 -64
- package/dist/src/alu/src/kernel-arith.d.ts +1 -5
- package/dist/src/alu/src/kernel-arith.js +195 -285
- package/dist/src/alu/src/kernel-bits.d.ts +1 -5
- package/dist/src/alu/src/kernel-bits.js +81 -115
- package/dist/src/alu/src/kernel-logic.js +33 -63
- package/dist/src/alu/src/kernel-nd.js +155 -195
- package/dist/src/alu/src/kernel-numeric.d.ts +16 -80
- package/dist/src/alu/src/kernel-numeric.js +259 -331
- package/dist/src/alu/src/operation.d.ts +112 -132
- package/dist/src/alu/src/operation.js +149 -163
- package/dist/src/alu/src/parser.d.ts +168 -175
- package/dist/src/alu/src/parser.js +382 -421
- package/dist/src/alu/src/resonance.d.ts +16 -25
- package/dist/src/alu/src/resonance.js +49 -56
- package/dist/src/alu/src/text.d.ts +3 -7
- package/dist/src/alu/src/text.js +31 -37
- package/dist/src/alu/src/value.d.ts +14 -14
- package/dist/src/alu/src/value.js +150 -160
- package/dist/src/alu/test/alu.test.js +543 -656
- package/dist/src/bytes.d.ts +1 -5
- package/dist/src/bytes.js +33 -40
- package/dist/src/config.d.ts +99 -102
- package/dist/src/config.js +78 -83
- package/dist/src/derive/src/deduction.d.ts +60 -62
- package/dist/src/derive/src/deduction.js +103 -109
- package/dist/src/derive/src/index.d.ts +1 -7
- package/dist/src/derive/src/priority-queue.d.ts +8 -8
- package/dist/src/derive/src/priority-queue.js +57 -61
- package/dist/src/derive/src/rewrite.d.ts +15 -18
- package/dist/src/derive/src/rewrite.js +59 -67
- package/dist/src/derive/src/trie.d.ts +56 -56
- package/dist/src/derive/src/trie.js +175 -179
- package/dist/src/derive/test/derive.test.js +96 -100
- package/dist/src/extension.d.ts +13 -16
- package/dist/src/geometry.d.ts +17 -42
- package/dist/src/geometry.js +235 -266
- package/dist/src/index.d.ts +2 -17
- package/dist/src/index.js +2 -12
- package/dist/src/ingest-cache.d.ts +25 -30
- package/dist/src/ingest-cache.js +108 -127
- package/dist/src/mind/articulation.d.ts +1 -5
- package/dist/src/mind/articulation.js +77 -112
- package/dist/src/mind/attention.d.ts +40 -108
- package/dist/src/mind/attention.js +749 -871
- package/dist/src/mind/canonical.d.ts +4 -19
- package/dist/src/mind/canonical.js +27 -31
- package/dist/src/mind/graph-search.d.ts +201 -225
- package/dist/src/mind/graph-search.js +742 -821
- package/dist/src/mind/index.d.ts +2 -10
- package/dist/src/mind/junction.d.ts +31 -58
- package/dist/src/mind/junction.js +172 -237
- package/dist/src/mind/learning.d.ts +16 -52
- package/dist/src/mind/learning.js +143 -165
- package/dist/src/mind/match.d.ts +20 -69
- package/dist/src/mind/match.js +259 -318
- package/dist/src/mind/mechanisms/alu.js +16 -16
- package/dist/src/mind/mechanisms/cast.d.ts +9 -13
- package/dist/src/mind/mechanisms/cast.js +363 -456
- package/dist/src/mind/mechanisms/confluence.d.ts +8 -12
- package/dist/src/mind/mechanisms/confluence.js +152 -183
- package/dist/src/mind/mechanisms/cover.d.ts +2 -8
- package/dist/src/mind/mechanisms/cover.js +148 -210
- package/dist/src/mind/mechanisms/extraction.d.ts +8 -34
- package/dist/src/mind/mechanisms/extraction.js +234 -288
- package/dist/src/mind/mechanisms/recall.d.ts +6 -10
- package/dist/src/mind/mechanisms/recall.js +126 -185
- package/dist/src/mind/mind.d.ts +129 -154
- package/dist/src/mind/mind.js +258 -288
- package/dist/src/mind/pipeline-mechanism.d.ts +112 -124
- package/dist/src/mind/pipeline-mechanism.js +161 -172
- package/dist/src/mind/pipeline.d.ts +4 -14
- package/dist/src/mind/pipeline.js +125 -189
- package/dist/src/mind/primitives.d.ts +8 -32
- package/dist/src/mind/primitives.js +99 -117
- package/dist/src/mind/rationale.d.ts +86 -98
- package/dist/src/mind/rationale.js +113 -121
- package/dist/src/mind/reasoning.d.ts +2 -13
- package/dist/src/mind/reasoning.js +129 -166
- package/dist/src/mind/recognition.d.ts +1 -4
- package/dist/src/mind/recognition.js +183 -208
- package/dist/src/mind/resonance.d.ts +5 -22
- package/dist/src/mind/resonance.js +0 -0
- package/dist/src/mind/trace.d.ts +6 -25
- package/dist/src/mind/trace.js +50 -58
- package/dist/src/mind/traverse.d.ts +16 -58
- package/dist/src/mind/traverse.js +304 -357
- package/dist/src/mind/types.d.ts +120 -127
- package/dist/src/mind/types.js +60 -69
- package/dist/src/rabitq-ivf/src/database.d.ts +113 -0
- package/dist/src/rabitq-ivf/src/database.js +201 -0
- package/dist/src/rabitq-ivf/src/index.d.ts +7 -0
- package/dist/src/{rabitq-hnsw → rabitq-ivf}/src/index.js +1 -3
- package/dist/src/rabitq-ivf/src/ivf.d.ts +200 -0
- package/dist/src/rabitq-ivf/src/ivf.js +1165 -0
- package/dist/src/rabitq-ivf/src/prng.d.ts +19 -0
- package/dist/src/rabitq-ivf/src/prng.js +36 -0
- package/dist/src/rabitq-ivf/src/rabitq.d.ts +95 -0
- package/dist/src/rabitq-ivf/src/rabitq.js +283 -0
- package/dist/src/sema.d.ts +9 -13
- package/dist/src/sema.js +26 -40
- package/dist/src/store-sqlite.d.ts +169 -166
- package/dist/src/store-sqlite.js +762 -662
- package/dist/src/store.d.ts +586 -630
- package/dist/src/store.js +1423 -1581
- package/dist/src/vec.d.ts +5 -9
- package/dist/src/vec.js +61 -73
- package/example/train_base.ts +13 -10
- package/package.json +1 -1
- package/src/alu/README.md +1 -1
- package/src/alu/src/index.ts +1 -1
- package/src/config.ts +19 -27
- package/src/index.ts +6 -11
- package/src/rabitq-ivf/README.md +56 -0
- package/src/rabitq-ivf/src/database.ts +276 -0
- package/src/{rabitq-hnsw → rabitq-ivf}/src/index.ts +2 -5
- package/src/rabitq-ivf/src/ivf.ts +1330 -0
- package/src/{rabitq-hnsw → rabitq-ivf}/src/prng.ts +1 -1
- package/src/store-sqlite.ts +196 -9
- package/src/store.ts +8 -32
- package/test/08-storage.test.mjs +3 -3
- package/test/14-scaling.test.mjs +2 -2
- package/test/35-ivf.test.mjs +263 -0
- package/test/36-bloom.test.mjs +123 -0
- package/dist/src/rabitq-hnsw/src/database.d.ts +0 -202
- package/dist/src/rabitq-hnsw/src/database.js +0 -405
- package/dist/src/rabitq-hnsw/src/heap.d.ts +0 -22
- package/dist/src/rabitq-hnsw/src/heap.js +0 -94
- package/dist/src/rabitq-hnsw/src/hnsw.d.ts +0 -125
- package/dist/src/rabitq-hnsw/src/hnsw.js +0 -500
- package/dist/src/rabitq-hnsw/src/index.d.ts +0 -15
- package/dist/src/rabitq-hnsw/src/prng.d.ts +0 -19
- package/dist/src/rabitq-hnsw/src/prng.js +0 -38
- package/dist/src/rabitq-hnsw/src/rabitq.d.ts +0 -95
- package/dist/src/rabitq-hnsw/src/rabitq.js +0 -299
- package/dist/src/rabitq-hnsw/src/store.d.ts +0 -162
- package/dist/src/rabitq-hnsw/src/store.js +0 -916
- package/dist/src/rabitq-hnsw/test/hnsw.test.d.ts +0 -1
- package/dist/src/rabitq-hnsw/test/hnsw.test.js +0 -1197
- package/src/rabitq-hnsw/README.md +0 -303
- package/src/rabitq-hnsw/src/database.ts +0 -492
- package/src/rabitq-hnsw/src/heap.ts +0 -90
- package/src/rabitq-hnsw/src/hnsw.ts +0 -514
- package/src/rabitq-hnsw/src/store.ts +0 -994
- package/src/rabitq-hnsw/test/hnsw.test.ts +0 -1213
- /package/src/{rabitq-hnsw → rabitq-ivf}/src/rabitq.ts +0 -0
|
@@ -5,76 +5,35 @@
|
|
|
5
5
|
// host's halo space, so the polymorphic inverse is exercised with zero coupling).
|
|
6
6
|
import { test } from "node:test";
|
|
7
7
|
import assert from "node:assert/strict";
|
|
8
|
-
import {
|
|
9
|
-
addBits,
|
|
10
|
-
Alu,
|
|
11
|
-
asReal,
|
|
12
|
-
compareBits,
|
|
13
|
-
converge,
|
|
14
|
-
decimalCodec,
|
|
15
|
-
diff,
|
|
16
|
-
dot,
|
|
17
|
-
freeVariables,
|
|
18
|
-
int,
|
|
19
|
-
integrate,
|
|
20
|
-
interpolate,
|
|
21
|
-
isNd,
|
|
22
|
-
linsolve,
|
|
23
|
-
matMul,
|
|
24
|
-
mulBits,
|
|
25
|
-
nd,
|
|
26
|
-
negateBits,
|
|
27
|
-
NO_RESONANCE,
|
|
28
|
-
odeSolve,
|
|
29
|
-
OperationRegistry,
|
|
30
|
-
optimize,
|
|
31
|
-
parseValue,
|
|
32
|
-
polyEval,
|
|
33
|
-
powerEig,
|
|
34
|
-
prefetchRecognisedOps,
|
|
35
|
-
real,
|
|
36
|
-
registerArith,
|
|
37
|
-
registerBits,
|
|
38
|
-
registerLogic,
|
|
39
|
-
registerNd,
|
|
40
|
-
registerNumeric,
|
|
41
|
-
regress,
|
|
42
|
-
signBits,
|
|
43
|
-
solve,
|
|
44
|
-
symbol,
|
|
45
|
-
tagOf,
|
|
46
|
-
topSingular,
|
|
47
|
-
} from "../src/index.js";
|
|
8
|
+
import { addBits, Alu, asReal, compareBits, converge, decimalCodec, diff, dot, freeVariables, int, integrate, interpolate, isNd, linsolve, matMul, mulBits, nd, negateBits, NO_RESONANCE, odeSolve, OperationRegistry, optimize, parseValue, polyEval, powerEig, prefetchRecognisedOps, real, registerArith, registerBits, registerLogic, registerNd, registerNumeric, regress, signBits, solve, symbol, tagOf, topSingular, } from "../src/index.js";
|
|
48
9
|
// ── helpers ─────────────────────────────────────────────────────────────────
|
|
49
10
|
const enc = (s) => new TextEncoder().encode(s);
|
|
50
11
|
const dec = (b) => new TextDecoder().decode(b);
|
|
51
|
-
const close = (a, b, eps = 1e-6) =>
|
|
52
|
-
assert.ok(Math.abs(a - b) <= eps, `${a} !~ ${b} (eps ${eps})`);
|
|
12
|
+
const close = (a, b, eps = 1e-6) => assert.ok(Math.abs(a - b) <= eps, `${a} !~ ${b} (eps ${eps})`);
|
|
53
13
|
/** Build an ALU Value from a JS literal — a TEST fixture, NOT a parser: the
|
|
54
14
|
* kernel no longer reads list structure from bytes (that is the host's
|
|
55
15
|
* meaning-level job — see src/mind/mind.ts recogniseValue), so the kernel's own tests
|
|
56
16
|
* construct Values directly. An array → nd, an integer → int, a fractional
|
|
57
17
|
* number → real, a string → an opaque symbol (its bytes verbatim). */
|
|
58
|
-
const V = (x) =>
|
|
59
|
-
Array.isArray(x)
|
|
18
|
+
const V = (x) => Array.isArray(x)
|
|
60
19
|
? nd(x.map(V))
|
|
61
20
|
: typeof x === "number"
|
|
62
|
-
|
|
63
|
-
|
|
21
|
+
? (Number.isInteger(x) ? int(BigInt(x)) : real(x))
|
|
22
|
+
: symbol(enc(x));
|
|
64
23
|
/** A logic-only registry + context — the substrate the bit bootstrap runs on. */
|
|
65
24
|
function logicCtx() {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
25
|
+
const r = new OperationRegistry();
|
|
26
|
+
registerLogic(r);
|
|
27
|
+
return r.context(NO_RESONANCE, { tol: 1e-10, maxIter: 1000 });
|
|
69
28
|
}
|
|
70
29
|
/** A full registry context (all kernels). */
|
|
71
30
|
function fullCtx(resonance = NO_RESONANCE) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
31
|
+
const r = new OperationRegistry();
|
|
32
|
+
registerLogic(r);
|
|
33
|
+
registerBits(r);
|
|
34
|
+
registerArith(r);
|
|
35
|
+
registerNumeric(r);
|
|
36
|
+
return { r, ctx: r.context(resonance, { tol: 1e-12, maxIter: 2000 }) };
|
|
78
37
|
}
|
|
79
38
|
const B = (v) => (v.domain === "bit" ? v.b : NaN);
|
|
80
39
|
const N = (v) => (v.domain === "int" ? v.n : NaN);
|
|
@@ -82,703 +41,640 @@ const N = (v) => (v.domain === "int" ? v.n : NaN);
|
|
|
82
41
|
// 1 — Logic: the completeness layer. nand is the one axiom; every gate derives.
|
|
83
42
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
84
43
|
test("nand truth table is the irreducible axiom", () => {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
44
|
+
const ctx = logicCtx();
|
|
45
|
+
const t = (a, b) => B(ctx.apply("nand", [int(BigInt(a)), int(BigInt(b))]));
|
|
46
|
+
assert.equal(t(0, 0), 1);
|
|
47
|
+
assert.equal(t(0, 1), 1);
|
|
48
|
+
assert.equal(t(1, 0), 1);
|
|
49
|
+
assert.equal(t(1, 1), 0);
|
|
91
50
|
});
|
|
92
51
|
test("not/and/or derive from nand and match their truth tables", () => {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
assert.equal(B(ctx.apply("or", [bit01(a), bit01(b)])), or);
|
|
107
|
-
}
|
|
52
|
+
const ctx = logicCtx();
|
|
53
|
+
const bit01 = (n) => int(BigInt(n));
|
|
54
|
+
assert.equal(B(ctx.apply("not", [bit01(0)])), 1);
|
|
55
|
+
assert.equal(B(ctx.apply("not", [bit01(1)])), 0);
|
|
56
|
+
for (const [a, b, and, or] of [
|
|
57
|
+
[0, 0, 0, 0],
|
|
58
|
+
[0, 1, 0, 1],
|
|
59
|
+
[1, 0, 0, 1],
|
|
60
|
+
[1, 1, 1, 1],
|
|
61
|
+
]) {
|
|
62
|
+
assert.equal(B(ctx.apply("and", [bit01(a), bit01(b)])), and);
|
|
63
|
+
assert.equal(B(ctx.apply("or", [bit01(a), bit01(b)])), or);
|
|
64
|
+
}
|
|
108
65
|
});
|
|
109
66
|
test("nor/xor/xnor/implies/iff all derive from nand", () => {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
[0,
|
|
114
|
-
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
assert.deepEqual(truth("xnor"), [1, 0, 0, 1]);
|
|
119
|
-
assert.deepEqual(truth("implies"), [1, 1, 0, 1]);
|
|
120
|
-
assert.deepEqual(truth("iff"), [1, 0, 0, 1]);
|
|
67
|
+
const ctx = logicCtx();
|
|
68
|
+
const bit01 = (n) => int(BigInt(n));
|
|
69
|
+
const truth = (name) => [0, 1].flatMap((a) => [0, 1].map((b) => B(ctx.apply(name, [bit01(a), bit01(b)]))));
|
|
70
|
+
assert.deepEqual(truth("nor"), [1, 0, 0, 0]);
|
|
71
|
+
assert.deepEqual(truth("xor"), [0, 1, 1, 0]);
|
|
72
|
+
assert.deepEqual(truth("xnor"), [1, 0, 0, 1]);
|
|
73
|
+
assert.deepEqual(truth("implies"), [1, 1, 0, 1]);
|
|
74
|
+
assert.deepEqual(truth("iff"), [1, 0, 0, 1]);
|
|
121
75
|
});
|
|
122
76
|
test("mux(s,a,b) selects a when s=0, b when s=1 — the bridge to control flow", () => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
77
|
+
const ctx = logicCtx();
|
|
78
|
+
const bit01 = (n) => int(BigInt(n));
|
|
79
|
+
// s=0 → a
|
|
80
|
+
assert.equal(B(ctx.apply("mux", [bit01(0), bit01(0), bit01(1)])), 0);
|
|
81
|
+
assert.equal(B(ctx.apply("mux", [bit01(0), bit01(1), bit01(0)])), 1);
|
|
82
|
+
// s=1 → b
|
|
83
|
+
assert.equal(B(ctx.apply("mux", [bit01(1), bit01(0), bit01(1)])), 1);
|
|
84
|
+
assert.equal(B(ctx.apply("mux", [bit01(1), bit01(1), bit01(0)])), 0);
|
|
131
85
|
});
|
|
132
86
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
133
87
|
// 2 — Arithmetic: the bit-vector bootstrap is EXACT (the nand→everything proof).
|
|
134
88
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
135
89
|
test("full_adder built from xor/and/or matches its truth table", () => {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
int(BigInt(c)),
|
|
90
|
+
const { r, ctx } = fullCtx();
|
|
91
|
+
assert.ok(r.get("bits.fullAdder"));
|
|
92
|
+
// sum in bit 0, carry in bit 1.
|
|
93
|
+
const fa = (a, b, c) => N(ctx.apply("bits.fullAdder", [
|
|
94
|
+
int(BigInt(a)),
|
|
95
|
+
int(BigInt(b)),
|
|
96
|
+
int(BigInt(c)),
|
|
144
97
|
]));
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
98
|
+
assert.equal(fa(0, 0, 0), 0n); // sum 0 carry 0
|
|
99
|
+
assert.equal(fa(1, 0, 0), 1n); // sum 1 carry 0
|
|
100
|
+
assert.equal(fa(1, 1, 0), 2n); // sum 0 carry 1
|
|
101
|
+
assert.equal(fa(1, 1, 1), 3n); // sum 1 carry 1
|
|
149
102
|
});
|
|
150
103
|
test("ripple add / two's-complement negate / shift-add multiply equal native bigint", () => {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
`sign ${a}`,
|
|
169
|
-
);
|
|
170
|
-
assert.equal(
|
|
171
|
-
compareBits(ctx, a, b),
|
|
172
|
-
a > b ? 1n : a < b ? -1n : 0n,
|
|
173
|
-
`compare ${a}?${b}`,
|
|
174
|
-
);
|
|
175
|
-
}
|
|
104
|
+
const ctx = logicCtx();
|
|
105
|
+
const cases = [
|
|
106
|
+
[0n, 0n],
|
|
107
|
+
[5n, 7n],
|
|
108
|
+
[12n, 30n],
|
|
109
|
+
[-3n, 8n],
|
|
110
|
+
[-15n, -9n],
|
|
111
|
+
[123n, 456n],
|
|
112
|
+
[1000n, -1n],
|
|
113
|
+
];
|
|
114
|
+
for (const [a, b] of cases) {
|
|
115
|
+
assert.equal(addBits(ctx, a, b), a + b, `add ${a}+${b}`);
|
|
116
|
+
assert.equal(negateBits(ctx, a), -a, `negate ${a}`);
|
|
117
|
+
assert.equal(mulBits(ctx, a, b), a * b, `mul ${a}*${b}`);
|
|
118
|
+
assert.equal(signBits(ctx, a), a > 0n ? 1n : a < 0n ? -1n : 0n, `sign ${a}`);
|
|
119
|
+
assert.equal(compareBits(ctx, a, b), a > b ? 1n : a < b ? -1n : 0n, `compare ${a}?${b}`);
|
|
120
|
+
}
|
|
176
121
|
});
|
|
177
122
|
test("the bit bootstrap stays exact past 2^53", () => {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
123
|
+
const ctx = logicCtx();
|
|
124
|
+
const a = 9007199254740993n; // 2^53 + 1, not representable as a double
|
|
125
|
+
const b = 1000000007n;
|
|
126
|
+
assert.equal(mulBits(ctx, a, b), a * b);
|
|
182
127
|
});
|
|
183
128
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
184
129
|
// 3 — Arithmetic: polymorphic primitives + the derived field and order.
|
|
185
130
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
186
131
|
test("add/multiply are exact on ints and lift to reals", () => {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
132
|
+
const { ctx } = fullCtx();
|
|
133
|
+
assert.equal(N(ctx.apply("add", [int(2n), int(2n)])), 4n);
|
|
134
|
+
assert.equal(N(ctx.apply("multiply", [int(6n), int(7n)])), 42n);
|
|
135
|
+
// A real operand lifts the whole expression.
|
|
136
|
+
const r = ctx.apply("add", [int(2n), real(0.5)]);
|
|
137
|
+
assert.equal(r.domain, "real");
|
|
138
|
+
close(asReal(r), 2.5);
|
|
194
139
|
});
|
|
195
140
|
test("subtract = add∘negate, divide = multiply∘reciprocal", () => {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
141
|
+
const { ctx } = fullCtx();
|
|
142
|
+
assert.equal(N(ctx.apply("subtract", [int(10n), int(3n)])), 7n);
|
|
143
|
+
close(asReal(ctx.apply("divide", [int(7n), int(2n)])), 3.5);
|
|
199
144
|
});
|
|
200
145
|
test("every comparison = sign∘subtract", () => {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
146
|
+
const { ctx } = fullCtx();
|
|
147
|
+
const b = (name, x, y) => ctx.apply(name, [real(x), real(y)]);
|
|
148
|
+
assert.equal(B(b("lt", 2, 3)), 1);
|
|
149
|
+
assert.equal(B(b("lt", 3, 3)), 0);
|
|
150
|
+
assert.equal(B(b("le", 3, 3)), 1);
|
|
151
|
+
assert.equal(B(b("gt", 5, 3)), 1);
|
|
152
|
+
assert.equal(B(b("ge", 2, 3)), 0);
|
|
153
|
+
assert.equal(B(b("eq", 4, 4)), 1);
|
|
154
|
+
assert.equal(B(b("ne", 4, 5)), 1);
|
|
210
155
|
});
|
|
211
156
|
test("abs/min/max/power/gcd derive correctly", () => {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
157
|
+
const { ctx } = fullCtx();
|
|
158
|
+
assert.equal(N(ctx.apply("abs", [int(-9n)])), 9n);
|
|
159
|
+
assert.equal(N(ctx.apply("min", [int(3n), int(8n)])), 3n);
|
|
160
|
+
assert.equal(N(ctx.apply("max", [int(3n), int(8n)])), 8n);
|
|
161
|
+
assert.equal(N(ctx.apply("power", [int(2n), int(10n)])), 1024n);
|
|
162
|
+
close(asReal(ctx.apply("power", [real(2), int(-2n)])), 0.25);
|
|
163
|
+
assert.equal(N(ctx.apply("gcd", [int(48n), int(36n)])), 12n);
|
|
219
164
|
});
|
|
220
165
|
test("real power via exp∘log agrees with Math.pow", () => {
|
|
221
|
-
|
|
222
|
-
|
|
166
|
+
const { ctx } = fullCtx();
|
|
167
|
+
close(asReal(ctx.apply("power", [real(2), real(0.5)])), Math.SQRT2, 1e-6);
|
|
223
168
|
});
|
|
224
169
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
225
170
|
// 4 — Arithmetic: polynomial / vector / matrix / linsolve (structured arith).
|
|
226
171
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
227
172
|
test("polyEval (Horner), dot, matMul", () => {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
173
|
+
// 1 + 2x + 3x^2 at x=2 → 1 + 4 + 12 = 17
|
|
174
|
+
close(polyEval([1, 2, 3], 2), 17);
|
|
175
|
+
close(dot([1, 2, 3], [4, 5, 6]), 32);
|
|
176
|
+
assert.deepEqual(matMul([[1, 2], [3, 4]], [[5, 6], [7, 8]]), [
|
|
177
|
+
[19, 22],
|
|
178
|
+
[43, 50],
|
|
179
|
+
]);
|
|
235
180
|
});
|
|
236
181
|
test("linsolve solves a 3x3 system (Gaussian elimination)", () => {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
182
|
+
// x + y + z = 6 ; 2y + 5z = -4 ; 2x + 5y - z = 27 → (5, 3, -2)
|
|
183
|
+
const x = linsolve([[1, 1, 1], [0, 2, 5], [2, 5, -1]], [6, -4, 27]);
|
|
184
|
+
assert.ok(x);
|
|
185
|
+
close(x[0], 5);
|
|
186
|
+
close(x[1], 3);
|
|
187
|
+
close(x[2], -2);
|
|
243
188
|
});
|
|
244
189
|
test("linsolve returns null for a singular matrix", () => {
|
|
245
|
-
|
|
190
|
+
assert.equal(linsolve([[1, 2], [2, 4]], [3, 6]), null);
|
|
246
191
|
});
|
|
247
192
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
248
193
|
// 5 — Numerical: the limit layer. Each op is a converge instance.
|
|
249
194
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
250
195
|
test("converge reaches a contraction's fixed point", () => {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
196
|
+
// x ← (x + 2/x)/2 converges to √2.
|
|
197
|
+
const fp = converge((x) => (x + 2 / x) / 2, 1, 1e-12, 1000);
|
|
198
|
+
close(fp, Math.SQRT2, 1e-9);
|
|
254
199
|
});
|
|
255
200
|
test("diff ≈ analytic derivative", () => {
|
|
256
|
-
|
|
257
|
-
|
|
201
|
+
close(diff((x) => x * x, 3, 1e-10, 200), 6, 1e-4);
|
|
202
|
+
close(diff(Math.sin, 0, 1e-10, 200), 1, 1e-4);
|
|
258
203
|
});
|
|
259
204
|
test("integrate ≈ closed form", () => {
|
|
260
|
-
|
|
261
|
-
|
|
205
|
+
close(integrate((x) => x, 0, 1, 1e-10, 50), 0.5, 1e-6);
|
|
206
|
+
close(integrate((x) => x * x, 0, 3, 1e-10, 50), 9, 1e-5);
|
|
262
207
|
});
|
|
263
208
|
test("solve (Newton) finds a root", () => {
|
|
264
|
-
|
|
209
|
+
close(solve((x) => x * x - 2, 1, 1e-12, 200), Math.SQRT2, 1e-8);
|
|
265
210
|
});
|
|
266
211
|
test("exp/log/sin/cos/sqrt converge to the right limits", () => {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
212
|
+
const { ctx } = fullCtx();
|
|
213
|
+
const f = (name, x) => asReal(ctx.apply(name, [real(x)]));
|
|
214
|
+
close(f("exp", 0), 1, 1e-9);
|
|
215
|
+
close(f("exp", 1), Math.E, 1e-7);
|
|
216
|
+
close(f("log", Math.E), 1, 1e-7);
|
|
217
|
+
close(f("sin", 0), 0, 1e-9);
|
|
218
|
+
close(f("sin", Math.PI / 2), 1, 1e-7);
|
|
219
|
+
close(f("cos", 0), 1, 1e-9);
|
|
220
|
+
close(f("sqrt", 2), Math.SQRT2, 1e-8);
|
|
221
|
+
close(f("sqrt", 144), 12, 1e-8);
|
|
277
222
|
});
|
|
278
223
|
test("optimize finds a minimum; odeSolve integrates y'=y", () => {
|
|
279
|
-
|
|
280
|
-
|
|
224
|
+
close(optimize((x) => (x - 3) * (x - 3), 0, 1e-12, 500), 3, 1e-4);
|
|
225
|
+
close(odeSolve((_t, y) => y, 0, 1, 1, 2000), Math.E, 1e-4);
|
|
281
226
|
});
|
|
282
227
|
test("regress recovers a known line; interpolate is linear", () => {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
228
|
+
// y = 2x + 1 sampled exactly → coeffs [1, 2].
|
|
229
|
+
const xs = [0, 1, 2, 3];
|
|
230
|
+
const ys = xs.map((x) => 2 * x + 1);
|
|
231
|
+
const c = regress(xs, ys, 1);
|
|
232
|
+
assert.ok(c);
|
|
233
|
+
close(c[0], 1, 1e-6);
|
|
234
|
+
close(c[1], 2, 1e-6);
|
|
235
|
+
close(interpolate([0, 10], [0, 100], 5), 50);
|
|
291
236
|
});
|
|
292
237
|
test("powerEig / topSingular by power iteration", () => {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
238
|
+
// Diagonal(5, 2): dominant eigenvalue 5.
|
|
239
|
+
const { value } = powerEig([[5, 0], [0, 2]], 1e-12, 1000);
|
|
240
|
+
close(value, 5, 1e-6);
|
|
241
|
+
// Largest singular value of diag(3,4) is 4.
|
|
242
|
+
close(topSingular([[3, 0], [0, 4]], 1e-12, 1000), 4, 1e-6);
|
|
298
243
|
});
|
|
299
244
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
300
245
|
// 6 — The Operation model: derived is indistinguishable; one-line extension.
|
|
301
246
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
302
247
|
test("a derived op has the same record shape as a primitive", () => {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
248
|
+
const { r } = fullCtx();
|
|
249
|
+
const nand = r.get("nand");
|
|
250
|
+
const sub = r.get("subtract");
|
|
251
|
+
assert.equal(nand.primitive, true);
|
|
252
|
+
assert.equal(sub.primitive, false);
|
|
253
|
+
// Same fields; a caller cannot tell them apart structurally.
|
|
254
|
+
for (const k of ["name", "arity", "primitive", "forms", "fn"]) {
|
|
255
|
+
assert.ok(k in nand && k in sub);
|
|
256
|
+
}
|
|
312
257
|
});
|
|
313
258
|
test("a brand-new derived op is registered in one call and computes", () => {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
]),
|
|
259
|
+
const { r, ctx } = fullCtx();
|
|
260
|
+
r.derive("hypot", 2, ["hypot"], (args, c) => c.apply("sqrt", [
|
|
261
|
+
c.apply("add", [
|
|
262
|
+
c.apply("multiply", [args[0], args[0]]),
|
|
263
|
+
c.apply("multiply", [args[1], args[1]]),
|
|
264
|
+
]),
|
|
321
265
|
]));
|
|
322
|
-
|
|
266
|
+
close(asReal(ctx.apply("hypot", [real(3), real(4)])), 5, 1e-7);
|
|
323
267
|
});
|
|
324
268
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
325
269
|
// 7 — Values: parse, the codec round-trip, and the polymorphic inverse.
|
|
326
270
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
327
271
|
test("parseValue: ints, reals, and symbols", () => {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
272
|
+
assert.deepEqual(parseValue(enc("42")), int(42n));
|
|
273
|
+
assert.deepEqual(parseValue(enc("-7")), int(-7n));
|
|
274
|
+
assert.deepEqual(parseValue(enc("3.5")), real(3.5));
|
|
275
|
+
assert.deepEqual(parseValue(enc("1e3")), real(1000));
|
|
276
|
+
// A token that merely begins with a digit stays a symbol.
|
|
277
|
+
assert.equal(parseValue(enc("3dogs")).domain, "symbol");
|
|
278
|
+
assert.equal(parseValue(enc("large")).domain, "symbol");
|
|
335
279
|
});
|
|
336
280
|
test("decimalCodec round-trips and formats reals deterministically", () => {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
281
|
+
const codec = decimalCodec(6);
|
|
282
|
+
assert.equal(dec(codec.encode(int(42n))), "42");
|
|
283
|
+
assert.equal(dec(codec.encode(real(0.5))), "0.5");
|
|
284
|
+
assert.equal(dec(codec.encode(real(2))), "2"); // trailing zeros trimmed
|
|
285
|
+
assert.equal(dec(codec.encode(real(-0))), "0"); // -0 normalised
|
|
286
|
+
// round-trip
|
|
287
|
+
const v = codec.decode(enc("8"));
|
|
288
|
+
assert.deepEqual(v, int(8n));
|
|
345
289
|
});
|
|
346
290
|
test("inverse is polymorphic: number negates, symbol resonates to its opposite", () => {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
291
|
+
// Stub resonance: "large" ↔ "small", modality-agnostic over bytes.
|
|
292
|
+
const opposites = new Map([["large", "small"]]);
|
|
293
|
+
const stub = {
|
|
294
|
+
opposite: (b) => {
|
|
295
|
+
const o = opposites.get(dec(b));
|
|
296
|
+
return o ? enc(o) : null;
|
|
297
|
+
},
|
|
298
|
+
recogniseOp: () => null,
|
|
299
|
+
};
|
|
300
|
+
const { ctx } = fullCtx(stub);
|
|
301
|
+
// numeric inverse = negate
|
|
302
|
+
assert.equal(N(ctx.apply("inverse", [int(3n)])), -3n);
|
|
303
|
+
// symbol inverse = resonant opposite
|
|
304
|
+
const r = ctx.apply("inverse", [symbol(enc("large"))]);
|
|
305
|
+
assert.equal(r.domain, "symbol");
|
|
306
|
+
assert.equal(dec(r.bytes), "small");
|
|
307
|
+
// a symbol with no known opposite is left unchanged (never fabricated)
|
|
308
|
+
const u = ctx.apply("inverse", [symbol(enc("zorp"))]);
|
|
309
|
+
assert.equal(dec(u.bytes), "zorp");
|
|
366
310
|
});
|
|
367
311
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
368
312
|
// 8 — The Alu facade: the synchronous scanner and applyBytes.
|
|
369
313
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
370
314
|
test("scan finds numeric operands and symbolic operators in raw bytes", () => {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
315
|
+
const u = new Alu();
|
|
316
|
+
const { operands, operators } = u.scan(enc("12+30"));
|
|
317
|
+
assert.deepEqual(operands.map((o) => [o.i, o.j]), [[0, 2], [3, 5]]);
|
|
318
|
+
assert.equal(operands[0].value.domain, "int");
|
|
319
|
+
assert.equal(operators.length, 1);
|
|
320
|
+
assert.equal(operators[0].name, "add");
|
|
321
|
+
assert.deepEqual([operators[0].i, operators[0].j], [2, 3]);
|
|
378
322
|
});
|
|
379
323
|
test("scan keeps a multi-digit / decimal number whole, ignores a bare trailing dot", () => {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
324
|
+
const u = new Alu();
|
|
325
|
+
const a = u.scan(enc("3.14"));
|
|
326
|
+
assert.equal(a.operands.length, 1);
|
|
327
|
+
assert.equal(a.operands[0].value.domain, "real");
|
|
328
|
+
// a trailing "." is not part of the numeral
|
|
329
|
+
const b = u.scan(enc("2+2."));
|
|
330
|
+
assert.deepEqual(b.operands.map((o) => o.j - o.i), [1, 1]);
|
|
387
331
|
});
|
|
388
332
|
test("scan resolves the longest symbolic operator form", () => {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
333
|
+
const u = new Alu();
|
|
334
|
+
const { operators } = u.scan(enc("3<=4"));
|
|
335
|
+
assert.equal(operators.length, 1);
|
|
336
|
+
assert.equal(operators[0].name, "le"); // "<=" beats "<"
|
|
393
337
|
});
|
|
394
338
|
test("applyBytes computes the canonical result bytes", () => {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
339
|
+
const u = new Alu();
|
|
340
|
+
assert.equal(dec(u.applyBytes("add", [enc("2"), enc("2")])), "4");
|
|
341
|
+
assert.equal(dec(u.applyBytes("multiply", [enc("6"), enc("7")])), "42");
|
|
342
|
+
assert.equal(dec(u.applyBytes("subtract", [enc("10"), enc("3")])), "7");
|
|
343
|
+
assert.equal(dec(u.applyBytes("divide", [enc("7"), enc("2")])), "3.5");
|
|
344
|
+
// an unknown op or an unparseable operand → null (the rule simply won't fire)
|
|
345
|
+
assert.equal(u.applyBytes("nope", [enc("1"), enc("2")]), null);
|
|
402
346
|
});
|
|
403
347
|
test("a computed result is itself an operand (composition for free)", () => {
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
348
|
+
const u = new Alu();
|
|
349
|
+
const first = u.applyBytes("add", [enc("2"), enc("3")]); // "5"
|
|
350
|
+
const second = u.applyBytes("multiply", [first, enc("4")]); // "20"
|
|
351
|
+
assert.equal(dec(second), "20");
|
|
408
352
|
});
|
|
409
353
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
410
354
|
// 9 — Expressions: a numerical op acts on a FUNCTION, evaluated by a recursive
|
|
411
355
|
// application of the same ALU (the "recursive call" case).
|
|
412
356
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
413
357
|
test("evalExpression evaluates through the kernel, auto-detecting the variable", () => {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
358
|
+
const u = new Alu();
|
|
359
|
+
// x^2 + 1 at x=3 → 10; variable auto-detected.
|
|
360
|
+
close(u.evalExpression(enc("x^2 + 1"), "", 3), 10);
|
|
361
|
+
// a different one-letter variable, any script, still auto-detected
|
|
362
|
+
close(u.evalExpression(enc("t*t"), "", 4), 16);
|
|
363
|
+
// a named function resolves against the kernel
|
|
364
|
+
close(u.evalExpression(enc("sin(x)"), "x", Math.PI / 2), 1, 1e-6);
|
|
365
|
+
// constants
|
|
366
|
+
close(u.evalExpression(enc("2*pi"), "", 0), 2 * Math.PI);
|
|
367
|
+
// a malformed expression declines
|
|
368
|
+
assert.equal(u.evalExpression(enc("x +"), "x", 1), null);
|
|
425
369
|
});
|
|
426
370
|
test("freeVariables lists candidates, excluding function names", () => {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
371
|
+
const u = new Alu();
|
|
372
|
+
const isFn = (n) => u.arityOf(n) === 1 && u.has(n);
|
|
373
|
+
assert.deepEqual(freeVariables("sin(x) + x", isFn), ["x"]);
|
|
374
|
+
assert.deepEqual(freeVariables("a*b + c", isFn).sort(), ["a", "b", "c"]);
|
|
431
375
|
});
|
|
432
376
|
test("diff: the derivative op acts on an expression operand", () => {
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
377
|
+
const u = new Alu();
|
|
378
|
+
// d/dx (x^2) at 3 = 6. Operand 0 is the expression symbol, operand 1 the point.
|
|
379
|
+
const r = u.apply("diff", [symbol(enc("x^2")), real(3)]);
|
|
380
|
+
assert.ok(r);
|
|
381
|
+
close(asReal(r), 6, 1e-4);
|
|
438
382
|
});
|
|
439
383
|
test("integrate: definite integral over an expression", () => {
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
asReal(u.apply("integrate", [symbol(enc("x")), real(0), real(
|
|
444
|
-
0.5,
|
|
445
|
-
1e-5,
|
|
446
|
-
);
|
|
447
|
-
close(
|
|
448
|
-
asReal(u.apply("integrate", [symbol(enc("x^2")), real(0), real(3)])),
|
|
449
|
-
9,
|
|
450
|
-
1e-4,
|
|
451
|
-
);
|
|
384
|
+
const u = new Alu();
|
|
385
|
+
// ∫₀¹ x dx = 0.5 ; ∫₀³ x^2 dx = 9
|
|
386
|
+
close(asReal(u.apply("integrate", [symbol(enc("x")), real(0), real(1)])), 0.5, 1e-5);
|
|
387
|
+
close(asReal(u.apply("integrate", [symbol(enc("x^2")), real(0), real(3)])), 9, 1e-4);
|
|
452
388
|
});
|
|
453
389
|
test("solve: a root of an expression near a guess", () => {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
asReal(u.apply("solve", [symbol(enc("x^2 - 2")), real(1)])),
|
|
458
|
-
Math.SQRT2,
|
|
459
|
-
1e-6,
|
|
460
|
-
);
|
|
390
|
+
const u = new Alu();
|
|
391
|
+
// root of x^2 - 2 near 1 → √2
|
|
392
|
+
close(asReal(u.apply("solve", [symbol(enc("x^2 - 2")), real(1)])), Math.SQRT2, 1e-6);
|
|
461
393
|
});
|
|
462
394
|
test("limit: the value an expression approaches (a converge instance)", () => {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
395
|
+
const u = new Alu();
|
|
396
|
+
// lim_{x→0} sin(x)/x = 1 — the removable singularity is skirted by sampling.
|
|
397
|
+
close(asReal(u.apply("limit", [symbol(enc("sin(x)/x")), real(0)])), 1, 1e-4);
|
|
466
398
|
});
|
|
467
399
|
test("optimize: a minimiser of an expression", () => {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
asReal(u.apply("optimize", [symbol(enc("(x-3)^2")), real(0)])),
|
|
472
|
-
3,
|
|
473
|
-
1e-3,
|
|
474
|
-
);
|
|
400
|
+
const u = new Alu();
|
|
401
|
+
// min of (x-3)^2 is at x=3
|
|
402
|
+
close(asReal(u.apply("optimize", [symbol(enc("(x-3)^2")), real(0)])), 3, 1e-3);
|
|
475
403
|
});
|
|
476
404
|
test("an expression op declines (null) when no evaluator can read the operand", () => {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
405
|
+
const u = new Alu();
|
|
406
|
+
// a non-expression symbol → the evaluator fails → apply returns null
|
|
407
|
+
assert.equal(u.apply("diff", [symbol(enc("@@@")), real(1)]), null);
|
|
480
408
|
});
|
|
481
409
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
482
410
|
// 10 — The operation CONCEPT vocabulary (the generic, resonant recognition seed).
|
|
483
411
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
484
412
|
test("conceptAnchors exposes the operation vocabulary for resonant recognition", () => {
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
413
|
+
const u = new Alu();
|
|
414
|
+
const dec = new TextDecoder();
|
|
415
|
+
const byName = new Map();
|
|
416
|
+
for (const { name, form } of u.conceptAnchors()) {
|
|
417
|
+
const a = byName.get(name) ?? [];
|
|
418
|
+
a.push(dec.decode(form));
|
|
419
|
+
byName.set(name, a);
|
|
420
|
+
}
|
|
421
|
+
// The numerical ops a query would NOT spell with a symbol still have named
|
|
422
|
+
// concepts a host can resonate against — generic over operations, not a fixed
|
|
423
|
+
// symbol table.
|
|
424
|
+
assert.ok(byName.get("integrate").includes("integral"));
|
|
425
|
+
assert.ok(byName.get("diff").includes("derivative"));
|
|
426
|
+
assert.ok(byName.get("limit").includes("limit"));
|
|
427
|
+
assert.ok(byName.get("add").includes("plus"));
|
|
428
|
+
// Forms the literal scanner already reads in full are NOT anchors: pure
|
|
429
|
+
// operator symbols and numerals stay on the literal path.
|
|
430
|
+
for (const forms of byName.values()) {
|
|
431
|
+
assert.ok(!forms.includes("<="));
|
|
432
|
+
assert.ok(!forms.includes("0"));
|
|
433
|
+
}
|
|
506
434
|
});
|
|
507
435
|
test("prefetchRecognisedOps bridges async recognition to a sync map", async () => {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
436
|
+
// A stub host resonance: "the rate of change of" means a derivative.
|
|
437
|
+
const stub = {
|
|
438
|
+
recogniseOp: async (b) => dec(b).includes("rate of change") ? "diff" : null,
|
|
439
|
+
opposite: async () => null,
|
|
440
|
+
};
|
|
441
|
+
const map = await prefetchRecognisedOps(stub, [
|
|
442
|
+
enc("the rate of change of"),
|
|
443
|
+
enc("nonsense"),
|
|
444
|
+
]);
|
|
445
|
+
assert.equal(map.get("the rate of change of"), "diff");
|
|
446
|
+
assert.equal(map.has("nonsense"), false);
|
|
519
447
|
});
|
|
520
448
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
521
449
|
// 11 — N-dimensional values: the recursive container. Representation + codec.
|
|
522
450
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
523
451
|
/** A full registry with the nd kernel too — the substrate for the nd tests. */
|
|
524
452
|
function ndCtx(resonance = NO_RESONANCE) {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
453
|
+
const r = new OperationRegistry();
|
|
454
|
+
registerLogic(r);
|
|
455
|
+
registerBits(r);
|
|
456
|
+
registerArith(r);
|
|
457
|
+
registerNumeric(r);
|
|
458
|
+
registerNd(r);
|
|
459
|
+
return { r, ctx: r.context(resonance, { tol: 1e-12, maxIter: 2000 }) };
|
|
532
460
|
}
|
|
533
461
|
test("nd is the recursive container: nested, ragged, heterogeneous", () => {
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
462
|
+
// A list of any element values — including other lists (nesting) of unequal
|
|
463
|
+
// length (ragged) and mixed domains (heterogeneous).
|
|
464
|
+
const v = nd([
|
|
465
|
+
int(1n),
|
|
466
|
+
real(3.5),
|
|
467
|
+
symbol(enc("large")),
|
|
468
|
+
nd([int(2n), int(3n)]), // a nested sub-list, shorter than the outer
|
|
469
|
+
]);
|
|
470
|
+
assert.equal(tagOf(v), "nd");
|
|
471
|
+
assert.ok(isNd(v));
|
|
472
|
+
assert.equal(v.items.length, 4);
|
|
473
|
+
assert.equal(tagOf(v.items[3]), "nd");
|
|
474
|
+
// a scalar is NOT an nd, even a numeric one
|
|
475
|
+
assert.equal(isNd(int(1n)), false);
|
|
548
476
|
});
|
|
549
477
|
test("decimalCodec encodes an nd to its canonical bracket spelling (nested, mixed)", () => {
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
478
|
+
// The bracket literal is the kernel's canonical OUTPUT spelling of a list — one
|
|
479
|
+
// deterministic form (the analogue of decimal for a number), so two derivations
|
|
480
|
+
// of the same list agree byte-for-byte. The kernel BUILDS the Value (the host
|
|
481
|
+
// recognised its structure); the codec only spells it.
|
|
482
|
+
const codec = decimalCodec(6);
|
|
483
|
+
const out = (v) => dec(codec.encode(v));
|
|
484
|
+
// flat
|
|
485
|
+
assert.equal(out(V([1, 2, 3])), "[1,2,3]");
|
|
486
|
+
// nested (a matrix) and ragged
|
|
487
|
+
assert.equal(out(V([[1, 2], [3, 4, 5]])), "[[1,2],[3,4,5]]");
|
|
488
|
+
// heterogeneous: int, real, symbol all spell their own form
|
|
489
|
+
assert.equal(out(V([1, 3.5, "large"])), "[1,3.5,large]");
|
|
490
|
+
// empty list
|
|
491
|
+
assert.equal(out(V([])), "[]");
|
|
564
492
|
});
|
|
565
493
|
test("parseValue reads only SCALARS — a bracket literal decodes to an opaque symbol", () => {
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
494
|
+
// The kernel no longer parses list STRUCTURE from bytes (that is the host's
|
|
495
|
+
// meaning-level job — src/mind/mind.ts recogniseValue). So decode is scalar-only: a
|
|
496
|
+
// bracket literal is not a list to the kernel, it is an opaque symbol, exactly
|
|
497
|
+
// like any other form that merely contains a bracket.
|
|
498
|
+
assert.equal(parseValue(enc("[1,2,3]")).domain, "symbol");
|
|
499
|
+
assert.equal(parseValue(enc("[1,2")).domain, "symbol");
|
|
500
|
+
assert.equal(parseValue(enc("[1,,2]")).domain, "symbol");
|
|
501
|
+
assert.equal(parseValue(enc("not a list")).domain, "symbol");
|
|
502
|
+
// the scalar floor still grounds a numeral
|
|
503
|
+
assert.deepEqual(parseValue(enc("42")), int(42n));
|
|
576
504
|
});
|
|
577
505
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
578
506
|
// 12 — Broadcast: EVERY scalar op lifts over nd automatically (one mechanism).
|
|
579
507
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
580
508
|
test("arithmetic broadcasts: list∘list zips, list∘scalar holds the scalar", () => {
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
"[
|
|
588
|
-
|
|
589
|
-
// a scalar operand is held constant against the list
|
|
590
|
-
assert.equal(out(ctx.apply("add", [V([1, 2, 3]), int(10n)])), "[11,12,13]");
|
|
591
|
-
assert.equal(out(ctx.apply("subtract", [int(10n), V([1, 2, 3])])), "[9,8,7]");
|
|
509
|
+
const { ctx } = ndCtx();
|
|
510
|
+
const out = (r) => dec(decimalCodec(6).encode(r));
|
|
511
|
+
// element-wise over two lists
|
|
512
|
+
assert.equal(out(ctx.apply("add", [V([1, 2, 3]), V([4, 5, 6])])), "[5,7,9]");
|
|
513
|
+
assert.equal(out(ctx.apply("multiply", [V([1, 2, 3]), V([10, 10, 10])])), "[10,20,30]");
|
|
514
|
+
// a scalar operand is held constant against the list
|
|
515
|
+
assert.equal(out(ctx.apply("add", [V([1, 2, 3]), int(10n)])), "[11,12,13]");
|
|
516
|
+
assert.equal(out(ctx.apply("subtract", [int(10n), V([1, 2, 3])])), "[9,8,7]");
|
|
592
517
|
});
|
|
593
518
|
test("broadcast RECURSES through nesting (a matrix op is the same code)", () => {
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
"[[
|
|
600
|
-
);
|
|
601
|
-
// scalar against a matrix reaches every leaf
|
|
602
|
-
assert.equal(
|
|
603
|
-
out(ctx.apply("multiply", [V([[1, 2], [3, 4]]), int(2n)])),
|
|
604
|
-
"[[2,4],[6,8]]",
|
|
605
|
-
);
|
|
519
|
+
const { ctx } = ndCtx();
|
|
520
|
+
const out = (r) => dec(decimalCodec(6).encode(r));
|
|
521
|
+
// nd-of-nd lifts twice with no extra machinery
|
|
522
|
+
assert.equal(out(ctx.apply("add", [V([[1, 2], [3, 4]]), V([[10, 20], [30, 40]])])), "[[11,22],[33,44]]");
|
|
523
|
+
// scalar against a matrix reaches every leaf
|
|
524
|
+
assert.equal(out(ctx.apply("multiply", [V([[1, 2], [3, 4]]), int(2n)])), "[[2,4],[6,8]]");
|
|
606
525
|
});
|
|
607
526
|
test("broadcast spans op classes: logic, comparison, transcendental, inverse", () => {
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
out(ctx.apply("inverse", [V(["hot", 3, "up"])])),
|
|
625
|
-
"[cold,-3,down]",
|
|
626
|
-
);
|
|
527
|
+
const opposites = new Map([["hot", "cold"], ["up", "down"]]);
|
|
528
|
+
const stub = {
|
|
529
|
+
opposite: (b) => opposites.has(dec(b)) ? enc(opposites.get(dec(b))) : null,
|
|
530
|
+
recogniseOp: () => null,
|
|
531
|
+
};
|
|
532
|
+
const { ctx } = ndCtx(stub);
|
|
533
|
+
const out = (r) => dec(decimalCodec(6).encode(r));
|
|
534
|
+
// logic gate over a bit-list
|
|
535
|
+
assert.equal(out(ctx.apply("not", [V([1, 0, 1])])), "[0,1,0]");
|
|
536
|
+
// comparison → a list of truth bits
|
|
537
|
+
assert.equal(out(ctx.apply("gt", [V([1, 5, 3]), int(2n)])), "[0,1,1]");
|
|
538
|
+
// a transcendental over a list
|
|
539
|
+
assert.equal(out(ctx.apply("sqrt", [V([1, 4, 9])])), "[1,2,3]");
|
|
540
|
+
// the POLYMORPHIC INVERSE broadcasts: numbers negate, symbols resonate, in one
|
|
541
|
+
// heterogeneous list — each element dispatched on its own domain.
|
|
542
|
+
assert.equal(out(ctx.apply("inverse", [V(["hot", 3, "up"])])), "[cold,-3,down]");
|
|
627
543
|
});
|
|
628
544
|
test("broadcast over lists of unequal length declines (no silent truncation)", () => {
|
|
629
|
-
|
|
630
|
-
|
|
545
|
+
const { ctx } = ndCtx();
|
|
546
|
+
assert.throws(() => ctx.apply("add", [V([1, 2, 3]), V([4, 5])]));
|
|
631
547
|
});
|
|
632
548
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
633
549
|
// 13 — The nd core: nd / length / at are the only ops that touch `items`.
|
|
634
550
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
635
551
|
test("core: nd packs, length counts, at projects (negative = from end)", () => {
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
552
|
+
const { ctx } = ndCtx();
|
|
553
|
+
const out = (r) => dec(decimalCodec(6).encode(r));
|
|
554
|
+
// pack
|
|
555
|
+
assert.equal(out(ctx.apply("nd", [int(1n), int(2n), int(3n)])), "[1,2,3]");
|
|
556
|
+
assert.equal(out(ctx.apply("nd", [])), "[]");
|
|
557
|
+
// length
|
|
558
|
+
const xs = V([5, 6, 7, 8]);
|
|
559
|
+
assert.equal(N(ctx.apply("length", [xs])), 4n);
|
|
560
|
+
// at, forward and from the end
|
|
561
|
+
assert.equal(N(ctx.apply("at", [xs, int(0n)])), 5n);
|
|
562
|
+
assert.equal(N(ctx.apply("at", [xs, int(-1n)])), 8n);
|
|
563
|
+
// out of range declines (throws → the facade maps it to "rule does not fire")
|
|
564
|
+
assert.throws(() => ctx.apply("at", [xs, int(9n)]));
|
|
649
565
|
});
|
|
650
566
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
651
567
|
// 14 — Higher-order ops: the FUNCTION ARGUMENT is ANY existing operation.
|
|
652
568
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
653
569
|
test("reduce folds by ANY binary op — +, *, max are sum, product, maximum", () => {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
N(ctx.apply("reduce", [
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
assert.equal(N(ctx.apply("reduce", [xs, symbol(enc("min"))])), 1n); // minimum
|
|
665
|
-
// a seed makes the fold total over an empty list
|
|
666
|
-
assert.equal(N(ctx.apply("reduce", [V([]), symbol(enc("+")), int(0n)])), 0n);
|
|
570
|
+
const { ctx } = ndCtx();
|
|
571
|
+
const xs = V([3, 1, 4, 1, 5, 9, 2, 6]);
|
|
572
|
+
// The op argument is a Value naming an operation; resolveOp turns it into the
|
|
573
|
+
// canonical op, so reduce reuses the whole scalar vocabulary as folds.
|
|
574
|
+
assert.equal(N(ctx.apply("reduce", [xs, symbol(enc("+"))])), 31n); // sum
|
|
575
|
+
assert.equal(N(ctx.apply("reduce", [V([1, 2, 3, 4]), symbol(enc("*"))])), 24n);
|
|
576
|
+
assert.equal(N(ctx.apply("reduce", [xs, symbol(enc("max"))])), 9n); // maximum
|
|
577
|
+
assert.equal(N(ctx.apply("reduce", [xs, symbol(enc("min"))])), 1n); // minimum
|
|
578
|
+
// a seed makes the fold total over an empty list
|
|
579
|
+
assert.equal(N(ctx.apply("reduce", [V([]), symbol(enc("+")), int(0n)])), 0n);
|
|
667
580
|
});
|
|
668
581
|
test("reduce by a binary op BROADCASTS — a column sum falls out of nesting", () => {
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
])),
|
|
678
|
-
"[5,7,9]",
|
|
679
|
-
);
|
|
582
|
+
const { ctx } = ndCtx();
|
|
583
|
+
const out = (r) => dec(decimalCodec(6).encode(r));
|
|
584
|
+
// reduce(rows, +) folds the row-LISTS with "+", and "+" itself broadcasts over
|
|
585
|
+
// them — so summing a list of rows yields the column sums, no matrix code.
|
|
586
|
+
assert.equal(out(ctx.apply("reduce", [
|
|
587
|
+
V([[1, 2, 3], [4, 5, 6]]),
|
|
588
|
+
symbol(enc("+")),
|
|
589
|
+
])), "[5,7,9]");
|
|
680
590
|
});
|
|
681
591
|
test("map applies a unary op to each element; works over nesting", () => {
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
out(ctx.apply("map", [V([1,
|
|
686
|
-
"[-1,-2,-3]",
|
|
687
|
-
);
|
|
688
|
-
assert.equal(
|
|
689
|
-
out(ctx.apply("map", [V([1, 4, 9]), symbol(enc("sqrt"))])),
|
|
690
|
-
"[1,2,3]",
|
|
691
|
-
);
|
|
592
|
+
const { ctx } = ndCtx();
|
|
593
|
+
const out = (r) => dec(decimalCodec(6).encode(r));
|
|
594
|
+
assert.equal(out(ctx.apply("map", [V([1, 2, 3]), symbol(enc("negate"))])), "[-1,-2,-3]");
|
|
595
|
+
assert.equal(out(ctx.apply("map", [V([1, 4, 9]), symbol(enc("sqrt"))])), "[1,2,3]");
|
|
692
596
|
});
|
|
693
597
|
test("filter keeps elements a unary predicate accepts; find returns the first", () => {
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
598
|
+
const { ctx } = ndCtx();
|
|
599
|
+
const out = (r) => dec(decimalCodec(6).encode(r));
|
|
600
|
+
const xs = V([0, 5, 0, 3, 0, 8]);
|
|
601
|
+
// "sign" is 0 for zero, 1 for positive → a truthiness predicate
|
|
602
|
+
assert.equal(out(ctx.apply("filter", [xs, symbol(enc("sign"))])), "[5,3,8]");
|
|
603
|
+
assert.equal(N(ctx.apply("find", [xs, symbol(enc("sign"))])), 5n);
|
|
604
|
+
// find with no match returns the empty nd (graph-evidenced "nothing"), not a guess
|
|
605
|
+
const none = ctx.apply("find", [
|
|
606
|
+
V([0, 0, 0]),
|
|
607
|
+
symbol(enc("sign")),
|
|
608
|
+
]);
|
|
609
|
+
assert.ok(isNd(none) && none.items.length === 0);
|
|
706
610
|
});
|
|
707
611
|
test("structural plumbing: concat, reverse, flatten, zip, range, rank, shape", () => {
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
])),
|
|
718
|
-
"[
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
assert.equal(N(ctx.apply("rank", [V([[1, 2], [3, 4]])])), 2n);
|
|
724
|
-
assert.equal(N(ctx.apply("rank", [int(7n)])), 0n);
|
|
725
|
-
assert.equal(out(ctx.apply("shape", [V([[1, 2, 3], [4, 5, 6]])])), "[2,3]");
|
|
612
|
+
const { ctx } = ndCtx();
|
|
613
|
+
const out = (r) => dec(decimalCodec(6).encode(r));
|
|
614
|
+
assert.equal(out(ctx.apply("concat", [V([1, 2]), V([3, 4])])), "[1,2,3,4]");
|
|
615
|
+
assert.equal(out(ctx.apply("reverse", [V([1, 2, 3])])), "[3,2,1]");
|
|
616
|
+
assert.equal(out(ctx.apply("flatten", [V([[1, 2], [3, 4]])])), "[1,2,3,4]");
|
|
617
|
+
assert.equal(out(ctx.apply("zip", [
|
|
618
|
+
V([1, 2, 3]),
|
|
619
|
+
V([4, 5, 6]),
|
|
620
|
+
])), "[[1,4],[2,5],[3,6]]");
|
|
621
|
+
assert.equal(out(ctx.apply("range", [int(4n)])), "[0,1,2,3]");
|
|
622
|
+
assert.equal(out(ctx.apply("range", [int(2n), int(5n)])), "[2,3,4]");
|
|
623
|
+
// rank = nesting depth; shape = size down the first axis
|
|
624
|
+
assert.equal(N(ctx.apply("rank", [V([[1, 2], [3, 4]])])), 2n);
|
|
625
|
+
assert.equal(N(ctx.apply("rank", [int(7n)])), 0n);
|
|
626
|
+
assert.equal(out(ctx.apply("shape", [V([[1, 2, 3], [4, 5, 6]])])), "[2,3]");
|
|
726
627
|
});
|
|
727
628
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
728
629
|
// 15 — resolveOp: the function argument is resolved by the SAME machinery as any
|
|
729
630
|
// operator — a surface form, then (when not literal) its resonant meaning.
|
|
730
631
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
731
632
|
test("resolveOp: a literal surface form names its canonical op", () => {
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
633
|
+
const { ctx } = ndCtx();
|
|
634
|
+
// canonical name, a synonym spelling, and a glyph all name the same op
|
|
635
|
+
assert.equal(ctx.resolveOp(symbol(enc("add"))), "add");
|
|
636
|
+
assert.equal(ctx.resolveOp(symbol(enc("plus"))), "add");
|
|
637
|
+
assert.equal(ctx.resolveOp(symbol(enc("+"))), "add");
|
|
638
|
+
assert.equal(ctx.resolveOp(symbol(enc("max"))), "max");
|
|
639
|
+
// nothing that names an op → null (the higher-order op then declines)
|
|
640
|
+
assert.equal(ctx.resolveOp(symbol(enc("zzz"))), null);
|
|
740
641
|
});
|
|
741
642
|
test("resolveOp disambiguates a shared surface form by arity", () => {
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
643
|
+
// Build a registry where one surface "@" is claimed by both a unary and a
|
|
644
|
+
// binary op, to exercise the arity tie-break directly (the kernel happens to
|
|
645
|
+
// share no surface across arities today, so this proves the mechanism).
|
|
646
|
+
const r = new OperationRegistry();
|
|
647
|
+
r.prim("u_at", 1, ["@"], (a) => a[0]);
|
|
648
|
+
r.prim("b_at", 2, ["@"], (a) => a[0]);
|
|
649
|
+
const ctx = r.context(NO_RESONANCE, { tol: 1e-10, maxIter: 100 });
|
|
650
|
+
assert.equal(ctx.resolveOp(symbol(enc("@")), 1), "u_at");
|
|
651
|
+
assert.equal(ctx.resolveOp(symbol(enc("@")), 2), "b_at");
|
|
652
|
+
// no arity hint → the first claimant (registration order)
|
|
653
|
+
assert.equal(ctx.resolveOp(symbol(enc("@"))), "u_at");
|
|
753
654
|
});
|
|
754
655
|
test("resolveOp falls through to RESONANCE when the bytes are not a literal form", () => {
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
])),
|
|
769
|
-
60n,
|
|
770
|
-
);
|
|
656
|
+
// The host's resonance maps a meaning ("grand total of") to an op; resolveOp
|
|
657
|
+
// uses it exactly as the scan would for an operator a query does not spell.
|
|
658
|
+
const stub = {
|
|
659
|
+
opposite: () => null,
|
|
660
|
+
recogniseOp: (b) => dec(b).includes("grand total") ? "add" : null,
|
|
661
|
+
};
|
|
662
|
+
const { ctx } = ndCtx(stub);
|
|
663
|
+
assert.equal(ctx.resolveOp(symbol(enc("the grand total of"))), "add");
|
|
664
|
+
// and a reduce driven by that meaning folds correctly
|
|
665
|
+
assert.equal(N(ctx.apply("reduce", [
|
|
666
|
+
V([10, 20, 30]),
|
|
667
|
+
symbol(enc("the grand total of")),
|
|
668
|
+
])), 60n);
|
|
771
669
|
});
|
|
772
670
|
test("a higher-order op declines (throws) when its operation argument is unrecognised", () => {
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
])
|
|
781
|
-
);
|
|
671
|
+
const { ctx } = ndCtx();
|
|
672
|
+
// no surface form, no resonance → resolveFn throws → the Alu facade would map
|
|
673
|
+
// this to "rule does not fire".
|
|
674
|
+
assert.throws(() => ctx.apply("reduce", [
|
|
675
|
+
V([1, 2, 3]),
|
|
676
|
+
symbol(enc("flibbertigibbet")),
|
|
677
|
+
]));
|
|
782
678
|
});
|
|
783
679
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
784
680
|
// 16 — The Alu facade over nd: applyValues (Values in, canonical bytes out).
|
|
@@ -787,38 +683,29 @@ test("a higher-order op declines (throws) when its operation argument is unrecog
|
|
|
787
683
|
// facade as an already-assembled Value, never as a bracket byte string.
|
|
788
684
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
789
685
|
test("applyValues computes nd results: broadcast, reduce, and a composed pipeline", () => {
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
"[
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
V([1, 2, 3, 4]),
|
|
804
|
-
symbol(enc("negate")),
|
|
805
|
-
]);
|
|
806
|
-
assert.equal(dec(mapped), "[-1,-2,-3,-4]");
|
|
807
|
-
// an unknown op or a declining computation → null (the rule does not fire)
|
|
808
|
-
assert.equal(
|
|
809
|
-
u.applyValues("reduce", [V([1, 2]), symbol(enc("nonexistent-op"))]),
|
|
810
|
-
null,
|
|
811
|
-
);
|
|
686
|
+
const u = new Alu();
|
|
687
|
+
// broadcast — two list Values in, the canonical bracket spelling out
|
|
688
|
+
assert.equal(dec(u.applyValues("add", [V([1, 2, 3]), V([4, 5, 6])])), "[5,7,9]");
|
|
689
|
+
// reduce with an operator surface form (the op argument is a symbol Value)
|
|
690
|
+
assert.equal(dec(u.applyValues("reduce", [V([1, 2, 3, 4]), symbol(enc("+"))])), "10");
|
|
691
|
+
// a computed nd is itself a valid operand (composition for free): map then reduce
|
|
692
|
+
const mapped = u.applyValues("map", [
|
|
693
|
+
V([1, 2, 3, 4]),
|
|
694
|
+
symbol(enc("negate")),
|
|
695
|
+
]);
|
|
696
|
+
assert.equal(dec(mapped), "[-1,-2,-3,-4]");
|
|
697
|
+
// an unknown op or a declining computation → null (the rule does not fire)
|
|
698
|
+
assert.equal(u.applyValues("reduce", [V([1, 2]), symbol(enc("nonexistent-op"))]), null);
|
|
812
699
|
});
|
|
813
700
|
test("applyBytes is SCALAR-only: it does not read list structure from bytes", () => {
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
701
|
+
const u = new Alu();
|
|
702
|
+
// applyBytes decodes operands through the codec, which is scalar-only — a
|
|
703
|
+
// bracket literal becomes an opaque symbol, so arithmetic over it declines.
|
|
704
|
+
// Reading list STRUCTURE from bytes is recogniseValue's job (§17), not the
|
|
705
|
+
// codec's; applyBytes stays the pure scalar-operand path the search uses.
|
|
706
|
+
assert.equal(u.applyBytes("add", [enc("[1,2,3]"), enc("[4,5,6]")]), null);
|
|
707
|
+
// scalar arithmetic through the byte facade is unaffected
|
|
708
|
+
assert.equal(dec(u.applyBytes("add", [enc("2"), enc("2")])), "4");
|
|
822
709
|
});
|
|
823
710
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
824
711
|
// 17 — recogniseValue: the bytes→Value boundary. A span is a scalar, or a LIST
|
|
@@ -827,51 +714,51 @@ test("applyBytes is SCALAR-only: it does not read list structure from bytes", ()
|
|
|
827
714
|
// layered over scan, so the kernel itself only ever computes on Values.
|
|
828
715
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
829
716
|
test("recogniseValue reads a SCALAR — a numeral, or an opaque symbol", () => {
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
717
|
+
const u = new Alu();
|
|
718
|
+
assert.deepEqual(u.recogniseValue(enc("42")), int(42n));
|
|
719
|
+
assert.deepEqual(u.recogniseValue(enc("3.5")), real(3.5));
|
|
720
|
+
// a single word / learnt form / operator name stays an opaque symbol
|
|
721
|
+
assert.equal(u.recogniseValue(enc("large")).domain, "symbol");
|
|
722
|
+
assert.equal(u.recogniseValue(enc("+")).domain, "symbol");
|
|
723
|
+
// surrounding whitespace is trimmed before the reading
|
|
724
|
+
assert.deepEqual(u.recogniseValue(enc(" 7 ")), int(7n));
|
|
838
725
|
});
|
|
839
726
|
test("recogniseValue reads a CONTAINER list, separator spelling not privileged", () => {
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
727
|
+
const u = new Alu();
|
|
728
|
+
const enc6 = decimalCodec(6);
|
|
729
|
+
const to = (s) => dec(enc6.encode(u.recogniseValue(enc(s))));
|
|
730
|
+
// comma, comma+space, or bare space inside the brackets — all the same list
|
|
731
|
+
for (const s of ["[1,2,3]", "[1, 2, 3]", "[1 2 3]"]) {
|
|
732
|
+
assert.equal(to(s), "[1,2,3]", s);
|
|
733
|
+
}
|
|
734
|
+
// nested (a matrix), ragged, and heterogeneous all recurse element-wise
|
|
735
|
+
assert.equal(to("[[1,2],[3,4,5]]"), "[[1,2],[3,4,5]]");
|
|
736
|
+
assert.equal(to("[1,3.5,large]"), "[1,3.5,large]");
|
|
737
|
+
assert.equal(to("[]"), "[]"); // empty container → the empty list
|
|
738
|
+
// a parsed container really is an nd with the right element domains
|
|
739
|
+
const v = u.recogniseValue(enc("[1,3.5,large]"));
|
|
740
|
+
assert.ok(isNd(v));
|
|
741
|
+
assert.deepEqual(v.items.map(tagOf), ["int", "real", "symbol"]);
|
|
855
742
|
});
|
|
856
743
|
test("recogniseValue reads a bare SEQUENCE by ANY consistent connective", () => {
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
744
|
+
const u = new Alu();
|
|
745
|
+
const enc6 = decimalCodec(6);
|
|
746
|
+
const to = (s) => dec(enc6.encode(u.recogniseValue(enc(s))));
|
|
747
|
+
// space, comma, comma+space, or the word " and " between ≥2 numeric operands
|
|
748
|
+
for (const s of ["1 2 3", "1,2,3", "1, 2, 3", "1 and 2 and 3"]) {
|
|
749
|
+
assert.equal(to(s), "[1,2,3]", s);
|
|
750
|
+
}
|
|
864
751
|
});
|
|
865
752
|
test("recogniseValue does NOT over-read: inconsistent or wordy spans stay scalar", () => {
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
753
|
+
const u = new Alu();
|
|
754
|
+
// a single numeral is a scalar, never a one-element list
|
|
755
|
+
assert.equal(u.recogniseValue(enc("5")).domain, "int");
|
|
756
|
+
// an INCONSISTENT separator ("space" then "comma") is not one sequence → symbol
|
|
757
|
+
assert.equal(u.recogniseValue(enc("1 2,3")).domain, "symbol");
|
|
758
|
+
// leftover material at an edge (a trailing word) is not a clean sequence
|
|
759
|
+
assert.equal(u.recogniseValue(enc("1 2 buckle")).domain, "symbol");
|
|
760
|
+
// a bare run of words (no numeric operands) stays an opaque symbol
|
|
761
|
+
assert.equal(u.recogniseValue(enc("large tall")).domain, "symbol");
|
|
762
|
+
// a stray unbalanced bracket is not a container → opaque symbol
|
|
763
|
+
assert.equal(u.recogniseValue(enc("[1,2")).domain, "symbol");
|
|
877
764
|
});
|