[][src]Struct egg::SimpleRunner

pub struct SimpleRunner { /* fields omitted */ }

A reasonable default Runner.

SimpleRunner is a Runner, so it runs rewrites over an EGraph. This implementation offers several conveniences to prevent rewriting from behaving badly and eating your computer:

  • Saturation checking

    SimpleRunner checks to see if any of the rules added anything new to the EGraph. If none did, then it stops, returning SimpleRunnerError::Saturated.

  • Iteration limits

    You can set a upper limit of iterations to do in case the search doesn't stop for some other reason. If this limit is hit, it stops with SimpleRunnerError::IterationLimit.

  • EGraph size limit

    You can set a upper limit on the number of enodes in the egraph. If this limit is hit, it stops with SimpleRunnerError::NodeLimit.

  • Rule backoff

    Some rules enable themselves, blowing up the EGraph and preventing other rewrites from running as many times. To prevent this, SimpleRunner implements exponentional rule backoff.

    For each rewrite, there exists a configurable initial match limit. If a rewrite search yield more than this limit, then we ban this rule for number of iterations, double its limit, and double the time it will be banned next time.

    This seems effective at preventing explosive rules like associativity from taking an unfair amount of resources.

Example

use egg::{*, rewrite as rw};

define_language! {
    enum SimpleLanguage {
        Num(i32),
        Add = "+",
        Mul = "*",
        Symbol(String),
    }
}

let rules: &[Rewrite<SimpleLanguage, ()>] = &[
    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 start = "(+ 0 (* 1 foo))".parse().unwrap();
// SimpleRunner is customizable in the builder pattern style.
let (egraph, report) = SimpleRunner::default()
    .with_iter_limit(10)
    .with_node_limit(10_000)
    .run_expr(start, &rules);
println!(
    "Stopped after {} iterations, reason: {:?}",
    report.iterations.len(),
    report.stop_reason
);

Methods

impl SimpleRunner[src]

pub fn with_iter_limit(self, iter_limit: usize) -> Self[src]

Sets the iteration limit. Default: 30

pub fn with_node_limit(self, node_limit: usize) -> Self[src]

Sets the egraph size limit (in enodes). Default: 10,000

pub fn with_initial_match_limit(self, initial_match_limit: usize) -> Self[src]

Sets the initial match limit before a rule is banned. Default: 1,000

Setting this to a really big number will effectively disable rule backoff.

Trait Implementations

impl Default for SimpleRunner[src]

impl<L, M> Runner<L, M> for SimpleRunner where
    L: Language,
    M: Metadata<L>, 
[src]

type Error = SimpleRunnerError

The type of an error that should stop the runner. Read more

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.