pub struct Pattern<L> {
pub ast: RecExpr<ENodeOrVar<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
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: RecExpr<ENodeOrVar<L>>
The actual pattern as a RecExpr
Implementations§
Trait Implementations§
Source§impl<L, A> Applier<L, A> for Pattern<L>
impl<L, A> Applier<L, A> for Pattern<L>
Source§fn get_pattern_ast(&self) -> Option<&RecExpr<ENodeOrVar<L>>>
fn get_pattern_ast(&self) -> Option<&RecExpr<ENodeOrVar<L>>>
Source§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>
Source§impl<L, A> Searcher<L, A> for Pattern<L>
impl<L, A> Searcher<L, A> for Pattern<L>
Source§fn get_pattern_ast(&self) -> Option<&RecExpr<ENodeOrVar<L>>>
fn get_pattern_ast(&self) -> Option<&RecExpr<ENodeOrVar<L>>>
Source§fn search(&self, egraph: &EGraph<L, A>) -> Vec<SearchMatches<'_, L>>
fn search(&self, egraph: &EGraph<L, A>) -> Vec<SearchMatches<'_, L>>
EGraph
, returning a list of all the
SearchMatches
where something was found.
This just calls search_eclass
on each eclass.