@hviana/sema 0.1.9 → 0.2.1
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/dist/example/demo.d.ts +1 -0
- package/dist/example/demo.js +39 -0
- package/dist/example/train_base.d.ts +87 -0
- package/dist/example/train_base.js +2252 -0
- package/dist/src/alphabet.d.ts +7 -0
- package/dist/src/alphabet.js +33 -0
- package/dist/src/alu/src/alu.d.ts +185 -0
- package/dist/src/alu/src/alu.js +440 -0
- package/dist/src/alu/src/expr.d.ts +61 -0
- package/dist/src/alu/src/expr.js +318 -0
- package/dist/src/alu/src/index.d.ts +11 -0
- package/dist/src/alu/src/index.js +19 -0
- package/dist/src/alu/src/kernel-arith.d.ts +16 -0
- package/dist/src/alu/src/kernel-arith.js +264 -0
- package/dist/src/alu/src/kernel-bits.d.ts +19 -0
- package/dist/src/alu/src/kernel-bits.js +152 -0
- package/dist/src/alu/src/kernel-logic.d.ts +4 -0
- package/dist/src/alu/src/kernel-logic.js +60 -0
- package/dist/src/alu/src/kernel-nd.d.ts +3 -0
- package/dist/src/alu/src/kernel-nd.js +208 -0
- package/dist/src/alu/src/kernel-numeric.d.ts +54 -0
- package/dist/src/alu/src/kernel-numeric.js +366 -0
- package/dist/src/alu/src/operation.d.ts +168 -0
- package/dist/src/alu/src/operation.js +189 -0
- package/dist/src/alu/src/parser.d.ts +221 -0
- package/dist/src/alu/src/parser.js +577 -0
- package/dist/src/alu/src/resonance.d.ts +55 -0
- package/dist/src/alu/src/resonance.js +126 -0
- package/dist/src/alu/src/text.d.ts +31 -0
- package/dist/src/alu/src/text.js +73 -0
- package/dist/src/alu/src/value.d.ts +109 -0
- package/dist/src/alu/src/value.js +300 -0
- package/dist/src/alu/test/alu.test.d.ts +1 -0
- package/dist/src/alu/test/alu.test.js +764 -0
- package/dist/src/bytes.d.ts +14 -0
- package/dist/src/bytes.js +59 -0
- package/dist/src/canon.d.ts +26 -0
- package/dist/src/canon.js +57 -0
- package/dist/src/config.d.ts +111 -0
- package/dist/src/config.js +91 -0
- package/dist/src/derive/src/deduction.d.ts +125 -0
- package/dist/src/derive/src/deduction.js +155 -0
- package/dist/src/derive/src/index.d.ts +7 -0
- package/dist/src/derive/src/index.js +11 -0
- package/dist/src/derive/src/priority-queue.d.ts +20 -0
- package/dist/src/derive/src/priority-queue.js +73 -0
- package/dist/src/derive/src/rewrite.d.ts +56 -0
- package/dist/src/derive/src/rewrite.js +100 -0
- package/dist/src/derive/src/trie.d.ts +90 -0
- package/dist/src/derive/src/trie.js +217 -0
- package/dist/src/derive/test/derive.test.d.ts +1 -0
- package/dist/src/derive/test/derive.test.js +122 -0
- package/dist/src/extension.d.ts +37 -0
- package/dist/src/extension.js +7 -0
- package/dist/src/geometry.d.ts +148 -0
- package/dist/src/geometry.js +511 -0
- package/dist/src/index.d.ts +16 -0
- package/dist/src/index.js +18 -0
- package/dist/src/ingest-cache.d.ts +41 -0
- package/dist/src/ingest-cache.js +161 -0
- package/dist/src/mind/articulation.d.ts +6 -0
- package/dist/src/mind/articulation.js +99 -0
- package/dist/src/mind/attention.d.ts +72 -0
- package/dist/src/mind/attention.js +898 -0
- package/dist/src/mind/canonical.d.ts +29 -0
- package/dist/src/mind/canonical.js +88 -0
- package/dist/src/mind/graph-search.d.ts +270 -0
- package/dist/src/mind/graph-search.js +927 -0
- package/dist/src/mind/index.d.ts +5 -0
- package/dist/src/mind/index.js +5 -0
- package/dist/src/mind/junction.d.ts +95 -0
- package/dist/src/mind/junction.js +262 -0
- package/dist/src/mind/learning.d.ts +47 -0
- package/dist/src/mind/learning.js +233 -0
- package/dist/src/mind/match.d.ts +113 -0
- package/dist/src/mind/match.js +446 -0
- package/dist/src/mind/mechanisms/alu.d.ts +4 -0
- package/dist/src/mind/mechanisms/alu.js +29 -0
- package/dist/src/mind/mechanisms/cast.d.ts +35 -0
- package/dist/src/mind/mechanisms/cast.js +461 -0
- package/dist/src/mind/mechanisms/confluence.d.ts +24 -0
- package/dist/src/mind/mechanisms/confluence.js +225 -0
- package/dist/src/mind/mechanisms/cover.d.ts +6 -0
- package/dist/src/mind/mechanisms/cover.js +179 -0
- package/dist/src/mind/mechanisms/extraction.d.ts +67 -0
- package/dist/src/mind/mechanisms/extraction.js +342 -0
- package/dist/src/mind/mechanisms/recall.d.ts +13 -0
- package/dist/src/mind/mechanisms/recall.js +235 -0
- package/dist/src/mind/mind.d.ts +273 -0
- package/dist/src/mind/mind.js +598 -0
- package/dist/src/mind/pipeline-mechanism.d.ts +142 -0
- package/dist/src/mind/pipeline-mechanism.js +213 -0
- package/dist/src/mind/pipeline.d.ts +20 -0
- package/dist/src/mind/pipeline.js +185 -0
- package/dist/src/mind/primitives.d.ts +63 -0
- package/dist/src/mind/primitives.js +251 -0
- package/dist/src/mind/rationale.d.ts +134 -0
- package/dist/src/mind/rationale.js +162 -0
- package/dist/src/mind/reasoning.d.ts +15 -0
- package/dist/src/mind/reasoning.js +162 -0
- package/dist/src/mind/recognition.d.ts +20 -0
- package/dist/src/mind/recognition.js +246 -0
- package/dist/src/mind/resonance.d.ts +23 -0
- package/dist/src/mind/resonance.js +0 -0
- package/dist/src/mind/trace.d.ts +15 -0
- package/dist/src/mind/trace.js +73 -0
- package/dist/src/mind/traverse.d.ts +113 -0
- package/dist/src/mind/traverse.js +488 -0
- package/dist/src/mind/types.d.ts +186 -0
- package/dist/src/mind/types.js +84 -0
- 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-ivf/src/index.js +4 -0
- 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 +31 -0
- package/dist/src/sema.js +63 -0
- package/dist/src/store-sqlite.d.ts +184 -0
- package/dist/src/store-sqlite.js +942 -0
- package/dist/src/store.d.ts +654 -0
- package/dist/src/store.js +1612 -0
- package/dist/src/vec.d.ts +31 -0
- package/dist/src/vec.js +109 -0
- package/package.json +1 -1
- package/src/mind/graph-search.ts +128 -46
- package/src/mind/mind.ts +37 -0
|
@@ -0,0 +1,764 @@
|
|
|
1
|
+
// Self-contained tests for the `alu` library: the ALU kernel — logic from nand,
|
|
2
|
+
// the exact bit-vector arithmetic bootstrap, the polymorphic real arithmetic,
|
|
3
|
+
// the numerical limit layer, and the synchronous operator/operand scanner.
|
|
4
|
+
// Uses node:test; no dependency on sema (a stub resonance stands in for the
|
|
5
|
+
// host's halo space, so the polymorphic inverse is exercised with zero coupling).
|
|
6
|
+
import { test } from "node:test";
|
|
7
|
+
import assert from "node:assert/strict";
|
|
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";
|
|
9
|
+
// ── helpers ─────────────────────────────────────────────────────────────────
|
|
10
|
+
const enc = (s) => new TextEncoder().encode(s);
|
|
11
|
+
const dec = (b) => new TextDecoder().decode(b);
|
|
12
|
+
const close = (a, b, eps = 1e-6) => assert.ok(Math.abs(a - b) <= eps, `${a} !~ ${b} (eps ${eps})`);
|
|
13
|
+
/** Build an ALU Value from a JS literal — a TEST fixture, NOT a parser: the
|
|
14
|
+
* kernel no longer reads list structure from bytes (that is the host's
|
|
15
|
+
* meaning-level job — see src/mind/mind.ts recogniseValue), so the kernel's own tests
|
|
16
|
+
* construct Values directly. An array → nd, an integer → int, a fractional
|
|
17
|
+
* number → real, a string → an opaque symbol (its bytes verbatim). */
|
|
18
|
+
const V = (x) => Array.isArray(x)
|
|
19
|
+
? nd(x.map(V))
|
|
20
|
+
: typeof x === "number"
|
|
21
|
+
? (Number.isInteger(x) ? int(BigInt(x)) : real(x))
|
|
22
|
+
: symbol(enc(x));
|
|
23
|
+
/** A logic-only registry + context — the substrate the bit bootstrap runs on. */
|
|
24
|
+
function logicCtx() {
|
|
25
|
+
const r = new OperationRegistry();
|
|
26
|
+
registerLogic(r);
|
|
27
|
+
return r.context(NO_RESONANCE, { tol: 1e-10, maxIter: 1000 });
|
|
28
|
+
}
|
|
29
|
+
/** A full registry context (all kernels). */
|
|
30
|
+
function fullCtx(resonance = NO_RESONANCE) {
|
|
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 }) };
|
|
37
|
+
}
|
|
38
|
+
const B = (v) => (v.domain === "bit" ? v.b : NaN);
|
|
39
|
+
const N = (v) => (v.domain === "int" ? v.n : NaN);
|
|
40
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
41
|
+
// 1 — Logic: the completeness layer. nand is the one axiom; every gate derives.
|
|
42
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
43
|
+
test("nand truth table is the irreducible axiom", () => {
|
|
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);
|
|
50
|
+
});
|
|
51
|
+
test("not/and/or derive from nand and match their truth tables", () => {
|
|
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
|
+
}
|
|
65
|
+
});
|
|
66
|
+
test("nor/xor/xnor/implies/iff all derive from nand", () => {
|
|
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]);
|
|
75
|
+
});
|
|
76
|
+
test("mux(s,a,b) selects a when s=0, b when s=1 — the bridge to control flow", () => {
|
|
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);
|
|
85
|
+
});
|
|
86
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
87
|
+
// 2 — Arithmetic: the bit-vector bootstrap is EXACT (the nand→everything proof).
|
|
88
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
89
|
+
test("full_adder built from xor/and/or matches its truth table", () => {
|
|
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)),
|
|
97
|
+
]));
|
|
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
|
|
102
|
+
});
|
|
103
|
+
test("ripple add / two's-complement negate / shift-add multiply equal native bigint", () => {
|
|
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
|
+
}
|
|
121
|
+
});
|
|
122
|
+
test("the bit bootstrap stays exact past 2^53", () => {
|
|
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);
|
|
127
|
+
});
|
|
128
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
129
|
+
// 3 — Arithmetic: polymorphic primitives + the derived field and order.
|
|
130
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
131
|
+
test("add/multiply are exact on ints and lift to reals", () => {
|
|
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);
|
|
139
|
+
});
|
|
140
|
+
test("subtract = add∘negate, divide = multiply∘reciprocal", () => {
|
|
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);
|
|
144
|
+
});
|
|
145
|
+
test("every comparison = sign∘subtract", () => {
|
|
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);
|
|
155
|
+
});
|
|
156
|
+
test("abs/min/max/power/gcd derive correctly", () => {
|
|
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);
|
|
164
|
+
});
|
|
165
|
+
test("real power via exp∘log agrees with Math.pow", () => {
|
|
166
|
+
const { ctx } = fullCtx();
|
|
167
|
+
close(asReal(ctx.apply("power", [real(2), real(0.5)])), Math.SQRT2, 1e-6);
|
|
168
|
+
});
|
|
169
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
170
|
+
// 4 — Arithmetic: polynomial / vector / matrix / linsolve (structured arith).
|
|
171
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
172
|
+
test("polyEval (Horner), dot, matMul", () => {
|
|
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
|
+
]);
|
|
180
|
+
});
|
|
181
|
+
test("linsolve solves a 3x3 system (Gaussian elimination)", () => {
|
|
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);
|
|
188
|
+
});
|
|
189
|
+
test("linsolve returns null for a singular matrix", () => {
|
|
190
|
+
assert.equal(linsolve([[1, 2], [2, 4]], [3, 6]), null);
|
|
191
|
+
});
|
|
192
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
193
|
+
// 5 — Numerical: the limit layer. Each op is a converge instance.
|
|
194
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
195
|
+
test("converge reaches a contraction's fixed point", () => {
|
|
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);
|
|
199
|
+
});
|
|
200
|
+
test("diff ≈ analytic derivative", () => {
|
|
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);
|
|
203
|
+
});
|
|
204
|
+
test("integrate ≈ closed form", () => {
|
|
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);
|
|
207
|
+
});
|
|
208
|
+
test("solve (Newton) finds a root", () => {
|
|
209
|
+
close(solve((x) => x * x - 2, 1, 1e-12, 200), Math.SQRT2, 1e-8);
|
|
210
|
+
});
|
|
211
|
+
test("exp/log/sin/cos/sqrt converge to the right limits", () => {
|
|
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);
|
|
222
|
+
});
|
|
223
|
+
test("optimize finds a minimum; odeSolve integrates y'=y", () => {
|
|
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);
|
|
226
|
+
});
|
|
227
|
+
test("regress recovers a known line; interpolate is linear", () => {
|
|
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);
|
|
236
|
+
});
|
|
237
|
+
test("powerEig / topSingular by power iteration", () => {
|
|
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);
|
|
243
|
+
});
|
|
244
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
245
|
+
// 6 — The Operation model: derived is indistinguishable; one-line extension.
|
|
246
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
247
|
+
test("a derived op has the same record shape as a primitive", () => {
|
|
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
|
+
}
|
|
257
|
+
});
|
|
258
|
+
test("a brand-new derived op is registered in one call and computes", () => {
|
|
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
|
+
]),
|
|
265
|
+
]));
|
|
266
|
+
close(asReal(ctx.apply("hypot", [real(3), real(4)])), 5, 1e-7);
|
|
267
|
+
});
|
|
268
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
269
|
+
// 7 — Values: parse, the codec round-trip, and the polymorphic inverse.
|
|
270
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
271
|
+
test("parseValue: ints, reals, and symbols", () => {
|
|
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");
|
|
279
|
+
});
|
|
280
|
+
test("decimalCodec round-trips and formats reals deterministically", () => {
|
|
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));
|
|
289
|
+
});
|
|
290
|
+
test("inverse is polymorphic: number negates, symbol resonates to its opposite", () => {
|
|
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");
|
|
310
|
+
});
|
|
311
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
312
|
+
// 8 — The Alu facade: the synchronous scanner and applyBytes.
|
|
313
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
314
|
+
test("scan finds numeric operands and symbolic operators in raw bytes", () => {
|
|
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]);
|
|
322
|
+
});
|
|
323
|
+
test("scan keeps a multi-digit / decimal number whole, ignores a bare trailing dot", () => {
|
|
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]);
|
|
331
|
+
});
|
|
332
|
+
test("scan resolves the longest symbolic operator form", () => {
|
|
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 "<"
|
|
337
|
+
});
|
|
338
|
+
test("applyBytes computes the canonical result bytes", () => {
|
|
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);
|
|
346
|
+
});
|
|
347
|
+
test("a computed result is itself an operand (composition for free)", () => {
|
|
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");
|
|
352
|
+
});
|
|
353
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
354
|
+
// 9 — Expressions: a numerical op acts on a FUNCTION, evaluated by a recursive
|
|
355
|
+
// application of the same ALU (the "recursive call" case).
|
|
356
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
357
|
+
test("evalExpression evaluates through the kernel, auto-detecting the variable", () => {
|
|
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);
|
|
369
|
+
});
|
|
370
|
+
test("freeVariables lists candidates, excluding function names", () => {
|
|
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"]);
|
|
375
|
+
});
|
|
376
|
+
test("diff: the derivative op acts on an expression operand", () => {
|
|
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);
|
|
382
|
+
});
|
|
383
|
+
test("integrate: definite integral over an expression", () => {
|
|
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);
|
|
388
|
+
});
|
|
389
|
+
test("solve: a root of an expression near a guess", () => {
|
|
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);
|
|
393
|
+
});
|
|
394
|
+
test("limit: the value an expression approaches (a converge instance)", () => {
|
|
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);
|
|
398
|
+
});
|
|
399
|
+
test("optimize: a minimiser of an expression", () => {
|
|
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);
|
|
403
|
+
});
|
|
404
|
+
test("an expression op declines (null) when no evaluator can read the operand", () => {
|
|
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);
|
|
408
|
+
});
|
|
409
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
410
|
+
// 10 — The operation CONCEPT vocabulary (the generic, resonant recognition seed).
|
|
411
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
412
|
+
test("conceptAnchors exposes the operation vocabulary for resonant recognition", () => {
|
|
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
|
+
}
|
|
434
|
+
});
|
|
435
|
+
test("prefetchRecognisedOps bridges async recognition to a sync map", async () => {
|
|
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);
|
|
447
|
+
});
|
|
448
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
449
|
+
// 11 — N-dimensional values: the recursive container. Representation + codec.
|
|
450
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
451
|
+
/** A full registry with the nd kernel too — the substrate for the nd tests. */
|
|
452
|
+
function ndCtx(resonance = NO_RESONANCE) {
|
|
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 }) };
|
|
460
|
+
}
|
|
461
|
+
test("nd is the recursive container: nested, ragged, heterogeneous", () => {
|
|
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);
|
|
476
|
+
});
|
|
477
|
+
test("decimalCodec encodes an nd to its canonical bracket spelling (nested, mixed)", () => {
|
|
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([])), "[]");
|
|
492
|
+
});
|
|
493
|
+
test("parseValue reads only SCALARS — a bracket literal decodes to an opaque symbol", () => {
|
|
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));
|
|
504
|
+
});
|
|
505
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
506
|
+
// 12 — Broadcast: EVERY scalar op lifts over nd automatically (one mechanism).
|
|
507
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
508
|
+
test("arithmetic broadcasts: list∘list zips, list∘scalar holds the scalar", () => {
|
|
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]");
|
|
517
|
+
});
|
|
518
|
+
test("broadcast RECURSES through nesting (a matrix op is the same code)", () => {
|
|
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]]");
|
|
525
|
+
});
|
|
526
|
+
test("broadcast spans op classes: logic, comparison, transcendental, inverse", () => {
|
|
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]");
|
|
543
|
+
});
|
|
544
|
+
test("broadcast over lists of unequal length declines (no silent truncation)", () => {
|
|
545
|
+
const { ctx } = ndCtx();
|
|
546
|
+
assert.throws(() => ctx.apply("add", [V([1, 2, 3]), V([4, 5])]));
|
|
547
|
+
});
|
|
548
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
549
|
+
// 13 — The nd core: nd / length / at are the only ops that touch `items`.
|
|
550
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
551
|
+
test("core: nd packs, length counts, at projects (negative = from end)", () => {
|
|
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)]));
|
|
565
|
+
});
|
|
566
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
567
|
+
// 14 — Higher-order ops: the FUNCTION ARGUMENT is ANY existing operation.
|
|
568
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
569
|
+
test("reduce folds by ANY binary op — +, *, max are sum, product, maximum", () => {
|
|
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);
|
|
580
|
+
});
|
|
581
|
+
test("reduce by a binary op BROADCASTS — a column sum falls out of nesting", () => {
|
|
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]");
|
|
590
|
+
});
|
|
591
|
+
test("map applies a unary op to each element; works over nesting", () => {
|
|
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]");
|
|
596
|
+
});
|
|
597
|
+
test("filter keeps elements a unary predicate accepts; find returns the first", () => {
|
|
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);
|
|
610
|
+
});
|
|
611
|
+
test("structural plumbing: concat, reverse, flatten, zip, range, rank, shape", () => {
|
|
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]");
|
|
627
|
+
});
|
|
628
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
629
|
+
// 15 — resolveOp: the function argument is resolved by the SAME machinery as any
|
|
630
|
+
// operator — a surface form, then (when not literal) its resonant meaning.
|
|
631
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
632
|
+
test("resolveOp: a literal surface form names its canonical op", () => {
|
|
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);
|
|
641
|
+
});
|
|
642
|
+
test("resolveOp disambiguates a shared surface form by arity", () => {
|
|
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");
|
|
654
|
+
});
|
|
655
|
+
test("resolveOp falls through to RESONANCE when the bytes are not a literal form", () => {
|
|
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);
|
|
669
|
+
});
|
|
670
|
+
test("a higher-order op declines (throws) when its operation argument is unrecognised", () => {
|
|
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
|
+
]));
|
|
678
|
+
});
|
|
679
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
680
|
+
// 16 — The Alu facade over nd: applyValues (Values in, canonical bytes out).
|
|
681
|
+
// The kernel computes on Values the HOST built — it does NOT parse list
|
|
682
|
+
// structure from bytes (src/mind/mind.ts recogniseValue does), so a list reaches the
|
|
683
|
+
// facade as an already-assembled Value, never as a bracket byte string.
|
|
684
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
685
|
+
test("applyValues computes nd results: broadcast, reduce, and a composed pipeline", () => {
|
|
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);
|
|
699
|
+
});
|
|
700
|
+
test("applyBytes is SCALAR-only: it does not read list structure from bytes", () => {
|
|
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");
|
|
709
|
+
});
|
|
710
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
711
|
+
// 17 — recogniseValue: the bytes→Value boundary. A span is a scalar, or a LIST
|
|
712
|
+
// (a run of element values joined by a CONSISTENT separator) — and no
|
|
713
|
+
// separator spelling is privileged. This is where structure is recognised,
|
|
714
|
+
// layered over scan, so the kernel itself only ever computes on Values.
|
|
715
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
716
|
+
test("recogniseValue reads a SCALAR — a numeral, or an opaque symbol", () => {
|
|
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));
|
|
725
|
+
});
|
|
726
|
+
test("recogniseValue reads a CONTAINER list, separator spelling not privileged", () => {
|
|
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"]);
|
|
742
|
+
});
|
|
743
|
+
test("recogniseValue reads a bare SEQUENCE by ANY consistent connective", () => {
|
|
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
|
+
}
|
|
751
|
+
});
|
|
752
|
+
test("recogniseValue does NOT over-read: inconsistent or wordy spans stay scalar", () => {
|
|
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");
|
|
764
|
+
});
|