trilean 1.1.0 → 1.2.0
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/README.md +98 -19
- package/dist/complex.cjs +34 -0
- package/dist/complex.d.cts +23 -0
- package/dist/complex.d.ts +23 -0
- package/dist/complex.js +30 -0
- package/dist/computed-value.cjs +10 -0
- package/dist/computed-value.d.cts +8 -0
- package/dist/computed-value.d.ts +8 -0
- package/dist/computed-value.js +10 -0
- package/dist/evaluator.cjs +113 -4
- package/dist/evaluator.js +113 -4
- package/dist/index.cjs +9 -0
- package/dist/index.d.cts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/tree.cjs +31 -1
- package/dist/tree.d.cts +53 -3
- package/dist/tree.d.ts +53 -3
- package/dist/tree.js +28 -2
- package/package.json +1 -1
- package/schemas/trilean.schema.json +1 -1
package/dist/evaluator.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_complex = require("./complex.cjs");
|
|
2
3
|
const require_computed_value = require("./computed-value.cjs");
|
|
3
4
|
const require_evaluation = require("./evaluation.cjs");
|
|
4
5
|
const require_functions = require("./functions.cjs");
|
|
@@ -76,6 +77,10 @@ function compareValues(op, left, right) {
|
|
|
76
77
|
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot compare numbers with incompatible units");
|
|
77
78
|
return require_evaluation.definite(applyComparisonOperator(op, left.value, right.value));
|
|
78
79
|
case "text": return require_evaluation.indeterminate("wrong-type", "text values are not comparable via 'compare'; use 'textCompare'");
|
|
80
|
+
case "boolean":
|
|
81
|
+
if (right.kind !== "boolean") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'boolean' value with a '${right.kind}' value`);
|
|
82
|
+
if (op !== "eq" && op !== "neq") return require_evaluation.indeterminate("wrong-type", `booleans have no natural ordering; '${op}' is only defined for 'eq'/'neq'`);
|
|
83
|
+
return require_evaluation.definite(op === "eq" ? left.value === right.value : left.value !== right.value);
|
|
79
84
|
case "instant": {
|
|
80
85
|
if (right.kind !== "instant") return require_evaluation.indeterminate("wrong-type", `cannot compare an 'instant' value with a '${right.kind}' value`);
|
|
81
86
|
const leftEpoch = toEpochMilliseconds(left.value);
|
|
@@ -87,6 +92,11 @@ function compareValues(op, left, right) {
|
|
|
87
92
|
case "duration":
|
|
88
93
|
if (right.kind !== "duration") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'duration' value with a '${right.kind}' value`);
|
|
89
94
|
return require_evaluation.definite(applyComparisonOperator(op, toMilliseconds(left.value, left.unit), toMilliseconds(right.value, right.unit)));
|
|
95
|
+
case "complex":
|
|
96
|
+
if (op !== "eq" && op !== "neq") return require_evaluation.indeterminate("wrong-type", `ordering operator '${op}' is not defined for complex values; the complex plane has no total order`);
|
|
97
|
+
if (right.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'complex' value with a '${right.kind}' value`);
|
|
98
|
+
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot compare complex values with incompatible units");
|
|
99
|
+
return require_evaluation.definite(op === "eq" ? left.re === right.re && left.im === right.im : left.re !== right.re || left.im !== right.im);
|
|
90
100
|
default: throw new Error("unreachable computed-value kind");
|
|
91
101
|
}
|
|
92
102
|
}
|
|
@@ -121,6 +131,9 @@ function computeMembershipMatch(operand, candidate) {
|
|
|
121
131
|
case "text":
|
|
122
132
|
if (candidate.kind !== "text") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'text' value with a '${candidate.kind}' value for membership`);
|
|
123
133
|
return require_evaluation.definite(operand.value === candidate.value);
|
|
134
|
+
case "boolean":
|
|
135
|
+
if (candidate.kind !== "boolean") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'boolean' value with a '${candidate.kind}' value for membership`);
|
|
136
|
+
return require_evaluation.definite(operand.value === candidate.value);
|
|
124
137
|
case "instant": {
|
|
125
138
|
if (candidate.kind !== "instant") return require_evaluation.indeterminate("wrong-type", `cannot compare an 'instant' value with a '${candidate.kind}' value for membership`);
|
|
126
139
|
const operandEpoch = toEpochMilliseconds(operand.value);
|
|
@@ -132,6 +145,10 @@ function computeMembershipMatch(operand, candidate) {
|
|
|
132
145
|
case "duration":
|
|
133
146
|
if (candidate.kind !== "duration") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'duration' value with a '${candidate.kind}' value for membership`);
|
|
134
147
|
return require_evaluation.definite(toMilliseconds(operand.value, operand.unit) === toMilliseconds(candidate.value, candidate.unit));
|
|
148
|
+
case "complex":
|
|
149
|
+
if (candidate.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'complex' value with a '${candidate.kind}' value for membership`);
|
|
150
|
+
if (!require_computed_value.unitsEqual(operand.unit, candidate.unit)) return require_evaluation.indeterminate("wrong-type", "cannot compare complex values with incompatible units for membership");
|
|
151
|
+
return require_evaluation.definite(operand.re === candidate.re && operand.im === candidate.im);
|
|
135
152
|
default: throw new Error("unreachable computed-value kind");
|
|
136
153
|
}
|
|
137
154
|
}
|
|
@@ -152,8 +169,15 @@ function applyNegate(operand) {
|
|
|
152
169
|
value: -operand.value,
|
|
153
170
|
unit: operand.unit
|
|
154
171
|
});
|
|
172
|
+
case "complex": return require_evaluation.definite({
|
|
173
|
+
kind: "complex",
|
|
174
|
+
re: -operand.re,
|
|
175
|
+
im: -operand.im,
|
|
176
|
+
unit: operand.unit
|
|
177
|
+
});
|
|
155
178
|
case "text":
|
|
156
|
-
case "instant":
|
|
179
|
+
case "instant":
|
|
180
|
+
case "boolean": return require_evaluation.indeterminate("wrong-type", `cannot negate a '${operand.kind}' value`);
|
|
157
181
|
default: throw new Error("unreachable computed-value kind");
|
|
158
182
|
}
|
|
159
183
|
}
|
|
@@ -223,6 +247,75 @@ function applyArithmeticOnDurations(op, left, right) {
|
|
|
223
247
|
default: throw new Error("unreachable arithmetic operator");
|
|
224
248
|
}
|
|
225
249
|
}
|
|
250
|
+
/** Every real number is a complex number with a zero imaginary part, so promoting one is exact and total -- unlike the temporal cross-kind combinations above, which each had to be enumerated because no such embedding exists between an `instant` and a `duration`. This is what lets a tree mix real and complex terms freely instead of forcing every real literal to be written as a complex one. */
|
|
251
|
+
function toComplexValue(value) {
|
|
252
|
+
if (value.kind === "complex") return value;
|
|
253
|
+
return {
|
|
254
|
+
kind: "complex",
|
|
255
|
+
re: value.value,
|
|
256
|
+
im: 0,
|
|
257
|
+
unit: value.unit
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
/** The complex product, as its own component-level helper rather than only a branch of the operator switch below, because `power`'s repeated multiplication needs exactly this and must not re-derive it. */
|
|
261
|
+
function multiplyComplexValues(left, right) {
|
|
262
|
+
return {
|
|
263
|
+
kind: "complex",
|
|
264
|
+
re: left.re * right.re - left.im * right.im,
|
|
265
|
+
im: left.re * right.im + left.im * right.re,
|
|
266
|
+
unit: require_computed_value.combineUnitsForMultiply(left.unit, right.unit)
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/** Complex arithmetic, over the one canonical rectangular representation the `complex` kind stores (see the "Complex values" section of README.md). `add`/`subtract` are component-wise and carry the same identical-units requirement real numbers already have. `power` is deliberately excluded from this operator set rather than handled and rejected here: its exponent must stay an un-promoted real number, so the dispatcher routes it to `applyComplexPower` first, and excluding it from the type is what makes the compiler enforce that routing instead of leaving a dead branch behind. */
|
|
270
|
+
function applyArithmeticOnComplex(op, left, right) {
|
|
271
|
+
switch (op) {
|
|
272
|
+
case "add":
|
|
273
|
+
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot add complex values with incompatible units");
|
|
274
|
+
return require_evaluation.definite({
|
|
275
|
+
kind: "complex",
|
|
276
|
+
re: left.re + right.re,
|
|
277
|
+
im: left.im + right.im,
|
|
278
|
+
unit: left.unit
|
|
279
|
+
});
|
|
280
|
+
case "subtract":
|
|
281
|
+
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot subtract complex values with incompatible units");
|
|
282
|
+
return require_evaluation.definite({
|
|
283
|
+
kind: "complex",
|
|
284
|
+
re: left.re - right.re,
|
|
285
|
+
im: left.im - right.im,
|
|
286
|
+
unit: left.unit
|
|
287
|
+
});
|
|
288
|
+
case "multiply": return require_evaluation.definite(multiplyComplexValues(left, right));
|
|
289
|
+
case "divide": {
|
|
290
|
+
const divisorSquaredMagnitude = right.re * right.re + right.im * right.im;
|
|
291
|
+
if (divisorSquaredMagnitude === 0) return require_evaluation.indeterminate("domain-error", "division by zero");
|
|
292
|
+
return require_evaluation.definite({
|
|
293
|
+
kind: "complex",
|
|
294
|
+
re: (left.re * right.re + left.im * right.im) / divisorSquaredMagnitude,
|
|
295
|
+
im: (left.im * right.re - left.re * right.im) / divisorSquaredMagnitude,
|
|
296
|
+
unit: require_computed_value.combineUnitsForDivide(left.unit, right.unit)
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
case "modulo": return require_evaluation.indeterminate("domain-error", "'modulo' is undefined for complex values");
|
|
300
|
+
default: throw new Error("unreachable arithmetic operator");
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/** `power` with a complex operand on either side, defined for exactly one case: a real integer exponent, evaluated as the repeated multiplication that integer exponentiation *is* (see the "Complex values" section of README.md for why an arbitrary complex exponent stays out of scope). */
|
|
304
|
+
function applyComplexPower(base, exponent) {
|
|
305
|
+
if (exponent.kind !== "number" || !Number.isInteger(exponent.value)) return require_evaluation.indeterminate("wrong-type", "'power' with a complex operand requires a real integer exponent");
|
|
306
|
+
if (!isDimensionless(base.unit) || !isDimensionless(exponent.unit)) return require_evaluation.indeterminate("wrong-type", "'power' requires dimensionless operands");
|
|
307
|
+
const complexUnit = {
|
|
308
|
+
kind: "complex",
|
|
309
|
+
re: 1,
|
|
310
|
+
im: 0,
|
|
311
|
+
unit: {}
|
|
312
|
+
};
|
|
313
|
+
const complexBase = toComplexValue(base);
|
|
314
|
+
let repeatedProduct = complexUnit;
|
|
315
|
+
for (let applied = 0; applied < Math.abs(exponent.value); applied += 1) repeatedProduct = multiplyComplexValues(repeatedProduct, complexBase);
|
|
316
|
+
if (exponent.value >= 0) return require_evaluation.definite(repeatedProduct);
|
|
317
|
+
return applyArithmeticOnComplex("divide", complexUnit, repeatedProduct);
|
|
318
|
+
}
|
|
226
319
|
/**
|
|
227
320
|
* Dispatches `arithmetic` by operand kind. The three cross-kind temporal combinations this design defines (`instant - instant`, `instant + duration`, `duration + instant`) are checked explicitly first, in that order, against the exact operator each requires; any other combination touching an `instant` is `wrong-type` (see "Temporal values" in README.md -- e.g. adding two instants, or subtracting a `duration` from an `instant`, are deliberately *not* defined). Same-kind `duration`/`duration` combinations are delegated to `applyArithmeticOnDurations`; a `duration` paired with anything other than an `instant` or another `duration` is `wrong-type`. Everything remaining requires two `number` operands.
|
|
228
321
|
*/
|
|
@@ -251,8 +344,12 @@ function applyArithmetic(op, left, right) {
|
|
|
251
344
|
if (left.kind === "instant" || right.kind === "instant") return require_evaluation.indeterminate("wrong-type", `arithmetic operator '${op}' is not defined between a '${left.kind}' and a '${right.kind}' value`);
|
|
252
345
|
if (left.kind === "duration" && right.kind === "duration") return applyArithmeticOnDurations(op, left, right);
|
|
253
346
|
if (left.kind === "duration" || right.kind === "duration") return require_evaluation.indeterminate("wrong-type", `arithmetic operator '${op}' is not defined between a '${left.kind}' and a '${right.kind}' value`);
|
|
254
|
-
if (left.kind !== "number") return require_evaluation.indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${left.kind}' value`);
|
|
255
|
-
if (right.kind !== "number") return require_evaluation.indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${right.kind}' value`);
|
|
347
|
+
if (left.kind !== "number" && left.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${left.kind}' value`);
|
|
348
|
+
if (right.kind !== "number" && right.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${right.kind}' value`);
|
|
349
|
+
if (left.kind === "complex" || right.kind === "complex") {
|
|
350
|
+
if (op === "power") return applyComplexPower(left, right);
|
|
351
|
+
return applyArithmeticOnComplex(op, toComplexValue(left), toComplexValue(right));
|
|
352
|
+
}
|
|
256
353
|
return applyArithmeticOnNumbers(op, left, right);
|
|
257
354
|
}
|
|
258
355
|
/** A defense-in-depth guard against a long acyclic `treeReference` chain exhausting the call stack -- distinct from, and layered on top of, the cycle detector below (`visitedTreeKeys`), which catches an actual repeat immediately and more precisely. */
|
|
@@ -367,7 +464,7 @@ async function evaluateValueInternal(node, context, resolvers, accumulator, func
|
|
|
367
464
|
const resolution = await resolvers.resolveValue(node.key, context);
|
|
368
465
|
if (!resolution.found) return require_evaluation.indeterminate("not-found", `no value found for reference key ${JSON.stringify(node.key)}`);
|
|
369
466
|
if (node.unit !== void 0) {
|
|
370
|
-
if (resolution.value.kind !== "number") return require_evaluation.indeterminate("wrong-type", "a unit was expected on a reference that resolved to a non-numeric value");
|
|
467
|
+
if (resolution.value.kind !== "number" && resolution.value.kind !== "complex") return require_evaluation.indeterminate("wrong-type", "a unit was expected on a reference that resolved to a non-numeric value");
|
|
371
468
|
if (!require_computed_value.unitsEqual(node.unit, resolution.value.unit)) return require_evaluation.indeterminate("wrong-type", "the resolved value's unit does not match the reference's expected unit");
|
|
372
469
|
}
|
|
373
470
|
return require_evaluation.definite(resolution.value);
|
|
@@ -397,6 +494,10 @@ async function evaluateValueInternal(node, context, resolvers, accumulator, func
|
|
|
397
494
|
kind: "text",
|
|
398
495
|
value: node.value
|
|
399
496
|
});
|
|
497
|
+
case "booleanLiteral": return require_evaluation.definite({
|
|
498
|
+
kind: "boolean",
|
|
499
|
+
value: node.value
|
|
500
|
+
});
|
|
400
501
|
case "instantLiteral": return require_evaluation.definite({
|
|
401
502
|
kind: "instant",
|
|
402
503
|
value: node.value
|
|
@@ -406,6 +507,14 @@ async function evaluateValueInternal(node, context, resolvers, accumulator, func
|
|
|
406
507
|
value: node.value,
|
|
407
508
|
unit: node.unit
|
|
408
509
|
});
|
|
510
|
+
case "complexLiteral":
|
|
511
|
+
if ("re" in node) return require_evaluation.definite({
|
|
512
|
+
kind: "complex",
|
|
513
|
+
re: node.re,
|
|
514
|
+
im: node.im,
|
|
515
|
+
unit: node.unit
|
|
516
|
+
});
|
|
517
|
+
return require_evaluation.definite(require_complex.complexFromPolar(node.magnitude, node.phase, node.unit));
|
|
409
518
|
case "arithmetic": {
|
|
410
519
|
const [left, right] = await Promise.all([evaluateValueInternal(node.left, context, resolvers, accumulator, functions, visitedTreeKeys, treeReferenceDepth), evaluateValueInternal(node.right, context, resolvers, accumulator, functions, visitedTreeKeys, treeReferenceDepth)]);
|
|
411
520
|
if (left.status === "indeterminate") return left;
|
package/dist/evaluator.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { complexFromPolar } from "./complex.js";
|
|
1
2
|
import { combineUnitsForDivide, combineUnitsForMultiply, unitsEqual } from "./computed-value.js";
|
|
2
3
|
import { definite, firstIndeterminate, indeterminate } from "./evaluation.js";
|
|
3
4
|
import { emptyFunctionRegistry } from "./functions.js";
|
|
@@ -75,6 +76,10 @@ function compareValues(op, left, right) {
|
|
|
75
76
|
if (!unitsEqual(left.unit, right.unit)) return indeterminate("wrong-type", "cannot compare numbers with incompatible units");
|
|
76
77
|
return definite(applyComparisonOperator(op, left.value, right.value));
|
|
77
78
|
case "text": return indeterminate("wrong-type", "text values are not comparable via 'compare'; use 'textCompare'");
|
|
79
|
+
case "boolean":
|
|
80
|
+
if (right.kind !== "boolean") return indeterminate("wrong-type", `cannot compare a 'boolean' value with a '${right.kind}' value`);
|
|
81
|
+
if (op !== "eq" && op !== "neq") return indeterminate("wrong-type", `booleans have no natural ordering; '${op}' is only defined for 'eq'/'neq'`);
|
|
82
|
+
return definite(op === "eq" ? left.value === right.value : left.value !== right.value);
|
|
78
83
|
case "instant": {
|
|
79
84
|
if (right.kind !== "instant") return indeterminate("wrong-type", `cannot compare an 'instant' value with a '${right.kind}' value`);
|
|
80
85
|
const leftEpoch = toEpochMilliseconds(left.value);
|
|
@@ -86,6 +91,11 @@ function compareValues(op, left, right) {
|
|
|
86
91
|
case "duration":
|
|
87
92
|
if (right.kind !== "duration") return indeterminate("wrong-type", `cannot compare a 'duration' value with a '${right.kind}' value`);
|
|
88
93
|
return definite(applyComparisonOperator(op, toMilliseconds(left.value, left.unit), toMilliseconds(right.value, right.unit)));
|
|
94
|
+
case "complex":
|
|
95
|
+
if (op !== "eq" && op !== "neq") return indeterminate("wrong-type", `ordering operator '${op}' is not defined for complex values; the complex plane has no total order`);
|
|
96
|
+
if (right.kind !== "complex") return indeterminate("wrong-type", `cannot compare a 'complex' value with a '${right.kind}' value`);
|
|
97
|
+
if (!unitsEqual(left.unit, right.unit)) return indeterminate("wrong-type", "cannot compare complex values with incompatible units");
|
|
98
|
+
return definite(op === "eq" ? left.re === right.re && left.im === right.im : left.re !== right.re || left.im !== right.im);
|
|
89
99
|
default: throw new Error("unreachable computed-value kind");
|
|
90
100
|
}
|
|
91
101
|
}
|
|
@@ -120,6 +130,9 @@ function computeMembershipMatch(operand, candidate) {
|
|
|
120
130
|
case "text":
|
|
121
131
|
if (candidate.kind !== "text") return indeterminate("wrong-type", `cannot compare a 'text' value with a '${candidate.kind}' value for membership`);
|
|
122
132
|
return definite(operand.value === candidate.value);
|
|
133
|
+
case "boolean":
|
|
134
|
+
if (candidate.kind !== "boolean") return indeterminate("wrong-type", `cannot compare a 'boolean' value with a '${candidate.kind}' value for membership`);
|
|
135
|
+
return definite(operand.value === candidate.value);
|
|
123
136
|
case "instant": {
|
|
124
137
|
if (candidate.kind !== "instant") return indeterminate("wrong-type", `cannot compare an 'instant' value with a '${candidate.kind}' value for membership`);
|
|
125
138
|
const operandEpoch = toEpochMilliseconds(operand.value);
|
|
@@ -131,6 +144,10 @@ function computeMembershipMatch(operand, candidate) {
|
|
|
131
144
|
case "duration":
|
|
132
145
|
if (candidate.kind !== "duration") return indeterminate("wrong-type", `cannot compare a 'duration' value with a '${candidate.kind}' value for membership`);
|
|
133
146
|
return definite(toMilliseconds(operand.value, operand.unit) === toMilliseconds(candidate.value, candidate.unit));
|
|
147
|
+
case "complex":
|
|
148
|
+
if (candidate.kind !== "complex") return indeterminate("wrong-type", `cannot compare a 'complex' value with a '${candidate.kind}' value for membership`);
|
|
149
|
+
if (!unitsEqual(operand.unit, candidate.unit)) return indeterminate("wrong-type", "cannot compare complex values with incompatible units for membership");
|
|
150
|
+
return definite(operand.re === candidate.re && operand.im === candidate.im);
|
|
134
151
|
default: throw new Error("unreachable computed-value kind");
|
|
135
152
|
}
|
|
136
153
|
}
|
|
@@ -151,8 +168,15 @@ function applyNegate(operand) {
|
|
|
151
168
|
value: -operand.value,
|
|
152
169
|
unit: operand.unit
|
|
153
170
|
});
|
|
171
|
+
case "complex": return definite({
|
|
172
|
+
kind: "complex",
|
|
173
|
+
re: -operand.re,
|
|
174
|
+
im: -operand.im,
|
|
175
|
+
unit: operand.unit
|
|
176
|
+
});
|
|
154
177
|
case "text":
|
|
155
|
-
case "instant":
|
|
178
|
+
case "instant":
|
|
179
|
+
case "boolean": return indeterminate("wrong-type", `cannot negate a '${operand.kind}' value`);
|
|
156
180
|
default: throw new Error("unreachable computed-value kind");
|
|
157
181
|
}
|
|
158
182
|
}
|
|
@@ -222,6 +246,75 @@ function applyArithmeticOnDurations(op, left, right) {
|
|
|
222
246
|
default: throw new Error("unreachable arithmetic operator");
|
|
223
247
|
}
|
|
224
248
|
}
|
|
249
|
+
/** Every real number is a complex number with a zero imaginary part, so promoting one is exact and total -- unlike the temporal cross-kind combinations above, which each had to be enumerated because no such embedding exists between an `instant` and a `duration`. This is what lets a tree mix real and complex terms freely instead of forcing every real literal to be written as a complex one. */
|
|
250
|
+
function toComplexValue(value) {
|
|
251
|
+
if (value.kind === "complex") return value;
|
|
252
|
+
return {
|
|
253
|
+
kind: "complex",
|
|
254
|
+
re: value.value,
|
|
255
|
+
im: 0,
|
|
256
|
+
unit: value.unit
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
/** The complex product, as its own component-level helper rather than only a branch of the operator switch below, because `power`'s repeated multiplication needs exactly this and must not re-derive it. */
|
|
260
|
+
function multiplyComplexValues(left, right) {
|
|
261
|
+
return {
|
|
262
|
+
kind: "complex",
|
|
263
|
+
re: left.re * right.re - left.im * right.im,
|
|
264
|
+
im: left.re * right.im + left.im * right.re,
|
|
265
|
+
unit: combineUnitsForMultiply(left.unit, right.unit)
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
/** Complex arithmetic, over the one canonical rectangular representation the `complex` kind stores (see the "Complex values" section of README.md). `add`/`subtract` are component-wise and carry the same identical-units requirement real numbers already have. `power` is deliberately excluded from this operator set rather than handled and rejected here: its exponent must stay an un-promoted real number, so the dispatcher routes it to `applyComplexPower` first, and excluding it from the type is what makes the compiler enforce that routing instead of leaving a dead branch behind. */
|
|
269
|
+
function applyArithmeticOnComplex(op, left, right) {
|
|
270
|
+
switch (op) {
|
|
271
|
+
case "add":
|
|
272
|
+
if (!unitsEqual(left.unit, right.unit)) return indeterminate("wrong-type", "cannot add complex values with incompatible units");
|
|
273
|
+
return definite({
|
|
274
|
+
kind: "complex",
|
|
275
|
+
re: left.re + right.re,
|
|
276
|
+
im: left.im + right.im,
|
|
277
|
+
unit: left.unit
|
|
278
|
+
});
|
|
279
|
+
case "subtract":
|
|
280
|
+
if (!unitsEqual(left.unit, right.unit)) return indeterminate("wrong-type", "cannot subtract complex values with incompatible units");
|
|
281
|
+
return definite({
|
|
282
|
+
kind: "complex",
|
|
283
|
+
re: left.re - right.re,
|
|
284
|
+
im: left.im - right.im,
|
|
285
|
+
unit: left.unit
|
|
286
|
+
});
|
|
287
|
+
case "multiply": return definite(multiplyComplexValues(left, right));
|
|
288
|
+
case "divide": {
|
|
289
|
+
const divisorSquaredMagnitude = right.re * right.re + right.im * right.im;
|
|
290
|
+
if (divisorSquaredMagnitude === 0) return indeterminate("domain-error", "division by zero");
|
|
291
|
+
return definite({
|
|
292
|
+
kind: "complex",
|
|
293
|
+
re: (left.re * right.re + left.im * right.im) / divisorSquaredMagnitude,
|
|
294
|
+
im: (left.im * right.re - left.re * right.im) / divisorSquaredMagnitude,
|
|
295
|
+
unit: combineUnitsForDivide(left.unit, right.unit)
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
case "modulo": return indeterminate("domain-error", "'modulo' is undefined for complex values");
|
|
299
|
+
default: throw new Error("unreachable arithmetic operator");
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/** `power` with a complex operand on either side, defined for exactly one case: a real integer exponent, evaluated as the repeated multiplication that integer exponentiation *is* (see the "Complex values" section of README.md for why an arbitrary complex exponent stays out of scope). */
|
|
303
|
+
function applyComplexPower(base, exponent) {
|
|
304
|
+
if (exponent.kind !== "number" || !Number.isInteger(exponent.value)) return indeterminate("wrong-type", "'power' with a complex operand requires a real integer exponent");
|
|
305
|
+
if (!isDimensionless(base.unit) || !isDimensionless(exponent.unit)) return indeterminate("wrong-type", "'power' requires dimensionless operands");
|
|
306
|
+
const complexUnit = {
|
|
307
|
+
kind: "complex",
|
|
308
|
+
re: 1,
|
|
309
|
+
im: 0,
|
|
310
|
+
unit: {}
|
|
311
|
+
};
|
|
312
|
+
const complexBase = toComplexValue(base);
|
|
313
|
+
let repeatedProduct = complexUnit;
|
|
314
|
+
for (let applied = 0; applied < Math.abs(exponent.value); applied += 1) repeatedProduct = multiplyComplexValues(repeatedProduct, complexBase);
|
|
315
|
+
if (exponent.value >= 0) return definite(repeatedProduct);
|
|
316
|
+
return applyArithmeticOnComplex("divide", complexUnit, repeatedProduct);
|
|
317
|
+
}
|
|
225
318
|
/**
|
|
226
319
|
* Dispatches `arithmetic` by operand kind. The three cross-kind temporal combinations this design defines (`instant - instant`, `instant + duration`, `duration + instant`) are checked explicitly first, in that order, against the exact operator each requires; any other combination touching an `instant` is `wrong-type` (see "Temporal values" in README.md -- e.g. adding two instants, or subtracting a `duration` from an `instant`, are deliberately *not* defined). Same-kind `duration`/`duration` combinations are delegated to `applyArithmeticOnDurations`; a `duration` paired with anything other than an `instant` or another `duration` is `wrong-type`. Everything remaining requires two `number` operands.
|
|
227
320
|
*/
|
|
@@ -250,8 +343,12 @@ function applyArithmetic(op, left, right) {
|
|
|
250
343
|
if (left.kind === "instant" || right.kind === "instant") return indeterminate("wrong-type", `arithmetic operator '${op}' is not defined between a '${left.kind}' and a '${right.kind}' value`);
|
|
251
344
|
if (left.kind === "duration" && right.kind === "duration") return applyArithmeticOnDurations(op, left, right);
|
|
252
345
|
if (left.kind === "duration" || right.kind === "duration") return indeterminate("wrong-type", `arithmetic operator '${op}' is not defined between a '${left.kind}' and a '${right.kind}' value`);
|
|
253
|
-
if (left.kind !== "number") return indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${left.kind}' value`);
|
|
254
|
-
if (right.kind !== "number") return indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${right.kind}' value`);
|
|
346
|
+
if (left.kind !== "number" && left.kind !== "complex") return indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${left.kind}' value`);
|
|
347
|
+
if (right.kind !== "number" && right.kind !== "complex") return indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${right.kind}' value`);
|
|
348
|
+
if (left.kind === "complex" || right.kind === "complex") {
|
|
349
|
+
if (op === "power") return applyComplexPower(left, right);
|
|
350
|
+
return applyArithmeticOnComplex(op, toComplexValue(left), toComplexValue(right));
|
|
351
|
+
}
|
|
255
352
|
return applyArithmeticOnNumbers(op, left, right);
|
|
256
353
|
}
|
|
257
354
|
/** A defense-in-depth guard against a long acyclic `treeReference` chain exhausting the call stack -- distinct from, and layered on top of, the cycle detector below (`visitedTreeKeys`), which catches an actual repeat immediately and more precisely. */
|
|
@@ -366,7 +463,7 @@ async function evaluateValueInternal(node, context, resolvers, accumulator, func
|
|
|
366
463
|
const resolution = await resolvers.resolveValue(node.key, context);
|
|
367
464
|
if (!resolution.found) return indeterminate("not-found", `no value found for reference key ${JSON.stringify(node.key)}`);
|
|
368
465
|
if (node.unit !== void 0) {
|
|
369
|
-
if (resolution.value.kind !== "number") return indeterminate("wrong-type", "a unit was expected on a reference that resolved to a non-numeric value");
|
|
466
|
+
if (resolution.value.kind !== "number" && resolution.value.kind !== "complex") return indeterminate("wrong-type", "a unit was expected on a reference that resolved to a non-numeric value");
|
|
370
467
|
if (!unitsEqual(node.unit, resolution.value.unit)) return indeterminate("wrong-type", "the resolved value's unit does not match the reference's expected unit");
|
|
371
468
|
}
|
|
372
469
|
return definite(resolution.value);
|
|
@@ -396,6 +493,10 @@ async function evaluateValueInternal(node, context, resolvers, accumulator, func
|
|
|
396
493
|
kind: "text",
|
|
397
494
|
value: node.value
|
|
398
495
|
});
|
|
496
|
+
case "booleanLiteral": return definite({
|
|
497
|
+
kind: "boolean",
|
|
498
|
+
value: node.value
|
|
499
|
+
});
|
|
399
500
|
case "instantLiteral": return definite({
|
|
400
501
|
kind: "instant",
|
|
401
502
|
value: node.value
|
|
@@ -405,6 +506,14 @@ async function evaluateValueInternal(node, context, resolvers, accumulator, func
|
|
|
405
506
|
value: node.value,
|
|
406
507
|
unit: node.unit
|
|
407
508
|
});
|
|
509
|
+
case "complexLiteral":
|
|
510
|
+
if ("re" in node) return definite({
|
|
511
|
+
kind: "complex",
|
|
512
|
+
re: node.re,
|
|
513
|
+
im: node.im,
|
|
514
|
+
unit: node.unit
|
|
515
|
+
});
|
|
516
|
+
return definite(complexFromPolar(node.magnitude, node.phase, node.unit));
|
|
408
517
|
case "arithmetic": {
|
|
409
518
|
const [left, right] = await Promise.all([evaluateValueInternal(node.left, context, resolvers, accumulator, functions, visitedTreeKeys, treeReferenceDepth), evaluateValueInternal(node.right, context, resolvers, accumulator, functions, visitedTreeKeys, treeReferenceDepth)]);
|
|
410
519
|
if (left.status === "indeterminate") return left;
|
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_complex = require("./complex.cjs");
|
|
2
3
|
const require_computed_value = require("./computed-value.cjs");
|
|
3
4
|
const require_derived_aggregates = require("./derived-aggregates.cjs");
|
|
4
5
|
const require_derived_connectives = require("./derived-connectives.cjs");
|
|
@@ -14,9 +15,13 @@ exports.AndNodeSchema = require_tree.AndNodeSchema;
|
|
|
14
15
|
exports.AnyOfNodeSchema = require_tree.AnyOfNodeSchema;
|
|
15
16
|
exports.ArithmeticNodeSchema = require_tree.ArithmeticNodeSchema;
|
|
16
17
|
exports.ArithmeticOperatorSchema = require_tree.ArithmeticOperatorSchema;
|
|
18
|
+
exports.BooleanLiteralNodeSchema = require_tree.BooleanLiteralNodeSchema;
|
|
17
19
|
exports.CallNodeSchema = require_tree.CallNodeSchema;
|
|
18
20
|
exports.CompareNodeSchema = require_tree.CompareNodeSchema;
|
|
19
21
|
exports.ComparisonOperatorSchema = require_tree.ComparisonOperatorSchema;
|
|
22
|
+
exports.ComplexLiteralNodeSchema = require_tree.ComplexLiteralNodeSchema;
|
|
23
|
+
exports.ComplexPolarLiteralNodeSchema = require_tree.ComplexPolarLiteralNodeSchema;
|
|
24
|
+
exports.ComplexRectangularLiteralNodeSchema = require_tree.ComplexRectangularLiteralNodeSchema;
|
|
20
25
|
exports.ComputedValueSchema = require_computed_value.ComputedValueSchema;
|
|
21
26
|
exports.ConditionalCaseSchema = require_tree.ConditionalCaseSchema;
|
|
22
27
|
exports.ConditionalNodeSchema = require_tree.ConditionalNodeSchema;
|
|
@@ -52,6 +57,10 @@ exports.average = require_derived_aggregates.average;
|
|
|
52
57
|
exports.coalesce = require_derived_values.coalesce;
|
|
53
58
|
exports.combineUnitsForDivide = require_computed_value.combineUnitsForDivide;
|
|
54
59
|
exports.combineUnitsForMultiply = require_computed_value.combineUnitsForMultiply;
|
|
60
|
+
exports.complexFromPolar = require_complex.complexFromPolar;
|
|
61
|
+
exports.complexLiteralFromPolar = require_complex.complexLiteralFromPolar;
|
|
62
|
+
exports.complexMagnitude = require_complex.complexMagnitude;
|
|
63
|
+
exports.complexPhase = require_complex.complexPhase;
|
|
55
64
|
exports.count = require_derived_aggregates.count;
|
|
56
65
|
exports.createEvaluator = require_evaluator.createEvaluator;
|
|
57
66
|
exports.definite = require_evaluation.definite;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ComputedValue, ComputedValueSchema, DurationUnit, DurationUnitSchema, Unit, UnitSchema, combineUnitsForDivide, combineUnitsForMultiply, unitsEqual } from "./computed-value.cjs";
|
|
2
2
|
import { n as JsonValueSchema, t as JsonValue } from "./json-value-B1Hjjjri.cjs";
|
|
3
|
-
import { AccumulatorNode, AccumulatorNodeSchema, AllOfNode, AllOfNodeSchema, AndNode, AndNodeSchema, AnyOfNode, AnyOfNodeSchema, ArithmeticNode, ArithmeticNodeSchema, ArithmeticOperator, ArithmeticOperatorSchema, CallNode, CallNodeSchema, CompareNode, CompareNodeSchema, ComparisonOperator, ComparisonOperatorSchema, ConditionalCase, ConditionalCaseSchema, ConditionalNode, ConditionalNodeSchema, DelegateNode, DelegateNodeSchema, DurationLiteralNode, DurationLiteralNodeSchema, EveryNode, EveryNodeSchema, ExistsNode, ExistsNodeSchema, ExpressionNode, ExpressionNodeSchema, FoldCombiner, FoldCombinerSchema, FoldNode, FoldNodeSchema, HitPolicy, HitPolicySchema, InstantLiteralNode, InstantLiteralNodeSchema, LookupNode, LookupNodeSchema, MemberOfNode, MemberOfNodeSchema, MembershipOperator, MembershipOperatorSchema, NegateNode, NegateNodeSchema, NotNode, NotNodeSchema, NumberLiteralNode, NumberLiteralNodeSchema, OrNode, OrNodeSchema, PredicateNode, PredicateNodeSchema, ReferenceNode, ReferenceNodeSchema, SomeNode, SomeNodeSchema, TextCompareNode, TextCompareNodeSchema, TextComparisonOperator, TextComparisonOperatorSchema, TextLiteralNode, TextLiteralNodeSchema, TreeReferenceNode, TreeReferenceNodeSchema } from "./tree.cjs";
|
|
3
|
+
import { AccumulatorNode, AccumulatorNodeSchema, AllOfNode, AllOfNodeSchema, AndNode, AndNodeSchema, AnyOfNode, AnyOfNodeSchema, ArithmeticNode, ArithmeticNodeSchema, ArithmeticOperator, ArithmeticOperatorSchema, BooleanLiteralNode, BooleanLiteralNodeSchema, CallNode, CallNodeSchema, CompareNode, CompareNodeSchema, ComparisonOperator, ComparisonOperatorSchema, ComplexLiteralNode, ComplexLiteralNodeSchema, ComplexPolarLiteralNode, ComplexPolarLiteralNodeSchema, ComplexRectangularLiteralNode, ComplexRectangularLiteralNodeSchema, ConditionalCase, ConditionalCaseSchema, ConditionalNode, ConditionalNodeSchema, DelegateNode, DelegateNodeSchema, DurationLiteralNode, DurationLiteralNodeSchema, EveryNode, EveryNodeSchema, ExistsNode, ExistsNodeSchema, ExpressionNode, ExpressionNodeSchema, FoldCombiner, FoldCombinerSchema, FoldNode, FoldNodeSchema, HitPolicy, HitPolicySchema, InstantLiteralNode, InstantLiteralNodeSchema, LookupNode, LookupNodeSchema, MemberOfNode, MemberOfNodeSchema, MembershipOperator, MembershipOperatorSchema, NegateNode, NegateNodeSchema, NotNode, NotNodeSchema, NumberLiteralNode, NumberLiteralNodeSchema, OrNode, OrNodeSchema, PredicateNode, PredicateNodeSchema, ReferenceNode, ReferenceNodeSchema, SomeNode, SomeNodeSchema, TextCompareNode, TextCompareNodeSchema, TextComparisonOperator, TextComparisonOperatorSchema, TextLiteralNode, TextLiteralNodeSchema, TreeReferenceNode, TreeReferenceNodeSchema } from "./tree.cjs";
|
|
4
|
+
import { complexFromPolar, complexLiteralFromPolar, complexMagnitude, complexPhase } from "./complex.cjs";
|
|
4
5
|
import { average, count, presenceOf, sum } from "./derived-aggregates.cjs";
|
|
5
6
|
import { and, iff, implies, nand, none, nor, not, or, xor } from "./derived-connectives.cjs";
|
|
6
7
|
import { coalesce } from "./derived-values.cjs";
|
|
@@ -8,4 +9,4 @@ import { Evaluation, IndeterminateReason, IndeterminateReasonSchema, definite, f
|
|
|
8
9
|
import { FunctionRegistry, emptyFunctionRegistry } from "./functions.cjs";
|
|
9
10
|
import { EvaluationContext, Resolution, Resolvers, TreeResolution } from "./resolvers.cjs";
|
|
10
11
|
import { createEvaluator, evaluatePredicate, evaluateValue } from "./evaluator.cjs";
|
|
11
|
-
export { type AccumulatorNode, AccumulatorNodeSchema, type AllOfNode, AllOfNodeSchema, type AndNode, AndNodeSchema, type AnyOfNode, AnyOfNodeSchema, type ArithmeticNode, ArithmeticNodeSchema, type ArithmeticOperator, ArithmeticOperatorSchema, type CallNode, CallNodeSchema, type CompareNode, CompareNodeSchema, type ComparisonOperator, ComparisonOperatorSchema, type ComputedValue, ComputedValueSchema, type ConditionalCase, ConditionalCaseSchema, type ConditionalNode, ConditionalNodeSchema, type DelegateNode, DelegateNodeSchema, type DurationLiteralNode, DurationLiteralNodeSchema, type DurationUnit, DurationUnitSchema, type Evaluation, type EvaluationContext, type EveryNode, EveryNodeSchema, type ExistsNode, ExistsNodeSchema, type ExpressionNode, ExpressionNodeSchema, type FoldCombiner, FoldCombinerSchema, type FoldNode, FoldNodeSchema, type FunctionRegistry, type HitPolicy, HitPolicySchema, type IndeterminateReason, IndeterminateReasonSchema, type InstantLiteralNode, InstantLiteralNodeSchema, type JsonValue, JsonValueSchema, type LookupNode, LookupNodeSchema, type MemberOfNode, MemberOfNodeSchema, type MembershipOperator, MembershipOperatorSchema, type NegateNode, NegateNodeSchema, type NotNode, NotNodeSchema, type NumberLiteralNode, NumberLiteralNodeSchema, type OrNode, OrNodeSchema, type PredicateNode, PredicateNodeSchema, type ReferenceNode, ReferenceNodeSchema, type Resolution, type Resolvers, type SomeNode, SomeNodeSchema, type TextCompareNode, TextCompareNodeSchema, type TextComparisonOperator, TextComparisonOperatorSchema, type TextLiteralNode, TextLiteralNodeSchema, type TreeReferenceNode, TreeReferenceNodeSchema, type TreeResolution, type Unit, UnitSchema, and, average, coalesce, combineUnitsForDivide, combineUnitsForMultiply, count, createEvaluator, definite, emptyFunctionRegistry, evaluatePredicate, evaluateValue, firstIndeterminate, iff, implies, indeterminate, nand, none, nor, not, or, presenceOf, sum, unitsEqual, xor };
|
|
12
|
+
export { type AccumulatorNode, AccumulatorNodeSchema, type AllOfNode, AllOfNodeSchema, type AndNode, AndNodeSchema, type AnyOfNode, AnyOfNodeSchema, type ArithmeticNode, ArithmeticNodeSchema, type ArithmeticOperator, ArithmeticOperatorSchema, type BooleanLiteralNode, BooleanLiteralNodeSchema, type CallNode, CallNodeSchema, type CompareNode, CompareNodeSchema, type ComparisonOperator, ComparisonOperatorSchema, type ComplexLiteralNode, ComplexLiteralNodeSchema, type ComplexPolarLiteralNode, ComplexPolarLiteralNodeSchema, type ComplexRectangularLiteralNode, ComplexRectangularLiteralNodeSchema, type ComputedValue, ComputedValueSchema, type ConditionalCase, ConditionalCaseSchema, type ConditionalNode, ConditionalNodeSchema, type DelegateNode, DelegateNodeSchema, type DurationLiteralNode, DurationLiteralNodeSchema, type DurationUnit, DurationUnitSchema, type Evaluation, type EvaluationContext, type EveryNode, EveryNodeSchema, type ExistsNode, ExistsNodeSchema, type ExpressionNode, ExpressionNodeSchema, type FoldCombiner, FoldCombinerSchema, type FoldNode, FoldNodeSchema, type FunctionRegistry, type HitPolicy, HitPolicySchema, type IndeterminateReason, IndeterminateReasonSchema, type InstantLiteralNode, InstantLiteralNodeSchema, type JsonValue, JsonValueSchema, type LookupNode, LookupNodeSchema, type MemberOfNode, MemberOfNodeSchema, type MembershipOperator, MembershipOperatorSchema, type NegateNode, NegateNodeSchema, type NotNode, NotNodeSchema, type NumberLiteralNode, NumberLiteralNodeSchema, type OrNode, OrNodeSchema, type PredicateNode, PredicateNodeSchema, type ReferenceNode, ReferenceNodeSchema, type Resolution, type Resolvers, type SomeNode, SomeNodeSchema, type TextCompareNode, TextCompareNodeSchema, type TextComparisonOperator, TextComparisonOperatorSchema, type TextLiteralNode, TextLiteralNodeSchema, type TreeReferenceNode, TreeReferenceNodeSchema, type TreeResolution, type Unit, UnitSchema, and, average, coalesce, combineUnitsForDivide, combineUnitsForMultiply, complexFromPolar, complexLiteralFromPolar, complexMagnitude, complexPhase, count, createEvaluator, definite, emptyFunctionRegistry, evaluatePredicate, evaluateValue, firstIndeterminate, iff, implies, indeterminate, nand, none, nor, not, or, presenceOf, sum, unitsEqual, xor };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ComputedValue, ComputedValueSchema, DurationUnit, DurationUnitSchema, Unit, UnitSchema, combineUnitsForDivide, combineUnitsForMultiply, unitsEqual } from "./computed-value.js";
|
|
2
2
|
import { n as JsonValueSchema, t as JsonValue } from "./json-value-B1Hjjjri.js";
|
|
3
|
-
import { AccumulatorNode, AccumulatorNodeSchema, AllOfNode, AllOfNodeSchema, AndNode, AndNodeSchema, AnyOfNode, AnyOfNodeSchema, ArithmeticNode, ArithmeticNodeSchema, ArithmeticOperator, ArithmeticOperatorSchema, CallNode, CallNodeSchema, CompareNode, CompareNodeSchema, ComparisonOperator, ComparisonOperatorSchema, ConditionalCase, ConditionalCaseSchema, ConditionalNode, ConditionalNodeSchema, DelegateNode, DelegateNodeSchema, DurationLiteralNode, DurationLiteralNodeSchema, EveryNode, EveryNodeSchema, ExistsNode, ExistsNodeSchema, ExpressionNode, ExpressionNodeSchema, FoldCombiner, FoldCombinerSchema, FoldNode, FoldNodeSchema, HitPolicy, HitPolicySchema, InstantLiteralNode, InstantLiteralNodeSchema, LookupNode, LookupNodeSchema, MemberOfNode, MemberOfNodeSchema, MembershipOperator, MembershipOperatorSchema, NegateNode, NegateNodeSchema, NotNode, NotNodeSchema, NumberLiteralNode, NumberLiteralNodeSchema, OrNode, OrNodeSchema, PredicateNode, PredicateNodeSchema, ReferenceNode, ReferenceNodeSchema, SomeNode, SomeNodeSchema, TextCompareNode, TextCompareNodeSchema, TextComparisonOperator, TextComparisonOperatorSchema, TextLiteralNode, TextLiteralNodeSchema, TreeReferenceNode, TreeReferenceNodeSchema } from "./tree.js";
|
|
3
|
+
import { AccumulatorNode, AccumulatorNodeSchema, AllOfNode, AllOfNodeSchema, AndNode, AndNodeSchema, AnyOfNode, AnyOfNodeSchema, ArithmeticNode, ArithmeticNodeSchema, ArithmeticOperator, ArithmeticOperatorSchema, BooleanLiteralNode, BooleanLiteralNodeSchema, CallNode, CallNodeSchema, CompareNode, CompareNodeSchema, ComparisonOperator, ComparisonOperatorSchema, ComplexLiteralNode, ComplexLiteralNodeSchema, ComplexPolarLiteralNode, ComplexPolarLiteralNodeSchema, ComplexRectangularLiteralNode, ComplexRectangularLiteralNodeSchema, ConditionalCase, ConditionalCaseSchema, ConditionalNode, ConditionalNodeSchema, DelegateNode, DelegateNodeSchema, DurationLiteralNode, DurationLiteralNodeSchema, EveryNode, EveryNodeSchema, ExistsNode, ExistsNodeSchema, ExpressionNode, ExpressionNodeSchema, FoldCombiner, FoldCombinerSchema, FoldNode, FoldNodeSchema, HitPolicy, HitPolicySchema, InstantLiteralNode, InstantLiteralNodeSchema, LookupNode, LookupNodeSchema, MemberOfNode, MemberOfNodeSchema, MembershipOperator, MembershipOperatorSchema, NegateNode, NegateNodeSchema, NotNode, NotNodeSchema, NumberLiteralNode, NumberLiteralNodeSchema, OrNode, OrNodeSchema, PredicateNode, PredicateNodeSchema, ReferenceNode, ReferenceNodeSchema, SomeNode, SomeNodeSchema, TextCompareNode, TextCompareNodeSchema, TextComparisonOperator, TextComparisonOperatorSchema, TextLiteralNode, TextLiteralNodeSchema, TreeReferenceNode, TreeReferenceNodeSchema } from "./tree.js";
|
|
4
|
+
import { complexFromPolar, complexLiteralFromPolar, complexMagnitude, complexPhase } from "./complex.js";
|
|
4
5
|
import { average, count, presenceOf, sum } from "./derived-aggregates.js";
|
|
5
6
|
import { and, iff, implies, nand, none, nor, not, or, xor } from "./derived-connectives.js";
|
|
6
7
|
import { coalesce } from "./derived-values.js";
|
|
@@ -8,4 +9,4 @@ import { Evaluation, IndeterminateReason, IndeterminateReasonSchema, definite, f
|
|
|
8
9
|
import { FunctionRegistry, emptyFunctionRegistry } from "./functions.js";
|
|
9
10
|
import { EvaluationContext, Resolution, Resolvers, TreeResolution } from "./resolvers.js";
|
|
10
11
|
import { createEvaluator, evaluatePredicate, evaluateValue } from "./evaluator.js";
|
|
11
|
-
export { type AccumulatorNode, AccumulatorNodeSchema, type AllOfNode, AllOfNodeSchema, type AndNode, AndNodeSchema, type AnyOfNode, AnyOfNodeSchema, type ArithmeticNode, ArithmeticNodeSchema, type ArithmeticOperator, ArithmeticOperatorSchema, type CallNode, CallNodeSchema, type CompareNode, CompareNodeSchema, type ComparisonOperator, ComparisonOperatorSchema, type ComputedValue, ComputedValueSchema, type ConditionalCase, ConditionalCaseSchema, type ConditionalNode, ConditionalNodeSchema, type DelegateNode, DelegateNodeSchema, type DurationLiteralNode, DurationLiteralNodeSchema, type DurationUnit, DurationUnitSchema, type Evaluation, type EvaluationContext, type EveryNode, EveryNodeSchema, type ExistsNode, ExistsNodeSchema, type ExpressionNode, ExpressionNodeSchema, type FoldCombiner, FoldCombinerSchema, type FoldNode, FoldNodeSchema, type FunctionRegistry, type HitPolicy, HitPolicySchema, type IndeterminateReason, IndeterminateReasonSchema, type InstantLiteralNode, InstantLiteralNodeSchema, type JsonValue, JsonValueSchema, type LookupNode, LookupNodeSchema, type MemberOfNode, MemberOfNodeSchema, type MembershipOperator, MembershipOperatorSchema, type NegateNode, NegateNodeSchema, type NotNode, NotNodeSchema, type NumberLiteralNode, NumberLiteralNodeSchema, type OrNode, OrNodeSchema, type PredicateNode, PredicateNodeSchema, type ReferenceNode, ReferenceNodeSchema, type Resolution, type Resolvers, type SomeNode, SomeNodeSchema, type TextCompareNode, TextCompareNodeSchema, type TextComparisonOperator, TextComparisonOperatorSchema, type TextLiteralNode, TextLiteralNodeSchema, type TreeReferenceNode, TreeReferenceNodeSchema, type TreeResolution, type Unit, UnitSchema, and, average, coalesce, combineUnitsForDivide, combineUnitsForMultiply, count, createEvaluator, definite, emptyFunctionRegistry, evaluatePredicate, evaluateValue, firstIndeterminate, iff, implies, indeterminate, nand, none, nor, not, or, presenceOf, sum, unitsEqual, xor };
|
|
12
|
+
export { type AccumulatorNode, AccumulatorNodeSchema, type AllOfNode, AllOfNodeSchema, type AndNode, AndNodeSchema, type AnyOfNode, AnyOfNodeSchema, type ArithmeticNode, ArithmeticNodeSchema, type ArithmeticOperator, ArithmeticOperatorSchema, type BooleanLiteralNode, BooleanLiteralNodeSchema, type CallNode, CallNodeSchema, type CompareNode, CompareNodeSchema, type ComparisonOperator, ComparisonOperatorSchema, type ComplexLiteralNode, ComplexLiteralNodeSchema, type ComplexPolarLiteralNode, ComplexPolarLiteralNodeSchema, type ComplexRectangularLiteralNode, ComplexRectangularLiteralNodeSchema, type ComputedValue, ComputedValueSchema, type ConditionalCase, ConditionalCaseSchema, type ConditionalNode, ConditionalNodeSchema, type DelegateNode, DelegateNodeSchema, type DurationLiteralNode, DurationLiteralNodeSchema, type DurationUnit, DurationUnitSchema, type Evaluation, type EvaluationContext, type EveryNode, EveryNodeSchema, type ExistsNode, ExistsNodeSchema, type ExpressionNode, ExpressionNodeSchema, type FoldCombiner, FoldCombinerSchema, type FoldNode, FoldNodeSchema, type FunctionRegistry, type HitPolicy, HitPolicySchema, type IndeterminateReason, IndeterminateReasonSchema, type InstantLiteralNode, InstantLiteralNodeSchema, type JsonValue, JsonValueSchema, type LookupNode, LookupNodeSchema, type MemberOfNode, MemberOfNodeSchema, type MembershipOperator, MembershipOperatorSchema, type NegateNode, NegateNodeSchema, type NotNode, NotNodeSchema, type NumberLiteralNode, NumberLiteralNodeSchema, type OrNode, OrNodeSchema, type PredicateNode, PredicateNodeSchema, type ReferenceNode, ReferenceNodeSchema, type Resolution, type Resolvers, type SomeNode, SomeNodeSchema, type TextCompareNode, TextCompareNodeSchema, type TextComparisonOperator, TextComparisonOperatorSchema, type TextLiteralNode, TextLiteralNodeSchema, type TreeReferenceNode, TreeReferenceNodeSchema, type TreeResolution, type Unit, UnitSchema, and, average, coalesce, combineUnitsForDivide, combineUnitsForMultiply, complexFromPolar, complexLiteralFromPolar, complexMagnitude, complexPhase, count, createEvaluator, definite, emptyFunctionRegistry, evaluatePredicate, evaluateValue, firstIndeterminate, iff, implies, indeterminate, nand, none, nor, not, or, presenceOf, sum, unitsEqual, xor };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { complexFromPolar, complexLiteralFromPolar, complexMagnitude, complexPhase } from "./complex.js";
|
|
1
2
|
import { ComputedValueSchema, DurationUnitSchema, UnitSchema, combineUnitsForDivide, combineUnitsForMultiply, unitsEqual } from "./computed-value.js";
|
|
2
3
|
import { average, count, presenceOf, sum } from "./derived-aggregates.js";
|
|
3
4
|
import { and, iff, implies, nand, none, nor, not, or, xor } from "./derived-connectives.js";
|
|
@@ -5,6 +6,6 @@ import { coalesce } from "./derived-values.js";
|
|
|
5
6
|
import { IndeterminateReasonSchema, definite, firstIndeterminate, indeterminate } from "./evaluation.js";
|
|
6
7
|
import { emptyFunctionRegistry } from "./functions.js";
|
|
7
8
|
import { JsonValueSchema } from "./json-value.js";
|
|
8
|
-
import { AccumulatorNodeSchema, AllOfNodeSchema, AndNodeSchema, AnyOfNodeSchema, ArithmeticNodeSchema, ArithmeticOperatorSchema, CallNodeSchema, CompareNodeSchema, ComparisonOperatorSchema, ConditionalCaseSchema, ConditionalNodeSchema, DelegateNodeSchema, DurationLiteralNodeSchema, EveryNodeSchema, ExistsNodeSchema, ExpressionNodeSchema, FoldCombinerSchema, FoldNodeSchema, HitPolicySchema, InstantLiteralNodeSchema, LookupNodeSchema, MemberOfNodeSchema, MembershipOperatorSchema, NegateNodeSchema, NotNodeSchema, NumberLiteralNodeSchema, OrNodeSchema, PredicateNodeSchema, ReferenceNodeSchema, SomeNodeSchema, TextCompareNodeSchema, TextComparisonOperatorSchema, TextLiteralNodeSchema, TreeReferenceNodeSchema } from "./tree.js";
|
|
9
|
+
import { AccumulatorNodeSchema, AllOfNodeSchema, AndNodeSchema, AnyOfNodeSchema, ArithmeticNodeSchema, ArithmeticOperatorSchema, BooleanLiteralNodeSchema, CallNodeSchema, CompareNodeSchema, ComparisonOperatorSchema, ComplexLiteralNodeSchema, ComplexPolarLiteralNodeSchema, ComplexRectangularLiteralNodeSchema, ConditionalCaseSchema, ConditionalNodeSchema, DelegateNodeSchema, DurationLiteralNodeSchema, EveryNodeSchema, ExistsNodeSchema, ExpressionNodeSchema, FoldCombinerSchema, FoldNodeSchema, HitPolicySchema, InstantLiteralNodeSchema, LookupNodeSchema, MemberOfNodeSchema, MembershipOperatorSchema, NegateNodeSchema, NotNodeSchema, NumberLiteralNodeSchema, OrNodeSchema, PredicateNodeSchema, ReferenceNodeSchema, SomeNodeSchema, TextCompareNodeSchema, TextComparisonOperatorSchema, TextLiteralNodeSchema, TreeReferenceNodeSchema } from "./tree.js";
|
|
9
10
|
import { createEvaluator, evaluatePredicate, evaluateValue } from "./evaluator.js";
|
|
10
|
-
export { AccumulatorNodeSchema, AllOfNodeSchema, AndNodeSchema, AnyOfNodeSchema, ArithmeticNodeSchema, ArithmeticOperatorSchema, CallNodeSchema, CompareNodeSchema, ComparisonOperatorSchema, ComputedValueSchema, ConditionalCaseSchema, ConditionalNodeSchema, DelegateNodeSchema, DurationLiteralNodeSchema, DurationUnitSchema, EveryNodeSchema, ExistsNodeSchema, ExpressionNodeSchema, FoldCombinerSchema, FoldNodeSchema, HitPolicySchema, IndeterminateReasonSchema, InstantLiteralNodeSchema, JsonValueSchema, LookupNodeSchema, MemberOfNodeSchema, MembershipOperatorSchema, NegateNodeSchema, NotNodeSchema, NumberLiteralNodeSchema, OrNodeSchema, PredicateNodeSchema, ReferenceNodeSchema, SomeNodeSchema, TextCompareNodeSchema, TextComparisonOperatorSchema, TextLiteralNodeSchema, TreeReferenceNodeSchema, UnitSchema, and, average, coalesce, combineUnitsForDivide, combineUnitsForMultiply, count, createEvaluator, definite, emptyFunctionRegistry, evaluatePredicate, evaluateValue, firstIndeterminate, iff, implies, indeterminate, nand, none, nor, not, or, presenceOf, sum, unitsEqual, xor };
|
|
11
|
+
export { AccumulatorNodeSchema, AllOfNodeSchema, AndNodeSchema, AnyOfNodeSchema, ArithmeticNodeSchema, ArithmeticOperatorSchema, BooleanLiteralNodeSchema, CallNodeSchema, CompareNodeSchema, ComparisonOperatorSchema, ComplexLiteralNodeSchema, ComplexPolarLiteralNodeSchema, ComplexRectangularLiteralNodeSchema, ComputedValueSchema, ConditionalCaseSchema, ConditionalNodeSchema, DelegateNodeSchema, DurationLiteralNodeSchema, DurationUnitSchema, EveryNodeSchema, ExistsNodeSchema, ExpressionNodeSchema, FoldCombinerSchema, FoldNodeSchema, HitPolicySchema, IndeterminateReasonSchema, InstantLiteralNodeSchema, JsonValueSchema, LookupNodeSchema, MemberOfNodeSchema, MembershipOperatorSchema, NegateNodeSchema, NotNodeSchema, NumberLiteralNodeSchema, OrNodeSchema, PredicateNodeSchema, ReferenceNodeSchema, SomeNodeSchema, TextCompareNodeSchema, TextComparisonOperatorSchema, TextLiteralNodeSchema, TreeReferenceNodeSchema, UnitSchema, and, average, coalesce, combineUnitsForDivide, combineUnitsForMultiply, complexFromPolar, complexLiteralFromPolar, complexMagnitude, complexPhase, count, createEvaluator, definite, emptyFunctionRegistry, evaluatePredicate, evaluateValue, firstIndeterminate, iff, implies, indeterminate, nand, none, nor, not, or, presenceOf, sum, unitsEqual, xor };
|
package/dist/tree.cjs
CHANGED
|
@@ -147,6 +147,10 @@ const TextLiteralNodeSchema = zod.z.object({
|
|
|
147
147
|
kind: zod.z.literal("textLiteral"),
|
|
148
148
|
value: zod.z.string()
|
|
149
149
|
});
|
|
150
|
+
const BooleanLiteralNodeSchema = zod.z.object({
|
|
151
|
+
kind: zod.z.literal("booleanLiteral"),
|
|
152
|
+
value: zod.z.boolean()
|
|
153
|
+
});
|
|
150
154
|
const InstantLiteralNodeSchema = zod.z.object({
|
|
151
155
|
kind: zod.z.literal("instantLiteral"),
|
|
152
156
|
/** ISO-8601 timestamp. */
|
|
@@ -157,6 +161,25 @@ const DurationLiteralNodeSchema = zod.z.object({
|
|
|
157
161
|
value: zod.z.number(),
|
|
158
162
|
unit: require_computed_value.DurationUnitSchema
|
|
159
163
|
});
|
|
164
|
+
/**
|
|
165
|
+
* The wire-format literal accepts either of the two authoring forms real-world complex data arrives in, discriminated structurally rather than by a `form` tag: rectangular (`re`/`im`), matching the `complex` computed-value kind's own single canonical representation exactly (see the "Complex values" section of README.md), or polar (`magnitude`/`phase`, in radians), for the domains that reason that way natively and would otherwise have to hand-compute the conversion before ever constructing the tree. `strictObject` on both members is what makes the two forms mutually exclusive on the wire -- a plain `object` would silently strip an unrecognised field rather than reject a payload that mixes both.
|
|
166
|
+
*
|
|
167
|
+
* The evaluator normalises whichever form was authored into the single rectangular `ComputedValue` immediately on evaluation (see the `complexLiteral` case in evaluator.ts), so nothing downstream of that -- arithmetic, `compare`, `memberOf`, `negate` -- ever sees a polar value; they keep consuming the one canonical `complex` shape unchanged.
|
|
168
|
+
*/
|
|
169
|
+
const ComplexRectangularLiteralNodeSchema = zod.z.strictObject({
|
|
170
|
+
kind: zod.z.literal("complexLiteral"),
|
|
171
|
+
re: zod.z.number(),
|
|
172
|
+
im: zod.z.number(),
|
|
173
|
+
unit: require_computed_value.UnitSchema.optional()
|
|
174
|
+
});
|
|
175
|
+
const ComplexPolarLiteralNodeSchema = zod.z.strictObject({
|
|
176
|
+
kind: zod.z.literal("complexLiteral"),
|
|
177
|
+
magnitude: zod.z.number(),
|
|
178
|
+
phase: zod.z.number(),
|
|
179
|
+
unit: require_computed_value.UnitSchema.optional()
|
|
180
|
+
});
|
|
181
|
+
/** Both literal forms share the one literal `kind: "complexLiteral"` value, which is exactly why this is a plain `z.union` rather than a `z.discriminatedUnion` -- `z.discriminatedUnion` requires a unique discriminant literal per member and throws ("Duplicate discriminator value") the moment two members share one. `ExpressionNodeSchema` below folds this plain union in alongside its own discriminated union of every other kind, rather than trying to host it as a normal member. */
|
|
182
|
+
const ComplexLiteralNodeSchema = zod.z.union([ComplexRectangularLiteralNodeSchema, ComplexPolarLiteralNodeSchema]);
|
|
160
183
|
const ReferenceNodeSchema = zod.z.object({
|
|
161
184
|
kind: zod.z.literal("reference"),
|
|
162
185
|
key: require_json_value.JsonValueSchema,
|
|
@@ -248,9 +271,11 @@ const DelegateNodeSchema = zod.z.object({
|
|
|
248
271
|
system: zod.z.string(),
|
|
249
272
|
payload: require_json_value.JsonValueSchema
|
|
250
273
|
});
|
|
251
|
-
|
|
274
|
+
/** Every expression node kind except `complexLiteral`, which cannot be a member of this same discriminated union alongside its own two forms (see `ComplexLiteralNodeSchema` above) -- folded back in below via a plain `z.union` rather than `z.discriminatedUnion`. */
|
|
275
|
+
const CoreExpressionNodeSchema = zod.z.discriminatedUnion("kind", [
|
|
252
276
|
NumberLiteralNodeSchema,
|
|
253
277
|
TextLiteralNodeSchema,
|
|
278
|
+
BooleanLiteralNodeSchema,
|
|
254
279
|
InstantLiteralNodeSchema,
|
|
255
280
|
DurationLiteralNodeSchema,
|
|
256
281
|
ReferenceNodeSchema,
|
|
@@ -264,6 +289,7 @@ const ExpressionNodeSchema = zod.z.discriminatedUnion("kind", [
|
|
|
264
289
|
DelegateNodeSchema,
|
|
265
290
|
TreeReferenceNodeSchema
|
|
266
291
|
]);
|
|
292
|
+
const ExpressionNodeSchema = zod.z.union([CoreExpressionNodeSchema, ComplexLiteralNodeSchema]);
|
|
267
293
|
//#endregion
|
|
268
294
|
exports.AccumulatorNodeSchema = AccumulatorNodeSchema;
|
|
269
295
|
exports.AllOfNodeSchema = AllOfNodeSchema;
|
|
@@ -271,9 +297,13 @@ exports.AndNodeSchema = AndNodeSchema;
|
|
|
271
297
|
exports.AnyOfNodeSchema = AnyOfNodeSchema;
|
|
272
298
|
exports.ArithmeticNodeSchema = ArithmeticNodeSchema;
|
|
273
299
|
exports.ArithmeticOperatorSchema = ArithmeticOperatorSchema;
|
|
300
|
+
exports.BooleanLiteralNodeSchema = BooleanLiteralNodeSchema;
|
|
274
301
|
exports.CallNodeSchema = CallNodeSchema;
|
|
275
302
|
exports.CompareNodeSchema = CompareNodeSchema;
|
|
276
303
|
exports.ComparisonOperatorSchema = ComparisonOperatorSchema;
|
|
304
|
+
exports.ComplexLiteralNodeSchema = ComplexLiteralNodeSchema;
|
|
305
|
+
exports.ComplexPolarLiteralNodeSchema = ComplexPolarLiteralNodeSchema;
|
|
306
|
+
exports.ComplexRectangularLiteralNodeSchema = ComplexRectangularLiteralNodeSchema;
|
|
277
307
|
exports.ConditionalCaseSchema = ConditionalCaseSchema;
|
|
278
308
|
exports.ConditionalNodeSchema = ConditionalNodeSchema;
|
|
279
309
|
exports.DelegateNodeSchema = DelegateNodeSchema;
|