trilean 1.11.1 → 1.13.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 +21 -0
- package/dist/evaluator-compiled.cjs +311 -0
- package/dist/evaluator-compiled.d.cts +20 -0
- package/dist/evaluator-compiled.d.ts +20 -0
- package/dist/evaluator-compiled.js +310 -0
- package/dist/evaluator-effect.cjs +10 -6
- package/dist/evaluator-effect.d.cts +5 -1
- package/dist/evaluator-effect.d.ts +5 -1
- package/dist/evaluator-effect.js +10 -7
- package/dist/evaluator-factory.cjs +3 -1
- package/dist/evaluator-factory.d.cts +3 -0
- package/dist/evaluator-factory.d.ts +3 -0
- package/dist/evaluator-factory.js +3 -1
- package/dist/evaluator-literals.cjs +43 -0
- package/dist/evaluator-literals.d.cts +10 -0
- package/dist/evaluator-literals.d.ts +10 -0
- package/dist/evaluator-literals.js +42 -0
- package/dist/evaluator.cjs +7 -31
- package/dist/evaluator.js +7 -31
- package/dist/index.cjs +3 -0
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -1
- package/dist/tree.cjs +1 -1
- package/dist/tree.d.cts +1 -1
- package/dist/tree.d.ts +1 -1
- package/dist/tree.js +1 -1
- package/dist-cli/trilean.mjs +379 -60
- package/package.json +1 -1
- package/schemas/trilean.schema.json +1 -1
package/README.md
CHANGED
|
@@ -858,11 +858,32 @@ When every resolver is a plain in-memory read, `createSyncEvaluator` builds the
|
|
|
858
858
|
function createSyncEvaluator(options: { functions?: FunctionRegistry; maxNodes?: number; maxNestingDepth?: number }): {
|
|
859
859
|
evaluatePredicate: (node: PredicateNode, context: EvaluationContext, resolvers: SyncResolvers) => Evaluation<boolean>;
|
|
860
860
|
evaluateValue: (node: ExpressionNode, context: EvaluationContext, resolvers: SyncResolvers) => Evaluation<ComputedValue>;
|
|
861
|
+
compilePredicate: (node: PredicateNode) => CompiledPredicate;
|
|
861
862
|
};
|
|
862
863
|
```
|
|
863
864
|
|
|
864
865
|
The node rules are written once and run by either driver, so the two evaluators agree on every node kind and every three-valued rule. A resolver that returns a promise (or any thenable) to a synchronous evaluator makes the evaluation throw a `TypeError`, since there is nothing to await it with. Because nothing is concurrent, independent operands (both sides of an `and`, the candidates of a `memberOf`, the items of a collection) are evaluated one after another; when a `maxNodes` or `maxNestingDepth` limit is exhausted partway through a tree, which operand meets it first can therefore differ from the asynchronous evaluator.
|
|
865
866
|
|
|
867
|
+
Both evaluators default `maxNodes` to `DEFAULT_MAX_EVALUATION_NODES` and `maxNestingDepth` to `DEFAULT_MAX_NESTING_DEPTH`, and both constants are exported, so a caller that checks a tree against the budget before evaluating it (counting its nodes, with the root at nesting depth 0 and every child one deeper) reads the same limits the evaluator applies. One evaluation visits a node at most once unless the tree has a `treeReference`, a `fold` or a `some`/`every` quantifier, which revisit a subtree per resolved tree or item, so for any other tree its node count is exactly the most the evaluation can spend.
|
|
868
|
+
|
|
869
|
+
### Evaluating one tree repeatedly
|
|
870
|
+
|
|
871
|
+
A caller that evaluates one predicate tree many times against changing resolvers (a constraint solver asking about each partial assignment, a rule checked against every record) pays for walking the tree on every call. `compilePredicate` on a synchronous evaluator walks it once and returns a function of the context and resolvers:
|
|
872
|
+
|
|
873
|
+
```ts
|
|
874
|
+
type CompiledPredicate = (context: EvaluationContext, resolvers: SyncResolvers) => Evaluation<boolean>;
|
|
875
|
+
|
|
876
|
+
const { compilePredicate } = createSyncEvaluator({ functions });
|
|
877
|
+
const check = compilePredicate(tree);
|
|
878
|
+
|
|
879
|
+
check(undefined, resolversForFirstAssignment);
|
|
880
|
+
check(undefined, resolversForSecondAssignment);
|
|
881
|
+
```
|
|
882
|
+
|
|
883
|
+
The result is the `Evaluation` that `evaluatePredicate` gives for the same tree, context and resolvers: the same status, value and indeterminate reason (including which of several candidate reasons is reported), the same resolver calls in the same order, the same `maxNodes` and `maxNestingDepth` limits met at the same node, and the same `TypeError` for a resolver that returns a promise. The functions and limits are those the evaluator was built with. The tree must be well formed and must not be mutated after it is compiled.
|
|
884
|
+
|
|
885
|
+
Every node kind is accepted. The compiled function evaluates `not`, `and`, `or`, `allOf`, `anyOf`, `compare`, `textCompare`, `memberOf` and `exists`, and the expression kinds `reference`, `accumulator`, the literals, `arithmetic`, `negate`, `call`, `lookup`, `conditional` and `delegate`, itself. A `some`, `every`, `fold` or `treeReference` node, which re-evaluates a subtree per item or per resolved tree, is handed to the interpreter inside the compiled function, sharing the call's budget, so a tree containing one still has a single entry point and one set of semantics. A tree that can never exhaust its budget (no such node, and no more nodes than `maxNodes`) skips the per-node budget charge; any other tree charges it at every node exactly as the interpreter does.
|
|
886
|
+
|
|
866
887
|
## Indeterminacy reference
|
|
867
888
|
|
|
868
889
|
How each reason category can arise, per node kind. "Propagates" means: an indeterminate operand/sub-result, with no other rule overriding it, makes the whole node indeterminate with that same reason (subject to the tie-break rule in [The evaluation model](#the-evaluation-model) when more than one candidate reason is present, and to the absorbing-value exceptions called out explicitly below).
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_evaluation = require("./evaluation.cjs");
|
|
3
|
+
const require_evaluator_budget = require("./evaluator-budget.cjs");
|
|
4
|
+
const require_evaluator_effect = require("./evaluator-effect.cjs");
|
|
5
|
+
const require_evaluator_operations = require("./evaluator-operations.cjs");
|
|
6
|
+
const require_evaluator_literals = require("./evaluator-literals.cjs");
|
|
7
|
+
const require_evaluator = require("./evaluator.cjs");
|
|
8
|
+
//#region src/evaluator-compiled.ts
|
|
9
|
+
/** Never charged: handed to the nodes of a tree compiled without budget counting, none of which reads it. */
|
|
10
|
+
const UNLIMITED_BUDGET = { checkNode: () => void 0 };
|
|
11
|
+
/** The `visitedTreeKeys` of a call that has not entered any `treeReference`; the interpreter copies it rather than mutating it, so one instance serves every call. */
|
|
12
|
+
const NO_TREE_KEYS = /* @__PURE__ */ new Set();
|
|
13
|
+
/** The message for the `accumulator` node outside a fold's `combine`, which is everywhere a compiled node can sit. */
|
|
14
|
+
const ACCUMULATOR_OUTSIDE_FOLD = "accumulator used outside a reduce fold's combine expression";
|
|
15
|
+
/** Compiles the nodes of one tree, for one setting of `counted`. */
|
|
16
|
+
var NodeCompiler = class {
|
|
17
|
+
functions;
|
|
18
|
+
maxNestingDepth;
|
|
19
|
+
counted;
|
|
20
|
+
/** The number of nodes compiled natively so far; an upper bound on the nodes one evaluation can visit, because every compiled node is visited at most once per call. */
|
|
21
|
+
nodesCompiled = 0;
|
|
22
|
+
/** Whether any node was left to the interpreter. */
|
|
23
|
+
interpreted = false;
|
|
24
|
+
/**
|
|
25
|
+
* `functions` is the registry `call` nodes resolve against. A node at `maxNestingDepth` or deeper is left to the interpreter, so compilation never descends further than an evaluation would. `counted` says whether every compiled node charges the call's `EvaluationBudget` before doing its own work, exactly as the interpreter does; a tree that can never exhaust its budget is compiled with it off, which removes the charge from every node.
|
|
26
|
+
*/
|
|
27
|
+
constructor(functions, maxNestingDepth, counted) {
|
|
28
|
+
this.functions = functions;
|
|
29
|
+
this.maxNestingDepth = maxNestingDepth;
|
|
30
|
+
this.counted = counted;
|
|
31
|
+
}
|
|
32
|
+
/** In a counted tree, charges the budget for the node before running it; otherwise `run` as it is. */
|
|
33
|
+
charged(depth, run) {
|
|
34
|
+
if (!this.counted) return run;
|
|
35
|
+
return (context, resolvers, budget) => budget.checkNode(depth) ?? run(context, resolvers, budget);
|
|
36
|
+
}
|
|
37
|
+
/** A node left to the interpreter. The interpreter charges the budget for it and its descendants itself, so this is not wrapped in `charged`. */
|
|
38
|
+
interpretedPredicate(node, depth) {
|
|
39
|
+
this.interpreted = true;
|
|
40
|
+
const { functions } = this;
|
|
41
|
+
return (context, resolvers, budget) => require_evaluator_effect.runSync(require_evaluator.evaluatePredicateInternal(node, context, resolvers, void 0, functions, NO_TREE_KEYS, 0, budget, depth));
|
|
42
|
+
}
|
|
43
|
+
interpretedValue(node, depth) {
|
|
44
|
+
this.interpreted = true;
|
|
45
|
+
const { functions } = this;
|
|
46
|
+
return (context, resolvers, budget) => require_evaluator_effect.runSync(require_evaluator.evaluateValueInternal(node, context, resolvers, void 0, functions, NO_TREE_KEYS, 0, budget, depth));
|
|
47
|
+
}
|
|
48
|
+
predicate(node, depth) {
|
|
49
|
+
if (depth >= this.maxNestingDepth) return this.interpretedPredicate(node, depth);
|
|
50
|
+
this.nodesCompiled += 1;
|
|
51
|
+
const child = (operand) => this.predicate(operand, depth + 1);
|
|
52
|
+
const value = (operand) => this.value(operand, depth + 1, false);
|
|
53
|
+
switch (node.kind) {
|
|
54
|
+
case "not": {
|
|
55
|
+
const operand = child(node.operand);
|
|
56
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
57
|
+
const result = operand(context, resolvers, budget);
|
|
58
|
+
return result.status === "indeterminate" ? result : require_evaluation.definite(!result.value);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
case "and": {
|
|
62
|
+
const left = child(node.left);
|
|
63
|
+
const right = child(node.right);
|
|
64
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
65
|
+
const leftResult = left(context, resolvers, budget);
|
|
66
|
+
const rightResult = right(context, resolvers, budget);
|
|
67
|
+
return require_evaluator_operations.combineAnd(leftResult, rightResult);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
case "or": {
|
|
71
|
+
const left = child(node.left);
|
|
72
|
+
const right = child(node.right);
|
|
73
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
74
|
+
const leftResult = left(context, resolvers, budget);
|
|
75
|
+
const rightResult = right(context, resolvers, budget);
|
|
76
|
+
return require_evaluator_operations.combineOr(leftResult, rightResult);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
case "allOf": {
|
|
80
|
+
const operands = node.operands.map(child);
|
|
81
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
82
|
+
let combined = require_evaluation.definite(true);
|
|
83
|
+
for (const operand of operands) combined = require_evaluator_operations.combineAnd(combined, operand(context, resolvers, budget));
|
|
84
|
+
return combined;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
case "anyOf": {
|
|
88
|
+
const operands = node.operands.map(child);
|
|
89
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
90
|
+
let combined = require_evaluation.definite(false);
|
|
91
|
+
for (const operand of operands) combined = require_evaluator_operations.combineOr(combined, operand(context, resolvers, budget));
|
|
92
|
+
return combined;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
case "compare": {
|
|
96
|
+
const left = value(node.left);
|
|
97
|
+
const right = value(node.right);
|
|
98
|
+
const { op } = node;
|
|
99
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
100
|
+
const leftResult = left(context, resolvers, budget);
|
|
101
|
+
const rightResult = right(context, resolvers, budget);
|
|
102
|
+
if (leftResult.status === "indeterminate") return leftResult;
|
|
103
|
+
if (rightResult.status === "indeterminate") return rightResult;
|
|
104
|
+
return require_evaluator_operations.compareValues(op, leftResult.value, rightResult.value);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
case "textCompare": {
|
|
108
|
+
const left = value(node.left);
|
|
109
|
+
const right = value(node.right);
|
|
110
|
+
const { op } = node;
|
|
111
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
112
|
+
const leftResult = left(context, resolvers, budget);
|
|
113
|
+
const rightResult = right(context, resolvers, budget);
|
|
114
|
+
if (leftResult.status === "indeterminate") return leftResult;
|
|
115
|
+
if (rightResult.status === "indeterminate") return rightResult;
|
|
116
|
+
return require_evaluator_operations.compareText(op, leftResult.value, rightResult.value);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
case "memberOf": {
|
|
120
|
+
const operand = value(node.operand);
|
|
121
|
+
const candidates = node.candidates.map(value);
|
|
122
|
+
const matched = node.op === "in";
|
|
123
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
124
|
+
const operandResult = operand(context, resolvers, budget);
|
|
125
|
+
if (operandResult.status === "indeterminate") return operandResult;
|
|
126
|
+
const outcomes = candidates.map((candidate) => {
|
|
127
|
+
const candidateResult = candidate(context, resolvers, budget);
|
|
128
|
+
if (candidateResult.status === "indeterminate") return candidateResult;
|
|
129
|
+
return require_evaluator_operations.computeMembershipMatch(operandResult.value, candidateResult.value);
|
|
130
|
+
});
|
|
131
|
+
for (const outcome of outcomes) if (outcome.status === "definite" && outcome.value) return require_evaluation.definite(matched);
|
|
132
|
+
const reason = require_evaluation.firstIndeterminate(...outcomes);
|
|
133
|
+
if (reason !== void 0) return {
|
|
134
|
+
status: "indeterminate",
|
|
135
|
+
reason
|
|
136
|
+
};
|
|
137
|
+
return require_evaluation.definite(!matched);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
case "exists": {
|
|
141
|
+
const operand = value(node.operand);
|
|
142
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
143
|
+
const result = operand(context, resolvers, budget);
|
|
144
|
+
if (result.status === "indeterminate") {
|
|
145
|
+
if (result.reason.code === "not-yet-known") return result;
|
|
146
|
+
if (result.reason.code === "not-found") return require_evaluation.definite(false);
|
|
147
|
+
}
|
|
148
|
+
return require_evaluation.definite(true);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
case "some":
|
|
152
|
+
case "every":
|
|
153
|
+
case "treeReference": return this.interpretedPredicate(node, depth);
|
|
154
|
+
default: return this.interpretedPredicate(node, depth);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Compiles an expression node. `fresh` says whether its value will be handed to code outside this package (a registered function or a resolver): a literal is then built anew on each call, as the interpreter builds it, so no mutation by that code can reach a later call; otherwise it is built once and shared, since only this package's own operators read it.
|
|
159
|
+
*/
|
|
160
|
+
value(node, depth, fresh) {
|
|
161
|
+
if (depth >= this.maxNestingDepth) return this.interpretedValue(node, depth);
|
|
162
|
+
this.nodesCompiled += 1;
|
|
163
|
+
const child = (operand) => this.value(operand, depth + 1, false);
|
|
164
|
+
const handedOver = (operand) => this.value(operand, depth + 1, true);
|
|
165
|
+
switch (node.kind) {
|
|
166
|
+
case "reference": {
|
|
167
|
+
const { key, unit } = node;
|
|
168
|
+
const keyText = JSON.stringify(key);
|
|
169
|
+
const notFound = `no value found for reference key ${keyText}`;
|
|
170
|
+
const notYetKnown = `the value for reference key ${keyText} is not yet known`;
|
|
171
|
+
return this.charged(depth, (context, resolvers) => {
|
|
172
|
+
const resolution = require_evaluator_effect.settleSync(resolvers.resolveValue(key, context));
|
|
173
|
+
if (!resolution.found) return require_evaluator_operations.absenceEvaluation(resolution, notFound, notYetKnown);
|
|
174
|
+
const unitError = require_evaluator_operations.checkReferenceUnit(unit, resolution.value);
|
|
175
|
+
if (unitError !== void 0) return unitError;
|
|
176
|
+
return require_evaluation.definite(resolution.value);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
case "accumulator": return this.charged(depth, () => require_evaluation.indeterminate("wrong-type", ACCUMULATOR_OUTSIDE_FOLD));
|
|
180
|
+
case "call": {
|
|
181
|
+
const args = node.args.map(handedOver);
|
|
182
|
+
const { fn } = node;
|
|
183
|
+
const { functions } = this;
|
|
184
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
185
|
+
const results = args.map((arg) => arg(context, resolvers, budget));
|
|
186
|
+
const values = [];
|
|
187
|
+
for (const result of results) {
|
|
188
|
+
if (result.status === "indeterminate") return result;
|
|
189
|
+
values.push(result.value);
|
|
190
|
+
}
|
|
191
|
+
return require_evaluator_operations.invokeRegisteredFunction(functions, fn, values);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
case "numberLiteral":
|
|
195
|
+
case "textLiteral":
|
|
196
|
+
case "booleanLiteral":
|
|
197
|
+
case "instantLiteral":
|
|
198
|
+
case "durationLiteral":
|
|
199
|
+
case "complexLiteral": {
|
|
200
|
+
if (fresh) return this.charged(depth, () => require_evaluation.definite(require_evaluator_literals.literalValue(node)));
|
|
201
|
+
const shared = require_evaluation.definite(require_evaluator_literals.literalValue(node));
|
|
202
|
+
return this.charged(depth, () => shared);
|
|
203
|
+
}
|
|
204
|
+
case "arithmetic": {
|
|
205
|
+
const left = child(node.left);
|
|
206
|
+
const right = child(node.right);
|
|
207
|
+
const { op } = node;
|
|
208
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
209
|
+
const leftResult = left(context, resolvers, budget);
|
|
210
|
+
const rightResult = right(context, resolvers, budget);
|
|
211
|
+
if (leftResult.status === "indeterminate") return leftResult;
|
|
212
|
+
if (rightResult.status === "indeterminate") return rightResult;
|
|
213
|
+
return require_evaluator_operations.applyArithmetic(op, leftResult.value, rightResult.value);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
case "negate": {
|
|
217
|
+
const operand = child(node.operand);
|
|
218
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
219
|
+
const result = operand(context, resolvers, budget);
|
|
220
|
+
if (result.status === "indeterminate") return result;
|
|
221
|
+
return require_evaluator_operations.applyNegate(result.value);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
case "lookup": {
|
|
225
|
+
const keys = node.keys.map(handedOver);
|
|
226
|
+
const { table } = node;
|
|
227
|
+
const tableText = JSON.stringify(table);
|
|
228
|
+
const notFound = `no match found in lookup table ${tableText}`;
|
|
229
|
+
const notYetKnown = `the match in lookup table ${tableText} is not yet known`;
|
|
230
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
231
|
+
const results = keys.map((key) => key(context, resolvers, budget));
|
|
232
|
+
const keyValues = [];
|
|
233
|
+
for (const result of results) {
|
|
234
|
+
if (result.status === "indeterminate") return result;
|
|
235
|
+
keyValues.push(result.value);
|
|
236
|
+
}
|
|
237
|
+
const resolution = require_evaluator_effect.settleSync(resolvers.resolveLookup(table, keyValues, context));
|
|
238
|
+
if (!resolution.found) return require_evaluator_operations.absenceEvaluation(resolution, notFound, notYetKnown);
|
|
239
|
+
return require_evaluation.definite(resolution.value);
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
case "conditional": {
|
|
243
|
+
const fallback = child(node.fallback);
|
|
244
|
+
const cases = node.cases.map(({ when, then }) => ({
|
|
245
|
+
when: this.predicate(when, depth + 1),
|
|
246
|
+
then: child(then)
|
|
247
|
+
}));
|
|
248
|
+
if ((node.hitPolicy ?? "first") === "first") return this.charged(depth, (context, resolvers, budget) => {
|
|
249
|
+
for (const { when, then } of cases) {
|
|
250
|
+
const whenResult = when(context, resolvers, budget);
|
|
251
|
+
if (whenResult.status === "indeterminate") return whenResult;
|
|
252
|
+
if (whenResult.value) return then(context, resolvers, budget);
|
|
253
|
+
}
|
|
254
|
+
return fallback(context, resolvers, budget);
|
|
255
|
+
});
|
|
256
|
+
return this.charged(depth, (context, resolvers, budget) => {
|
|
257
|
+
const whenResults = cases.map(({ when }) => when(context, resolvers, budget));
|
|
258
|
+
let matches = 0;
|
|
259
|
+
let matched;
|
|
260
|
+
cases.forEach(({ then }, index) => {
|
|
261
|
+
const whenResult = whenResults[index];
|
|
262
|
+
if (whenResult?.status === "definite" && whenResult.value) {
|
|
263
|
+
matches += 1;
|
|
264
|
+
matched = then;
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
if (matches >= 2) return require_evaluation.indeterminate("domain-error", "more than one case matched under the 'unique' hit policy");
|
|
268
|
+
const reason = require_evaluation.firstIndeterminate(...whenResults);
|
|
269
|
+
if (reason !== void 0) return {
|
|
270
|
+
status: "indeterminate",
|
|
271
|
+
reason
|
|
272
|
+
};
|
|
273
|
+
return (matched ?? fallback)(context, resolvers, budget);
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
case "delegate": {
|
|
277
|
+
const { system, payload } = node;
|
|
278
|
+
const notFound = `delegate handler for external system '${system}' reported no value`;
|
|
279
|
+
const notYetKnown = `the value from the delegate handler for external system '${system}' is not yet known`;
|
|
280
|
+
const noHandler = `no delegate handler registered for external system '${system}'`;
|
|
281
|
+
return this.charged(depth, (context, resolvers) => {
|
|
282
|
+
if (resolvers.resolveDelegate === void 0) return require_evaluation.indeterminate("wrong-type", noHandler);
|
|
283
|
+
const resolution = require_evaluator_effect.settleSync(resolvers.resolveDelegate(system, payload, context));
|
|
284
|
+
if (!resolution.found) return require_evaluator_operations.absenceEvaluation(resolution, notFound, notYetKnown);
|
|
285
|
+
return require_evaluation.definite(resolution.value);
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
case "fold":
|
|
289
|
+
case "treeReference": return this.interpretedValue(node, depth);
|
|
290
|
+
default: return this.interpretedValue(node, depth);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
/**
|
|
295
|
+
* Compiles `node` to a function that evaluates it over the resolvers of each call, with the same `Evaluation` as the synchronous interpreter: status, value, and the reason an indeterminate result carries, including which of several candidate reasons it is. The tree is walked once here, so each call does no per-node dispatch, builds no generator or suspension objects, and precomputes everything that depends only on the tree (the messages of absence, the literal values).
|
|
296
|
+
*
|
|
297
|
+
* Compiled natively: `not`, `and`, `or`, `allOf`, `anyOf`, `compare`, `textCompare`, `memberOf` and `exists`, and the expression kinds `reference`, `accumulator`, every literal, `arithmetic`, `negate`, `call`, `lookup`, `conditional` (both hit policies) and `delegate`. Left to the interpreter, as a subtree evaluated by `evaluatePredicateInternal` or `evaluateValueInternal` with the same budget: `some`, `every`, `fold` and `treeReference`, since each re-evaluates a subtree per item or per resolved tree. A tree containing any of them, or more nodes than `maxNodes`, charges the budget at every node exactly as the interpreter does, so a limit is met at the same node; any other tree cannot exhaust either limit and skips the charge.
|
|
298
|
+
*
|
|
299
|
+
* Operands are evaluated in the interpreter's synchronous order, and every operand is evaluated even when an earlier one has settled the result, so the resolvers are called in the same sequence as `evaluatePredicate` calls them. A resolver that returns a promise makes the call throw, as it does there.
|
|
300
|
+
*
|
|
301
|
+
* Precondition: `node` is a well-formed `PredicateNode` and is not mutated after this call, since what the tree fixes is read here once. Errors: none at compile time; a call throws exactly when the interpreter would (a resolver or registered function that throws, a promise from a resolver).
|
|
302
|
+
*/
|
|
303
|
+
function compilePredicateTree(node, functions, maxNodes, maxNestingDepth) {
|
|
304
|
+
const uncounted = new NodeCompiler(functions, maxNestingDepth, false);
|
|
305
|
+
const uncountedRun = uncounted.predicate(node, 0);
|
|
306
|
+
if (!uncounted.interpreted && uncounted.nodesCompiled <= maxNodes) return (context, resolvers) => uncountedRun(context, resolvers, UNLIMITED_BUDGET);
|
|
307
|
+
const countedRun = new NodeCompiler(functions, maxNestingDepth, true).predicate(node, 0);
|
|
308
|
+
return (context, resolvers) => countedRun(context, resolvers, require_evaluator_budget.createEvaluationBudget(maxNodes, maxNestingDepth));
|
|
309
|
+
}
|
|
310
|
+
//#endregion
|
|
311
|
+
exports.compilePredicateTree = compilePredicateTree;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { FunctionRegistry } from "./functions.cjs";
|
|
2
|
+
import { PredicateNode } from "./tree.cjs";
|
|
3
|
+
import { Evaluation } from "./evaluation.cjs";
|
|
4
|
+
import { EvaluationContext, SyncResolvers } from "./resolvers.cjs";
|
|
5
|
+
//#region src/evaluator-compiled.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* A predicate tree prepared for repeated evaluation: the same result as `createSyncEvaluator`'s `evaluatePredicate` over the tree it was compiled from, for any context and resolvers.
|
|
8
|
+
*/
|
|
9
|
+
export type CompiledPredicate = (context: EvaluationContext, resolvers: Readonly<SyncResolvers>) => Evaluation<boolean>;
|
|
10
|
+
/**
|
|
11
|
+
* Compiles `node` to a function that evaluates it over the resolvers of each call, with the same `Evaluation` as the synchronous interpreter: status, value, and the reason an indeterminate result carries, including which of several candidate reasons it is. The tree is walked once here, so each call does no per-node dispatch, builds no generator or suspension objects, and precomputes everything that depends only on the tree (the messages of absence, the literal values).
|
|
12
|
+
*
|
|
13
|
+
* Compiled natively: `not`, `and`, `or`, `allOf`, `anyOf`, `compare`, `textCompare`, `memberOf` and `exists`, and the expression kinds `reference`, `accumulator`, every literal, `arithmetic`, `negate`, `call`, `lookup`, `conditional` (both hit policies) and `delegate`. Left to the interpreter, as a subtree evaluated by `evaluatePredicateInternal` or `evaluateValueInternal` with the same budget: `some`, `every`, `fold` and `treeReference`, since each re-evaluates a subtree per item or per resolved tree. A tree containing any of them, or more nodes than `maxNodes`, charges the budget at every node exactly as the interpreter does, so a limit is met at the same node; any other tree cannot exhaust either limit and skips the charge.
|
|
14
|
+
*
|
|
15
|
+
* Operands are evaluated in the interpreter's synchronous order, and every operand is evaluated even when an earlier one has settled the result, so the resolvers are called in the same sequence as `evaluatePredicate` calls them. A resolver that returns a promise makes the call throw, as it does there.
|
|
16
|
+
*
|
|
17
|
+
* Precondition: `node` is a well-formed `PredicateNode` and is not mutated after this call, since what the tree fixes is read here once. Errors: none at compile time; a call throws exactly when the interpreter would (a resolver or registered function that throws, a promise from a resolver).
|
|
18
|
+
*/
|
|
19
|
+
export declare function compilePredicateTree(node: PredicateNode, functions: Readonly<FunctionRegistry>, maxNodes: number, maxNestingDepth: number): CompiledPredicate;
|
|
20
|
+
//#endregion
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { FunctionRegistry } from "./functions.js";
|
|
2
|
+
import { PredicateNode } from "./tree.js";
|
|
3
|
+
import { Evaluation } from "./evaluation.js";
|
|
4
|
+
import { EvaluationContext, SyncResolvers } from "./resolvers.js";
|
|
5
|
+
//#region src/evaluator-compiled.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* A predicate tree prepared for repeated evaluation: the same result as `createSyncEvaluator`'s `evaluatePredicate` over the tree it was compiled from, for any context and resolvers.
|
|
8
|
+
*/
|
|
9
|
+
export type CompiledPredicate = (context: EvaluationContext, resolvers: Readonly<SyncResolvers>) => Evaluation<boolean>;
|
|
10
|
+
/**
|
|
11
|
+
* Compiles `node` to a function that evaluates it over the resolvers of each call, with the same `Evaluation` as the synchronous interpreter: status, value, and the reason an indeterminate result carries, including which of several candidate reasons it is. The tree is walked once here, so each call does no per-node dispatch, builds no generator or suspension objects, and precomputes everything that depends only on the tree (the messages of absence, the literal values).
|
|
12
|
+
*
|
|
13
|
+
* Compiled natively: `not`, `and`, `or`, `allOf`, `anyOf`, `compare`, `textCompare`, `memberOf` and `exists`, and the expression kinds `reference`, `accumulator`, every literal, `arithmetic`, `negate`, `call`, `lookup`, `conditional` (both hit policies) and `delegate`. Left to the interpreter, as a subtree evaluated by `evaluatePredicateInternal` or `evaluateValueInternal` with the same budget: `some`, `every`, `fold` and `treeReference`, since each re-evaluates a subtree per item or per resolved tree. A tree containing any of them, or more nodes than `maxNodes`, charges the budget at every node exactly as the interpreter does, so a limit is met at the same node; any other tree cannot exhaust either limit and skips the charge.
|
|
14
|
+
*
|
|
15
|
+
* Operands are evaluated in the interpreter's synchronous order, and every operand is evaluated even when an earlier one has settled the result, so the resolvers are called in the same sequence as `evaluatePredicate` calls them. A resolver that returns a promise makes the call throw, as it does there.
|
|
16
|
+
*
|
|
17
|
+
* Precondition: `node` is a well-formed `PredicateNode` and is not mutated after this call, since what the tree fixes is read here once. Errors: none at compile time; a call throws exactly when the interpreter would (a resolver or registered function that throws, a promise from a resolver).
|
|
18
|
+
*/
|
|
19
|
+
export declare function compilePredicateTree(node: PredicateNode, functions: Readonly<FunctionRegistry>, maxNodes: number, maxNestingDepth: number): CompiledPredicate;
|
|
20
|
+
//#endregion
|