[][src]Struct egg::Pattern

pub struct Pattern<L> {
    pub ast: PatternAst<L>,
    // some fields omitted
}

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

impl<L: Language> Pattern<L>[src]

pub fn vars(&self) -> Vec<Var>[src]

Returns a list of the Vars in this pattern.

pub fn pretty(&self, width: usize) -> String[src]

Pretty print this pattern as a sexp with the given width

Trait Implementations

impl<L, A> Applier<L, A> for Pattern<L> where
    L: Language,
    A: Analysis<L>, 
[src]

impl<L: Clone> Clone for Pattern<L>[src]

impl<L: Debug> Debug for Pattern<L>[src]

impl<L: Language> Display for Pattern<L>[src]

impl<'a, L: Language> From<&'a [L]> for Pattern<L>[src]

impl<'a, L: Language> From<RecExpr<ENodeOrVar<L>>> for Pattern<L>[src]

impl<L: Language> FromStr for Pattern<L>[src]

type Err = String

The associated error which can be returned from parsing.

impl<L: PartialEq> PartialEq<Pattern<L>> for Pattern<L>[src]

impl<L: Language, A: Analysis<L>> Searcher<L, A> for Pattern<L>[src]

impl<L> StructuralPartialEq for Pattern<L>[src]

impl<L: Language> TryFrom<Pattern<L>> for RecExpr<L>[src]

type Error = Var

The type returned in the event of a conversion error.

Auto Trait Implementations

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

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> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[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.