Struct egg::Pattern

source ·
pub struct Pattern<L> {
    pub ast: PatternAst<L>,
    /* private fields */
}
Expand description

A pattern that can function as either a Searcher or Applier.

A Pattern is essentially a for-all quantified expression with Vars as the variables (in the logical sense).

When creating a Rewrite, the most common thing to use as either the left hand side (the Searcher) or the right hand side (the Applier) is a Pattern.

As a Searcher, a Pattern does the intuitive thing. Here is a somewhat verbose formal-ish statement: Searching for a pattern in an egraph yields substitutions (Substs) s such that, for any s’—where instead of mapping a variables to an eclass as s does, s’ maps a variable to an arbitrary expression represented by that eclass—p[s’] (the pattern under substitution s’) is also represented by the egraph.

As an Applier, a Pattern performs the given substitution and adds the result to the EGraph.

Importantly, Pattern implements FromStr if the Language does. This is probably how you’ll create most Patterns.

use egg::*;
define_language! {
    enum Math {
        Num(i32),
        "+" = Add([Id; 2]),
    }
}

let mut egraph = EGraph::<Math, ()>::default();
let a11 = egraph.add_expr(&"(+ 1 1)".parse().unwrap());
let a22 = egraph.add_expr(&"(+ 2 2)".parse().unwrap());

// use Var syntax (leading question mark) to get a
// variable in the Pattern
let same_add: Pattern<Math> = "(+ ?a ?a)".parse().unwrap();

// Rebuild before searching
egraph.rebuild();

// This is the search method from the Searcher trait
let matches = same_add.search(&egraph);
let matched_eclasses: Vec<Id> = matches.iter().map(|m| m.eclass).collect();
assert_eq!(matched_eclasses, vec![a11, a22]);

Fields§

§ast: PatternAst<L>

The actual pattern as a RecExpr

Implementations§

Creates a new pattern from the given pattern ast.

Returns a list of the Vars in this pattern.

Pretty print this pattern as a sexp with the given width

Trait Implementations§

For patterns, get the ast directly as a reference.
Apply many substititions. Read more
Apply a single substitition. Read more
Returns a list of variables that this Applier assumes are bound. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
For patterns, return the ast directly as a reference
Similar to search, but return at most limit many matches. Read more
Similar to search_eclass, but return at most limit many matches. Read more
Returns a list of the variables bound by this Searcher
Search one eclass, returning None if no matches can be found. This should not return a SearchMatches with no substs. Read more
Search the whole EGraph, returning a list of all the SearchMatches where something was found. This just calls search_eclass on each eclass. Read more
Returns the number of matches in the e-graph
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.