Public repository: timo / feel

Reader

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

feat(core): opaque comparison-test value `(< 10)` in value position

public

feat(core): opaque comparison-test value `(< 10)` in value position

A parenthesised comparison test in value position — `(< 10)`, `(= 10)`,
`(!= 10)` — is now an opaque unary-test value, not a boolean and not a
promoted range. It equals a structurally identical test reflexively
(`(<10) = (<10)` → true) but is never equal to a `Range`
(`(<10) = (null..10)`, `(=10) = [10..10]` → false) — the measured 0068
semantics that falsified the desugar-to-range hypothesis (ADR-28's
`range_006` question).

- grammar: one new `Primary` alternative `"(" CmpOp Sum ")"` — LR(1)-clean
  (no `CmpOp` in `FIRST(Expr)`, so no clash with grouping or the range
  form), carried by a new `Expr::UnaryTestVal(CmpOp, Box<Expr>)` node.
- value: `Value::UnaryTestVal(Box<UnaryTestVal>)`, boxed so `Value` stays
  40 bytes. Equality is structural + reflexive and a definite `false`
  against a `Range` or anything else (not the type-mismatch error-null).
- it has no order, no `instance of` type but `Any`; `string(...)` echoes
  the written form; the feel-py / feel-lua wrappers cross it out as
  host `None`/`nil` like a native closure.

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

modified crates/feel-core/src/ast.rs +9 −1

@@ -67,6 +67,14 @@
6767
     /// ([`UnaryTest::Not`]), `x in not(t)` ≡ `not(x satisfies t)` (Kleene), never the
6868
     /// boolean `not(…)` built-in.
6969
     In(Box<Expr>, Vec<UnaryTest>),
70
+    /// A parenthesised comparison-test *value* in value position — `(< 10)`,
71
+    /// `(= 10)`, `(!= 10)` (DMN §10.3.4.6). Distinct from a [`Compare`](Expr::Compare):
72
+    /// here the comparison has no left operand, so it is not a boolean but an **opaque
73
+    /// unary-test value** ([`Value::UnaryTestVal`](crate::Value::UnaryTestVal)) with
74
+    /// reflexive, range-*incompatible* equality (ADR-32) — `(<10) = (<10)` is true but
75
+    /// `(<10) = (null..10)` is false. The `in`-position forms (`x in (=10)`) keep the
76
+    /// dedicated [`UnaryTest::Compare`] test; this is the value-position sibling.
77
+    UnaryTestVal(CmpOp, Box<Expr>),
7078
     /// Type predicate `value instance of type` (DMN §10.3.2.9, grammar rule 51).
7179
     /// The right operand is a [`FeelType`] resolved at parse time (it carries no
7280
     /// runtime data); an unmappable type name parses as [`FeelType::Unknown`] so
@@ -366,7 +374,7 @@
366374
                     && args.named.iter().all(|(_, e)| e.is_clock_free())
367375
             }
368376
             Expr::FnDef { body, .. } => body.is_clock_free(),
369
-            Expr::Neg(e) | Expr::Not(e) => e.is_clock_free(),
377
+            Expr::Neg(e) | Expr::Not(e) | Expr::UnaryTestVal(_, e) => e.is_clock_free(),
370378
             Expr::Arith(_, a, b) | Expr::Compare(_, a, b) | Expr::And(a, b) | Expr::Or(a, b) => {
371379
                 a.is_clock_free() && b.is_clock_free()
372380
             }

modified crates/feel-core/src/builtins.rs +20 −0

@@ -911,7 +911,27 @@
911911
             r.end.as_ref().map(render_member).unwrap_or_default(),
912912
             if r.end_open { ')' } else { ']' },
913913
         ),
914
+        // An opaque comparison-test value `(< 10)` (ADR-32) renders in its written
915
+        // parenthesised form, `(< 10)` / `(= 10)` / `(!= 10)`, with the endpoint quoted
916
+        // as a list member is (so a string endpoint keeps its quotes).
917
+        Value::UnaryTestVal(t) => {
918
+            format!("({} {})", cmp_op_str(t.op), render_member(&t.endpoint))
919
+        }
914920
         Value::Null | Value::Error => "null".to_string(),
