[−][src]Crate egg
EGraph
s (and almost everything else in this crate) are
parameterized over the language given by the user (by implementing
the Language
trait).
If your Language implements FromStr
(and Languages derived using
define_language!
do), you can easily create RecExpr
s to add to
an EGraph
.
Add egg
to your Cargo.toml
like this:
[dependencies]
egg = "0.4.0"
Example
use egg::{*, rewrite as rw}; define_language! { enum SimpleLanguage { Num(i32), "+" = Add([Id; 2]), "*" = Mul([Id; 2]), // language items are parsed in order, and we want symbol to // be a fallback, so we put it last Symbol(String), } } let rules: &[Rewrite<SimpleLanguage, ()>] = &[ rw!("commute-add"; "(+ ?a ?b)" => "(+ ?b ?a)"), rw!("commute-mul"; "(* ?a ?b)" => "(* ?b ?a)"), rw!("add-0"; "(+ ?a 0)" => "?a"), rw!("mul-0"; "(* ?a 0)" => "0"), rw!("mul-1"; "(* ?a 1)" => "?a"), ]; let start = "(+ 0 (* 1 foo))".parse().unwrap(); let runner = Runner::default().with_expr(&start).run(&rules); println!( "Stopped after {} iterations, reason: {:?}", runner.iterations.len(), runner.stop_reason );
Macros
define_language | A macro to easily create a |
rewrite | A macro to easily make |
test_fn | Make a test function |
Structs
AstDepth | A simple |
AstSize | A simple |
BackoffScheduler | A |
ConditionEqual | A |
ConditionalApplier | |
Dot | A wrapper for an |
EClass | An equivalence class of enodes. |
EGraph | A data structure to keep track of equalities between expressions. |
Extractor | |
Iteration | Data generated by running a |
Pattern | A pattern that can function as either a |
RecExpr | A recursive expression from a user-defined |
Rewrite | A rewrite that searches for the lefthand side and applies the righthand side. |
Runner | Faciliates running rewrites over an |
SearchMatches | The result of searching a |
SimpleScheduler | A very simple |
StringLang | A simple language used for testing. |
Subst | |
Var |
Enums
StopReason | Error returned by |
Traits
Analysis | Arbitrary data associated with an |
Applier | The righthand side of a |
Condition | A condition to check in a |
CostFunction | A cost function that can be used by an |
IterationData | Custom data to inject into the |
Language | Trait that defines a Language whose terms will be in the |
RewriteScheduler | |
Searcher | The lefthand side of a |
Functions
merge_if_different | Replace the first with second value if they are different returning whether or not something was done. |
Type Definitions
Id |