Public repository: timo / feel

Reader

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

docs: record ADR-15 (single-word built-in functions)

public

docs: record ADR-15 (single-word built-in functions)

Document the call grammar, builtin dispatch table, error/empty conventions,
varargs rule, and the regex-xpath-dialect deviation. Update the roadmap:
the single-word non-temporal slice is done; space-containing names, temporal
functions, ranges, and function-value args remain deferred.

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

modified DECISIONS.md +53 −10

@@ -244,20 +244,63 @@
244244
   lenient view. An in-expression `assert non null(x)` tripwire is a natural
245245
   complement once built-in functions land (see Roadmap).
246246
 
247
+## ADR-15: Single-word built-in functions (call grammar + dispatch table)
248
+
249
+- **Context:** The slice had no built-in functions (ADR-7/ADR-8). Full FEEL also
250
+  allows space-containing names (`string length`), which the lexer cannot
251
+  tokenise as one `Name`. The first useful, low-risk increment is the *single-
252
+  word*, *non-temporal* functions — they reuse the existing `Value` kinds and
253
+  unlock a large batch of self-contained `feel-*-function` TCK dirs.
254
+- **Decision:** Add function calls as a new production in the `Postfix` grammar
255
+  rule: `Name "(" args ")"` → `Expr::FnCall(name, args)`. An LR(1) lookahead of
256
+  `(` after a `Name` shifts into the call, otherwise it reduces to a `Var`, so
257
+  plain variable references are unaffected; `not(...)` keeps its own keyword rule.
258
+  A `builtins` module dispatches by name + arity. Implemented (per spec §10.3.4):
259
+  conversion `string`/`number`; string `substring`/`contains`/`replace`/`split`/
260
+  `matches`; list `count`/`min`/`max`/`sum`/`mean`/`product`/`median`/`stddev`/
261
+  `mode`/`all`/`any`/`append`/`concatenate`/`union`/`remove`/`reverse`/`flatten`/
262
+  `sublist`; numeric `decimal`/`floor`/`ceiling`/`abs`/`modulo`.
263
+  - **Error policy (ADR-14):** wrong arity / wrong type / domain error / unknown
264
+    name → *error-null*; spec-legitimate empty/absent results (`sum([]) = null`,
265
+    `min([]) = null`, `mode([]) = []`, `count([]) = 0`) → *value-null*. The N=0
266
+    vararg form (`all()`, `mode()`) is `null`, distinct from the empty-list arg.
267
+  - **Varargs:** aggregates accept either one list argument or a list of scalars
268
+    (`sum([1,2,3])` == `sum(1,2,3)`); a single list arg is unwrapped (`coerce_list`).
269
+  - **Regex (`regex-xpath-dialect` deviation):** `replace`/`split`/`matches` use
270
+    Rust's `regex` crate, whose dialect differs from the spec's normative
271
+    XQuery/XPath 2.0 F&O regex. A minimal translation (`$N` → `${N}` in the
272
+    replacement; rejecting non-`smixq` flags) covers the common cases; features
273
+    Rust's finite-automaton engine cannot do (backreferences, class subtraction
274
+    `[A-Z-[OI]]`, `\p{IsBasicLatin}` block names) — plus XPath string escapes
275
+    (`\u`, `\n`) the lexer does not decode — are skipped via the documented
276
+    `REGEX_DIALECT_SKIPS` list in `tools/ingest_tck.py` (not silently rewritten).
277
+- **Consequence:** `feel-core` gains a `regex` dependency (portable, so ADR-1
278
+  holds). The ingest predicate whitelists implemented built-in *call heads* so
279
+  their calls fit the slice while unimplemented/temporal/multi-word/user-defined
280
+  ones stay skipped. Two latent ingest bugs surfaced and were fixed: a declared
281
+  `xsd:string` value was mis-typed as a number (`string(2) = "2"`), and string
282
+  results were whitespace-stripped (`substring("foob r", -2, 1) = " "`). Unlocked
283
+  20 `feel-*-function` / list / string TCK dirs (303 → 541 fixture cases).
284
+- **Deferred (still out of scope):** space-containing names (`string length`),
285
+  temporal functions and `abs` on durations (await ADR temporal types),
286
+  `sort`/`list replace` (need function values), range relations and `is` (need
287
+  first-class ranges / temporal identity), and a full XPath-regex dialect bridge.
288
+
247289
 ## Roadmap: deferred scope (recorded, not yet built)
248290
 
249291
 These were considered and deliberately deferred to keep the slice thin. Listed so
250292
 the rationale and entry points survive:
251293
 
252
-- **Built-in functions** — numeric (`abs`, `sqrt`, `even`/`odd`, `min`/`max`,
253
-  `sum`, `count`, modulo), string (`string length`, `upper`/`lower case`,
254
-  `substring`, `contains`), list (`append`, `concatenate`, `distinct values`,
255
-  `sort`). Needs a function-call grammar production, FEEL's space-containing names
256
-  (`string length`), a builtin dispatch table, and reactivation of the many
257
-  already-self-contained `feel-*-function` TCK dirs. Would also enable an
258
-  explicit `assert non null(x)` tripwire — an in-expression complement to the
259
-  strict boundary policy (ADR-14) for surfacing error-nulls where the author
260
-  wants them loud.
294
+- **Built-in functions** — *single-word, non-temporal slice done (ADR-15):* a
295
+  `Name "(" … ")"` production in `Postfix`, a `builtins` dispatch table, and the
296
+  conversion/string/list/numeric functions listed there (incl. `regex`-backed
297
+  `replace`/`split`/`matches`). *Still deferred:* FEEL's space-containing names
298
+  (`string length`, `upper`/`lower case`, `distinct values`); `sort`/`list
299
+  replace` (need function values); range relations and `is`; the temporal
300
+  functions (await temporal types). Built-ins would also enable an explicit
301
+  `assert non null(x)` tripwire — an in-expression complement to the strict
302
+  boundary policy (ADR-14) for surfacing error-nulls where the author wants them
303
+  loud.
261304
 - **Temporal types** — `date`/`time`/`date and time`/`duration` values, their
262305
   constructor functions, and `@"…"` literals. Largest lift; reverses ADR-5's
263306
   deferral and activates the placeholder `temporal-interval-mapping` deviation
@@ -268,7 +311,7 @@
268311
   built into a `Context` in the fixture test and bound during evaluation.
269312
   Unlocked 0016-some-every (path access over a list of contexts) and
270313
   0032-conditionals (cases 001–003, after the `if`-semantics decision in ADR-13).
271
-  0012-list-functions stays skipped (all list built-ins).
314
+  0012-list-functions is now unlocked too (list built-ins landed in ADR-15).
272315
 
273316
 - **Array index access** — FEEL `list[n]` is 1-based, supports negative-from-end
274317
   indexing, and is `null` out of range; the same `[...]` syntax is also a boolean