Skip to main content

panproto_expr/
expr.rs

1//! Expression AST, pattern, and builtin operation types.
2//!
3//! The expression language is a pure functional language — lambda calculus
4//! with pattern matching, algebraic data types, and built-in operations on
5//! strings, numbers, records, and lists. Comparable to a pure subset of ML.
6
7use std::sync::Arc;
8
9use crate::Literal;
10
11/// An expression in the pure functional language.
12///
13/// All variants are serializable, content-addressable, and evaluate
14/// deterministically on any platform (including WASM).
15#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
16pub enum Expr {
17    /// Variable reference.
18    Var(Arc<str>),
19    /// Lambda abstraction: `λparam. body`.
20    Lam(Arc<str>, Box<Self>),
21    /// Function application: `func(arg)`.
22    App(Box<Self>, Box<Self>),
23    /// Literal value.
24    Lit(Literal),
25    /// Record construction: `{ name: expr, ... }`.
26    Record(Vec<(Arc<str>, Self)>),
27    /// List construction: `[expr, ...]`.
28    List(Vec<Self>),
29    /// Field access: `expr.field`.
30    Field(Box<Self>, Arc<str>),
31    /// Index access: `expr[index]`.
32    Index(Box<Self>, Box<Self>),
33    /// Pattern matching: `match scrutinee { pat => body, ... }`.
34    Match {
35        /// The value being matched against.
36        scrutinee: Box<Self>,
37        /// Arms: (pattern, body) pairs tried in order.
38        arms: Vec<(Pattern, Self)>,
39    },
40    /// Let binding: `let name = value in body`.
41    Let {
42        /// The bound variable name.
43        name: Arc<str>,
44        /// The value to bind.
45        value: Box<Self>,
46        /// The body where the binding is visible.
47        body: Box<Self>,
48    },
49    /// Built-in operation applied to arguments.
50    Builtin(BuiltinOp, Vec<Self>),
51}
52
53/// A destructuring pattern for match expressions.
54#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
55pub enum Pattern {
56    /// Matches anything, binds nothing.
57    Wildcard,
58    /// Matches anything, binds the value to a name.
59    Var(Arc<str>),
60    /// Matches a specific literal value.
61    Lit(Literal),
62    /// Matches a record with specific field patterns.
63    Record(Vec<(Arc<str>, Self)>),
64    /// Matches a list with element patterns.
65    List(Vec<Self>),
66    /// Matches a tagged constructor with argument patterns.
67    Constructor(Arc<str>, Vec<Self>),
68}
69
70/// Simple type classification for expressions.
71///
72/// This is a lightweight type system for the expression language,
73/// independent of the GAT type system in `panproto-gat`. Used for
74/// type inference and coercion validation within expressions.
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
76pub enum ExprType {
77    /// 64-bit signed integer.
78    Int,
79    /// 64-bit IEEE 754 float.
80    Float,
81    /// UTF-8 string.
82    Str,
83    /// Boolean.
84    Bool,
85    /// Homogeneous list.
86    List,
87    /// Record (ordered map of fields to values).
88    Record,
89    /// Unknown or polymorphic type.
90    Any,
91}
92
93/// Built-in operations, grouped by domain.
94///
95/// Each operation has a fixed arity enforced at evaluation time.
96/// All operations are pure — no IO, no mutation, deterministic.
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
98pub enum BuiltinOp {
99    // --- Arithmetic (7) ---
100    /// `add(a: int|float, b: int|float) → int|float`
101    Add,
102    /// `sub(a: int|float, b: int|float) → int|float`
103    Sub,
104    /// `mul(a: int|float, b: int|float) → int|float`
105    Mul,
106    /// `div(a: int|float, b: int|float) → int|float` (truncating for ints)
107    Div,
108    /// `mod_(a: int, b: int) → int`
109    Mod,
110    /// `neg(a: int|float) → int|float`
111    Neg,
112    /// `abs(a: int|float) → int|float`
113    Abs,
114
115    // --- Rounding (3) ---
116    /// `floor(a: float) → int`
117    Floor,
118    /// `ceil(a: float) → int`
119    Ceil,
120    /// `round(a: float) → int` (rounds to nearest, ties to even)
121    Round,
122
123    // --- Comparison (6) ---
124    /// `eq(a, b) → bool`
125    Eq,
126    /// `neq(a, b) → bool`
127    Neq,
128    /// `lt(a, b) → bool`
129    Lt,
130    /// `lte(a, b) → bool`
131    Lte,
132    /// `gt(a, b) → bool`
133    Gt,
134    /// `gte(a, b) → bool`
135    Gte,
136
137    // --- Boolean (3) ---
138    /// `and(a: bool, b: bool) → bool`
139    And,
140    /// `or(a: bool, b: bool) → bool`
141    Or,
142    /// `not(a: bool) → bool`
143    Not,
144
145    // --- String (10) ---
146    /// `concat(a: string, b: string) → string`
147    Concat,
148    /// `len(s: string) → int` (byte length)
149    Len,
150    /// `slice(s: string, start: int, end: int) → string`
151    Slice,
152    /// `upper(s: string) → string`
153    Upper,
154    /// `lower(s: string) → string`
155    Lower,
156    /// `trim(s: string) → string`
157    Trim,
158    /// `split(s: string, delim: string) → [string]`
159    Split,
160    /// `join(parts: [string], delim: string) → string`
161    Join,
162    /// `replace(s: string, from: string, to: string) → string`
163    Replace,
164    /// `contains(s: string, substr: string) → bool`
165    Contains,
166
167    // --- List (9) ---
168    /// `map(list: [a], f: a → b) → [b]`
169    Map,
170    /// `filter(list: [a], pred: a → bool) → [a]`
171    Filter,
172    /// `fold(list: [a], init: b, f: (b, a) → b) → b`
173    Fold,
174    /// `append(list: [a], item: a) → [a]`
175    Append,
176    /// `head(list: [a]) → a`
177    Head,
178    /// `tail(list: [a]) → [a]`
179    Tail,
180    /// `reverse(list: [a]) → [a]`
181    Reverse,
182    /// `flat_map(list: [a], f: a → [b]) → [b]`
183    FlatMap,
184    /// `length(list: [a]) → int` (list length, distinct from string Len)
185    Length,
186
187    // --- Record (4) ---
188    /// `merge(a: record, b: record) → record` (b fields override a)
189    MergeRecords,
190    /// `keys(r: record) → [string]`
191    Keys,
192    /// `values(r: record) → [any]`
193    Values,
194    /// `has_field(r: record, name: string) → bool`
195    HasField,
196
197    // --- Utility (3) ---
198    /// `default(x, fallback)`: returns fallback if x is null, else x.
199    DefaultVal,
200    /// `clamp(x, min, max)`: clamp a numeric value to the range [min, max].
201    Clamp,
202    /// `truncate_str(s, max_len)`: truncate a string to at most `max_len` bytes
203    /// (char-boundary safe).
204    TruncateStr,
205
206    // --- Type coercions (6) ---
207    /// `int_to_float(n: int) → float`
208    IntToFloat,
209    /// `float_to_int(f: float) → int` (truncates)
210    FloatToInt,
211    /// `int_to_str(n: int) → string`
212    IntToStr,
213    /// `float_to_str(f: float) → string`
214    FloatToStr,
215    /// `str_to_int(s: string) → int` (fails on non-numeric)
216    StrToInt,
217    /// `str_to_float(s: string) → float` (fails on non-numeric)
218    StrToFloat,
219
220    // --- Type inspection (3) ---
221    /// `type_of(v) → string` (returns type name)
222    TypeOf,
223    /// `is_null(v) → bool`
224    IsNull,
225    /// `is_list(v) → bool`
226    IsList,
227
228    // --- Graph traversal (5) ---
229    // These builtins require an instance context (`InstanceEnv` in
230    // panproto-inst) and are evaluated by `eval_with_instance`, not
231    // the standard `eval`. In the standard evaluator they return Null.
232    /// `edge(node_ref: string, edge_kind: string) → value`
233    /// Follow a named edge from a node in the instance tree.
234    Edge,
235    /// `children(node_ref: string) → [value]`
236    /// Get all children of a node in the instance tree.
237    Children,
238    /// `has_edge(node_ref: string, edge_kind: string) → bool`
239    /// Check if a node has a specific outgoing edge.
240    HasEdge,
241    /// `edge_count(node_ref: string) → int`
242    /// Count outgoing edges from a node.
243    EdgeCount,
244    /// `anchor(node_ref: string) → string`
245    /// Get the schema anchor (sort/kind) of a node.
246    Anchor,
247}
248
249impl BuiltinOp {
250    /// Returns the expected number of arguments for this builtin.
251    #[must_use]
252    pub const fn arity(self) -> usize {
253        match self {
254            // Unary
255            Self::Neg
256            | Self::Abs
257            | Self::Floor
258            | Self::Ceil
259            | Self::Round
260            | Self::Not
261            | Self::Upper
262            | Self::Lower
263            | Self::Trim
264            | Self::Head
265            | Self::Tail
266            | Self::Reverse
267            | Self::Keys
268            | Self::Values
269            | Self::IntToFloat
270            | Self::FloatToInt
271            | Self::IntToStr
272            | Self::FloatToStr
273            | Self::StrToInt
274            | Self::StrToFloat
275            | Self::TypeOf
276            | Self::IsNull
277            | Self::IsList
278            | Self::Len
279            | Self::Length
280            | Self::Children
281            | Self::EdgeCount
282            | Self::Anchor => 1,
283            // Binary
284            Self::Add
285            | Self::Sub
286            | Self::Mul
287            | Self::Div
288            | Self::Mod
289            | Self::Eq
290            | Self::Neq
291            | Self::Lt
292            | Self::Lte
293            | Self::Gt
294            | Self::Gte
295            | Self::And
296            | Self::Or
297            | Self::Concat
298            | Self::Split
299            | Self::Join
300            | Self::Append
301            | Self::Map
302            | Self::Filter
303            | Self::HasField
304            | Self::MergeRecords
305            | Self::Contains
306            | Self::FlatMap
307            | Self::Edge
308            | Self::HasEdge
309            | Self::DefaultVal
310            | Self::TruncateStr => 2,
311            // Ternary
312            Self::Slice | Self::Replace | Self::Fold | Self::Clamp => 3,
313        }
314    }
315
316    /// Returns the type signature `(input_types, output_type)` for builtins
317    /// with a known, monomorphic signature. Polymorphic builtins (e.g., `Add`
318    /// works on both int and float) return `None`.
319    #[must_use]
320    pub const fn signature(self) -> Option<(&'static [ExprType], ExprType)> {
321        match self {
322            // Coercions: precise source→target signatures.
323            Self::IntToFloat => Some((&[ExprType::Int], ExprType::Float)),
324            Self::FloatToInt | Self::Floor | Self::Ceil | Self::Round => {
325                Some((&[ExprType::Float], ExprType::Int))
326            }
327            Self::IntToStr => Some((&[ExprType::Int], ExprType::Str)),
328            Self::FloatToStr => Some((&[ExprType::Float], ExprType::Str)),
329            Self::StrToInt | Self::Len => Some((&[ExprType::Str], ExprType::Int)),
330            Self::StrToFloat => Some((&[ExprType::Str], ExprType::Float)),
331
332            // Boolean operations.
333            Self::And | Self::Or => Some((&[ExprType::Bool, ExprType::Bool], ExprType::Bool)),
334            Self::Not => Some((&[ExprType::Bool], ExprType::Bool)),
335
336            // Comparison: polymorphic inputs, bool output.
337            Self::Eq | Self::Neq | Self::Lt | Self::Lte | Self::Gt | Self::Gte => {
338                Some((&[ExprType::Any, ExprType::Any], ExprType::Bool))
339            }
340
341            // String operations.
342            Self::Concat => Some((&[ExprType::Str, ExprType::Str], ExprType::Str)),
343            Self::Slice => Some((
344                &[ExprType::Str, ExprType::Int, ExprType::Int],
345                ExprType::Str,
346            )),
347            Self::Upper | Self::Lower | Self::Trim => Some((&[ExprType::Str], ExprType::Str)),
348            Self::Split => Some((&[ExprType::Str, ExprType::Str], ExprType::List)),
349            Self::Join => Some((&[ExprType::List, ExprType::Str], ExprType::Str)),
350            Self::Replace => Some((
351                &[ExprType::Str, ExprType::Str, ExprType::Str],
352                ExprType::Str,
353            )),
354            Self::Contains => Some((&[ExprType::Str, ExprType::Str], ExprType::Bool)),
355            Self::TruncateStr => Some((&[ExprType::Str, ExprType::Int], ExprType::Str)),
356
357            // List operations.
358            Self::Length => Some((&[ExprType::List], ExprType::Int)),
359            Self::Reverse => Some((&[ExprType::List], ExprType::List)),
360
361            // Record operations.
362            Self::MergeRecords => Some((&[ExprType::Record, ExprType::Record], ExprType::Record)),
363            Self::Keys | Self::Values => Some((&[ExprType::Record], ExprType::List)),
364            Self::HasField => Some((&[ExprType::Record, ExprType::Str], ExprType::Bool)),
365
366            // Type inspection.
367            Self::TypeOf => Some((&[ExprType::Any], ExprType::Str)),
368            Self::IsNull | Self::IsList => Some((&[ExprType::Any], ExprType::Bool)),
369
370            // Polymorphic builtins: return None.
371            Self::Add
372            | Self::Sub
373            | Self::Mul
374            | Self::Div
375            | Self::Mod
376            | Self::Neg
377            | Self::Abs
378            | Self::Map
379            | Self::Filter
380            | Self::Fold
381            | Self::FlatMap
382            | Self::Append
383            | Self::Head
384            | Self::Tail
385            | Self::DefaultVal
386            | Self::Clamp
387            | Self::Edge
388            | Self::Children
389            | Self::HasEdge
390            | Self::EdgeCount
391            | Self::Anchor => None,
392        }
393    }
394}
395
396impl Expr {
397    /// Create a variable expression.
398    #[must_use]
399    pub fn var(name: impl Into<Arc<str>>) -> Self {
400        Self::Var(name.into())
401    }
402
403    /// Create a lambda expression.
404    #[must_use]
405    pub fn lam(param: impl Into<Arc<str>>, body: Self) -> Self {
406        Self::Lam(param.into(), Box::new(body))
407    }
408
409    /// Create an application expression.
410    #[must_use]
411    pub fn app(func: Self, arg: Self) -> Self {
412        Self::App(Box::new(func), Box::new(arg))
413    }
414
415    /// Create a let-binding expression.
416    #[must_use]
417    pub fn let_in(name: impl Into<Arc<str>>, value: Self, body: Self) -> Self {
418        Self::Let {
419            name: name.into(),
420            value: Box::new(value),
421            body: Box::new(body),
422        }
423    }
424
425    /// Create a field access expression.
426    #[must_use]
427    pub fn field(expr: Self, name: impl Into<Arc<str>>) -> Self {
428        Self::Field(Box::new(expr), name.into())
429    }
430
431    /// Create a builtin operation applied to arguments.
432    #[must_use]
433    pub const fn builtin(op: BuiltinOp, args: Vec<Self>) -> Self {
434        Self::Builtin(op, args)
435    }
436
437    /// Coerce an integer to a float.
438    #[must_use]
439    pub fn int_to_float(arg: Self) -> Self {
440        Self::Builtin(BuiltinOp::IntToFloat, vec![arg])
441    }
442
443    /// Coerce a float to an integer (truncates toward zero).
444    #[must_use]
445    pub fn float_to_int(arg: Self) -> Self {
446        Self::Builtin(BuiltinOp::FloatToInt, vec![arg])
447    }
448
449    /// Coerce an integer to a string.
450    #[must_use]
451    pub fn int_to_str(arg: Self) -> Self {
452        Self::Builtin(BuiltinOp::IntToStr, vec![arg])
453    }
454
455    /// Coerce a float to a string.
456    #[must_use]
457    pub fn float_to_str(arg: Self) -> Self {
458        Self::Builtin(BuiltinOp::FloatToStr, vec![arg])
459    }
460
461    /// Parse a string as an integer.
462    #[must_use]
463    pub fn str_to_int(arg: Self) -> Self {
464        Self::Builtin(BuiltinOp::StrToInt, vec![arg])
465    }
466
467    /// Parse a string as a float.
468    #[must_use]
469    pub fn str_to_float(arg: Self) -> Self {
470        Self::Builtin(BuiltinOp::StrToFloat, vec![arg])
471    }
472}
473
474#[cfg(test)]
475mod tests {
476    use super::*;
477
478    #[test]
479    fn builtin_arities() {
480        assert_eq!(BuiltinOp::Add.arity(), 2);
481        assert_eq!(BuiltinOp::Not.arity(), 1);
482        assert_eq!(BuiltinOp::Fold.arity(), 3);
483        assert_eq!(BuiltinOp::Slice.arity(), 3);
484    }
485
486    #[test]
487    fn expr_constructors() {
488        let e = Expr::let_in(
489            "x",
490            Expr::Lit(Literal::Int(42)),
491            Expr::builtin(
492                BuiltinOp::Add,
493                vec![Expr::var("x"), Expr::Lit(Literal::Int(1))],
494            ),
495        );
496        assert!(matches!(e, Expr::Let { .. }));
497    }
498}