Public repository: timo / feel

Reader

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

docs: record ADR-23 (user-defined functions) and update README

public

docs: record ADR-23 (user-defined functions) and update README

ADR-23 documents the design: a single Value::Function unifying native FEEL
closures and host callables behind the ForeignFunction seam; dispatch precedence
and unforced arguments (reusing ADR-21 laziness); lenient arity; named arguments
as a tolerant mixed superset of spec rule 39 (mapping onto Python *args/**kwargs)
including named args for built-ins; the two host-injection paths and their
per-wrapper mapping (Python kwargs, JS options object, feel-pg none); and why
DMN-TCK 0030/0031 are hand-written rather than ingested. README lists UDFs and
named built-in arguments as supported and narrows the out-of-scope list.

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

modified DECISIONS.md +56 −0

@@ -648,6 +648,62 @@
648648
   I2 (ADR-1) relaxes here exactly as it does elsewhere — no Lua regex (RD-7), no native
649649
   Lua temporal (RD-8) — both loud and documented, neither a surprise.
650650
 
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
+
651707
 ## Adding a wrapper — the host-binding contract
652708
 
653709
 The four wrappers (feel-pg, feel-wasm, feel-py, planned feel-lua) implement the same

modified README.md +8 −2

@@ -29,7 +29,12 @@
2929
   `a..b`), and variable references.
3030
 - **Built-in functions:** single- and multi-word string / list / numeric /
3131
   context functions (`abs`, `string length`, `distinct values`, `get value`, …;
32
-  ADR-15/ADR-17).
32
+  ADR-15/ADR-17), callable positionally or with named arguments
33
+  (`substring(string: "foobar", start position: 3)`; ADR-23).
34
+- **User-defined functions:** FEEL `function(a, b) a + b` closures, and
35
+  host-provided functions — a Python callable or JS function passed in the context
36
+  (or an ambient `functions` registry) and invoked from the expression, with
37
+  positional / named / mixed arguments (Python kwargs, JS options object; ADR-23).
3338
 - **Temporal types:** `date`, `time`, `date and time`, and the two durations,
3439
   with `@"…"` literals, arithmetic / comparison / component access (`.year`,
3540
   `.time offset`, …), the temporal constructor and component built-ins
@@ -41,7 +46,8 @@
4146
   vs the ~479 KB offset-only default, where a named zone evaluates to null).
4247
 
4348
 Still out of scope: unary tests (the non-interval `in` forms), special characters
44
-in names, and user-defined functions. See [DECISIONS.md](DECISIONS.md) for the
49
+in names, typed function parameters, and general invocation on an arbitrary
50
+expression (`expr(args)`, imported `lib.fn(args)`). See [DECISIONS.md](DECISIONS.md) for the
4551
 architecture decisions, the deferred-scope roadmap, and accepted deviations
4652
 (notably: numbers are `f64`, not spec `decimal128`; type errors evaluate to
4753
 `null`; temporal values outside jiff's range are skipped) — plus the