pub trait Substitutable {
// Required methods
fn subs(&self, old: &Expression, new: &Expression) -> Expression;
fn subs_multiple(
&self,
substitutions: &[(Expression, Expression)],
) -> Expression;
}Expand description
Trait for types that support substitution operations
Required Methods§
Sourcefn subs(&self, old: &Expression, new: &Expression) -> Expression
fn subs(&self, old: &Expression, new: &Expression) -> Expression
Substitute a single expression with another
Recursively walks the expression tree and replaces all occurrences
of old with new. The replacement is structural - it compares
expressions using PartialEq.
§Arguments
old- The expression to replacenew- The expression to substitute in
§Examples
use mathhook_core::prelude::*;
use mathhook_core::pattern::Substitutable;
let x = symbol!(x);
let expr = Expression::add(vec![
Expression::symbol(x.clone()),
Expression::integer(1)
]);
let result = expr.subs(&Expression::symbol(x.clone()), &Expression::integer(5));
let expected = Expression::add(vec![
Expression::integer(5),
Expression::integer(1)
]);
assert_eq!(result, expected);Sourcefn subs_multiple(
&self,
substitutions: &[(Expression, Expression)],
) -> Expression
fn subs_multiple( &self, substitutions: &[(Expression, Expression)], ) -> Expression
Apply multiple substitutions simultaneously
This is more efficient than chaining multiple subs() calls because
it performs all substitutions in a single tree traversal.
§Arguments
substitutions- Slice of (old, new) expression pairs
§Examples
use mathhook_core::prelude::*;
use mathhook_core::pattern::Substitutable;
let x = symbol!(x);
let y = symbol!(y);
let expr = Expression::add(vec![
Expression::symbol(x.clone()),
Expression::symbol(y.clone())
]);
let result = expr.subs_multiple(&[
(Expression::symbol(x.clone()), Expression::integer(1)),
(Expression::symbol(y.clone()), Expression::integer(2)),
]);
let expected = Expression::add(vec![
Expression::integer(1),
Expression::integer(2)
]);
assert_eq!(result, expected);