Struct egg::Extractor

source ·
pub struct Extractor<'a, CF: CostFunction<L>, L: Language, N: Analysis<L>> { /* private fields */ }
Expand description

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());

Implementations§

source§

impl<'a, CF, L, N> Extractor<'a, CF, L, N>where CF: CostFunction<L>, L: Language, N: Analysis<L>,

source

pub fn new(egraph: &'a EGraph<L, N>, cost_function: CF) -> Self

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.

source

pub fn find_best(&self, eclass: Id) -> (CF::Cost, RecExpr<L>)

Find the cheapest (lowest cost) represented RecExpr in the given eclass.

source

pub fn find_best_node(&self, eclass: Id) -> &L

Find the cheapest e-node in the given e-class.

source

pub fn find_best_cost(&self, eclass: Id) -> CF::Cost

Find the cost of the term that would be extracted from this e-class.

Trait Implementations§

source§

impl<'a, CF: Debug + CostFunction<L>, L: Debug + Language, N: Debug + Analysis<L>> Debug for Extractor<'a, CF, L, N>where CF::Cost: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, CF, L, N> RefUnwindSafe for Extractor<'a, CF, L, N>where CF: RefUnwindSafe, L: RefUnwindSafe, N: RefUnwindSafe, <CF as CostFunction<L>>::Cost: RefUnwindSafe, <N as Analysis<L>>::Data: RefUnwindSafe, <L as DiscriminantKind>::Discriminant: RefUnwindSafe,

§

impl<'a, CF, L, N> Send for Extractor<'a, CF, L, N>where CF: Send, L: Send + Sync, N: Sync, <CF as CostFunction<L>>::Cost: Send, <N as Analysis<L>>::Data: Sync,

§

impl<'a, CF, L, N> Sync for Extractor<'a, CF, L, N>where CF: Sync, L: Sync, N: Sync, <CF as CostFunction<L>>::Cost: Sync, <N as Analysis<L>>::Data: Sync,

§

impl<'a, CF, L, N> Unpin for Extractor<'a, CF, L, N>where CF: Unpin, L: Unpin, <CF as CostFunction<L>>::Cost: Unpin,

§

impl<'a, CF, L, N> UnwindSafe for Extractor<'a, CF, L, N>where CF: UnwindSafe, L: UnwindSafe + RefUnwindSafe, N: RefUnwindSafe, <CF as CostFunction<L>>::Cost: UnwindSafe, <N as Analysis<L>>::Data: RefUnwindSafe, <L as DiscriminantKind>::Discriminant: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.