MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / evaluateForAll

Function evaluateForAll

src/compute-engine/library/logic.ts:277–341  ·  view source on GitHub ↗

* Evaluate ForAll over a finite domain. * ∀x∈S. P(x) is true iff P(x) is true for all x in S. * * Symbolic simplifications: * - ∀x. True → True * - ∀x. False → False * - ∀x. P (where P doesn't contain x) → P * * Nested quantifiers: * - ∀x∈S. ∀y∈T. P(x,y) evaluates over the Cartesian product

(
  args: ReadonlyArray<Expression>,
  { engine: ce }: { engine: ComputeEngine }
)

Source from the content-addressed store, hash-verified

275 ops: ReadonlyArray<Expression>,
276 options: EvaluateHandlerOptions
277) => Promise<Expression | undefined> {
278 return async (ops, options) => {
279 const ce = options.engine;
280 const evalOptions = evaluateOptionsOf(options);
281 const elementwise = ops.some(isCollectionShaped);
282 const values: Expression[] = [];
283 let failed: Expression | undefined = undefined;
284 for (let i = 0; i < ops.length; i++) {
285 const v = await ops[i].evaluateAsync(evalOptions);
286 if (!elementwise) {
287 const decided = decide(ce, v, i);
288 if (decided) return decided;
289 if (!v.isValid) {
290 failed ??= v;
291 continue;
292 }
293 }
294 values.push(v);
295 }
296 if (failed) return failed;
297 return finishShortCircuit(
298 ce,
299 name,
300 ops,
301 values,
302 decide,
303 reduce,
304 evalOptions
305 );
306 };
307}
308
309type ShortCircuitOperator = 'And' | 'Or' | 'Nand' | 'Nor' | 'Implies';
310
311/** Does the evaluated operand `v`, at position `i`, decide the result by
312 * itself? Returns the result if so, `undefined` otherwise. */
313type Decider = (
314 ce: ComputeEngine,
315 v: Expression,
316 i: number
317) => Expression | undefined;
318
319/** The order-independent symbolic reducer applied to the surviving values. */
320type Reducer = (
321 args: ReadonlyArray<Expression>,
322 options: { engine: ComputeEngine }
323) => Expression | undefined;
324
325/** Is `v` `False`, or an absent value (`Missing`), which a
326 * connective reads as `False`? The logic is two-valued: an absent value is
327 * not a third truth value (user decision 2026-11-02, `docs/ERROR-MODEL.md`,
328 * two-valued comparisons). */
329function readsFalse(v: Expression): boolean {
330 const s = sym(v);
331 return s === 'False' || s === 'Missing';
332}
333/** `And` stops at the first `False` (result `False`). */
334const decideAnd: Decider = (ce, v) => (readsFalse(v) ? ce.False : undefined);

Callers 1

logic.tsFile · 0.85

Calls 9

symFunction · 0.90
isFunctionFunction · 0.90
bodyContainsVariableFunction · 0.90
collectNestedDomainsFunction · 0.90
evaluateForAllCartesianFunction · 0.90
getInnermostBodyFunction · 0.90
evaluateMethod · 0.65
subsMethod · 0.65

Tested by

no test coverage detected