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

Function canonicalLoop

src/compute-engine/library/control-structures.ts:244–314  ·  view source on GitHub ↗

* Canonicalize a Loop expression. * * Two forms are supported: * * - Variadic Element form (new): `Loop(body, Element(x, L1), Element(y, L2), ...)`. * Push a fresh scope with `noAutoDeclare = true`, declare each Element's * index variable in that scope, and canonicalize each clause + body

(
  ops: ReadonlyArray<Expression>,
  options: { engine: ComputeEngine; scope: Scope | undefined }
)

Source from the content-addressed store, hash-verified

242const BOOLEAN_MASK_TYPE = parseType('collection<boolean>')!;
243
244/**
245 * The collection types whose values are always FINITE: a list (and a
246 * dimensioned `vector`, `matrix` or tensor, which are lists), a dictionary, a
247 * record, and a `range`. A `Range` whose length is not known to be finite
248 * (`Range(1, ∞)`, `Range(1, n)`) is typed `indexed_collection<integer>`, not
249 * `range`. A `set` is not in this family: `Integers` is a `set<integer>` and
250 * has no end.
251 */
252const FINITE_COLLECTION_TYPE = parseType(
253 'list<any> | dictionary<any> | range'
254)!;
255
256/**
257 * Whether a restriction carrier of type `type` is an ORDERED collection that
258 * a mask condition zips element by element: a list, a vector, a matrix, a
259 * range or any other `indexed_collection<T>`, finite or not. A type with a
260 * tuple arm (a point) or a string arm is excluded, also when that arm is one
261 * arm of a union such as `tuple<number, number> | list<tuple<number,
262 * number>>`: such types are indexed collections, but a value of the tuple
263 * or string arm is one value under a restriction, never zipped. A `set`, a
264 * dictionary and a record are not indexed and are excluded too.
265 *
266 * A mask condition of length `k` pairs such a carrier with the mask over the
267 * MASK's length: cell `i` is element `i` of the carrier where the mask is
268 * true, and the absence marker of that element where it is false. The
269 * carrier is read at the first `k` positions only, so an infinite carrier
270 * such as `Range(1, ∞)` is never materialized (user decision 2026-09-29).
271 *
272 * `evaluateRestriction()` and `restrictionCollectionHandlers()` both
273 * call this predicate with the static type of the carrier OPERAND as written
274 * (a symbol's declared type, not the type of the value it holds), so the held
275 * form and the evaluated form decide the same way.
276 */
277function isOrderedCarrierType(type: Type): boolean {
278 return (
279 isSubtype(type, INDEXED_COLLECTION_SHAPE_TYPE) && !hasTupleOrStringArm(type)
280 );
281}
282
283/**
284 * Whether the value `value` held by a restriction carrier whose operand has
285 * the static type `carrierType` is ONE value that a list of conditions never
286 * splits into its elements. A string is always one value. A tuple (a point)
287 * is one value, except when the carrier's static type is ordered
288 * (`isOrderedCarrierType`): a symbol declared `indexed_collection<number>`
289 * that holds `(1, 2)` is a collection of numbers by its declaration, so the
290 * tuple is zipped with the mask. The compiled `_SYS.restrict` agrees: it
291 * aligns an array with the mask and masks any other value whole, and a
292 * string is not an array at run time.
293 */
294function isAtomicCarrierValue(carrierType: Type, value: Expression): boolean {
295 if (isString(value)) return true;
296 return isTuple(value) && !isOrderedCarrierType(carrierType);
297}
298
299/**
300 * The first `k` elements of the ORDERED carrier `value`, read with ONE
301 * iterator that stops after `k` elements, so an infinite or very long

Callers

nothing calls this directly

Calls 10

isFunctionFunction · 0.90
isSymbolFunction · 0.90
_fnMethod · 0.65
sliceMethod · 0.65
mapMethod · 0.65
pushScopeMethod · 0.65
errorMethod · 0.65
hasMethod · 0.65
declareMethod · 0.65
popScopeMethod · 0.65

Tested by

no test coverage detected