1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use crate::ast::Expr;
use std::collections::BTreeMap;
pub trait Tactic {
fn apply(&self, expr: Expr) -> Expr;
}
pub struct Simplifier;
impl Tactic for Simplifier {
fn apply(&self, expr: Expr) -> Expr {
Self::simplify(expr)
}
}
pub struct SolveEqs;
impl Tactic for SolveEqs {
fn apply(&self, expr: Expr) -> Expr {
match expr {
Expr::And(args) => {
let mut substs = BTreeMap::new();
let mut remaining = Vec::new();
for arg in args {
if let Expr::Eq(a, b) = &arg {
let (var, term) = match (&**a, &**b) {
(Expr::Var(name, _), term) if !term.contains_var(name) => {
(name.clone(), term.clone())
}
(term, Expr::Var(name, _)) if !term.contains_var(name) => {
(name.clone(), term.clone())
}
_ => {
remaining.push(arg);
continue;
}
};
substs.insert(var, term);
} else {
remaining.push(arg);
}
}
if substs.is_empty() {
return Expr::And(remaining);
}
let mut finalized = Vec::new();
for expr in remaining {
finalized.push(expr.substitute(&substs));
}
Expr::And(finalized)
}
_ => expr,
}
}
}
impl Simplifier {
pub fn simplify(expr: Expr) -> Expr {
// ... (el código existente de simplify)
match expr {
Expr::And(args) => {
let mut new_args = Vec::new();
for arg in args {
let simplified = Self::simplify(arg);
match simplified {
Expr::Bool(true) => continue,
Expr::Bool(false) => return Expr::Bool(false),
Expr::And(inner_args) => new_args.extend(inner_args),
_ => new_args.push(simplified),
}
}
if new_args.is_empty() {
return Expr::Bool(true);
}
if new_args.len() == 1 {
return new_args.remove(0);
}
new_args.sort_unstable();
new_args.dedup();
if new_args.len() == 1 {
return new_args.remove(0);
}
Expr::And(new_args)
}
Expr::Or(args) => {
let mut new_args = Vec::new();
for arg in args {
let simplified = Self::simplify(arg);
match simplified {
Expr::Bool(false) => continue,
Expr::Bool(true) => return Expr::Bool(true),
Expr::Or(inner_args) => new_args.extend(inner_args),
_ => new_args.push(simplified),
}
}
if new_args.is_empty() {
return Expr::Bool(false);
}
if new_args.len() == 1 {
return new_args.remove(0);
}
new_args.sort_unstable();
new_args.dedup();
if new_args.len() == 1 {
return new_args.remove(0);
}
Expr::Or(new_args)
}
Expr::Not(inner) => {
let simplified = Self::simplify(*inner);
match simplified {
Expr::Bool(b) => Expr::Bool(!b),
Expr::Not(double_inner) => *double_inner,
_ => Expr::Not(Box::new(simplified)),
}
}
Expr::Add(args) => {
let mut new_args = Vec::new();
let mut const_sum = 0;
for arg in args {
let simplified = Self::simplify(arg);
match simplified {
Expr::Int(val) => const_sum += val,
Expr::Add(inner_args) => {
for ia in inner_args {
if let Expr::Int(v) = ia {
const_sum += v;
} else {
new_args.push(ia);
}
}
}
_ => new_args.push(simplified),
}
}
if new_args.is_empty() {
return Expr::Int(const_sum);
}
if const_sum != 0 {
new_args.push(Expr::Int(const_sum));
}
if new_args.len() == 1 {
return new_args.remove(0);
}
new_args.sort_unstable();
Expr::Add(new_args)
}
Expr::Sub(args) => {
if args.is_empty() {
return Expr::Int(0);
}
let mut simplified_args: Vec<Expr> = args.into_iter().map(Self::simplify).collect();
if simplified_args.len() == 2 && simplified_args[1] == Expr::Int(0) {
return simplified_args.remove(0);
}
if simplified_args.len() == 2 && simplified_args[0] == simplified_args[1] {
return Expr::Int(0);
}
if simplified_args.len() == 2 {
if let (Expr::Int(a), Expr::Int(b)) = (&simplified_args[0], &simplified_args[1])
{
return Expr::Int(a - b);
}
}
Expr::Sub(simplified_args)
}
Expr::Mul(args) => {
let mut new_args = Vec::new();
let mut const_prod = 1;
let mut has_const = false;
for arg in args {
let simplified = Self::simplify(arg);
match simplified {
Expr::Int(0) => return Expr::Int(0),
Expr::Int(1) => continue,
Expr::Int(val) => {
const_prod *= val;
has_const = true;
}
Expr::Mul(inner_args) => {
for ia in inner_args {
if let Expr::Int(v) = ia {
if v == 0 {
return Expr::Int(0);
}
if v != 1 {
const_prod *= v;
has_const = true;
}
} else {
new_args.push(ia);
}
}
}
_ => new_args.push(simplified),
}
}
if new_args.is_empty() {
return Expr::Int(if has_const { const_prod } else { 1 });
}
if const_prod != 1 || (new_args.is_empty() && has_const) {
new_args.push(Expr::Int(const_prod));
}
if new_args.len() == 1 {
return new_args.remove(0);
}
new_args.sort_unstable();
Expr::Mul(new_args)
}
Expr::Lt(a, b) => {
let sa = Self::simplify(*a);
let sb = Self::simplify(*b);
if let (Expr::Int(va), Expr::Int(vb)) = (&sa, &sb) {
return Expr::Bool(va < vb);
}
Expr::Lt(Box::new(sa), Box::new(sb))
}
Expr::Le(a, b) => {
let sa = Self::simplify(*a);
let sb = Self::simplify(*b);
if let (Expr::Int(va), Expr::Int(vb)) = (&sa, &sb) {
return Expr::Bool(va <= vb);
}
Expr::Le(Box::new(sa), Box::new(sb))
}
Expr::Gt(a, b) => {
let sa = Self::simplify(*a);
let sb = Self::simplify(*b);
if let (Expr::Int(va), Expr::Int(vb)) = (&sa, &sb) {
return Expr::Bool(va > vb);
}
Expr::Gt(Box::new(sa), Box::new(sb))
}
Expr::Ge(a, b) => {
let sa = Self::simplify(*a);
let sb = Self::simplify(*b);
if let (Expr::Int(va), Expr::Int(vb)) = (&sa, &sb) {
return Expr::Bool(va >= vb);
}
Expr::Ge(Box::new(sa), Box::new(sb))
}
Expr::Eq(a, b) => {
let mut sa = Self::simplify(*a);
let mut sb = Self::simplify(*b);
if sa == sb {
return Expr::Bool(true);
}
if let (Expr::Bool(va), Expr::Bool(vb)) = (&sa, &sb) {
return Expr::Bool(va == vb);
}
if let (Expr::Int(va), Expr::Int(vb)) = (&sa, &sb) {
return Expr::Bool(va == vb);
}
if sa > sb {
std::mem::swap(&mut sa, &mut sb);
}
Expr::Eq(Box::new(sa), Box::new(sb))
}
Expr::Implies(a, b) => {
Expr::Implies(Box::new(Self::simplify(*a)), Box::new(Self::simplify(*b)))
}
Expr::Ite(c, t, e) => {
let sc = Self::simplify(*c);
if let Expr::Bool(val) = sc {
if val {
return Self::simplify(*t);
} else {
return Self::simplify(*e);
}
}
Expr::Ite(
Box::new(sc),
Box::new(Self::simplify(*t)),
Box::new(Self::simplify(*e)),
)
}
_ => expr,
}
}
}
pub struct TacticEngine {
tactics: Vec<Box<dyn Tactic>>,
}
impl Default for TacticEngine {
fn default() -> Self {
Self::new()
}
}
impl TacticEngine {
pub fn new() -> Self {
Self {
tactics: Vec::new(),
}
}
pub fn add_tactic(&mut self, tactic: Box<dyn Tactic>) {
self.tactics.push(tactic);
}
pub fn apply(&self, mut expr: Expr) -> Expr {
for tactic in &self.tactics {
expr = tactic.apply(expr);
}
expr
}
}