use std::collections::HashMap;
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AlgExpr {
Const(i64),
Var(String),
Add(Box<AlgExpr>, Box<AlgExpr>),
Sub(Box<AlgExpr>, Box<AlgExpr>),
Mul(Box<AlgExpr>, Box<AlgExpr>),
Div(Box<AlgExpr>, Box<AlgExpr>),
Neg(Box<AlgExpr>),
Pow(Box<AlgExpr>, Box<AlgExpr>),
Mod(Box<AlgExpr>, Box<AlgExpr>),
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct SimplRule {
pub name: String,
pub pattern: String,
pub replacement: String,
}
impl SimplRule {
pub fn new(
name: impl Into<String>,
pattern: impl Into<String>,
replacement: impl Into<String>,
) -> Self {
SimplRule {
name: name.into(),
pattern: pattern.into(),
replacement: replacement.into(),
}
}
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct SimplResult {
pub expr: AlgExpr,
pub steps: Vec<String>,
pub reduced: bool,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct AlgSimplConfig {
pub max_passes: usize,
pub fold_constants: bool,
pub expand: bool,
pub factor: bool,
}
impl Default for AlgSimplConfig {
fn default() -> Self {
AlgSimplConfig {
max_passes: 20,
fold_constants: true,
expand: false,
factor: false,
}
}
}
#[allow(dead_code)]
#[derive(Debug, Clone, Default)]
pub struct SimplStats {
pub rules_applied: usize,
pub passes_completed: usize,
pub size_before: usize,
pub size_after: usize,
}
pub fn builtin_rules() -> Vec<SimplRule> {
vec![
SimplRule::new("add_zero_right", "x + 0", "x"),
SimplRule::new("add_zero_left", "0 + x", "x"),
SimplRule::new("mul_one_right", "x * 1", "x"),
SimplRule::new("mul_one_left", "1 * x", "x"),
SimplRule::new("mul_zero_right", "x * 0", "0"),
SimplRule::new("mul_zero_left", "0 * x", "0"),
SimplRule::new("sub_zero", "x - 0", "x"),
SimplRule::new("add_self", "x + x", "2*x"),
SimplRule::new("sub_self", "x - x", "0"),
SimplRule::new("div_self", "x / x", "1"),
SimplRule::new("neg_zero", "0 - x", "-x"),
SimplRule::new("double_neg", "-(-x)", "x"),
SimplRule::new("pow_zero", "x ^ 0", "1"),
SimplRule::new("pow_one", "x ^ 1", "x"),
SimplRule::new("zero_pow", "0 ^ x", "0"),
]
}