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

Function compileGuards

scripts/fungrim/compile-rules.ts:279–524  ·  view source on GitHub ↗
(
  assumptions: MathJSON,
  variables: ReadonlyArray<string>
)

Source from the content-addressed store, hash-verified

277 assumptions: MathJSON,
278 variables: ReadonlyArray<string>
279): { guards: GuardSpec[] } | { error: string } {
280 const guards: GuardSpec[] = [];
281 const wc = (v: string) => '_' + v;
282 const W = (x: MathJSON) => wildcardize(x, variables);
283
284 const pushBound = (
285 v: string,
286 raw: MathJSON,
287 openOp: 'gt' | 'lt',
288 closedOp: 'ge' | 'le'
289 ): void => {
290 let bound = raw;
291 let open = false;
292 if (Array.isArray(bound) && bound[0] === 'Open') {
293 open = true;
294 bound = bound[1];
295 }
296 if (isInfiniteBound(bound)) return; // skip infinite bounds
297 guards.push({ k: 'cmp', wc: wc(v), op: open ? openOp : closedOp, bound: W(bound) });
298 };
299
300 // Same, for a part extractor of a variable (Element(Im(z), Interval(a,b)))
301 const pushPartBound = (
302 part: 're' | 'im' | 'abs' | 'arg',
303 v: string,
304 raw: MathJSON,
305 openOp: 'gt' | 'lt',
306 closedOp: 'ge' | 'le'
307 ): void => {
308 let bound = raw;
309 let open = false;
310 if (Array.isArray(bound) && bound[0] === 'Open') {
311 open = true;
312 bound = bound[1];
313 }
314 if (isInfiniteBound(bound)) return; // skip infinite bounds
315 guards.push({
316 k: 'part-cmp',
317 wc: wc(v),
318 part,
319 op: open ? openOp : closedOp,
320 bound: W(bound),
321 });
322 };
323
324 // Element(v, domain) for a bare entry variable v
325 const element = (v: string, dom: MathJSON): string | null => {
326 if (typeof dom === 'string') {
327 switch (dom) {
328 case 'Integers':
329 guards.push({ k: 'type', wc: wc(v), t: 'integer' });
330 return null;
331 case 'NonNegativeIntegers':
332 guards.push({ k: 'type', wc: wc(v), t: 'integer' });
333 guards.push({ k: 'cmp', wc: wc(v), op: 'ge', bound: 0 });
334 return null;
335 case 'PositiveIntegers':
336 guards.push({ k: 'type', wc: wc(v), t: 'integer' });

Callers 3

guardsFunction · 0.90
guardErrorFunction · 0.90
compileEntriesFunction · 0.85

Calls 6

flattenAndFunction · 0.85
partOfVariableFunction · 0.85
elementFunction · 0.85
pushPartBoundFunction · 0.85
WFunction · 0.85
wcFunction · 0.85

Tested by 2

guardsFunction · 0.72
guardErrorFunction · 0.72