| @@ -648,6 +648,62 @@ |
| 648 | 648 | I2 (ADR-1) relaxes here exactly as it does elsewhere — no Lua regex (RD-7), no native |
| 649 | 649 | Lua temporal (RD-8) — both loud and documented, neither a surprise. |
| 650 | 650 | |
| 651 | +## ADR-23: User-defined functions — FEEL closures and host callables |
| 652 | + |
| 653 | +- **Context:** the engine could only call built-ins; FEEL's own `function(a, b) body` |
| 654 | + literal (DMN-TCK level 3, 0030/0031) and — the bigger driver for this project — |
| 655 | + *host-provided* functions (a JS function, a Python callable passed into evaluation) |
| 656 | + had no representation. The seam was already anticipated: ADR-21's `ForeignObject` is |
| 657 | + the object analogue, and the builtins note already required a UDF call to bind its |
| 658 | + arguments *unforced*. |
| 659 | +- **Decision:** one first-class `Value::Function(Arc<Func>)` where `Func` is either a |
| 660 | + native `Closure { params, body, captured }` (a `function(…)` literal, captured with |
| 661 | + its definition scope — FEEL functions are lexical closures, spec §10.3.2.9) or a |
| 662 | + `Foreign(Arc<dyn ForeignFunction>)` host callable. `ForeignFunction` is the function |
| 663 | + analogue of `ForeignObject`: std-only, target-agnostic, constructed only by wrappers |
| 664 | + (ADR-1 holds). A single `Arc` keeps the variant one word, so `Value` stays 40 bytes |
| 665 | + (the size-regression test pins it). |
| 666 | + - **Grammar:** `function` becomes a keyword; the literal is a loosest-binding form |
| 667 | + (body extends right, like `if`). A unified `Arg` rule parses positional (`expr`) and |
| 668 | + named (`name: expr`) arguments — clean LR(1), the `:` lookahead decides — into |
| 669 | + `CallArgs { positional, named }`. |
| 670 | + - **Dispatch** (in eval): a function-valued *context binding* wins first (so a binding |
| 671 | + can shadow a built-in — host override), then the built-in, then the ambient |
| 672 | + `FunctionRegistry` on `EvalEnv`, else error-null. Arguments are evaluated **unforced** |
| 673 | + so ADR-21 path laziness flows through a call (built-ins force internally; closures and |
| 674 | + host callables receive them lazy). |
| 675 | + - **Binding** is lenient vs. spec §10.3.2.13.5: an unsupplied parameter is value-null |
| 676 | + (the spec's named-arg rule, extended to positional under-supply — no strict arity), |
| 677 | + while genuine non-conformance (unknown name, double-bind, too many positional) is an |
| 678 | + error-null. A recursion depth cap turns runaway self-application into an error-null |
| 679 | + instead of a stack overflow. |
| 680 | + - **Named arguments are a deliberate, tolerant superset of spec rule 39:** the spec |
| 681 | + makes positional and named *exclusive*, but we allow them **mixed** (`f(2, b: 3)`), |
| 682 | + because that maps one-to-one onto Python's `f(*positional, **named)` and so reaches a |
| 683 | + host callable's positional-only *and* keyword-only parameters. Pure-positional and |
| 684 | + pure-named stay spec-conformant. Named arguments work for **built-ins** too (spec |
| 685 | + §10.3.2.13.5) via a per-built-in parameter-name table + reorder shim; the purely |
| 686 | + variadic aggregates (`min`/`max`/`sum`/…) have no named-parameter form and reject |
| 687 | + named calls. |
| 688 | + - **Host injection has two paths** (both, per the design choice): bind a callable |
| 689 | + directly as a context value, or pass an ambient `functions` registry. Per wrapper: |
| 690 | + feel-py maps named args to real Python **kwargs** (`f(*pos, **named)`); feel-wasm has |
| 691 | + no kwargs, so named args become a single trailing **options object** `{name: value}`; |
| 692 | + a returned host callable passes back out **by identity** (`as_any` downcast). feel-pg |
| 693 | + has **no** host-function seam (its input is JSONB, not host handles) — native |
| 694 | + `function(…)` literals still evaluate there, and a function reaching the JSONB output |
| 695 | + is null. |
| 696 | +- **Testing:** DMN-TCK 0030/0031 **cannot be auto-ingested** — their decisions are boxed |
| 697 | + `<context>`/`<functionDefinition>` (not literal expressions) and use named-arg and |
| 698 | + library-import forms outside the slice, so `tools/ingest_tck.py` skips them. The tests |
| 699 | + are therefore hand-written: feel-core unit tests + a hand-written `0030` fixture (each |
| 700 | + case carrying a `source` provenance note reconstructing the TCK semantics as a |
| 701 | + self-contained expression), plus pytest and Node smoke cases for the host paths. |
| 702 | +- **Consequence:** FEEL gains first-class functions across every target that has a host |
| 703 | + function concept, reusing ADR-21's laziness and identity pass-through wholesale. Still |
| 704 | + out of scope (documented): typed parameters `function(a: string)`, general invocation on |
| 705 | + an arbitrary expression (`expr(args)`, `lib.fn(args)`), and external/imported functions. |
| 706 | + |
| 651 | 707 | ## Adding a wrapper — the host-binding contract |
| 652 | 708 | |
| 653 | 709 | The four wrappers (feel-pg, feel-wasm, feel-py, planned feel-lua) implement the same |