921
+    }
922
+}
923
+
924
+/// The FEEL source spelling of a comparison operator (for stringifying a
925
+/// [`Value::UnaryTestVal`]).
926
+fn cmp_op_str(op: crate::ast::CmpOp) -> &'static str {
927
+    use crate::ast::CmpOp::*;
928
+    match op {
929
+        Lt => "<",
930
+        Le => "<=",
931
+        Gt => ">",
932
+        Ge => ">=",
933
+        Eq => "=",
934
+        Ne => "!=",
915935
     }
916936
 }
917937
 

modified crates/feel-core/src/eval.rs +26 −0

@@ -87,6 +87,15 @@
8787
                     end_open: iv.end_open,
8888
                 })))
8989
             }
90
+            // A parenthesised comparison-test value `(< 10)` (DMN §10.3.4.6; ADR-32):
91
+            // evaluate the endpoint and wrap it in an opaque `Value::UnaryTestVal`. It is
92
+            // NOT a range — its equality is structural and range-incompatible (see below).
93
+            Expr::UnaryTestVal(op, e) => Ok(Value::UnaryTestVal(Box::new(
94
+                crate::value::UnaryTestVal {
95
+                    op: *op,
96
+                    endpoint: self.eval(e, ctx)?.forced(),
97
+                },
98
+            ))),
9099
             Expr::Var(name) => Ok(ctx.get(name).cloned().unwrap_or(Value::Null)),
91100
             Expr::ContextLit(entries) => self.eval_context_lit(entries, ctx),
92101
             Expr::Path(target, name) => Ok(match self.eval(target, ctx)? {
@@ -480,6 +489,23 @@
480489
                 (Some(true), Some(true)) => Some(true),
481490
             }
482491
         }
492
+        // An opaque parenthesised comparison-test value `(< 10)` (ADR-32). It equals
493
+        // ONLY a structurally identical test — same operator AND FEEL-equal endpoint —
494
+        // reflexively (`(<10) = (<10)`, `(=10) = (=10)`, `(!=10) = (!=10)` → true). A
495
+        // differing operator is a definite `false`; the endpoint comparison three-values
496
+        // through (an endpoint type mismatch is the usual error-null).
497
+        (Value::UnaryTestVal(x), Value::UnaryTestVal(y)) => {
498
+            if x.op != y.op {
499
+                Some(false)
500
+            } else {
501
+                feel_equal_depth(&x.endpoint, &y.endpoint, depth + 1)
502
+            }
503
+        }
504
+        // …but it is NEVER equal to a `Range` or anything else: `(<10) = (null..10)` and
505
+        // `(=10) = [10..10]` are a definite `false` (the measured 0068 semantics that
506
+        // falsified the desugar-to-range hypothesis), not a type-mismatch error-null.
507
+        // (Null/error on either side was already settled above.)
508
+        (Value::UnaryTestVal(_), _) | (_, Value::UnaryTestVal(_)) => Some(false),
483509
         // Temporal: same-type -> Some(bool); cross temporal type -> None (-> error-null).
484510
         (x, y) if temporal::is_temporal(x) && temporal::is_temporal(y) => temporal::equal(x, y),
485511
         _ => None, // different, non-null types

modified crates/feel-core/src/feel.lalrpop +6 −0

@@ -238,6 +238,12 @@
238238
     Context,
239239
     "not" "(" <e:Expr> ")" => Expr::Not(Box::new(e)),
240240
     "(" <e:Expr> ")" => e,
241
+    // A parenthesised comparison-test *value* `(< 10)` / `(= 10)` / `(!= 10)` (DMN
242
+    // §10.3.4.6; ADR-32). LR(1)-clean: no `CmpOp` token is in FIRST(Expr), so seeing a
243
+    // `CmpOp` right after `(` is an unambiguous new path that never competes with the
244
+    // `"(" Expr ")"` grouping or the `"(" Sum ".." …` range form. It evaluates to an
245
+    // opaque `Value::UnaryTestVal`, NOT a promoted range (the measured 0068 semantics).
246
+    "(" <op:CmpOp> <e:Sum> ")" => Expr::UnaryTestVal(op, Box::new(e)),
241247
     Name => Expr::Var(<>),
