Skip to main content

tensorlogic_compiler/partial_eval/
mod.rs

1//! Partial evaluation / expression specialization for TLExpr.
2//!
3//! Given a set of concrete bindings for **some** variables while leaving others
4//! free/symbolic, this module reduces an expression as much as possible. This is
5//! strictly more powerful than plain constant propagation because it handles
6//! *mixed* symbolic-concrete environments and enables expression specialization
7//! across a range of inputs.
8//!
9//! # Variable Convention
10//!
11//! Following the established codebase convention, a scalar "variable" named `"x"`
12//! is represented as a zero-arity predicate: `TLExpr::Pred { name: "x", args: [] }`.
13//! Booleans are encoded as `TLExpr::Constant(1.0)` (true) and
14//! `TLExpr::Constant(0.0)` (false).
15//!
16//! # Example
17//!
18//! ```rust
19//! use tensorlogic_compiler::partial_eval::{PEEnv, PEConfig, partially_evaluate};
20//! use tensorlogic_ir::TLExpr;
21//!
22//! // Expression: x + y  (both are zero-arity predicates acting as variables)
23//! let x = TLExpr::pred("x", vec![]);
24//! let y = TLExpr::pred("y", vec![]);
25//! let expr = TLExpr::add(x, y);
26//!
27//! // Partially evaluate with x = 3.0; y stays symbolic
28//! let env = PEEnv::new().with_f64("x", 3.0);
29//! let config = PEConfig::default();
30//! let result = partially_evaluate(&expr, &env, &config);
31//!
32//! // Result should be: Add(Constant(3.0), Pred("y", []))
33//! // (not fully concrete because y is still free)
34//! assert!(result.residual_vars.contains(&"y".to_string()));
35//! ```
36
37mod api;
38mod helpers;
39mod pe_arith;
40mod pe_core;
41mod pe_logic;
42mod pe_math;
43mod pe_passthrough;
44mod pe_quantifiers;
45mod types;
46
47#[cfg(test)]
48mod tests;
49
50pub use api::{partially_evaluate, specialize, specialize_batch};
51pub use types::{PEConfig, PEEnv, PEResult, PEStats, PEValue};