Public repository: timo / feel

Reader

An interpreter of FEEL, written in Rust, with integrations for PostgreSQL, WASM, Lua, and Python.

docs: ADR-37 + ROADMAP — higher-order built-ins (sort + matching list replace)

public

docs: ADR-37 + ROADMAP — higher-order built-ins (sort + matching list replace)

ADR-37 in DECISIONS.md: Applier seam, sort/list-replace matching design,
ingester extension, wrapper-crate Func::Builtin fix, suite delta 2896 → 2907.

ROADMAP.md: convert the sort/list-replace deferral bullet under "Smaller,
independent deferrals" into a ### Shipped: entry (ADR-37) with the suite
count; update the 0092 shipped entry to reference ADR-37 for 017/018.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Changeset820ab8a62c7fc69a963f6c4cdeaa953e5f81407c
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parentsef2d86f96bcb
Children
Pushlog
by timo via ssh

modified DECISIONS.md +24 −0

@@ -1768,3 +1768,27 @@
17681768
 - **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.
17691769
 - **Tests:** `builtin_as_first_class_value` and `builtin_passed_as_argument_and_invoked` in `lib.rs`.
17701770
 - **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.**

modified ROADMAP.md +13 −6

@@ -196,7 +196,7 @@
196196
 names (e.g. `abs`, `sqrt`) evaluate to `Value::Function(Func::Builtin(name))` as
197197
 first-class values, enabling them to be passed as arguments and invoked through closure
198198
 parameters. Together these unlock DMN-TCK `0092-feel-lambda` testCases 001–014 and 016
199
-(17/18; 015 is TCK-commented-out; 017/018 need `sort(list, function)` — deferred below).
199
+(015 is TCK-commented-out; 017/018 needed `sort(list, function)` — shipped in ADR-37).
200200
 Fixture is hand-written (`crates/feel-core/tests/fixtures/0092-feel-lambda.json`),
201201
 mirroring the 0030 pattern. **Suite 2880 → 2896**.
202202
 
@@ -227,13 +227,20 @@
227227
 
228228
 ---
229229
 
230
+### Shipped: higher-order `sort` + matching `list replace` (ADR-37)
231
+
232
+An `Applier` trait threads the evaluator's function-apply capability (invoke +
233
+recursion-depth guard) into `builtins::call`, enabling `sort(list, precedes)` and
234
+the matching form of `list replace(list, match, newItem)`. Stable insertion sort for
235
+`sort`; dispatch on `Value::Function` in `args[1]` for `list replace`. Ingester
236
+extended: `"sort"` added to `_BUILTINS`; `fits_slice_grammar` blanks untyped
237
+`function(...)` literals and adds their params to the allowed-name set, enabling
238
+auto-ingest of `0013-sort` and the 6 previously-skipped function-form cases in
239
+`1155-list-replace-function`. `0092-feel-lambda` testCases 017/018 hand-written.
240
+**Suite 2896 → 2907.**
241
+
230242
 ## Smaller, independent deferrals
231243
 
232
-- **`sort(list, function)`** and the matching-function form of `list replace`
233
-  (`0013-sort`, and `0092-feel-lambda` testCases 017/018) — need a higher-order
234
-  `sort` built-in whose comparator is invoked inside the evaluator. Function values
235
-  already exist (ADR-23) and built-ins are now first-class (ADR-36), so this is a
236
-  contained built-in addition.
237244
 - **General invocation** `expr(args)` / `lib.fn(args)` (e.g. `null()` in
238245
   `1131-feel-function-invocation`) — invoking an arbitrary expression, not just a
239246
   named function. Out of slice (ADR-23).