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 309 310 311 312 313 314 315
use std::cmp::Ordering;
use std::fmt::Debug;
use crate::util::HashMap;
use crate::{Analysis, EClass, EGraph, Id, Language, RecExpr};
/** Extracting a single [`RecExpr`] from an [`EGraph`].
```
use egg::*;
define_language! {
enum SimpleLanguage {
Num(i32),
"+" = Add([Id; 2]),
"*" = Mul([Id; 2]),
}
}
let rules: &[Rewrite<SimpleLanguage, ()>] = &[
rewrite!("commute-add"; "(+ ?a ?b)" => "(+ ?b ?a)"),
rewrite!("commute-mul"; "(* ?a ?b)" => "(* ?b ?a)"),
rewrite!("add-0"; "(+ ?a 0)" => "?a"),
rewrite!("mul-0"; "(* ?a 0)" => "0"),
rewrite!("mul-1"; "(* ?a 1)" => "?a"),
];
let start = "(+ 0 (* 1 10))".parse().unwrap();
let runner = Runner::default().with_expr(&start).run(rules);
let (egraph, root) = (runner.egraph, runner.roots[0]);
let mut extractor = Extractor::new(&egraph, AstSize);
let (best_cost, best) = extractor.find_best(root);
assert_eq!(best_cost, 1);
assert_eq!(best, "10".parse().unwrap());
```
**/
#[derive(Debug)]
pub struct Extractor<'a, CF: CostFunction<L>, L: Language, N: Analysis<L>> {
cost_function: CF,
costs: HashMap<Id, (CF::Cost, L)>,
egraph: &'a EGraph<L, N>,
}
/** A cost function that can be used by an [`Extractor`].
To extract an expression from an [`EGraph`], the [`Extractor`]
requires a cost function to performs its greedy search.
`egg` provides the simple [`AstSize`] and [`AstDepth`] cost functions.
The example below illustrates a silly but realistic example of
implementing a cost function that is essentially AST size weighted by
the operator:
```
# use egg::*;
struct SillyCostFn;
impl CostFunction<SymbolLang> for SillyCostFn {
type Cost = f64;
fn cost<C>(&mut self, enode: &SymbolLang, mut costs: C) -> Self::Cost
where
C: FnMut(Id) -> Self::Cost
{
let op_cost = match enode.op.as_str() {
"foo" => 100.0,
"bar" => 0.7,
_ => 1.0
};
enode.fold(op_cost, |sum, id| sum + costs(id))
}
}
let e: RecExpr<SymbolLang> = "(do_it foo bar baz)".parse().unwrap();
assert_eq!(SillyCostFn.cost_rec(&e), 102.7);
assert_eq!(AstSize.cost_rec(&e), 4);
assert_eq!(AstDepth.cost_rec(&e), 2);
```
If you'd like to access the [`Analysis`] data or anything else in the e-graph,
you can put a reference to the e-graph in your [`CostFunction`]:
```
# use egg::*;
# type MyAnalysis = ();
struct EGraphCostFn<'a> {
egraph: &'a EGraph<SymbolLang, MyAnalysis>,
}
impl<'a> CostFunction<SymbolLang> for EGraphCostFn<'a> {
type Cost = usize;
fn cost<C>(&mut self, enode: &SymbolLang, mut costs: C) -> Self::Cost
where
C: FnMut(Id) -> Self::Cost
{
// use self.egraph however you want here
println!("the egraph has {} classes", self.egraph.number_of_classes());
return 1
}
}
let mut egraph = EGraph::<SymbolLang, MyAnalysis>::default();
let id = egraph.add_expr(&"(foo bar)".parse().unwrap());
let cost_func = EGraphCostFn { egraph: &egraph };
let mut extractor = Extractor::new(&egraph, cost_func);
let _ = extractor.find_best(id);
```
Note that a particular e-class might occur in an expression multiple times.
This means that pathological, but nevertheless realistic cases
might overflow `usize` if you implement a cost function like [`AstSize`],
even if the actual [`RecExpr`] fits compactly in memory.
You might want to use [`saturating_add`](u64::saturating_add) to
ensure your cost function is still monotonic in this situation.
**/
pub trait CostFunction<L: Language> {
/// The `Cost` type. It only requires `PartialOrd` so you can use
/// floating point types, but failed comparisons (`NaN`s) will
/// result in a panic.
type Cost: PartialOrd + Debug + Clone;
/// Calculates the cost of an enode whose children are `Cost`s.
///
/// For this to work properly, your cost function should be
/// _monotonic_, i.e. `cost` should return a `Cost` greater than
/// any of the child costs of the given enode.
fn cost<C>(&mut self, enode: &L, costs: C) -> Self::Cost
where
C: FnMut(Id) -> Self::Cost;
/// Calculates the total cost of a [`RecExpr`].
///
/// As provided, this just recursively calls `cost` all the way
/// down the [`RecExpr`].
///
fn cost_rec(&mut self, expr: &RecExpr<L>) -> Self::Cost {
let mut costs: HashMap<Id, Self::Cost> = HashMap::default();
for (i, node) in expr.as_ref().iter().enumerate() {
let cost = self.cost(node, |i| costs[&i].clone());
costs.insert(Id::from(i), cost);
}
let last_id = Id::from(expr.as_ref().len() - 1);
costs[&last_id].clone()
}
}
/** A simple [`CostFunction`] that counts total AST size.
```
# use egg::*;
let e: RecExpr<SymbolLang> = "(do_it foo bar baz)".parse().unwrap();
assert_eq!(AstSize.cost_rec(&e), 4);
```
**/
#[derive(Debug)]
pub struct AstSize;
impl<L: Language> CostFunction<L> for AstSize {
type Cost = usize;
fn cost<C>(&mut self, enode: &L, mut costs: C) -> Self::Cost
where
C: FnMut(Id) -> Self::Cost,
{
enode.fold(1, |sum, id| sum.saturating_add(costs(id)))
}
}
/** A simple [`CostFunction`] that counts maximum AST depth.
```
# use egg::*;
let e: RecExpr<SymbolLang> = "(do_it foo bar baz)".parse().unwrap();
assert_eq!(AstDepth.cost_rec(&e), 2);
```
**/
#[derive(Debug)]
pub struct AstDepth;
impl<L: Language> CostFunction<L> for AstDepth {
type Cost = usize;
fn cost<C>(&mut self, enode: &L, mut costs: C) -> Self::Cost
where
C: FnMut(Id) -> Self::Cost,
{
1 + enode.fold(0, |max, id| max.max(costs(id)))
}
}
fn cmp<T: PartialOrd>(a: &Option<T>, b: &Option<T>) -> Ordering {
// None is high
match (a, b) {
(None, None) => Ordering::Equal,
(None, Some(_)) => Ordering::Greater,
(Some(_), None) => Ordering::Less,
(Some(a), Some(b)) => a.partial_cmp(b).unwrap(),
}
}
impl<'a, CF, L, N> Extractor<'a, CF, L, N>
where
CF: CostFunction<L>,
L: Language,
N: Analysis<L>,
{
/// Create a new `Extractor` given an `EGraph` and a
/// `CostFunction`.
///
/// The extraction does all the work on creation, so this function
/// performs the greedy search for cheapest representative of each
/// eclass.
pub fn new(egraph: &'a EGraph<L, N>, cost_function: CF) -> Self {
let costs = HashMap::default();
let mut extractor = Extractor {
costs,
egraph,
cost_function,
};
extractor.find_costs();
extractor
}
/// Find the cheapest (lowest cost) represented `RecExpr` in the
/// given eclass.
pub fn find_best(&self, eclass: Id) -> (CF::Cost, RecExpr<L>) {
let (cost, root) = self.costs[&self.egraph.find(eclass)].clone();
let expr = root.build_recexpr(|id| self.find_best_node(id).clone());
(cost, expr)
}
/// Find the cheapest e-node in the given e-class.
pub fn find_best_node(&self, eclass: Id) -> &L {
&self.costs[&self.egraph.find(eclass)].1
}
/// Find the cost of the term that would be extracted from this e-class.
pub fn find_best_cost(&self, eclass: Id) -> CF::Cost {
let (cost, _) = &self.costs[&self.egraph.find(eclass)];
cost.clone()
}
fn node_total_cost(&mut self, node: &L) -> Option<CF::Cost> {
let eg = &self.egraph;
let has_cost = |id| self.costs.contains_key(&eg.find(id));
if node.all(has_cost) {
let costs = &self.costs;
let cost_f = |id| costs[&eg.find(id)].0.clone();
Some(self.cost_function.cost(node, cost_f))
} else {
None
}
}
fn find_costs(&mut self) {
let mut did_something = true;
while did_something {
did_something = false;
for class in self.egraph.classes() {
let pass = self.make_pass(class);
match (self.costs.get(&class.id), pass) {
(None, Some(new)) => {
self.costs.insert(class.id, new);
did_something = true;
}
(Some(old), Some(new)) if new.0 < old.0 => {
self.costs.insert(class.id, new);
did_something = true;
}
_ => (),
}
}
}
for class in self.egraph.classes() {
if !self.costs.contains_key(&class.id) {
log::warn!(
"Failed to compute cost for eclass {}: {:?}",
class.id,
class.nodes
)
}
}
}
fn make_pass(&mut self, eclass: &EClass<L, N::Data>) -> Option<(CF::Cost, L)> {
let (cost, node) = eclass
.iter()
.map(|n| (self.node_total_cost(n), n))
.min_by(|a, b| cmp(&a.0, &b.0))
.unwrap_or_else(|| panic!("Can't extract, eclass is empty: {:#?}", eclass));
cost.map(|c| (c, node.clone()))
}
}
#[cfg(test)]
mod tests {
use crate::*;
#[test]
fn ast_size_overflow() {
let rules: &[Rewrite<SymbolLang, ()>] =
&[rewrite!("explode"; "(meow ?a)" => "(meow (meow ?a ?a))")];
let start = "(meow 42)".parse().unwrap();
let runner = Runner::default()
.with_iter_limit(100)
.with_expr(&start)
.run(rules);
let extractor = Extractor::new(&runner.egraph, AstSize);
let (_, best_expr) = extractor.find_best(runner.roots[0]);
assert_eq!(best_expr, start);
}
}