Public repository: timo / feel

Reader

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

docs: ADR-39 + ROADMAP/README — §10.3.2.9.4 target-type coercion (0082)

public

docs: ADR-39 + ROADMAP/README — §10.3.2.9.4 target-type coercion (0082)

ADR-39 documents:
- The four implicit conversion rules in coerce() (null, from-singleton-list
  ONE level only, to-singleton-list, conforms-to)
- Typed formal parameters completing the ADR-35 follow-up
- The public parse_type() / coerce() API added to lib.rs
- The coerce_to fixture field and how it models the typeRef boundary
- All four new ingester capabilities (itemDef resolution, BKM inlining,
  invocation form, boxed-context result cell) and the critical ordering fix
  in fits_slice_grammar (fn literal params before context-key blanking)
- The scope cut: 5 decisionService_* cases remain skipped (DRG layer)

ROADMAP: move 0082 from out-of-scope to shipped; update suite count 2915 → 2946.
README: update fixture stats to 92 files / ~2946 cases.

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

modified DECISIONS.md +75 −0

@@ -1835,3 +1835,78 @@
18351835
   a function* (`var(args)`, `lib.fn(args)`) remains deferred (would require widening
18361836
   `Invocable` to `Postfix`, reintroducing the `Name`-vs-`Var` ambiguity). **Suite
18371837
   2907 → 2915.**
1838
+
1839
+## ADR-39: Target-type coercion — §10.3.2.9.4 implicit conversions (0082)
1840
+
1841
+- **Context:** `0082-feel-coercion` tests the DMN rule that a decision's result
1842
+  is coerced against its declared output type (`typeRef` on `<variable>` or
1843
+  `<literalExpression>`). Four conversions apply, in order: (1) `null`/error →
1844
+  `null`; (2) from-singleton-list — unwrap `[e]` to `e` by ONE level if `e`
1845
+  conforms to the target (NOT recursive: `[[10]]` → `number` → null); (3)
1846
+  to-singleton-list — wrap `v` into `[v]` iff `v` conforms to the list element
1847
+  type; (4) conforms-to — pass `v` through if it structurally conforms, else
1848
+  `null`. The suite was previously listed as out-of-scope ("DRG semantics"), but
1849
+  the coercion rules are FEEL-level concerns that `feel-core` can implement
1850
+  directly — only the five `decisionService_*` cases require the DRG layer.
1851
+
1852
+- **Decision:**
1853
+
1854
+  - **`types.rs`:** New `pub(crate) fn coerce(v: Value, t: &FeelType) -> Value`
1855
+    implementing the four rules beside the existing `value_conforms_to`. The
1856
+    from-singleton-list rule (Rule 2) performs exactly ONE level of unwrapping:
1857
+    `if items.len() == 1 { value_conforms_to(&items[0], t) ? items[0] : Null }`
1858
+    — NOT `coerce(items[0], t)`, which would incorrectly recurse.
1859
+
1860
+  - **Typed formal parameters** (completing the ADR-35 follow-up):
1861
+    - `ast.rs`: `Expr::FnDef.params: Vec<(String, Option<FeelType>)>` (was
1862
+      `Vec<String>` — the type annotation was previously parsed then discarded).
1863
+    - `function.rs`: `Closure.params` updated to match.
1864
+    - `feel.lalrpop`: `FormalParam` rule now returns `(String, Option<FeelType>)`;
1865
+      `pub Type: FeelType = InstanceType;` added as a new public entry point so
1866
+      `parse_type()` can deserialise `"coerce_to"` strings from the fixture.
1867
+    - `eval.rs / bind_params`: for each non-null actual arg with a declared param
1868
+      type, `coerce(arg, &ty)` is called; if it yields `Null` (non-conforming
1869
+      argument), `bind_params` returns `Err(())` and the whole call is
1870
+      error-null. A null actual arg has type `Null <: T` per the nested-null rule
1871
+      and is bound as-is.
1872
+
1873
+  - **`lib.rs`:** Exports `pub use ast::FeelType`, `pub fn parse_type(s) ->
1874
+    Option<FeelType>` (via `feel::TypeParser`), and `pub fn coerce(v, t) -> Value`.
1875
+
1876
+  - **Fixture harness** (`fixtures_test.rs`): optional `"coerce_to"` field — a
1877
+    structural FEEL type string — triggers `coerce(result, parse_type(coerce_to))`
1878
+    after evaluation and before comparison.
1879
+
1880
+  - **Ingester** (`ingest_tck.py`) — four new capabilities:
1881
+    1. `parse_item_definitions` resolves `<itemDefinition>` trees into structural
1882
+       FEEL type strings (`isCollection` → `list<…>`, `itemComponent` → `context<…>`,
1883
+       bare `typeRef` → primitive); `_resolve_typeref_to_feel` / `parse_decision_typerefs`
1884
+       map decision variables to their resolved type for `coerce_to` emission.
1885
+    2. `parse_bkms` + `parse_knowledge_requirements` + `_inline_bkms`: BKMs are
1886
+       inlined as FEEL function literals in a synthetic context wrapper
1887
+       `{bkm: function(p: T, …) body, __result: call_expr}.__result`. The BKM's
1888
+       return `typeRef` (when present) becomes `coerce_to` for the case.
1889
+    3. `parse_invocation_decisions` converts `<invocation>` form decisions into
1890
+       named-argument FEEL calls; the `<invocation typeRef>` becomes `coerce_to`
1891
+       with the BKM return type as fallback.
1892
+    4. `parse_context_result_decisions` handles boxed-context decisions with a
1893
+       trailing unnamed result cell → `{named_entries…, __result: result_expr}.__result`.
1894
+    5. `_fn_literal_params_and_blank` replaces the old `_fn_literal_pat` regex:
1895
+       it now handles typed formal parameters (`function(arg: number)`,
1896
+       `function(ctx: context<name: string, age: number>)`) by finding the
1897
+       balanced closing `)` and splitting the param list on top-level commas
1898
+       (not those nested inside `< >`). **Critically, this helper runs BEFORE the
1899
+       context-key blanker** — `arg:` in `function(arg: number)` looks like a
1900
+       context key to the regex and would be destroyed before the param name could
1901
+       be recorded as bound.
1902
+
1903
+- **Scope cut:** the five `decisionService_*` cases require DRG service
1904
+  encapsulation and remain skipped (`type="decisionService"` structural skip + two
1905
+  `ds_invoke_*` decisions naturally rejected by `fits_slice_grammar` because the
1906
+  DS name is a free identifier). No `OUT_OF_SCOPE_EXPR_SKIPS` entries needed — the
1907
+  existing machinery handles it.
1908
+
1909
+- **Tests:** 31 cases in `0082-feel-coercion.json`; exercised by `tck_fixtures_pass`
1910
+  as part of the 92-fixture suite. Unit tests `typed_formal_params_parse_and_eval`
1911
+  and `typed_params_closure_and_currying` in `lib.rs::function_tests`. **Suite
1912
+  2915 → 2946.**

