Struct egg::Pattern [−][src]
pub struct Pattern<L> {
pub ast: PatternAst<L>,
// some fields omitted
}
Expand description
A pattern that can function as either a Searcher
or Applier
.
A Pattern
is essentially a for-all quantified expression with
Var
s 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
(Subst
s) 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 Pattern
s.
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
Trait Implementations
For patterns, get the ast directly as a reference.
fn apply_matches(
&self,
egraph: &mut EGraph<L, A>,
matches: &[SearchMatches<'_, L>],
rule_name: Symbol
) -> Vec<Id>
fn apply_matches(
&self,
egraph: &mut EGraph<L, A>,
matches: &[SearchMatches<'_, L>],
rule_name: Symbol
) -> Vec<Id>
Apply many substititions. Read more
Apply a single substitition. Read more
Performs the conversion.
For patterns, return the ast directly as a reference
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
Search one eclass, returning None if no matches can be found. This should not return a SearchMatches with no substs. Read more
Auto Trait Implementations
impl<L> RefUnwindSafe for Pattern<L> where
L: RefUnwindSafe,
impl<L> UnwindSafe for Pattern<L> where
L: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more