242248
 };
243249
 

modified crates/feel-core/src/value.rs +26 −0

@@ -34,6 +34,18 @@
3434
     /// orderable, same-typed [`Value`]s; the booleans carry each bracket's openness
3535
     /// (`[`/`]` closed, `(`/`)` open).
3636
     Range(Box<RangeVal>),
37
+    /// An opaque parenthesised comparison-test *value* — `(< 10)`, `(= 10)`, `(!= 10)`
38
+    /// in value position (DMN §10.3.4.6; ADR-32). Boxed so the variant stays one word
39
+    /// and `Value` does not grow past 40 bytes (see the size test in `lib.rs`).
40
+    ///
41
+    /// Unlike the desugar hypothesis ADR-28 floated, this is **not** a promoted
42
+    /// [`Range`](Value::Range): the measured TCK semantics (0068) are that it equals an
43
+    /// *identical test* reflexively — `(<10) = (<10)`, `(=10) = (=10)`, `(!=10) = (!=10)`
44
+    /// are all true — but is **never** equal to a `Range` (`(<10) = (null..10)` and
45
+    /// `(=10) = [10..10]` are both false). So equality is structural (same op + FEEL-equal
46
+    /// endpoint) and range-incompatible. It has no order, no host type, and conforms to no
47
+    /// `instance of` type but `Any`.
48
+    UnaryTestVal(Box<UnaryTestVal>),
3749
     /// A lazily-resolved host value (a Python/JS object behind a target-specific
3850
     /// [`ForeignObject`](crate::foreign::ForeignObject)). Only the wrapper crates
3951
     /// ever construct one (ADR-21); the FEEL dot resolves it by name on demand,
@@ -83,6 +95,15 @@
8395
     pub end_open: bool,
8496
 }
8597
 
98
+/// The payload of a [`Value::UnaryTestVal`]: the comparison operator and the
99
+/// already-evaluated endpoint of an opaque parenthesised comparison-test value
100
+/// (`(< 10)` → `op: Lt, endpoint: Number(10)`; ADR-32).
101
+#[derive(Debug, Clone, PartialEq)]
102
+pub struct UnaryTestVal {
103
+    pub op: crate::ast::CmpOp,
104
+    pub endpoint: Value,
105
+}
106
+
86107
 impl Value {
87108
     /// True if this value is, or transitively contains, an [`Value::Error`].
88109
     /// Used by the strict boundary policy in the wrappers.
@@ -95,6 +116,7 @@
95116
                 r.start.as_ref().is_some_and(Value::contains_error)
96117
                     || r.end.as_ref().is_some_and(Value::contains_error)
97118
             }
119
+            Value::UnaryTestVal(t) => t.endpoint.contains_error(),
98120
             _ => false,
99121
         }
100122
     }
@@ -132,6 +154,10 @@
132154
                 end: r.end.map(Value::into_lenient),
133155
                 ..*r
134156
             })),
157
+            Value::UnaryTestVal(t) => Value::UnaryTestVal(Box::new(UnaryTestVal {
158
+                op: t.op,
159
+                endpoint: t.endpoint.into_lenient(),
160
+            })),
135161
             other => other,
136162
         }
137163
     }

modified crates/feel-lua/src/lib.rs +3 −0

@@ -414,6 +414,9 @@
414414
             },
415415
             Func::Closure(_) => LuaValue::Nil,
416416
         },
417
+        // An opaque comparison-test value `(< 10)` (ADR-32) has no native Lua type;
418
+        // like a native FEEL closure it crosses out as `nil`.
419
+        Value::UnaryTestVal(_) => LuaValue::Nil,
417420
     })
418421
 }
419422
 

modified crates/feel-py/src/lib.rs +3 −0

@@ -543,6 +543,9 @@
543543
             },
544544
             Func::Closure(_) => py.None().into_bound(py),
545545
         },
546
+        // An opaque comparison-test value `(< 10)` (ADR-32) has no native Python type;
547
+        // like a native FEEL closure it crosses out as `None`.
548
+        Value::UnaryTestVal(_) => py.None().into_bound(py),
546549
     })
547550
 }
548551