modified README.md +1 −1

@@ -137,7 +137,7 @@
137137
 `context put` *overloads* whose name is absent from the single-row table
138138
 (`date(from: …)`, `context put(…, keys: …)`; see `NAMED_ARG_OVERLOAD_SKIPS`).
139139
 
140
-The fixtures used by the test (72 files, ~2270 self-contained cases) are
140
+The fixtures used by the test (92 files, ~2946 self-contained cases) are
141141
 regenerated by pointing the script at each TCK dir, e.g.:
142142
 
143143
 ```sh

modified ROADMAP.md +10 −3

@@ -248,12 +248,19 @@
248248
   Still deferred: invoking an arbitrary *expression* that holds a function —
249249
   `var(args)` or `lib.fn(args)` — would require widening the grammar beyond literal
250250
   callees and reintroduces the `Name`-vs-`Var` dispatch ambiguity.
251
+- **Target-type coercion** (§10.3.2.9.4) ✅ **shipped (ADR-39).**
252
+  `0082-feel-coercion` — 31 of 36 active cases ingested; the 5 skips are all
253
+  `decisionService_*` invocations requiring the DRG layer. Four implicit conversion
254
+  rules implemented in `coerce()` (`types.rs`): null → null; from-singleton-list
255
+  (ONE level); to-singleton-list; conforms-to. Typed formal parameters now flow
256
+  all the way through `FnDef` → `Closure` → `bind_params` where an arg that
257
+  doesn't conform to its declared type coerces the call result to null. BKMs
258
+  inlined as function literals in the ingester. **Suite 2915 → 2946.**
251259
 - **Emoji / non-XID names** as bare context keys (`{🐎: "bar"}`,
252260
   `0083-feel-unicode`) — the lexer accepts XID identifiers only; 2 cases skipped
253261
   (`NON_XID_NAME_SKIPS`).
254262
 - **Singleton-list ↔ scalar coercion** at decision/argument boundaries
255263
   (`0021-singleton-list`, §10.3.2.9.4) — a DMN typing rule, not an expression
256264
   rule; `OUT_OF_SCOPE_EXPR_SKIPS`.
257
-- **Decision-table / DRG semantics** (decision services, BKM invocation, output
258
-  type validation — `0082-feel-coercion`, the `errorResult` cases) — out of scope
259
-  for a FEEL *expression* engine; skipped structurally in the ingester.
265
+- **Decision-table / DRG semantics** (decision services) — out of scope for a
266
+  FEEL *expression* engine; 5 `decisionService_*` cases in `0082` skipped.