oxilean_codegen/opt_algebraic/
types.rs1use std::collections::HashMap;
4
5#[allow(dead_code)]
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum AlgExpr {
9 Const(i64),
11 Var(String),
13 Add(Box<AlgExpr>, Box<AlgExpr>),
15 Sub(Box<AlgExpr>, Box<AlgExpr>),
17 Mul(Box<AlgExpr>, Box<AlgExpr>),
19 Div(Box<AlgExpr>, Box<AlgExpr>),
21 Neg(Box<AlgExpr>),
23 Pow(Box<AlgExpr>, Box<AlgExpr>),
25 Mod(Box<AlgExpr>, Box<AlgExpr>),
27}
28
29#[allow(dead_code)]
32#[derive(Debug, Clone)]
33pub struct SimplRule {
34 pub name: String,
36 pub pattern: String,
38 pub replacement: String,
40}
41
42impl SimplRule {
43 pub fn new(
45 name: impl Into<String>,
46 pattern: impl Into<String>,
47 replacement: impl Into<String>,
48 ) -> Self {
49 SimplRule {
50 name: name.into(),
51 pattern: pattern.into(),
52 replacement: replacement.into(),
53 }
54 }
55}
56
57#[allow(dead_code)]
59#[derive(Debug, Clone)]
60pub struct SimplResult {
61 pub expr: AlgExpr,
63 pub steps: Vec<String>,
65 pub reduced: bool,
67}
68
69#[allow(dead_code)]
71#[derive(Debug, Clone)]
72pub struct AlgSimplConfig {
73 pub max_passes: usize,
75 pub fold_constants: bool,
77 pub expand: bool,
79 pub factor: bool,
81}
82
83impl Default for AlgSimplConfig {
84 fn default() -> Self {
85 AlgSimplConfig {
86 max_passes: 20,
87 fold_constants: true,
88 expand: false,
89 factor: false,
90 }
91 }
92}
93
94#[allow(dead_code)]
96#[derive(Debug, Clone, Default)]
97pub struct SimplStats {
98 pub rules_applied: usize,
100 pub passes_completed: usize,
102 pub size_before: usize,
104 pub size_after: usize,
106}
107
108pub fn builtin_rules() -> Vec<SimplRule> {
110 vec![
111 SimplRule::new("add_zero_right", "x + 0", "x"),
112 SimplRule::new("add_zero_left", "0 + x", "x"),
113 SimplRule::new("mul_one_right", "x * 1", "x"),
114 SimplRule::new("mul_one_left", "1 * x", "x"),
115 SimplRule::new("mul_zero_right", "x * 0", "0"),
116 SimplRule::new("mul_zero_left", "0 * x", "0"),
117 SimplRule::new("sub_zero", "x - 0", "x"),
118 SimplRule::new("add_self", "x + x", "2*x"),
119 SimplRule::new("sub_self", "x - x", "0"),
120 SimplRule::new("div_self", "x / x", "1"),
121 SimplRule::new("neg_zero", "0 - x", "-x"),
122 SimplRule::new("double_neg", "-(-x)", "x"),
123 SimplRule::new("pow_zero", "x ^ 0", "1"),
124 SimplRule::new("pow_one", "x ^ 1", "x"),
125 SimplRule::new("zero_pow", "0 ^ x", "0"),
126 ]
127}