tensorlogic_compiler/symbolic_diff/mod.rs
1//! Symbolic differentiation of TLExpr.
2//!
3//! This module implements formal symbolic differentiation of TensorLogic expressions
4//! with respect to named variables. It supports:
5//!
6//! - Standard arithmetic differentiation rules (sum, product, quotient, power, chain)
7//! - Unary transcendental functions (sin, cos, exp, log, sqrt, abs)
8//! - Logical differentiation (AND, OR, NOT, implication)
9//! - Quantifiers treated as scoped operators (bound variable treated as independent)
10//! - Let-bindings via substitution
11//! - Jacobian computation for multiple variables
12//! - Post-differentiation algebraic simplification
13//!
14//! # Design Notes
15//!
16//! In TLExpr, a scalar variable named `"x"` is represented as a zero-arity predicate:
17//! `TLExpr::Pred { name: "x".to_string(), args: vec![] }`.
18//! This convention is used throughout the codebase and is the basis for variable
19//! detection in this module.
20//!
21//! Arithmetic negation (unary minus) does **not** have its own `TLExpr` variant.
22//! It is represented as `TLExpr::Sub(Constant(0.0), inner)` or
23//! `TLExpr::Mul(Constant(-1.0), inner)`. The simplification pass recognises the
24//! `Sub(0, e)` form and folds it to a negated constant where possible.
25//!
26//! # Example
27//!
28//! ```rust
29//! use tensorlogic_compiler::symbolic_diff::{differentiate, DiffConfig};
30//! use tensorlogic_ir::TLExpr;
31//!
32//! // d(x * x)/dx → x * 1 + x * 1 → (after simplification) x + x
33//! let x = TLExpr::pred("x", vec![]);
34//! let expr = TLExpr::mul(x.clone(), x.clone());
35//! let config = DiffConfig::default();
36//! let result = differentiate(&expr, "x", &config).expect("differentiate");
37//! // result.derivative is the symbolic derivative (x + x)
38//! ```
39
40mod api;
41mod diff_arith;
42mod diff_core;
43mod diff_fuzzy;
44mod diff_logic;
45mod diff_modal;
46mod diff_sets;
47mod helpers;
48mod types;
49
50#[cfg(test)]
51mod tests;
52
53pub use api::{differentiate, jacobian};
54pub use helpers::simplify_derivative;
55pub use types::{DiffConfig, DiffError, DiffResult};