(args: ReadonlyArray<Expression>)
| 486 | * reading of `x(2)` is already made there. */ |
| 487 | liftedBody?: boolean; |
| 488 | } |
| 489 | ): Expression | undefined { |
| 490 | if (!expr) return undefined; |
| 491 | |
| 492 | // |
| 493 | // 0/ A string literal is never a function. Without this guard a string |
| 494 | // falls through to the shorthand path below and becomes a constant |
| 495 | // nullary function `() ↦ "s"`, so e.g. `Map("nf", [1,2,3])` would map to |
| 496 | // `["nf","nf","nf"]` instead of being rejected. |
| 497 | // |
| 498 | if (isString(expr)) return undefined; |
| 499 | |
| 500 | // |
| 501 | // 1/ Canonical function literal |
| 502 | // |
| 503 | if (expr.operator === 'Function' && expr.isCanonical) return expr; |
| 504 | |
| 505 | // |
| 506 | // 2/ If a symbol, e.g. "Sin", return unchanged |
| 507 | // When evaluating, the type of the symbol need to be checked to |
| 508 | // make sure it's a function |
| 509 | // |
| 510 | // Exception: the BARE wildcard `_` is the identity-function shorthand |
| 511 | // (`Map(_, xs)` ≡ `Map(x ↦ x, xs)`). It falls through to the shorthand |
| 512 | // path below, which turns it into `(_1) ↦ _1`. Only the bare `_` |
| 513 | // qualifies: `_1`/`_2`/… are positional parameters of an ENCLOSING |
| 514 | // shorthand, and a named wildcard is a pattern variable — neither is an |
| 515 | // identity function. |
| 516 | // |
| 517 | // A raw symbol with no definition is a free name: its canonical form |
| 518 | // records the scope it is boxed in, from which a callback reads its |
| 519 | // definition when it is applied (`callableBindingInContext()`). Boxing |
| 520 | // declares no name, so the canonical form writes nothing. |
| 521 | if (isSymbol(expr) && !isSymbol(expr, '_')) { |
| 522 | const symbol: Expression = expr; |
| 523 | return !symbol.isCanonical && |
| 524 | symbol.engine._resolveOnlyDepth === 0 && |
| 525 | symbol.engine.lookupDefinition(sym(symbol)!) === undefined |
| 526 | ? symbol.canonical |
| 527 | : symbol; |
| 528 | } |
| 529 | |
| 530 | // |
| 531 | // 3/ `BuiltinFunction`, e.g. ["BuiltinFunction", "Sin"] |
| 532 | // This operator is just a "tag" indicating the nature of the |
| 533 | // symbol. |
| 534 | // |
| 535 | if (isFunction(expr, 'BuiltinFunction')) return expr.op1; |
| 536 | |
| 537 | // |
| 538 | // 3b/ A qualified name, e.g. ["Module", "Floor"]: its canonical form is |
| 539 | // the symbol bound to the library definition (or an error when the |
| 540 | // name is not in the library), which is a function operand as the |
| 541 | // bare symbol is in step 2. |
| 542 | // |
| 543 | if (isFunction(expr, 'Module')) return expr.canonical; |
| 544 | |
| 545 | // |
nothing calls this directly
no test coverage detected