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
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![a22, a11]);
Fields§
§ast: PatternAst<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<&PatternAst<L>>
fn get_pattern_ast(&self) -> Option<&PatternAst<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: Language> From<RecExpr<ENodeOrVar<L>>> for Pattern<L>
impl<L: Language> From<RecExpr<ENodeOrVar<L>>> for Pattern<L>
Source§fn from(ast: PatternAst<L>) -> Self
fn from(ast: PatternAst<L>) -> Self
Source§impl<L: Language, A: Analysis<L>> Searcher<L, A> for Pattern<L>
impl<L: Language, A: Analysis<L>> Searcher<L, A> for Pattern<L>
Source§fn get_pattern_ast(&self) -> Option<&PatternAst<L>>
fn get_pattern_ast(&self) -> Option<&PatternAst<L>>
Source§fn search_with_limit(
&self,
egraph: &EGraph<L, A>,
limit: usize,
) -> Vec<SearchMatches<'_, L>>
fn search_with_limit( &self, egraph: &EGraph<L, A>, limit: usize, ) -> Vec<SearchMatches<'_, L>>
search
, but return at most limit
many matches.Source§fn search_eclass_with_limit(
&self,
egraph: &EGraph<L, A>,
eclass: Id,
limit: usize,
) -> Option<SearchMatches<'_, L>>
fn search_eclass_with_limit( &self, egraph: &EGraph<L, A>, eclass: Id, limit: usize, ) -> Option<SearchMatches<'_, L>>
Source§fn search_eclass(
&self,
egraph: &EGraph<L, N>,
eclass: Id,
) -> Option<SearchMatches<'_, L>>
fn search_eclass( &self, egraph: &EGraph<L, N>, eclass: Id, ) -> Option<SearchMatches<'_, L>>
Source§fn search(&self, egraph: &EGraph<L, N>) -> Vec<SearchMatches<'_, L>>
fn search(&self, egraph: &EGraph<L, N>) -> Vec<SearchMatches<'_, L>>
EGraph
, returning a list of all the
SearchMatches
where something was found.
This just calls Searcher::search_with_limit
with a big limit.impl<L: Eq> Eq for Pattern<L>
impl<L> StructuralPartialEq for Pattern<L>
Auto Trait Implementations§
impl<L> Freeze for Pattern<L>
impl<L> RefUnwindSafe for Pattern<L>where
L: RefUnwindSafe,
impl<L> Send for Pattern<L>where
L: Send,
impl<L> Sync for Pattern<L>where
L: Sync,
impl<L> Unpin for Pattern<L>where
L: Unpin,
impl<L> UnwindSafe for Pattern<L>where
L: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.