| @@ -1835,3 +1835,78 @@ |
| 1835 | 1835 | a function* (`var(args)`, `lib.fn(args)`) remains deferred (would require widening |
| 1836 | 1836 | `Invocable` to `Postfix`, reintroducing the `Name`-vs-`Var` ambiguity). **Suite |
| 1837 | 1837 | 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.** |