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
use Debug;
use ExactSizeIterator;
use crate::;
/** Arbitrary data associated with an [`EClass`].
`egg` allows you to associate arbitrary data with each eclass.
The [`Metadata`] allows that data to behave well even across eclasses merges.
[`Metadata`] can prove useful in many situtations.
One common one is constant folding, a kind of partial evaluation.
In that case, the metadata is basically `Option<L>`, storing
the cheapest constant expression (if any) that's equivalent to the
enodes in this eclass.
See the test files [`math.rs`] and [`prop.rs`] for more complex
examples on this usage of [`Metadata`].
If you don't care about [`Metadata`], `()` implements it trivally,
just use that.
# Example
```
use egg::{*, rewrite as rw};
define_language! {
enum EasyMath {
Num(i32),
Add = "+",
Mul = "*",
Variable(String),
}
}
use EasyMath::*;
type Meta = Option<i32>;
impl Metadata<EasyMath> for Meta {
type Error = ();
fn merge(&self, other: &Self) -> Self {
self.clone().and(other.clone())
}
fn make(enode: ENode<EasyMath, &Self>) -> Self {
let c = |i: usize| enode.children[i].clone();
match enode.op {
Num(i) => Some(i),
Add => Some(c(0)? + c(1)?),
Mul => Some(c(0)? * c(1)?),
_ => None,
}
}
fn modify(eclass: &mut EClass<EasyMath, Self>) {
if let Some(i) = eclass.metadata {
eclass.nodes.push(enode!(Num(i)))
}
}
}
let rules: &[Rewrite<EasyMath, Meta>] = &[
rw!("commute-add"; "(+ ?a ?b)" => "(+ ?b ?a)"),
rw!("commute-mul"; "(* ?a ?b)" => "(* ?b ?a)"),
rw!("add-0"; "(+ ?a 0)" => "?a"),
rw!("mul-0"; "(* ?a 0)" => "0"),
rw!("mul-1"; "(* ?a 1)" => "?a"),
];
let mut egraph = EGraph::<EasyMath, Meta>::default();
let whole_thing = egraph.add_expr(&"(+ 0 (* (+ 4 -3) foo))".parse().unwrap());
SimpleRunner::default().run(&mut egraph, &rules);
let just_foo = egraph.add(enode!(Variable("foo".into())));
assert_eq!(egraph.find(whole_thing), egraph.find(just_foo));
```
[`Metadata`]: trait.Metadata.html
[`EClass`]: struct.EClass.html
[`ENode`]: struct.ENode.html
[`math.rs`]: https://github.com/mwillsey/egg/blob/master/tests/math.rs
[`prop.rs`]: https://github.com/mwillsey/egg/blob/master/tests/prop.rs
*/
/// An equivalence class of [`ENode`]s
///
/// [`ENode`]: struct.ENode.html