Public repository: timo / feel

Reader

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

docs: ADR-48 (proposed) — resolution levels, backticks, adaptive compile, path members

draft feat/special-char-names

docs: ADR-48 (proposed) — resolution levels, backticks, adaptive compile, path members

Plans the ADR-47 follow-up: backtick-quoted names (feel-scala syntax,
always on), a `NameResolution` parse option (Quoted / Symbols / Full —
pg as argument, never GUC), a parse cache keyed by bound candidates that
makes `compile` scope-adaptive and lets `feel_eval_immutable` drop its
ERROR, and a static-path member probe. ROADMAP v2 becomes the A–D plan.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Changeset93b43c1c833ba3e4495214d24ef62daae035e072
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parents93c4c8ad8806
Children
Pushlog
by timo via ssh

modified DECISIONS.md +127 −0

@@ -2356,3 +2356,130 @@
23562356
   IMMUTABLE rejection). All suites pass (feel-pg via `cargo pgrx test pg18`). The research
23572357
   record is `docs/special-char-names-research.md`; the follow-up increment is decision-result
23582358
   binding in the ingester, which stands on its own feet (hyphen-free DRG chains need it too).
2359
+
2360
+## ADR-48: Name-resolution levels, backtick-quoted names, candidate-keyed parse cache, path-member probe
2361
+
2362
+- **Status:** *Proposed* (2026-10-14) — planned, not implemented. Builds on ADR-47.
2363
+
2364
+- **Context:** ADR-47 (v1) resolves special-character names against the root scope at parse
2365
+  time. Its review and follow-up left four limits:
2366
+  1. `compile()` is probe-free, so `compile("a-b").evaluate({"a-b": 5})` is null while
2367
+     `evaluate("a-b", {"a-b": 5})` is 5 — the compiled path cannot reach such names at all.
2368
+  2. feel-pg re-parses *every* row that binds a candidate (no cache), and `feel_eval_immutable`
2369
+     rejects such rows with an ERROR.
2370
+  3. Only root keys are probed: `applicant.first-name` stays `applicant.first - name` —
2371
+     probably the most common real-world shape (nested jsonb/JSON keys).
2372
+  4. Space-joined keyword names (`Profit and loss`, §10.3.1.6's own example) stay unsupported:
2373
+     treating keyword adjacency as runs gives nearly every condition candidates. The
2374
+     §10.3.1.4 name-start rule (runs start only at a `Name`) roughly halves them —
2375
+     `if a then b else c`: 15 → 6, `a and b or c`: 10 → 6 (hand-counted) — but they remain
2376
+     present in almost every condition. A name may still *end* with a keyword (`a then`):
2377
+     the spec allows it, so those candidates stay.
2378
+
2379
+  Cost model (measured with `name_run_candidates`): a run of *n* words yields n·(n−1)/2
2380
+  candidates; numbers, parentheses and comparison operators end a run. Typical expressions
2381
+  have 0–4 (`applicant.age >= 18`: 1; `price * quantity - discount`: 3); the worst realistic
2382
+  case is a name-only formula — `w1*x1 + … + w10*x10` is one 20-word run, 190 candidates. Per
2383
+  evaluation a probing host pays one scope lookup per candidate; a *re-parse* only when a
2384
+  candidate is bound.
2385
+
2386
+  Prior art, researched in the feel-scala source (Camunda): names are plain Java identifiers
2387
+  and are **never** resolved against the scope (`a-b` is always subtraction); special names
2388
+  are written in backticks — `` `a-b` ``, `` `first name` ``, `` order.`total price` `` —
2389
+  grammar `` "`" (!"`" AnyChar)+ "`" `` (≥ 1 char, no escape, content verbatim), accepted
2390
+  wherever a name is (references, path members, `for`/quantifier variables, parameters,
2391
+  context keys, call heads). Introduced in feel-scala 1.5.0 (2018, with `'`), switched to
2392
+  backticks in 1.8.0 (2019, PR #63). Camunda documents it as a *Camunda extension*; its own
2393
+  Modeler tooling rejects it (standard FEEL only) and points to `get value(ctx, "key")`.
2394
+  feel-scala also accepts space names unquoted when followed by `(` or `:` (call heads,
2395
+  parameter names, context keys) — a text-only lookahead.
2396
+
2397
+  Spec (§10.3.1.4): "A name start (grammar rule 26) SHALL NOT be a literal terminal symbol";
2398
+  "a name part MAY be". Implemented on the ADR-47 branch: runs start only at a `Name`
2399
+  (`x-in` is a name, `if-then` is not).
2400
+
2401
+- **Decision (planned):**
2402
+
2403
+  1. **Backtick-quoted names — a non-standard extension, always on.** Syntax identical to
2404
+     feel-scala, so Camunda expressions run unchanged. Lexed as a **dedicated token**
2405
+     (`Tok::QuotedName`) that becomes a name only in the grammar: it must never be joined by
2406
+     `join_names` (`` `a b` c `` is not `a b c`) nor fused by the ADR-47 pass
2407
+     (`` `a`-b `` is not `a-b`). Scope-independent — decided by the text alone — so it
2408
+     works in `compile`, in the feel-pg cache and in `feel_eval_immutable` at zero per-row
2409
+     cost, and in every slot the probe cannot reach: keyword names (`` `Profit and loss` ``),
2410
+     inner scopes (`` applicant.`first-name` ``, `` items[`unit-price` > 10] ``), declarations
2411
+     (`` {`a-b`: 1} ``, `` for `x-y` in … ``), `?` names (`` `what if?` ``). The ingester's
2412
+     slice scanner treats a quoted name as one name. Marked as an extension in README and
2413
+     docs.
2414
+
2415
+  2. **Name-resolution level as a parse option.** `#[non_exhaustive] enum NameResolution`:
2416
+     - `Quoted` — backticks only; no run fusion, no candidates. Camunda semantics; cheapest.
2417
+     - `Symbols` (**default**) — backticks plus ADR-47 runs over `- + * / .`.
2418
+     - `Full` — additionally keyword-joined names (`Profit and loss`); **deferred** (see
2419
+       below), the variant is reserved so it can land without an API break.
2420
+
2421
+     API: `ParseOptions { names: NameResolution }` on `compile`/`evaluate` variants in
2422
+     feel-core; a keyword argument in the wrappers (`feel.compile(expr, names="quoted")`);
2423
+     in feel-pg a **function argument with a default** (`name_resolution text DEFAULT
2424
+     'symbols'`) — **never a GUC**: a session setting would make `feel_eval_immutable`'s
2425
+     result depend on something other than its arguments and silently break expression
2426
+     indexes. The level is stored in `CompiledExpr`, shown in its `Debug`/`repr`, and part
2427
+     of every cache key. "Who wants spec compatibility pays with performance": `Full` costs
2428
+     most, `Quoted` (Camunda compatibility) costs nothing.
2429
+
2430
+  3. **Candidate-keyed parse cache; `compile` becomes scope-adaptive.** The parse is a pure
2431
+     function of *(text, level, which candidates are bound)* — fusion queries the probe only
2432
+     for `name_run_candidates`. `CompiledExpr` therefore keeps the source, the level, the
2433
+     candidates and a small bounded cache (≤ 8 entries, LRU, behind a `Mutex` — `Clone` and
2434
+     pyo3 `Send`) of parses keyed by the bound-candidate bitset. `evaluate` computes the
2435
+     bitset (one lookup per candidate; none when there are no candidates) and picks or
2436
+     builds the matching parse. Consequences:
2437
+     - `compile` and `evaluate` agree; the compiled wrapper classes get it for free. This
2438
+       **reverses ADR-47 point 3** (probe-free compile); `has_name_runs()` becomes obsolete.
2439
+     - feel-pg keeps the cache in `fn_extra`: no more per-row re-parse, one parse per
2440
+       distinct bitset.
2441
+     - `feel_eval_immutable` **drops the ERROR**: the result remains a pure function of
2442
+       `(expr, context, level)`, which is all IMMUTABLE requires; a row that binds `a-b`
2443
+       indexes the name reading, as the spec demands for that row. Documented.
2444
+     - `FunctionRegistry::contains(name)` (default `get(name).is_some()`, overridden
2445
+       allocation-free in py/lua/wasm) so `ScopeWithFunctions` no longer allocates an `Arc`
2446
+       — or crosses into Python — per candidate per evaluation.
2447
+     - Guard test: **no built-in name contains a name symbol** (`- + * / .`). Built-ins are
2448
+       in scope, so a future built-in matching a candidate would silently change parses —
2449
+       and stored index values.
2450
+     - Benchmark: the 190-candidate formula (worst case) and `applicant.age >= 18` (the
2451
+       RD-10 fast path) against today's numbers.
2452
+
2453
+  4. **Path-member probe (ROADMAP v2, inner scopes).** `ScopeProbe::contains_member(path,
2454
+     name)` with a default of `false` (non-breaking). When a member run follows a *static*
2455
+     path chain from a root name (`applicant.`, `p.q.`), the lexer asks the probe, which
2456
+     navigates the context value; candidates become path-qualified
2457
+     (`(["applicant"], "first-name")`) and take part in the bitset of 3. A list-valued step
2458
+     (`orders.unit-price`) checks **any** element (deterministic; "first element" breaks on
2459
+     empty or heterogeneous lists). Dynamic bases (`f(x).a-b`, `(e).a-b`) stay arithmetic —
2460
+     backticks cover them. Filter-element keys (`items[unit-price > 10]`) stay deferred;
2461
+     backticks cover them too. Cost: one jsonb navigation per path-qualified candidate per
2462
+     row — benchmarked against the plain path case.
2463
+
2464
+  **Order:** (A) 1 + 2 with `Quoted`/`Symbols` — `Quoted` is nearly free (skip the fusion
2465
+  pass); (B) 3; (C) 4; (D) `Full` only on concrete demand, after measuring whether the TCK
2466
+  uses unquoted keyword names at all. Deciding which keywords may join (`in` must not —
2467
+  `for x in y`) and how `Full` interacts with the fixed joins (`date and time`, ADR-26) is
2468
+  part of D; excluding `in` as a name part (as Drools does) removes loop headers from
2469
+  `Full` candidates entirely.
2470
+
2471
+- **Rejected / deferred:**
2472
+  - *GUC for the level* — breaks IMMUTABLE (see 2).
2473
+  - *`Full` now* — cost and ambiguity for a shape with no measured demand; backticks give a
2474
+    standard-adjacent escape meanwhile.
2475
+  - *Escape for a backtick inside a quoted name* — feel-scala has none; staying identical
2476
+    keeps expressions portable.
2477
+  - *Schema-based probe* (DMN typeRefs/ItemDefinitions — the spec-intended way to know member
2478
+    names, and the one that would make `compile` sound without a context) — needs a type
2479
+    model the engine does not have.
2480
+  - *Compile-time name lists* (`compile(expr, names=[…])`) — subsumed by 3.
2481
+
2482
+- **Consequence (expected):** one expression text can mean different things per level —
2483
+  intended and made visible. Fixture suite runs at `Symbols`; the special-name tests run at
2484
+  all levels. Camunda-written expressions run unchanged (`Quoted` or `Symbols`); spec-full
2485
+  keyword names remain the one documented gap until D.

modified ROADMAP.md +32 −29

@@ -316,35 +316,38 @@
316316
 documented scope-blindness); `?`/non-XID name starts (`what if?`, rule 28 — the
317317
 0083-feel-unicode bucket); names in inner scopes (see v2 below).
318318
 
319
-### v2: names in inner scopes (open)
320
-
321
-The probe sees only the **root level**. A special-character key one level down
322
-keeps its arithmetic reading — `applicant.first-name` parses as
323
-`applicant.first - name`, `items[unit-price > 10]` compares `unit - price`.
324
-For jsonb rows (feel-pg) and nested JSON in the wrappers this is likely the
325
-most common real-world shape, ahead of root-level hyphen keys. Pinned by a
326
-feel-core test; workaround today: `get value(applicant, "first-name")`.
327
-
328
-Planned shape (no grammar change, lexer + probe only):
329
-
330
-1. **Static path members** — when a member run follows a *static* path chain
331
-   from a root name (`applicant.`, `p.q.`), ask the probe
332
-   `contains_member(path, name)` (new `ScopeProbe` method, default `false` —
333
-   non-breaking), which navigates the context value. Candidates become
334
-   path-qualified (`(["applicant"], "first-name")`), so feel-pg's per-row
335
-   guard navigates the jsonb the same way. Open: a list-valued step
336
-   (`orders.unit-price` maps over a list) — "first element" vs "any element".
337
-   Dynamic bases (`f(x).a-b`, `(e).a-b`) stay arithmetic.
338
-2. **Filter-element keys** — inside a filter bracket over a static path, the
339
-   element keys are in scope (`items[unit-price > 10]`,
340
-   `items[item.unit-price > 10]`). Needs filter-vs-list-literal bracket
341
-   tracking in the lexer (the `mark_reverse_open` value-position classifier
342
-   already has that information). Same list-element question as 1.
343
-
344
-Prerequisite worth doing first: a parse cache keyed by *which* candidates are
345
-bound (the parse is a pure function of the text and that subset), so nested
346
-candidates do not turn into per-row re-parses in feel-pg and `compile()` can
347
-follow the same rules.
319
+### v2: levels, backticks, adaptive compile, path members (open — ADR-48, proposed)
320
+
321
+Remaining v1 limits: `compile()` cannot reach special names (probe-free);
322
+feel-pg re-parses every row that binds a candidate and `feel_eval_immutable`
323
+rejects it; only **root** keys are probed — `applicant.first-name` parses as
324
+`applicant.first - name` (pinned by a feel-core test; workaround today:
325
+`get value(applicant, "first-name")`); keyword names (`Profit and loss`) are
326
+unsupported. Plan in [DECISIONS.md](DECISIONS.md) ADR-48, in this order:
327
+
328
+- **A — Backtick-quoted names + resolution levels.** `` `a-b` ``,
329
+  `` `Profit and loss` ``, `` applicant.`first-name` `` — feel-scala's syntax
330
+  (Camunda extension since feel-scala 1.5/1.8; ≥ 1 char, no escape, verbatim),
331
+  a dedicated token never joined or fused, valid in every name slot and at
332
+  every level. Parse option `NameResolution`: `Quoted` (Camunda semantics, no
333
+  candidates), `Symbols` (default, ADR-47), `Full` (reserved). feel-pg takes
334
+  it as a function argument with a default — never a GUC (IMMUTABLE).
335
+- **B — Candidate-keyed parse cache.** The parse is a pure function of
336
+  (text, level, bound candidates): `CompiledExpr` caches parses per
337
+  bound-candidate bitset (≤ 8, LRU), so `compile` follows the scope like
338
+  `evaluate`, feel-pg stops re-parsing per row, and `feel_eval_immutable`
339
+  drops its ERROR (still a pure function of its arguments). Plus
340
+  allocation-free `FunctionRegistry::contains`, a guard test that no built-in
341
+  name contains `- + * / .`, and benchmarks (190-candidate formula vs
342
+  `applicant.age >= 18`).
343
+- **C — Path-member probe.** `ScopeProbe::contains_member(path, name)`
344
+  (default `false`) for member runs on a *static* path from a root name;
345
+  path-qualified candidates join B's bitset; list steps check any element.
346
+  Dynamic bases and filter-element keys (`items[unit-price > 10]`) stay
347
+  deferred — backticks cover them.
348
+- **D — `Full` (keyword names), on demand only.** First measure whether the
349
+  TCK uses unquoted keyword names; decide which keywords may join (not `in`)
350
+  and the interplay with the fixed joins (`date and time`).
348351
 
349352
 ### Test ingestion (measured reality, and the hand-written fixture)
350353