trilean 1.6.0 → 1.6.2
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/complex.d.cts +1 -1
- package/dist/complex.d.ts +1 -1
- package/dist/computed-value-CbRF0Izc.d.cts +51 -0
- package/dist/computed-value-CbRF0Izc.d.ts +51 -0
- package/dist/computed-value.cjs +1 -1
- package/dist/computed-value.d.cts +1 -50
- package/dist/computed-value.d.ts +1 -50
- package/dist/computed-value.js +1 -1
- package/dist/evaluator-budget.cjs +24 -0
- package/dist/evaluator-budget.d.cts +21 -0
- package/dist/evaluator-budget.d.ts +21 -0
- package/dist/evaluator-budget.js +20 -0
- package/dist/evaluator-collection.cjs +33 -0
- package/dist/evaluator-collection.d.cts +23 -0
- package/dist/evaluator-collection.d.ts +23 -0
- package/dist/evaluator-collection.js +31 -0
- package/dist/evaluator-factory.cjs +19 -0
- package/dist/evaluator-factory.d.cts +22 -0
- package/dist/evaluator-factory.d.ts +22 -0
- package/dist/evaluator-factory.js +16 -0
- package/dist/evaluator-indeterminacy-helpers.cjs +180 -0
- package/dist/evaluator-indeterminacy-helpers.d.cts +57 -0
- package/dist/evaluator-indeterminacy-helpers.d.ts +57 -0
- package/dist/evaluator-indeterminacy-helpers.js +158 -0
- package/dist/evaluator-operations.cjs +385 -0
- package/dist/evaluator-operations.d.cts +27 -0
- package/dist/evaluator-operations.d.ts +27 -0
- package/dist/evaluator-operations.js +376 -0
- package/dist/evaluator-test-helpers.cjs +40 -0
- package/dist/evaluator-test-helpers.d.cts +13 -0
- package/dist/evaluator-test-helpers.d.ts +13 -0
- package/dist/evaluator-test-helpers.js +35 -0
- package/dist/evaluator.cjs +25 -442
- package/dist/evaluator.d.cts +6 -14
- package/dist/evaluator.d.ts +6 -14
- package/dist/evaluator.js +12 -428
- package/dist/functions.d.cts +2 -2
- package/dist/functions.d.ts +2 -2
- package/dist/index.cjs +5 -5
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/magic-string.es-BWtG0AyA.js +1017 -0
- package/dist/magic-string.es-BvKy4sJx.cjs +1017 -0
- package/dist/resolvers.d.cts +1 -1
- package/dist/resolvers.d.ts +1 -1
- package/dist/test.DNmyFkvJ-Dasq3mQO.cjs +13578 -0
- package/dist/test.DNmyFkvJ-SCEEf0VK.js +13573 -0
- package/package.json +3 -3
- package/schemas/trilean.schema.json +1 -1
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_computed_value = require("./computed-value.cjs");
|
|
3
|
+
const require_evaluation = require("./evaluation.cjs");
|
|
4
|
+
let trilean_regex = require("trilean-regex");
|
|
5
|
+
//#region src/evaluator-operations.ts
|
|
6
|
+
/** The three-valued AND table: `false` is absorbing regardless of the other operand's indeterminacy; otherwise both-definite folds to a boolean AND; otherwise indeterminate, with the tie-break rule (declared operand order, left before right) applied via `firstIndeterminate`. */
|
|
7
|
+
function combineAnd(left, right) {
|
|
8
|
+
if (left.status === "definite" && !left.value) return require_evaluation.definite(false);
|
|
9
|
+
if (right.status === "definite" && !right.value) return require_evaluation.definite(false);
|
|
10
|
+
const reason = require_evaluation.firstIndeterminate(left, right);
|
|
11
|
+
if (reason !== void 0) return {
|
|
12
|
+
status: "indeterminate",
|
|
13
|
+
reason
|
|
14
|
+
};
|
|
15
|
+
return require_evaluation.definite(true);
|
|
16
|
+
}
|
|
17
|
+
/** The three-valued OR table: mirror image of `combineAnd`, with `true` absorbing. */
|
|
18
|
+
function combineOr(left, right) {
|
|
19
|
+
if (left.status === "definite" && left.value) return require_evaluation.definite(true);
|
|
20
|
+
if (right.status === "definite" && right.value) return require_evaluation.definite(true);
|
|
21
|
+
const reason = require_evaluation.firstIndeterminate(left, right);
|
|
22
|
+
if (reason !== void 0) return {
|
|
23
|
+
status: "indeterminate",
|
|
24
|
+
reason
|
|
25
|
+
};
|
|
26
|
+
return require_evaluation.definite(false);
|
|
27
|
+
}
|
|
28
|
+
function applyComparisonOperator(op, left, right) {
|
|
29
|
+
switch (op) {
|
|
30
|
+
case "gt": return left > right;
|
|
31
|
+
case "gte": return left >= right;
|
|
32
|
+
case "lt": return left < right;
|
|
33
|
+
case "lte": return left <= right;
|
|
34
|
+
case "eq": return left === right;
|
|
35
|
+
case "neq": return left !== right;
|
|
36
|
+
default: throw new Error("unreachable comparison operator");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const millisecondsPerDurationUnit = {
|
|
40
|
+
ms: 1,
|
|
41
|
+
s: 1e3,
|
|
42
|
+
min: 6e4,
|
|
43
|
+
h: 36e5,
|
|
44
|
+
d: 864e5
|
|
45
|
+
};
|
|
46
|
+
/** Normalises a `duration`'s magnitude to milliseconds, the common base unit for combining or comparing two `duration`s (or an `instant` and a `duration`) of potentially different `DurationUnit`s -- shared by `compareValues`'s own `duration` branch below and by the temporal arithmetic further down. */
|
|
47
|
+
function toMilliseconds(value, unit) {
|
|
48
|
+
return value * millisecondsPerDurationUnit[unit];
|
|
49
|
+
}
|
|
50
|
+
/** `Date.parse` reports an unparseable timestamp as `NaN`, and every downstream use of that `NaN` then fails silently rather than loudly: every comparison against it is `false` (so `neq` between two unparseable instants would come back definitely `true`), a subtraction yields a `NaN`-magnitude duration, and `new Date(NaN).toISOString()` throws a `RangeError` outright -- a thrown exception for a data-quality problem, which this design never does (see `Evaluation<T>`'s own doc comment in evaluation.ts). An `instant` whose string will not parse is a value the operation cannot use, so every caller below reports it as `wrong-type`, exactly like any other unusable operand. */
|
|
51
|
+
function toEpochMilliseconds(value) {
|
|
52
|
+
const epochMilliseconds = Date.parse(value);
|
|
53
|
+
return Number.isNaN(epochMilliseconds) ? void 0 : epochMilliseconds;
|
|
54
|
+
}
|
|
55
|
+
function unparseableInstant(value) {
|
|
56
|
+
return require_evaluation.indeterminate("wrong-type", `'${value}' is not a parseable ISO-8601 timestamp`);
|
|
57
|
+
}
|
|
58
|
+
/** The representable timestamp range is finite, so a valid instant shifted by a large enough duration lands outside it, where `new Date(...).toISOString()` throws a `RangeError`. Checking the shifted date against the platform's own notion of a valid time value keeps that inside the three-outcome model as a `domain-error` -- an operation pushed outside its valid domain, the same category as division by zero -- and avoids hardcoding the range bound. */
|
|
59
|
+
function instantFromEpochMilliseconds(epochMilliseconds) {
|
|
60
|
+
const shifted = new Date(epochMilliseconds);
|
|
61
|
+
if (Number.isNaN(shifted.getTime())) return require_evaluation.indeterminate("domain-error", "the resulting instant falls outside the representable timestamp range");
|
|
62
|
+
return require_evaluation.definite({
|
|
63
|
+
kind: "instant",
|
|
64
|
+
value: shifted.toISOString()
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** `compare`'s two operands: valid kinds are `number` (matching units required), `instant` (ordered by parsed epoch millisecond), or `duration` (ordered by magnitude normalised to milliseconds) -- `text` is never valid here (see `textCompare`), and a kind mismatch between the two operands is `wrong-type`. Narrowing `right` against a literal `left.kind` case (rather than asserting it) is what lets both sides stay properly typed with no `as`. */
|
|
68
|
+
function compareValues(op, left, right) {
|
|
69
|
+
switch (left.kind) {
|
|
70
|
+
case "number":
|
|
71
|
+
if (right.kind !== "number") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'number' value with a '${right.kind}' value`);
|
|
72
|
+
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot compare numbers with incompatible units");
|
|
73
|
+
return require_evaluation.definite(applyComparisonOperator(op, left.value, right.value));
|
|
74
|
+
case "text": return require_evaluation.indeterminate("wrong-type", "text values are not comparable via 'compare'; use 'textCompare'");
|
|
75
|
+
case "boolean":
|
|
76
|
+
if (right.kind !== "boolean") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'boolean' value with a '${right.kind}' value`);
|
|
77
|
+
if (op !== "eq" && op !== "neq") return require_evaluation.indeterminate("wrong-type", `booleans have no natural ordering; '${op}' is only defined for 'eq'/'neq'`);
|
|
78
|
+
return require_evaluation.definite(op === "eq" ? left.value === right.value : left.value !== right.value);
|
|
79
|
+
case "instant": {
|
|
80
|
+
if (right.kind !== "instant") return require_evaluation.indeterminate("wrong-type", `cannot compare an 'instant' value with a '${right.kind}' value`);
|
|
81
|
+
const leftEpoch = toEpochMilliseconds(left.value);
|
|
82
|
+
if (leftEpoch === void 0) return unparseableInstant(left.value);
|
|
83
|
+
const rightEpoch = toEpochMilliseconds(right.value);
|
|
84
|
+
if (rightEpoch === void 0) return unparseableInstant(right.value);
|
|
85
|
+
return require_evaluation.definite(applyComparisonOperator(op, leftEpoch, rightEpoch));
|
|
86
|
+
}
|
|
87
|
+
case "duration":
|
|
88
|
+
if (right.kind !== "duration") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'duration' value with a '${right.kind}' value`);
|
|
89
|
+
return require_evaluation.definite(applyComparisonOperator(op, toMilliseconds(left.value, left.unit), toMilliseconds(right.value, right.unit)));
|
|
90
|
+
case "complex":
|
|
91
|
+
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`);
|
|
92
|
+
if (right.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'complex' value with a '${right.kind}' value`);
|
|
93
|
+
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot compare complex values with incompatible units");
|
|
94
|
+
return require_evaluation.definite(op === "eq" ? left.re === right.re && left.im === right.im : left.re !== right.re || left.im !== right.im);
|
|
95
|
+
default: throw new Error("unreachable computed-value kind");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** `textCompare`'s two operands must both resolve to the `text` computed-value kind -- any other kind, on either operand, is `wrong-type` (see the `textCompare` section of README.md). `equals`/`notEquals` are exact string equality; `matches`/`notMatches` interpret `right`'s text as an ECMAScript regular expression tested against `left`'s text; `portableMatches`/`portableNotMatches` interpret it as a pattern in `trilean-regex`'s own restricted grammar instead, matched by that package's finite-automaton reference matcher rather than the host's native `RegExp` engine -- see the `textCompare` section of README.md for when to reach for the portable pair instead of the plain one. An invalid pattern is `wrong-type` rather than a thrown exception either way -- every data-quality problem stays inside the `Evaluation` result, per `Evaluation<T>`'s own doc comment in evaluation.ts. */
|
|
99
|
+
function compareText(op, left, right) {
|
|
100
|
+
if (left.kind !== "text") return require_evaluation.indeterminate("wrong-type", `'textCompare' requires 'text' operands; the left operand is '${left.kind}'`);
|
|
101
|
+
if (right.kind !== "text") return require_evaluation.indeterminate("wrong-type", `'textCompare' requires 'text' operands; the right operand is '${right.kind}'`);
|
|
102
|
+
switch (op) {
|
|
103
|
+
case "equals": return require_evaluation.definite(left.value === right.value);
|
|
104
|
+
case "notEquals": return require_evaluation.definite(left.value !== right.value);
|
|
105
|
+
case "matches":
|
|
106
|
+
case "notMatches": {
|
|
107
|
+
let pattern;
|
|
108
|
+
try {
|
|
109
|
+
pattern = new RegExp(right.value);
|
|
110
|
+
} catch {
|
|
111
|
+
return require_evaluation.indeterminate("wrong-type", `'${right.value}' is not a valid regular expression pattern`);
|
|
112
|
+
}
|
|
113
|
+
const isMatch = pattern.test(left.value);
|
|
114
|
+
return require_evaluation.definite(op === "matches" ? isMatch : !isMatch);
|
|
115
|
+
}
|
|
116
|
+
case "portableMatches":
|
|
117
|
+
case "portableNotMatches": {
|
|
118
|
+
let compiled;
|
|
119
|
+
try {
|
|
120
|
+
compiled = (0, trilean_regex.compilePattern)(right.value);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
const reason = error instanceof trilean_regex.RegexParseError ? error.message : String(error);
|
|
123
|
+
return require_evaluation.indeterminate("wrong-type", `'${right.value}' is not a valid trilean-regex pattern: ${reason}`);
|
|
124
|
+
}
|
|
125
|
+
const isMatch = compiled.test(left.value);
|
|
126
|
+
return require_evaluation.definite(op === "portableMatches" ? isMatch : !isMatch);
|
|
127
|
+
}
|
|
128
|
+
default: throw new Error("unreachable text comparison operator");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** `memberOf`'s own value-equality test between two computed values -- kind-agnostic across `number`/`text`/`instant`/`duration` (see the `memberOf` section of README.md, and its "Derived aggregates" note that this equality is kind-agnostic across every computed-value kind), unlike `compare`'s own `eq`, which rejects a `text` operand outright and directs callers to `textCompare` instead. A kind mismatch, or a `number` pair with an incompatible unit, is `wrong-type` -- never simply "not equal". Narrowing `candidate` against a literal `operand.kind` case (rather than asserting it) is the same technique `compareValues` uses above. */
|
|
132
|
+
function computeMembershipMatch(operand, candidate) {
|
|
133
|
+
switch (operand.kind) {
|
|
134
|
+
case "number":
|
|
135
|
+
if (candidate.kind !== "number") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'number' value with a '${candidate.kind}' value for membership`);
|
|
136
|
+
if (!require_computed_value.unitsEqual(operand.unit, candidate.unit)) return require_evaluation.indeterminate("wrong-type", "cannot compare numbers with incompatible units for membership");
|
|
137
|
+
return require_evaluation.definite(operand.value === candidate.value);
|
|
138
|
+
case "text":
|
|
139
|
+
if (candidate.kind !== "text") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'text' value with a '${candidate.kind}' value for membership`);
|
|
140
|
+
return require_evaluation.definite(operand.value === candidate.value);
|
|
141
|
+
case "boolean":
|
|
142
|
+
if (candidate.kind !== "boolean") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'boolean' value with a '${candidate.kind}' value for membership`);
|
|
143
|
+
return require_evaluation.definite(operand.value === candidate.value);
|
|
144
|
+
case "instant": {
|
|
145
|
+
if (candidate.kind !== "instant") return require_evaluation.indeterminate("wrong-type", `cannot compare an 'instant' value with a '${candidate.kind}' value for membership`);
|
|
146
|
+
const operandEpoch = toEpochMilliseconds(operand.value);
|
|
147
|
+
if (operandEpoch === void 0) return unparseableInstant(operand.value);
|
|
148
|
+
const candidateEpoch = toEpochMilliseconds(candidate.value);
|
|
149
|
+
if (candidateEpoch === void 0) return unparseableInstant(candidate.value);
|
|
150
|
+
return require_evaluation.definite(operandEpoch === candidateEpoch);
|
|
151
|
+
}
|
|
152
|
+
case "duration":
|
|
153
|
+
if (candidate.kind !== "duration") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'duration' value with a '${candidate.kind}' value for membership`);
|
|
154
|
+
return require_evaluation.definite(toMilliseconds(operand.value, operand.unit) === toMilliseconds(candidate.value, candidate.unit));
|
|
155
|
+
case "complex":
|
|
156
|
+
if (candidate.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'complex' value with a '${candidate.kind}' value for membership`);
|
|
157
|
+
if (!require_computed_value.unitsEqual(operand.unit, candidate.unit)) return require_evaluation.indeterminate("wrong-type", "cannot compare complex values with incompatible units for membership");
|
|
158
|
+
return require_evaluation.definite(operand.re === candidate.re && operand.im === candidate.im);
|
|
159
|
+
default: throw new Error("unreachable computed-value kind");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/** `power`/`modulo` have no defined unit-combination rule in this design (unlike `add`/`subtract`'s "identical units" requirement or `multiply`/`divide`'s dimensional-exponent combination) -- scoping them to dimensionless operands avoids inventing an unspecified unit-scaling semantics for a fractional or runtime-determined exponent. */
|
|
163
|
+
function isDimensionless(unit) {
|
|
164
|
+
return require_computed_value.unitsEqual(unit, void 0);
|
|
165
|
+
}
|
|
166
|
+
/** `negate` is never sugar for "zero minus the value" (see the `arithmetic`/`negate` section of README.md) -- a `duration`'s magnitude is negated directly, in its own original unit, with no subtraction or millisecond normalisation involved. */
|
|
167
|
+
function applyNegate(operand) {
|
|
168
|
+
switch (operand.kind) {
|
|
169
|
+
case "number": return require_evaluation.definite({
|
|
170
|
+
kind: "number",
|
|
171
|
+
value: -operand.value,
|
|
172
|
+
unit: operand.unit
|
|
173
|
+
});
|
|
174
|
+
case "duration": return require_evaluation.definite({
|
|
175
|
+
kind: "duration",
|
|
176
|
+
value: -operand.value,
|
|
177
|
+
unit: operand.unit
|
|
178
|
+
});
|
|
179
|
+
case "complex": return require_evaluation.definite({
|
|
180
|
+
kind: "complex",
|
|
181
|
+
re: -operand.re,
|
|
182
|
+
im: -operand.im,
|
|
183
|
+
unit: operand.unit
|
|
184
|
+
});
|
|
185
|
+
case "text":
|
|
186
|
+
case "instant":
|
|
187
|
+
case "boolean": return require_evaluation.indeterminate("wrong-type", `cannot negate a '${operand.kind}' value`);
|
|
188
|
+
default: throw new Error("unreachable computed-value kind");
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function applyArithmeticOnNumbers(op, left, right) {
|
|
192
|
+
switch (op) {
|
|
193
|
+
case "add":
|
|
194
|
+
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot add numbers with incompatible units");
|
|
195
|
+
return require_evaluation.definite({
|
|
196
|
+
kind: "number",
|
|
197
|
+
value: left.value + right.value,
|
|
198
|
+
unit: left.unit
|
|
199
|
+
});
|
|
200
|
+
case "subtract":
|
|
201
|
+
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot subtract numbers with incompatible units");
|
|
202
|
+
return require_evaluation.definite({
|
|
203
|
+
kind: "number",
|
|
204
|
+
value: left.value - right.value,
|
|
205
|
+
unit: left.unit
|
|
206
|
+
});
|
|
207
|
+
case "multiply": return require_evaluation.definite({
|
|
208
|
+
kind: "number",
|
|
209
|
+
value: left.value * right.value,
|
|
210
|
+
unit: require_computed_value.combineUnitsForMultiply(left.unit, right.unit)
|
|
211
|
+
});
|
|
212
|
+
case "divide":
|
|
213
|
+
if (right.value === 0) return require_evaluation.indeterminate("domain-error", "division by zero");
|
|
214
|
+
return require_evaluation.definite({
|
|
215
|
+
kind: "number",
|
|
216
|
+
value: left.value / right.value,
|
|
217
|
+
unit: require_computed_value.combineUnitsForDivide(left.unit, right.unit)
|
|
218
|
+
});
|
|
219
|
+
case "power":
|
|
220
|
+
if (!isDimensionless(left.unit) || !isDimensionless(right.unit)) return require_evaluation.indeterminate("wrong-type", "'power' requires dimensionless operands");
|
|
221
|
+
if (left.value < 0 && !Number.isInteger(right.value)) return require_evaluation.indeterminate("domain-error", "a negative base raised to a non-integer power is not a real number");
|
|
222
|
+
if (left.value === 0 && right.value < 0) return require_evaluation.indeterminate("domain-error", "zero raised to a negative power is a division by zero");
|
|
223
|
+
return require_evaluation.definite({
|
|
224
|
+
kind: "number",
|
|
225
|
+
value: left.value ** right.value
|
|
226
|
+
});
|
|
227
|
+
case "modulo":
|
|
228
|
+
if (!isDimensionless(left.unit) || !isDimensionless(right.unit)) return require_evaluation.indeterminate("wrong-type", "'modulo' requires dimensionless operands");
|
|
229
|
+
if (right.value === 0) return require_evaluation.indeterminate("domain-error", "modulo by zero");
|
|
230
|
+
return require_evaluation.definite({
|
|
231
|
+
kind: "number",
|
|
232
|
+
value: left.value % right.value
|
|
233
|
+
});
|
|
234
|
+
default: throw new Error("unreachable arithmetic operator");
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/** The only same-kind, non-`number` arithmetic this design defines: two `duration`s combine by normalising both to milliseconds first (see `toMilliseconds`), reporting the result in milliseconds -- `multiply`/`divide`/`power`/`modulo` have no representable result unit for a `duration` squared or a dimensionless ratio, so they are `wrong-type` rather than invented. */
|
|
238
|
+
function applyArithmeticOnDurations(op, left, right) {
|
|
239
|
+
switch (op) {
|
|
240
|
+
case "add": return require_evaluation.definite({
|
|
241
|
+
kind: "duration",
|
|
242
|
+
value: toMilliseconds(left.value, left.unit) + toMilliseconds(right.value, right.unit),
|
|
243
|
+
unit: "ms"
|
|
244
|
+
});
|
|
245
|
+
case "subtract": return require_evaluation.definite({
|
|
246
|
+
kind: "duration",
|
|
247
|
+
value: toMilliseconds(left.value, left.unit) - toMilliseconds(right.value, right.unit),
|
|
248
|
+
unit: "ms"
|
|
249
|
+
});
|
|
250
|
+
case "multiply":
|
|
251
|
+
case "divide":
|
|
252
|
+
case "power":
|
|
253
|
+
case "modulo": return require_evaluation.indeterminate("wrong-type", `arithmetic operator '${op}' is not defined between two 'duration' values`);
|
|
254
|
+
default: throw new Error("unreachable arithmetic operator");
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/** 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. */
|
|
258
|
+
function toComplexValue(value) {
|
|
259
|
+
if (value.kind === "complex") return value;
|
|
260
|
+
return {
|
|
261
|
+
kind: "complex",
|
|
262
|
+
re: value.value,
|
|
263
|
+
im: 0,
|
|
264
|
+
unit: value.unit
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
/** 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. */
|
|
268
|
+
function multiplyComplexValues(left, right) {
|
|
269
|
+
return {
|
|
270
|
+
kind: "complex",
|
|
271
|
+
re: left.re * right.re - left.im * right.im,
|
|
272
|
+
im: left.re * right.im + left.im * right.re,
|
|
273
|
+
unit: require_computed_value.combineUnitsForMultiply(left.unit, right.unit)
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
/** 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. */
|
|
277
|
+
function applyArithmeticOnComplex(op, left, right) {
|
|
278
|
+
switch (op) {
|
|
279
|
+
case "add":
|
|
280
|
+
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot add complex values with incompatible units");
|
|
281
|
+
return require_evaluation.definite({
|
|
282
|
+
kind: "complex",
|
|
283
|
+
re: left.re + right.re,
|
|
284
|
+
im: left.im + right.im,
|
|
285
|
+
unit: left.unit
|
|
286
|
+
});
|
|
287
|
+
case "subtract":
|
|
288
|
+
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot subtract complex values with incompatible units");
|
|
289
|
+
return require_evaluation.definite({
|
|
290
|
+
kind: "complex",
|
|
291
|
+
re: left.re - right.re,
|
|
292
|
+
im: left.im - right.im,
|
|
293
|
+
unit: left.unit
|
|
294
|
+
});
|
|
295
|
+
case "multiply": return require_evaluation.definite(multiplyComplexValues(left, right));
|
|
296
|
+
case "divide": {
|
|
297
|
+
const divisorSquaredMagnitude = right.re * right.re + right.im * right.im;
|
|
298
|
+
if (divisorSquaredMagnitude === 0) return require_evaluation.indeterminate("domain-error", "division by zero");
|
|
299
|
+
return require_evaluation.definite({
|
|
300
|
+
kind: "complex",
|
|
301
|
+
re: (left.re * right.re + left.im * right.im) / divisorSquaredMagnitude,
|
|
302
|
+
im: (left.im * right.re - left.re * right.im) / divisorSquaredMagnitude,
|
|
303
|
+
unit: require_computed_value.combineUnitsForDivide(left.unit, right.unit)
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
case "modulo": return require_evaluation.indeterminate("domain-error", "'modulo' is undefined for complex values");
|
|
307
|
+
default: throw new Error("unreachable arithmetic operator");
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
/** `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). */
|
|
311
|
+
function applyComplexPower(base, exponent) {
|
|
312
|
+
if (exponent.kind !== "number" || !Number.isInteger(exponent.value)) return require_evaluation.indeterminate("wrong-type", "'power' with a complex operand requires a real integer exponent");
|
|
313
|
+
if (!isDimensionless(base.unit) || !isDimensionless(exponent.unit)) return require_evaluation.indeterminate("wrong-type", "'power' requires dimensionless operands");
|
|
314
|
+
const complexUnit = {
|
|
315
|
+
kind: "complex",
|
|
316
|
+
re: 1,
|
|
317
|
+
im: 0,
|
|
318
|
+
unit: {}
|
|
319
|
+
};
|
|
320
|
+
const complexBase = toComplexValue(base);
|
|
321
|
+
let repeatedProduct = complexUnit;
|
|
322
|
+
for (let applied = 0; applied < Math.abs(exponent.value); applied += 1) repeatedProduct = multiplyComplexValues(repeatedProduct, complexBase);
|
|
323
|
+
if (exponent.value >= 0) return require_evaluation.definite(repeatedProduct);
|
|
324
|
+
return applyArithmeticOnComplex("divide", complexUnit, repeatedProduct);
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* 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.
|
|
328
|
+
*/
|
|
329
|
+
function applyArithmetic(op, left, right) {
|
|
330
|
+
if (left.kind === "instant" && right.kind === "instant" && op === "subtract") {
|
|
331
|
+
const leftEpoch = toEpochMilliseconds(left.value);
|
|
332
|
+
if (leftEpoch === void 0) return unparseableInstant(left.value);
|
|
333
|
+
const rightEpoch = toEpochMilliseconds(right.value);
|
|
334
|
+
if (rightEpoch === void 0) return unparseableInstant(right.value);
|
|
335
|
+
return require_evaluation.definite({
|
|
336
|
+
kind: "duration",
|
|
337
|
+
value: leftEpoch - rightEpoch,
|
|
338
|
+
unit: "ms"
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
if (left.kind === "instant" && right.kind === "duration" && op === "add") {
|
|
342
|
+
const leftEpoch = toEpochMilliseconds(left.value);
|
|
343
|
+
if (leftEpoch === void 0) return unparseableInstant(left.value);
|
|
344
|
+
return instantFromEpochMilliseconds(leftEpoch + toMilliseconds(right.value, right.unit));
|
|
345
|
+
}
|
|
346
|
+
if (left.kind === "duration" && right.kind === "instant" && op === "add") {
|
|
347
|
+
const rightEpoch = toEpochMilliseconds(right.value);
|
|
348
|
+
if (rightEpoch === void 0) return unparseableInstant(right.value);
|
|
349
|
+
return instantFromEpochMilliseconds(rightEpoch + toMilliseconds(left.value, left.unit));
|
|
350
|
+
}
|
|
351
|
+
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`);
|
|
352
|
+
if (left.kind === "duration" && right.kind === "duration") return applyArithmeticOnDurations(op, left, right);
|
|
353
|
+
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`);
|
|
354
|
+
if (left.kind !== "number" && left.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${left.kind}' value`);
|
|
355
|
+
if (right.kind !== "number" && right.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${right.kind}' value`);
|
|
356
|
+
if (left.kind === "complex" || right.kind === "complex") {
|
|
357
|
+
if (op === "power") return applyComplexPower(left, right);
|
|
358
|
+
return applyArithmeticOnComplex(op, toComplexValue(left), toComplexValue(right));
|
|
359
|
+
}
|
|
360
|
+
return applyArithmeticOnNumbers(op, left, right);
|
|
361
|
+
}
|
|
362
|
+
/** `reference`'s optional expected-unit check: when `expectedUnit` is declared (from `node.unit`), the resolved value must be numeric (`number` or `complex`) and its own unit must match. Returns the `wrong-type` `Evaluation` to return immediately if the check fails, or `undefined` if the reference may resolve as-is. */
|
|
363
|
+
function checkReferenceUnit(expectedUnit, resolvedValue) {
|
|
364
|
+
if (expectedUnit === void 0) return void 0;
|
|
365
|
+
if (resolvedValue.kind !== "number" && resolvedValue.kind !== "complex") return require_evaluation.indeterminate("wrong-type", "a unit was expected on a reference that resolved to a non-numeric value");
|
|
366
|
+
if (!require_computed_value.unitsEqual(expectedUnit, resolvedValue.unit)) return require_evaluation.indeterminate("wrong-type", "the resolved value's unit does not match the reference's expected unit");
|
|
367
|
+
}
|
|
368
|
+
/** `call`'s own function-registry dispatch, given the `fn` name off the tree and its already-evaluated `args`. `node.fn` comes off the serialised tree, which this design treats as data that may have been authored anywhere (see README.md's opening section), while `FunctionRegistry` is an ordinary object with `Object.prototype` on its chain. A bare index lookup would therefore resolve `toString`, `valueOf`, `constructor` and friends as though a consumer had registered them; only the registry's own keys count as registered function names. */
|
|
369
|
+
function invokeRegisteredFunction(functions, fnName, args) {
|
|
370
|
+
const fn = Object.hasOwn(functions, fnName) ? functions[fnName] : void 0;
|
|
371
|
+
if (fn === void 0) return require_evaluation.indeterminate("wrong-type", `no function registered under the name '${fnName}'`);
|
|
372
|
+
const outcome = fn(args);
|
|
373
|
+
if ("domainError" in outcome) return require_evaluation.indeterminate("domain-error", outcome.domainError);
|
|
374
|
+
return require_evaluation.definite(outcome);
|
|
375
|
+
}
|
|
376
|
+
//#endregion
|
|
377
|
+
exports.applyArithmetic = applyArithmetic;
|
|
378
|
+
exports.applyNegate = applyNegate;
|
|
379
|
+
exports.checkReferenceUnit = checkReferenceUnit;
|
|
380
|
+
exports.combineAnd = combineAnd;
|
|
381
|
+
exports.combineOr = combineOr;
|
|
382
|
+
exports.compareText = compareText;
|
|
383
|
+
exports.compareValues = compareValues;
|
|
384
|
+
exports.computeMembershipMatch = computeMembershipMatch;
|
|
385
|
+
exports.invokeRegisteredFunction = invokeRegisteredFunction;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { a as Unit, t as ComputedValue } from "./computed-value-CbRF0Izc.cjs";
|
|
2
|
+
import { ArithmeticOperator, ComparisonOperator, TextComparisonOperator } from "./tree.cjs";
|
|
3
|
+
import { Evaluation } from "./evaluation.cjs";
|
|
4
|
+
import { FunctionRegistry } from "./functions.cjs";
|
|
5
|
+
//#region src/evaluator-operations.d.ts
|
|
6
|
+
/** The three-valued AND table: `false` is absorbing regardless of the other operand's indeterminacy; otherwise both-definite folds to a boolean AND; otherwise indeterminate, with the tie-break rule (declared operand order, left before right) applied via `firstIndeterminate`. */
|
|
7
|
+
declare function combineAnd(left: Evaluation<boolean>, right: Evaluation<boolean>): Evaluation<boolean>;
|
|
8
|
+
/** The three-valued OR table: mirror image of `combineAnd`, with `true` absorbing. */
|
|
9
|
+
declare function combineOr(left: Evaluation<boolean>, right: Evaluation<boolean>): Evaluation<boolean>;
|
|
10
|
+
/** `compare`'s two operands: valid kinds are `number` (matching units required), `instant` (ordered by parsed epoch millisecond), or `duration` (ordered by magnitude normalised to milliseconds) -- `text` is never valid here (see `textCompare`), and a kind mismatch between the two operands is `wrong-type`. Narrowing `right` against a literal `left.kind` case (rather than asserting it) is what lets both sides stay properly typed with no `as`. */
|
|
11
|
+
declare function compareValues(op: ComparisonOperator, left: ComputedValue, right: ComputedValue): Evaluation<boolean>;
|
|
12
|
+
/** `textCompare`'s two operands must both resolve to the `text` computed-value kind -- any other kind, on either operand, is `wrong-type` (see the `textCompare` section of README.md). `equals`/`notEquals` are exact string equality; `matches`/`notMatches` interpret `right`'s text as an ECMAScript regular expression tested against `left`'s text; `portableMatches`/`portableNotMatches` interpret it as a pattern in `trilean-regex`'s own restricted grammar instead, matched by that package's finite-automaton reference matcher rather than the host's native `RegExp` engine -- see the `textCompare` section of README.md for when to reach for the portable pair instead of the plain one. An invalid pattern is `wrong-type` rather than a thrown exception either way -- every data-quality problem stays inside the `Evaluation` result, per `Evaluation<T>`'s own doc comment in evaluation.ts. */
|
|
13
|
+
declare function compareText(op: TextComparisonOperator, left: ComputedValue, right: ComputedValue): Evaluation<boolean>;
|
|
14
|
+
/** `memberOf`'s own value-equality test between two computed values -- kind-agnostic across `number`/`text`/`instant`/`duration` (see the `memberOf` section of README.md, and its "Derived aggregates" note that this equality is kind-agnostic across every computed-value kind), unlike `compare`'s own `eq`, which rejects a `text` operand outright and directs callers to `textCompare` instead. A kind mismatch, or a `number` pair with an incompatible unit, is `wrong-type` -- never simply "not equal". Narrowing `candidate` against a literal `operand.kind` case (rather than asserting it) is the same technique `compareValues` uses above. */
|
|
15
|
+
declare function computeMembershipMatch(operand: ComputedValue, candidate: ComputedValue): Evaluation<boolean>;
|
|
16
|
+
/** `negate` is never sugar for "zero minus the value" (see the `arithmetic`/`negate` section of README.md) -- a `duration`'s magnitude is negated directly, in its own original unit, with no subtraction or millisecond normalisation involved. */
|
|
17
|
+
declare function applyNegate(operand: ComputedValue): Evaluation<ComputedValue>;
|
|
18
|
+
/**
|
|
19
|
+
* 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.
|
|
20
|
+
*/
|
|
21
|
+
declare function applyArithmetic(op: ArithmeticOperator, left: ComputedValue, right: ComputedValue): Evaluation<ComputedValue>;
|
|
22
|
+
/** `reference`'s optional expected-unit check: when `expectedUnit` is declared (from `node.unit`), the resolved value must be numeric (`number` or `complex`) and its own unit must match. Returns the `wrong-type` `Evaluation` to return immediately if the check fails, or `undefined` if the reference may resolve as-is. */
|
|
23
|
+
declare function checkReferenceUnit(expectedUnit: Unit | undefined, resolvedValue: ComputedValue): Evaluation<never> | undefined;
|
|
24
|
+
/** `call`'s own function-registry dispatch, given the `fn` name off the tree and its already-evaluated `args`. `node.fn` comes off the serialised tree, which this design treats as data that may have been authored anywhere (see README.md's opening section), while `FunctionRegistry` is an ordinary object with `Object.prototype` on its chain. A bare index lookup would therefore resolve `toString`, `valueOf`, `constructor` and friends as though a consumer had registered them; only the registry's own keys count as registered function names. */
|
|
25
|
+
declare function invokeRegisteredFunction(functions: Readonly<FunctionRegistry>, fnName: string, args: readonly ComputedValue[]): Evaluation<ComputedValue>;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { applyArithmetic, applyNegate, checkReferenceUnit, combineAnd, combineOr, compareText, compareValues, computeMembershipMatch, invokeRegisteredFunction };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { a as Unit, t as ComputedValue } from "./computed-value-CbRF0Izc.js";
|
|
2
|
+
import { ArithmeticOperator, ComparisonOperator, TextComparisonOperator } from "./tree.js";
|
|
3
|
+
import { Evaluation } from "./evaluation.js";
|
|
4
|
+
import { FunctionRegistry } from "./functions.js";
|
|
5
|
+
//#region src/evaluator-operations.d.ts
|
|
6
|
+
/** The three-valued AND table: `false` is absorbing regardless of the other operand's indeterminacy; otherwise both-definite folds to a boolean AND; otherwise indeterminate, with the tie-break rule (declared operand order, left before right) applied via `firstIndeterminate`. */
|
|
7
|
+
declare function combineAnd(left: Evaluation<boolean>, right: Evaluation<boolean>): Evaluation<boolean>;
|
|
8
|
+
/** The three-valued OR table: mirror image of `combineAnd`, with `true` absorbing. */
|
|
9
|
+
declare function combineOr(left: Evaluation<boolean>, right: Evaluation<boolean>): Evaluation<boolean>;
|
|
10
|
+
/** `compare`'s two operands: valid kinds are `number` (matching units required), `instant` (ordered by parsed epoch millisecond), or `duration` (ordered by magnitude normalised to milliseconds) -- `text` is never valid here (see `textCompare`), and a kind mismatch between the two operands is `wrong-type`. Narrowing `right` against a literal `left.kind` case (rather than asserting it) is what lets both sides stay properly typed with no `as`. */
|
|
11
|
+
declare function compareValues(op: ComparisonOperator, left: ComputedValue, right: ComputedValue): Evaluation<boolean>;
|
|
12
|
+
/** `textCompare`'s two operands must both resolve to the `text` computed-value kind -- any other kind, on either operand, is `wrong-type` (see the `textCompare` section of README.md). `equals`/`notEquals` are exact string equality; `matches`/`notMatches` interpret `right`'s text as an ECMAScript regular expression tested against `left`'s text; `portableMatches`/`portableNotMatches` interpret it as a pattern in `trilean-regex`'s own restricted grammar instead, matched by that package's finite-automaton reference matcher rather than the host's native `RegExp` engine -- see the `textCompare` section of README.md for when to reach for the portable pair instead of the plain one. An invalid pattern is `wrong-type` rather than a thrown exception either way -- every data-quality problem stays inside the `Evaluation` result, per `Evaluation<T>`'s own doc comment in evaluation.ts. */
|
|
13
|
+
declare function compareText(op: TextComparisonOperator, left: ComputedValue, right: ComputedValue): Evaluation<boolean>;
|
|
14
|
+
/** `memberOf`'s own value-equality test between two computed values -- kind-agnostic across `number`/`text`/`instant`/`duration` (see the `memberOf` section of README.md, and its "Derived aggregates" note that this equality is kind-agnostic across every computed-value kind), unlike `compare`'s own `eq`, which rejects a `text` operand outright and directs callers to `textCompare` instead. A kind mismatch, or a `number` pair with an incompatible unit, is `wrong-type` -- never simply "not equal". Narrowing `candidate` against a literal `operand.kind` case (rather than asserting it) is the same technique `compareValues` uses above. */
|
|
15
|
+
declare function computeMembershipMatch(operand: ComputedValue, candidate: ComputedValue): Evaluation<boolean>;
|
|
16
|
+
/** `negate` is never sugar for "zero minus the value" (see the `arithmetic`/`negate` section of README.md) -- a `duration`'s magnitude is negated directly, in its own original unit, with no subtraction or millisecond normalisation involved. */
|
|
17
|
+
declare function applyNegate(operand: ComputedValue): Evaluation<ComputedValue>;
|
|
18
|
+
/**
|
|
19
|
+
* 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.
|
|
20
|
+
*/
|
|
21
|
+
declare function applyArithmetic(op: ArithmeticOperator, left: ComputedValue, right: ComputedValue): Evaluation<ComputedValue>;
|
|
22
|
+
/** `reference`'s optional expected-unit check: when `expectedUnit` is declared (from `node.unit`), the resolved value must be numeric (`number` or `complex`) and its own unit must match. Returns the `wrong-type` `Evaluation` to return immediately if the check fails, or `undefined` if the reference may resolve as-is. */
|
|
23
|
+
declare function checkReferenceUnit(expectedUnit: Unit | undefined, resolvedValue: ComputedValue): Evaluation<never> | undefined;
|
|
24
|
+
/** `call`'s own function-registry dispatch, given the `fn` name off the tree and its already-evaluated `args`. `node.fn` comes off the serialised tree, which this design treats as data that may have been authored anywhere (see README.md's opening section), while `FunctionRegistry` is an ordinary object with `Object.prototype` on its chain. A bare index lookup would therefore resolve `toString`, `valueOf`, `constructor` and friends as though a consumer had registered them; only the registry's own keys count as registered function names. */
|
|
25
|
+
declare function invokeRegisteredFunction(functions: Readonly<FunctionRegistry>, fnName: string, args: readonly ComputedValue[]): Evaluation<ComputedValue>;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { applyArithmetic, applyNegate, checkReferenceUnit, combineAnd, combineOr, compareText, compareValues, computeMembershipMatch, invokeRegisteredFunction };
|