| @@ -1768,3 +1768,27 @@ |
| 1768 | 1768 | - **Consequence:** `keyword_phrase_builtin_parses_in_call_position` test updated: the multi-word name `"date and time"` out of call position now correctly returns a `Function` value rather than null (it was null only because the name was unbound; now it resolves to the built-in). Unlocks 0092 cases 014 and 016. |
| 1769 | 1769 | - **Tests:** `builtin_as_first_class_value` and `builtin_passed_as_argument_and_invoked` in `lib.rs`. |
| 1770 | 1770 | - **Coverage:** +16 fixture cases (0092 testCases 001–014 incl. 010_a, and 016). Suite 2880 → 2896. |
| 1771 | + |
| 1772 | +## ADR-37: Higher-order built-ins — `sort` + matching `list replace` (DMN §10.3.4.9 / §10.3.4.4) |
| 1773 | + |
| 1774 | +- **Context:** `sort(list, precedes)` and the matching-function form of |
| 1775 | + `list replace(list, match, newItem)` have been deferred since ADR-15 (list |
| 1776 | + functions) and ADR-24 (list filter), noted again in ADR-36 (`sort/2`). Both take |
| 1777 | + a function-valued argument that must be **invoked inside the evaluator** — a |
| 1778 | + capability `builtins::call(name, args, env)` has never had: it receives only |
| 1779 | + `&EvalEnv`, not the `Evaluator` that owns `invoke`/`invoke_closure` and the |
| 1780 | + `call_depth` recursion guard. Function values now exist (ADR-23) and are |
| 1781 | + first-class (ADR-36), so this is a contained addition. Unlocks `0013-sort` (3 |
| 1782 | + cases), `0092-feel-lambda` testCases 017/018, and 6 previously-skipped function-form |
| 1783 | + cases in `1155-list-replace-function`. |
| 1784 | +- **Decision:** |
| 1785 | + - **`function.rs`:** Add `pub(crate) trait Applier { fn apply(&self, f: &Func, args: Args) -> Value; }` — a narrow seam mirroring `ForeignFunction`/`FunctionRegistry`. Collapses a `FeelError` (unreachable inside the evaluator) to `Value::Error`. |
| 1786 | + - **`eval.rs`:** `impl Applier for Evaluator<'_>` delegates to `self.invoke(f, args).unwrap_or(Value::Error)`, reusing the existing closure path (lexical scope binding via `bind_params`, recursion guard via `call_depth`). Both call sites that pass `self` to `builtins::call` are updated to supply `self` as the applier. |
| 1787 | + - **`builtins.rs`:** `call` signature gains `applier: &dyn Applier`. Two new functions: |
| 1788 | + - **`sort_fn(args, applier)`** — `[list, precedes]`. Null list → null; non-list → error; non-function second arg → error. Closure arity guard rejects a non-2-param predicate before invoking. Stable insertion sort: move `items[i]` left while `precedes(items[i], items[i-1])` returns `Boolean(true)`, stop on `Boolean(false)`, any other result → error for the whole call. |
| 1789 | + - **`list_replace_fn(args, applier)`** — dispatches on whether `args[1]` is `Value::Function`: matching form (iterate, apply `match(item, newItem)`, replace on `true`, error on non-boolean; closure arity-2 guard) or positional form (existing implementation, unchanged). Named `match:` overload routed in `reorder_named` by checking whether the named args contain the key `"match"` before the generic table lookup. |
| 1790 | + - **Registration:** `"sort"` added to `is_builtin`, `call` dispatch (list section), and `builtin_param_names("sort") ⇒ &["list", "precedes"]`. `builtin_param_names("list replace")` entry documents that `reorder_named` handles the dual overload. |
| 1791 | + - **Wrapper crates:** Fixed missing `Func::Builtin(_)` arms in `feel-wasm`, `feel-py`, `feel-lua` function-value marshal paths (gap left by ADR-36). All three treat a built-in ref like a native closure — cross out as `null`/`nil`/`None` (no host representation). |
| 1792 | + - **Ingester:** `"sort"` added to `_BUILTINS`. `fits_slice_grammar` gains a function-literal extraction step before the free-name scan: `\bfunction\s*\(bare_params\)` patterns are found, param names collected into `fn_params` (added to `allowed`), and the head blanked from `scan`. Only untyped bare-identifier param lists match; a `:` annotation keeps the expression outside auto-ingest. |
| 1793 | +- **Tests:** `sort_ascending_descending`, `sort_by_context_field`, `sort_stable`, `sort_named_args`, `sort_as_first_class_builtin`, `sort_error_cases`, `sort_higher_order_closure_over_input`, `list_replace_matching_form`, `list_replace_matching_named_args`, `list_replace_matching_error_cases` in `lib.rs` `function_tests`. |
| 1794 | +- **Consequence:** Resolves the `sort`/`list replace` matching-form deferrals at ADR-15 (L331), ADR-24 (L779), the roadmap "Smaller deferrals" note, and ADR-36 (L1761). **Suite 2896 → 2907.** |