trilean 1.13.12 → 2.0.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 +1 -1
- package/dist/evaluator-operations.js +24 -4
- package/dist-cli/trilean.mjs +25 -5
- package/package.json +10 -20
- package/schemas/trilean.schema.json +1 -1
- package/dist/builtin-functions.cjs +0 -242
- package/dist/builtin-functions.d.cts +0 -13
- package/dist/complex-division.cjs +0 -47
- package/dist/complex-division.d.cts +0 -23
- package/dist/complex.cjs +0 -35
- package/dist/complex.d.cts +0 -23
- package/dist/computed-value.cjs +0 -80
- package/dist/computed-value.d.cts +0 -48
- package/dist/derived-aggregates.cjs +0 -73
- package/dist/derived-aggregates.d.cts +0 -9
- package/dist/derived-connectives.cjs +0 -39
- package/dist/derived-connectives.d.cts +0 -14
- package/dist/derived-patterns.cjs +0 -93
- package/dist/derived-patterns.d.cts +0 -4
- package/dist/derived-values.cjs +0 -23
- package/dist/derived-values.d.cts +0 -7
- package/dist/dist-ed78Tuwe.js +0 -1612
- package/dist/dist-ySX7cb5d.cjs +0 -1612
- package/dist/duration-order.cjs +0 -83
- package/dist/duration-order.d.cts +0 -21
- package/dist/evaluate-document.cjs +0 -92
- package/dist/evaluate-document.d.cts +0 -42
- package/dist/evaluation.cjs +0 -47
- package/dist/evaluation.d.cts +0 -29
- package/dist/evaluator-budget.cjs +0 -37
- package/dist/evaluator-budget.d.cts +0 -22
- package/dist/evaluator-collection.cjs +0 -33
- package/dist/evaluator-collection.d.cts +0 -21
- package/dist/evaluator-compiled.cjs +0 -310
- package/dist/evaluator-compiled.d.cts +0 -18
- package/dist/evaluator-effect.cjs +0 -57
- package/dist/evaluator-effect.d.cts +0 -40
- package/dist/evaluator-factory.cjs +0 -31
- package/dist/evaluator-factory.d.cts +0 -34
- package/dist/evaluator-literals.cjs +0 -42
- package/dist/evaluator-literals.d.cts +0 -8
- package/dist/evaluator-operations.cjs +0 -440
- package/dist/evaluator-operations.d.cts +0 -27
- package/dist/evaluator-test-helpers.cjs +0 -15030
- package/dist/evaluator-test-helpers.d.cts +0 -10
- package/dist/evaluator-test-helpers.d.ts +0 -10
- package/dist/evaluator-test-helpers.js +0 -14902
- package/dist/evaluator.cjs +0 -228
- package/dist/evaluator.d.cts +0 -12
- package/dist/functions.cjs +0 -4
- package/dist/functions.d.cts +0 -12
- package/dist/index.cjs +0 -97
- package/dist/index.d.cts +0 -18
- package/dist/input-resolvers.cjs +0 -109
- package/dist/input-resolvers.d.cts +0 -22
- package/dist/integer-power.cjs +0 -19
- package/dist/integer-power.d.cts +0 -11
- package/dist/json-value.cjs +0 -12
- package/dist/json-value.d.cts +0 -8
- package/dist/plain-record.cjs +0 -7
- package/dist/plain-record.d.cts +0 -2
- package/dist/resolvers.cjs +0 -0
- package/dist/resolvers.d.cts +0 -54
- package/dist/tree.cjs +0 -332
- package/dist/tree.d.cts +0 -446
|
@@ -1,440 +0,0 @@
|
|
|
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
|
-
const require_complex_division = require("./complex-division.cjs");
|
|
5
|
-
const require_duration_order = require("./duration-order.cjs");
|
|
6
|
-
const require_integer_power = require("./integer-power.cjs");
|
|
7
|
-
let trilean_regex = require("trilean-regex");
|
|
8
|
-
// src/evaluator-operations.ts
|
|
9
|
-
/** The indeterminate result for a resolver's report of absence: `not-yet-known` when the resolver said the value has not been supplied yet, `not-found` otherwise. The two share every propagation rule except `exists` (and so `coalesce`), which is why every resolver call site goes through this one function. */
|
|
10
|
-
function absenceEvaluation(absence, notFoundMessage, notYetKnownMessage) {
|
|
11
|
-
return absence.notYetKnown === true ? require_evaluation.indeterminate("not-yet-known", notYetKnownMessage) : require_evaluation.indeterminate("not-found", notFoundMessage);
|
|
12
|
-
}
|
|
13
|
-
/** 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`. */
|
|
14
|
-
function combineAnd(left, right) {
|
|
15
|
-
if (left.status === "definite" && !left.value) return require_evaluation.definite(false);
|
|
16
|
-
if (right.status === "definite" && !right.value) return require_evaluation.definite(false);
|
|
17
|
-
const reason = require_evaluation.firstIndeterminate(left, right);
|
|
18
|
-
if (reason !== void 0) return {
|
|
19
|
-
status: "indeterminate",
|
|
20
|
-
reason
|
|
21
|
-
};
|
|
22
|
-
return require_evaluation.definite(true);
|
|
23
|
-
}
|
|
24
|
-
/** The three-valued OR table: mirror image of `combineAnd`, with `true` absorbing. */
|
|
25
|
-
function combineOr(left, right) {
|
|
26
|
-
if (left.status === "definite" && left.value) return require_evaluation.definite(true);
|
|
27
|
-
if (right.status === "definite" && right.value) return require_evaluation.definite(true);
|
|
28
|
-
const reason = require_evaluation.firstIndeterminate(left, right);
|
|
29
|
-
if (reason !== void 0) return {
|
|
30
|
-
status: "indeterminate",
|
|
31
|
-
reason
|
|
32
|
-
};
|
|
33
|
-
return require_evaluation.definite(false);
|
|
34
|
-
}
|
|
35
|
-
function applyComparisonOperator(op, left, right) {
|
|
36
|
-
switch (op) {
|
|
37
|
-
case "gt": return left > right;
|
|
38
|
-
case "gte": return left >= right;
|
|
39
|
-
case "lt": return left < right;
|
|
40
|
-
case "lte": return left <= right;
|
|
41
|
-
case "eq": return left === right;
|
|
42
|
-
case "neq": return left !== right;
|
|
43
|
-
default: throw new Error("unreachable comparison operator");
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
/** Converts a `duration`'s magnitude to milliseconds by multiplying, the base unit the temporal arithmetic below combines durations of different `DurationUnit`s in and shifts an `instant` by. Comparing two durations does not use it: `compareDurations` does that without converting, because this multiplication overflows for very large spans. */
|
|
47
|
-
function toMilliseconds(value, unit) {
|
|
48
|
-
return value * require_duration_order.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 `compareDurations`, in whatever unit each carries, with a NaN unordered) -- `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
|
-
const order = require_duration_order.compareDurations(left, right);
|
|
90
|
-
return require_evaluation.definite(applyComparisonOperator(op, order ?? NaN, 0));
|
|
91
|
-
}
|
|
92
|
-
case "complex":
|
|
93
|
-
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`);
|
|
94
|
-
if (right.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'complex' value with a '${right.kind}' value`);
|
|
95
|
-
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot compare complex values with incompatible units");
|
|
96
|
-
return require_evaluation.definite(op === "eq" ? left.re === right.re && left.im === right.im : left.re !== right.re || left.im !== right.im);
|
|
97
|
-
default: throw new Error("unreachable computed-value kind");
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
/** `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. */
|
|
101
|
-
function compareText(op, left, right) {
|
|
102
|
-
if (left.kind !== "text") return require_evaluation.indeterminate("wrong-type", `'textCompare' requires 'text' operands; the left operand is '${left.kind}'`);
|
|
103
|
-
if (right.kind !== "text") return require_evaluation.indeterminate("wrong-type", `'textCompare' requires 'text' operands; the right operand is '${right.kind}'`);
|
|
104
|
-
switch (op) {
|
|
105
|
-
case "equals": return require_evaluation.definite(left.value === right.value);
|
|
106
|
-
case "notEquals": return require_evaluation.definite(left.value !== right.value);
|
|
107
|
-
case "matches":
|
|
108
|
-
case "notMatches": {
|
|
109
|
-
let pattern;
|
|
110
|
-
try {
|
|
111
|
-
pattern = new RegExp(right.value);
|
|
112
|
-
} catch {
|
|
113
|
-
return require_evaluation.indeterminate("wrong-type", `'${right.value}' is not a valid regular expression pattern`);
|
|
114
|
-
}
|
|
115
|
-
const isMatch = pattern.test(left.value);
|
|
116
|
-
return require_evaluation.definite(op === "matches" ? isMatch : !isMatch);
|
|
117
|
-
}
|
|
118
|
-
case "portableMatches":
|
|
119
|
-
case "portableNotMatches": {
|
|
120
|
-
let compiled;
|
|
121
|
-
try {
|
|
122
|
-
compiled = (0, trilean_regex.compilePattern)(right.value);
|
|
123
|
-
} catch (error) {
|
|
124
|
-
const reason = error instanceof trilean_regex.RegexParseError ? error.message : String(error);
|
|
125
|
-
return require_evaluation.indeterminate("wrong-type", `'${right.value}' is not a valid trilean-regex pattern: ${reason}`);
|
|
126
|
-
}
|
|
127
|
-
const isMatch = compiled.test(left.value);
|
|
128
|
-
return require_evaluation.definite(op === "portableMatches" ? isMatch : !isMatch);
|
|
129
|
-
}
|
|
130
|
-
default: throw new Error("unreachable text comparison operator");
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
/** `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. */
|
|
134
|
-
function computeMembershipMatch(operand, candidate) {
|
|
135
|
-
switch (operand.kind) {
|
|
136
|
-
case "number":
|
|
137
|
-
if (candidate.kind !== "number") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'number' value with a '${candidate.kind}' value for membership`);
|
|
138
|
-
if (!require_computed_value.unitsEqual(operand.unit, candidate.unit)) return require_evaluation.indeterminate("wrong-type", "cannot compare numbers with incompatible units for membership");
|
|
139
|
-
return require_evaluation.definite(operand.value === candidate.value);
|
|
140
|
-
case "text":
|
|
141
|
-
if (candidate.kind !== "text") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'text' value with a '${candidate.kind}' value for membership`);
|
|
142
|
-
return require_evaluation.definite(operand.value === candidate.value);
|
|
143
|
-
case "boolean":
|
|
144
|
-
if (candidate.kind !== "boolean") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'boolean' value with a '${candidate.kind}' value for membership`);
|
|
145
|
-
return require_evaluation.definite(operand.value === candidate.value);
|
|
146
|
-
case "instant": {
|
|
147
|
-
if (candidate.kind !== "instant") return require_evaluation.indeterminate("wrong-type", `cannot compare an 'instant' value with a '${candidate.kind}' value for membership`);
|
|
148
|
-
const operandEpoch = toEpochMilliseconds(operand.value);
|
|
149
|
-
if (operandEpoch === void 0) return unparseableInstant(operand.value);
|
|
150
|
-
const candidateEpoch = toEpochMilliseconds(candidate.value);
|
|
151
|
-
if (candidateEpoch === void 0) return unparseableInstant(candidate.value);
|
|
152
|
-
return require_evaluation.definite(operandEpoch === candidateEpoch);
|
|
153
|
-
}
|
|
154
|
-
case "duration":
|
|
155
|
-
if (candidate.kind !== "duration") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'duration' value with a '${candidate.kind}' value for membership`);
|
|
156
|
-
return require_evaluation.definite(require_duration_order.compareDurations(operand, candidate) === 0);
|
|
157
|
-
case "complex":
|
|
158
|
-
if (candidate.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `cannot compare a 'complex' value with a '${candidate.kind}' value for membership`);
|
|
159
|
-
if (!require_computed_value.unitsEqual(operand.unit, candidate.unit)) return require_evaluation.indeterminate("wrong-type", "cannot compare complex values with incompatible units for membership");
|
|
160
|
-
return require_evaluation.definite(operand.re === candidate.re && operand.im === candidate.im);
|
|
161
|
-
default: throw new Error("unreachable computed-value kind");
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
/** `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. */
|
|
165
|
-
function isDimensionless(unit) {
|
|
166
|
-
return require_computed_value.unitsEqual(unit, void 0);
|
|
167
|
-
}
|
|
168
|
-
/** `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. */
|
|
169
|
-
function applyNegate(operand) {
|
|
170
|
-
switch (operand.kind) {
|
|
171
|
-
case "number": return require_evaluation.definite({
|
|
172
|
-
kind: "number",
|
|
173
|
-
value: -operand.value,
|
|
174
|
-
unit: operand.unit
|
|
175
|
-
});
|
|
176
|
-
case "duration": return require_evaluation.definite({
|
|
177
|
-
kind: "duration",
|
|
178
|
-
value: -operand.value,
|
|
179
|
-
unit: operand.unit
|
|
180
|
-
});
|
|
181
|
-
case "complex": return require_evaluation.definite({
|
|
182
|
-
kind: "complex",
|
|
183
|
-
re: -operand.re,
|
|
184
|
-
im: -operand.im,
|
|
185
|
-
unit: operand.unit
|
|
186
|
-
});
|
|
187
|
-
case "text":
|
|
188
|
-
case "instant":
|
|
189
|
-
case "boolean": return require_evaluation.indeterminate("wrong-type", `cannot negate a '${operand.kind}' value`);
|
|
190
|
-
default: throw new Error("unreachable computed-value kind");
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
function applyArithmeticOnNumbers(op, left, right) {
|
|
194
|
-
switch (op) {
|
|
195
|
-
case "add":
|
|
196
|
-
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot add numbers with incompatible units");
|
|
197
|
-
return require_evaluation.definite({
|
|
198
|
-
kind: "number",
|
|
199
|
-
value: left.value + right.value,
|
|
200
|
-
unit: left.unit
|
|
201
|
-
});
|
|
202
|
-
case "subtract":
|
|
203
|
-
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot subtract numbers with incompatible units");
|
|
204
|
-
return require_evaluation.definite({
|
|
205
|
-
kind: "number",
|
|
206
|
-
value: left.value - right.value,
|
|
207
|
-
unit: left.unit
|
|
208
|
-
});
|
|
209
|
-
case "multiply": return require_evaluation.definite({
|
|
210
|
-
kind: "number",
|
|
211
|
-
value: left.value * right.value,
|
|
212
|
-
unit: require_computed_value.combineUnitsForMultiply(left.unit, right.unit)
|
|
213
|
-
});
|
|
214
|
-
case "divide":
|
|
215
|
-
if (right.value === 0) return require_evaluation.indeterminate("domain-error", "division by zero");
|
|
216
|
-
return require_evaluation.definite({
|
|
217
|
-
kind: "number",
|
|
218
|
-
value: left.value / right.value,
|
|
219
|
-
unit: require_computed_value.combineUnitsForDivide(left.unit, right.unit)
|
|
220
|
-
});
|
|
221
|
-
case "power":
|
|
222
|
-
if (!isDimensionless(left.unit) || !isDimensionless(right.unit)) return require_evaluation.indeterminate("wrong-type", "'power' requires dimensionless operands");
|
|
223
|
-
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");
|
|
224
|
-
if (left.value === 0 && right.value < 0) return require_evaluation.indeterminate("domain-error", "zero raised to a negative power is a division by zero");
|
|
225
|
-
return require_evaluation.definite({
|
|
226
|
-
kind: "number",
|
|
227
|
-
value: left.value ** right.value
|
|
228
|
-
});
|
|
229
|
-
case "modulo":
|
|
230
|
-
if (!isDimensionless(left.unit) || !isDimensionless(right.unit)) return require_evaluation.indeterminate("wrong-type", "'modulo' requires dimensionless operands");
|
|
231
|
-
if (right.value === 0) return require_evaluation.indeterminate("domain-error", "modulo by zero");
|
|
232
|
-
return require_evaluation.definite({
|
|
233
|
-
kind: "number",
|
|
234
|
-
value: left.value % right.value
|
|
235
|
-
});
|
|
236
|
-
default: throw new Error("unreachable arithmetic operator");
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
/** 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. */
|
|
240
|
-
function applyArithmeticOnDurations(op, left, right) {
|
|
241
|
-
switch (op) {
|
|
242
|
-
case "add": return require_evaluation.definite({
|
|
243
|
-
kind: "duration",
|
|
244
|
-
value: toMilliseconds(left.value, left.unit) + toMilliseconds(right.value, right.unit),
|
|
245
|
-
unit: "ms"
|
|
246
|
-
});
|
|
247
|
-
case "subtract": return require_evaluation.definite({
|
|
248
|
-
kind: "duration",
|
|
249
|
-
value: toMilliseconds(left.value, left.unit) - toMilliseconds(right.value, right.unit),
|
|
250
|
-
unit: "ms"
|
|
251
|
-
});
|
|
252
|
-
case "multiply":
|
|
253
|
-
case "divide":
|
|
254
|
-
case "power":
|
|
255
|
-
case "modulo": return require_evaluation.indeterminate("wrong-type", `arithmetic operator '${op}' is not defined between two 'duration' values`);
|
|
256
|
-
default: throw new Error("unreachable arithmetic operator");
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
/** 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. */
|
|
260
|
-
function toComplexValue(value) {
|
|
261
|
-
if (value.kind === "complex") return value;
|
|
262
|
-
return {
|
|
263
|
-
kind: "complex",
|
|
264
|
-
re: value.value,
|
|
265
|
-
im: 0,
|
|
266
|
-
unit: value.unit
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
/** The complex product, as its own component-level helper rather than only a branch of the operator switch below, because `power`'s exponentiation by squaring needs exactly this and must not re-derive it. */
|
|
270
|
-
function multiplyComplexValues(left, right) {
|
|
271
|
-
return {
|
|
272
|
-
kind: "complex",
|
|
273
|
-
re: left.re * right.re - left.im * right.im,
|
|
274
|
-
im: left.re * right.im + left.im * right.re,
|
|
275
|
-
unit: require_computed_value.combineUnitsForMultiply(left.unit, right.unit)
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
/** 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. */
|
|
279
|
-
function applyArithmeticOnComplex(op, left, right) {
|
|
280
|
-
switch (op) {
|
|
281
|
-
case "add":
|
|
282
|
-
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot add complex values with incompatible units");
|
|
283
|
-
return require_evaluation.definite({
|
|
284
|
-
kind: "complex",
|
|
285
|
-
re: left.re + right.re,
|
|
286
|
-
im: left.im + right.im,
|
|
287
|
-
unit: left.unit
|
|
288
|
-
});
|
|
289
|
-
case "subtract":
|
|
290
|
-
if (!require_computed_value.unitsEqual(left.unit, right.unit)) return require_evaluation.indeterminate("wrong-type", "cannot subtract complex values with incompatible units");
|
|
291
|
-
return require_evaluation.definite({
|
|
292
|
-
kind: "complex",
|
|
293
|
-
re: left.re - right.re,
|
|
294
|
-
im: left.im - right.im,
|
|
295
|
-
unit: left.unit
|
|
296
|
-
});
|
|
297
|
-
case "multiply": return require_evaluation.definite(multiplyComplexValues(left, right));
|
|
298
|
-
case "divide": {
|
|
299
|
-
if (right.re === 0 && right.im === 0) return require_evaluation.indeterminate("domain-error", "division by zero");
|
|
300
|
-
const quotient = require_complex_division.divideComplexComponents(left, right);
|
|
301
|
-
return require_evaluation.definite({
|
|
302
|
-
kind: "complex",
|
|
303
|
-
re: quotient.re,
|
|
304
|
-
im: quotient.im,
|
|
305
|
-
unit: require_computed_value.combineUnitsForDivide(left.unit, right.unit)
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
case "modulo": return require_evaluation.indeterminate("domain-error", "'modulo' is undefined for complex values");
|
|
309
|
-
default: throw new Error("unreachable arithmetic operator");
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
/** `1 / value` for a non-zero complex value, carrying the inverse unit. */
|
|
313
|
-
function reciprocalOf(value) {
|
|
314
|
-
return {
|
|
315
|
-
kind: "complex",
|
|
316
|
-
...require_complex_division.divideComplexComponents({
|
|
317
|
-
re: 1,
|
|
318
|
-
im: 0
|
|
319
|
-
}, value),
|
|
320
|
-
unit: require_computed_value.combineUnitsForDivide({}, value.unit)
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
/** Whether a complex value is finite and not zero, which is what dividing by it needs. A positive power that has overflowed or underflowed to zero is neither, and cannot be inverted. */
|
|
324
|
-
function isFiniteAndNonZero(value) {
|
|
325
|
-
const larger = Math.max(Math.abs(value.re), Math.abs(value.im));
|
|
326
|
-
return Number.isFinite(larger) && larger > 0;
|
|
327
|
-
}
|
|
328
|
-
/** `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*, grouped by squaring so the work grows with the logarithm of the exponent rather than the exponent itself. The price is accuracy: the error of squaring grows in proportion to the exponent, so for large exponents the result is many times less accurate than a plain multiplication loop's (the "Complex values" section of README.md has the measurement, and explains why an arbitrary complex exponent stays out of scope). */
|
|
329
|
-
function applyComplexPower(base, exponent) {
|
|
330
|
-
if (exponent.kind !== "number" || !Number.isInteger(exponent.value)) return require_evaluation.indeterminate("wrong-type", "'power' with a complex operand requires a real integer exponent");
|
|
331
|
-
if (!isDimensionless(base.unit) || !isDimensionless(exponent.unit)) return require_evaluation.indeterminate("wrong-type", "'power' requires dimensionless operands");
|
|
332
|
-
const complexUnit = {
|
|
333
|
-
kind: "complex",
|
|
334
|
-
re: 1,
|
|
335
|
-
im: 0,
|
|
336
|
-
unit: {}
|
|
337
|
-
};
|
|
338
|
-
const complexBase = toComplexValue(base);
|
|
339
|
-
if (exponent.value >= 0) return require_evaluation.definite(require_integer_power.powerByRepeatedSquaring({
|
|
340
|
-
base: complexBase,
|
|
341
|
-
exponent: exponent.value,
|
|
342
|
-
identity: complexUnit,
|
|
343
|
-
multiply: multiplyComplexValues
|
|
344
|
-
}));
|
|
345
|
-
if (complexBase.re === 0 && complexBase.im === 0) return require_evaluation.indeterminate("domain-error", "division by zero");
|
|
346
|
-
const positivePower = require_integer_power.powerByRepeatedSquaring({
|
|
347
|
-
base: complexBase,
|
|
348
|
-
exponent: -exponent.value,
|
|
349
|
-
identity: complexUnit,
|
|
350
|
-
multiply: multiplyComplexValues
|
|
351
|
-
});
|
|
352
|
-
if (isFiniteAndNonZero(positivePower)) return require_evaluation.definite(reciprocalOf(positivePower));
|
|
353
|
-
return require_evaluation.definite(require_integer_power.powerByRepeatedSquaring({
|
|
354
|
-
base: reciprocalOf(complexBase),
|
|
355
|
-
exponent: -exponent.value,
|
|
356
|
-
identity: complexUnit,
|
|
357
|
-
multiply: multiplyComplexValues
|
|
358
|
-
}));
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* 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.
|
|
362
|
-
*/
|
|
363
|
-
function applyArithmeticByKind(op, left, right) {
|
|
364
|
-
if (left.kind === "instant" && right.kind === "instant" && op === "subtract") {
|
|
365
|
-
const leftEpoch = toEpochMilliseconds(left.value);
|
|
366
|
-
if (leftEpoch === void 0) return unparseableInstant(left.value);
|
|
367
|
-
const rightEpoch = toEpochMilliseconds(right.value);
|
|
368
|
-
if (rightEpoch === void 0) return unparseableInstant(right.value);
|
|
369
|
-
return require_evaluation.definite({
|
|
370
|
-
kind: "duration",
|
|
371
|
-
value: leftEpoch - rightEpoch,
|
|
372
|
-
unit: "ms"
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
if (left.kind === "instant" && right.kind === "duration" && op === "add") {
|
|
376
|
-
const leftEpoch = toEpochMilliseconds(left.value);
|
|
377
|
-
if (leftEpoch === void 0) return unparseableInstant(left.value);
|
|
378
|
-
return instantFromEpochMilliseconds(leftEpoch + toMilliseconds(right.value, right.unit));
|
|
379
|
-
}
|
|
380
|
-
if (left.kind === "duration" && right.kind === "instant" && op === "add") {
|
|
381
|
-
const rightEpoch = toEpochMilliseconds(right.value);
|
|
382
|
-
if (rightEpoch === void 0) return unparseableInstant(right.value);
|
|
383
|
-
return instantFromEpochMilliseconds(rightEpoch + toMilliseconds(left.value, left.unit));
|
|
384
|
-
}
|
|
385
|
-
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`);
|
|
386
|
-
if (left.kind === "duration" && right.kind === "duration") return applyArithmeticOnDurations(op, left, right);
|
|
387
|
-
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`);
|
|
388
|
-
if (left.kind !== "number" && left.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${left.kind}' value`);
|
|
389
|
-
if (right.kind !== "number" && right.kind !== "complex") return require_evaluation.indeterminate("wrong-type", `arithmetic requires numeric operands; got a '${right.kind}' value`);
|
|
390
|
-
if (left.kind === "complex" || right.kind === "complex") {
|
|
391
|
-
if (op === "power") return applyComplexPower(left, right);
|
|
392
|
-
return applyArithmeticOnComplex(op, toComplexValue(left), toComplexValue(right));
|
|
393
|
-
}
|
|
394
|
-
return applyArithmeticOnNumbers(op, left, right);
|
|
395
|
-
}
|
|
396
|
-
/** Whether a computed value's numeric components are all finite. Only the kinds an arithmetic operator can produce carry one; any other kind has nothing to overflow. */
|
|
397
|
-
function hasFiniteMagnitude(value) {
|
|
398
|
-
switch (value.kind) {
|
|
399
|
-
case "number":
|
|
400
|
-
case "duration": return Number.isFinite(value.value);
|
|
401
|
-
case "complex": return Number.isFinite(value.re) && Number.isFinite(value.im);
|
|
402
|
-
case "text":
|
|
403
|
-
case "boolean":
|
|
404
|
-
case "instant": return true;
|
|
405
|
-
default: throw new Error("unreachable computed-value kind");
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* `arithmetic`'s entry point: the result of `applyArithmeticByKind`, except that a definite result whose magnitude overflowed to `Infinity` (or became `NaN`) is a `domain-error` instead. A value of that kind is outside what `ComputedValue` can serialise (JSON writes it as `null`), and an operation pushed outside its valid domain is exactly the category division by zero already uses. Checking once here, after every operator and operand kind has produced its result, is what keeps each of them honest without repeating the check per operator.
|
|
410
|
-
*/
|
|
411
|
-
function applyArithmetic(op, left, right) {
|
|
412
|
-
const result = applyArithmeticByKind(op, left, right);
|
|
413
|
-
if (result.status === "definite" && !hasFiniteMagnitude(result.value)) return require_evaluation.indeterminate("domain-error", "result is not a finite number");
|
|
414
|
-
return result;
|
|
415
|
-
}
|
|
416
|
-
/** `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. */
|
|
417
|
-
function checkReferenceUnit(expectedUnit, resolvedValue) {
|
|
418
|
-
if (expectedUnit === void 0) return void 0;
|
|
419
|
-
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");
|
|
420
|
-
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");
|
|
421
|
-
}
|
|
422
|
-
/** `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. */
|
|
423
|
-
function invokeRegisteredFunction(functions, fnName, args) {
|
|
424
|
-
const fn = Object.hasOwn(functions, fnName) ? functions[fnName] : void 0;
|
|
425
|
-
if (fn === void 0) return require_evaluation.indeterminate("wrong-type", `no function registered under the name '${fnName}'`);
|
|
426
|
-
const outcome = fn(args);
|
|
427
|
-
if ("domainError" in outcome) return require_evaluation.indeterminate("domain-error", outcome.domainError);
|
|
428
|
-
if ("wrongType" in outcome) return require_evaluation.indeterminate("wrong-type", outcome.wrongType);
|
|
429
|
-
return require_evaluation.definite(outcome);
|
|
430
|
-
}
|
|
431
|
-
exports.absenceEvaluation = absenceEvaluation;
|
|
432
|
-
exports.applyArithmetic = applyArithmetic;
|
|
433
|
-
exports.applyNegate = applyNegate;
|
|
434
|
-
exports.checkReferenceUnit = checkReferenceUnit;
|
|
435
|
-
exports.combineAnd = combineAnd;
|
|
436
|
-
exports.combineOr = combineOr;
|
|
437
|
-
exports.compareText = compareText;
|
|
438
|
-
exports.compareValues = compareValues;
|
|
439
|
-
exports.computeMembershipMatch = computeMembershipMatch;
|
|
440
|
-
exports.invokeRegisteredFunction = invokeRegisteredFunction;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { ComputedValue, Unit } from "./computed-value.cjs";
|
|
2
|
-
import { Evaluation } from "./evaluation.cjs";
|
|
3
|
-
import { FunctionRegistry } from "./functions.cjs";
|
|
4
|
-
import { Absence } from "./resolvers.cjs";
|
|
5
|
-
import { ArithmeticOperator, ComparisonOperator, TextComparisonOperator } from "./tree.cjs";
|
|
6
|
-
/** The indeterminate result for a resolver's report of absence: `not-yet-known` when the resolver said the value has not been supplied yet, `not-found` otherwise. The two share every propagation rule except `exists` (and so `coalesce`), which is why every resolver call site goes through this one function. */
|
|
7
|
-
export declare function absenceEvaluation(absence: Readonly<Absence>, notFoundMessage: string, notYetKnownMessage: string): Evaluation<never>;
|
|
8
|
-
/** 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`. */
|
|
9
|
-
export declare function combineAnd(left: Evaluation<boolean>, right: Evaluation<boolean>): Evaluation<boolean>;
|
|
10
|
-
/** The three-valued OR table: mirror image of `combineAnd`, with `true` absorbing. */
|
|
11
|
-
export declare function combineOr(left: Evaluation<boolean>, right: Evaluation<boolean>): Evaluation<boolean>;
|
|
12
|
-
/** `compare`'s two operands: valid kinds are `number` (matching units required), `instant` (ordered by parsed epoch millisecond), or `duration` (ordered by `compareDurations`, in whatever unit each carries, with a NaN unordered) -- `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`. */
|
|
13
|
-
export declare function compareValues(op: ComparisonOperator, left: ComputedValue, right: ComputedValue): Evaluation<boolean>;
|
|
14
|
-
/** `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. */
|
|
15
|
-
export declare function compareText(op: TextComparisonOperator, left: ComputedValue, right: ComputedValue): Evaluation<boolean>;
|
|
16
|
-
/** `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. */
|
|
17
|
-
export declare function computeMembershipMatch(operand: ComputedValue, candidate: ComputedValue): Evaluation<boolean>;
|
|
18
|
-
/** `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. */
|
|
19
|
-
export declare function applyNegate(operand: ComputedValue): Evaluation<ComputedValue>;
|
|
20
|
-
/**
|
|
21
|
-
* `arithmetic`'s entry point: the result of `applyArithmeticByKind`, except that a definite result whose magnitude overflowed to `Infinity` (or became `NaN`) is a `domain-error` instead. A value of that kind is outside what `ComputedValue` can serialise (JSON writes it as `null`), and an operation pushed outside its valid domain is exactly the category division by zero already uses. Checking once here, after every operator and operand kind has produced its result, is what keeps each of them honest without repeating the check per operator.
|
|
22
|
-
*/
|
|
23
|
-
export declare function applyArithmetic(op: ArithmeticOperator, left: ComputedValue, right: ComputedValue): Evaluation<ComputedValue>;
|
|
24
|
-
/** `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. */
|
|
25
|
-
export declare function checkReferenceUnit(expectedUnit: Unit | undefined, resolvedValue: ComputedValue): Evaluation<never> | undefined;
|
|
26
|
-
/** `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. */
|
|
27
|
-
export declare function invokeRegisteredFunction(functions: Readonly<FunctionRegistry>, fnName: string, args: readonly ComputedValue[]): Evaluation<ComputedValue>;
|