Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename RecExpr to Expr #314

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
name = "egg"
readme = "README.md"
repository = "https://github.com/egraphs-good/egg"
version = "0.9.5"
version = "0.10.0"

[dependencies]
env_logger = { version = "0.9.0", default-features = false }
Expand Down
34 changes: 17 additions & 17 deletions src/egraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl<L: Language, N: Analysis<L>> EGraph<L, N> {
/// Calling this function on an uncanonical `Id` returns a representative based on the how it
/// was obtained (see [`add_uncanoncial`](EGraph::add_uncanonical),
/// [`add_expr_uncanonical`](EGraph::add_expr_uncanonical))
pub fn id_to_expr(&self, id: Id) -> RecExpr<L> {
pub fn id_to_expr(&self, id: Id) -> Expr<L> {
let mut res = Default::default();
let mut cache = Default::default();
self.id_to_expr_internal(&mut res, id, &mut cache);
Expand All @@ -353,7 +353,7 @@ impl<L: Language, N: Analysis<L>> EGraph<L, N> {

fn id_to_expr_internal(
&self,
res: &mut RecExpr<L>,
res: &mut Expr<L>,
node_id: Id,
cache: &mut HashMap<Id, Id>,
) -> Id {
Expand Down Expand Up @@ -460,8 +460,8 @@ impl<L: Language, N: Analysis<L>> EGraph<L, N> {
/// given by [`get_flat_string`](Explanation::get_flat_string) and [`get_string`](Explanation::get_string).
pub fn explain_equivalence(
&mut self,
left_expr: &RecExpr<L>,
right_expr: &RecExpr<L>,
left_expr: &Expr<L>,
right_expr: &Expr<L>,
) -> Explanation<L> {
let left = self.add_expr_uncanonical(left_expr);
let right = self.add_expr_uncanonical(right_expr);
Expand Down Expand Up @@ -503,7 +503,7 @@ impl<L: Language, N: Analysis<L>> EGraph<L, N> {
/// into the egraph and ends with the given `expr`.
/// Note that this function can be called again to explain any intermediate terms
/// used in the output [`Explanation`].
pub fn explain_existance(&mut self, expr: &RecExpr<L>) -> Explanation<L> {
pub fn explain_existance(&mut self, expr: &Expr<L>) -> Explanation<L> {
let id = self.add_expr_uncanonical(expr);
self.explain_existance_id(id)
}
Expand Down Expand Up @@ -535,7 +535,7 @@ impl<L: Language, N: Analysis<L>> EGraph<L, N> {
/// Get an explanation for why an expression matches a pattern.
pub fn explain_matches(
&mut self,
left_expr: &RecExpr<L>,
left_expr: &Expr<L>,
right_pattern: &PatternAst<L>,
subst: &Subst,
) -> Explanation<L> {
Expand Down Expand Up @@ -825,7 +825,7 @@ impl<L: Language, N: Analysis<L>> std::ops::IndexMut<Id> for EGraph<L, N> {
}

impl<L: Language, N: Analysis<L>> EGraph<L, N> {
/// Adds a [`RecExpr`] to the [`EGraph`], returning the id of the RecExpr's eclass.
/// Adds a [`Expr`] to the [`EGraph`], returning the id of the Expr's eclass.
///
/// # Example
/// ```
Expand All @@ -834,20 +834,20 @@ impl<L: Language, N: Analysis<L>> EGraph<L, N> {
/// let x = egraph.add(S::leaf("x"));
/// let y = egraph.add(S::leaf("y"));
/// let plus = egraph.add(S::new("+", vec![x, y]));
/// let plus_recexpr = "(+ x y)".parse().unwrap();
/// assert_eq!(plus, egraph.add_expr(&plus_recexpr));
/// let plus_expr = "(+ x y)".parse().unwrap();
/// assert_eq!(plus, egraph.add_expr(&plus_expr));
/// ```
///
/// [`add_expr`]: EGraph::add_expr()
pub fn add_expr(&mut self, expr: &RecExpr<L>) -> Id {
pub fn add_expr(&mut self, expr: &Expr<L>) -> Id {
let id = self.add_expr_uncanonical(expr);
self.find(id)
}

/// Similar to [`add_expr`](EGraph::add_expr) but the `Id` returned may not be canonical
///
/// Calling [`id_to_expr`](EGraph::id_to_expr) on this `Id` return a copy of `expr` when explanations are enabled
pub fn add_expr_uncanonical(&mut self, expr: &RecExpr<L>) -> Id {
pub fn add_expr_uncanonical(&mut self, expr: &Expr<L>) -> Id {
let nodes = expr.as_ref();
let mut new_ids = Vec::with_capacity(nodes.len());
let mut new_node_q = Vec::with_capacity(nodes.len());
Expand Down Expand Up @@ -962,16 +962,16 @@ impl<L: Language, N: Analysis<L>> EGraph<L, N> {
self.memo.get(enode).copied()
}

/// Lookup the eclass of the given [`RecExpr`].
/// Lookup the eclass of the given [`Expr`].
///
/// Equivalent to the last value in [`EGraph::lookup_expr_ids`].
pub fn lookup_expr(&self, expr: &RecExpr<L>) -> Option<Id> {
pub fn lookup_expr(&self, expr: &Expr<L>) -> Option<Id> {
self.lookup_expr_ids(expr)
.and_then(|ids| ids.last().copied())
}

/// Lookup the eclasses of all the nodes in the given [`RecExpr`].
pub fn lookup_expr_ids(&self, expr: &RecExpr<L>) -> Option<Vec<Id>> {
/// Lookup the eclasses of all the nodes in the given [`Expr`].
pub fn lookup_expr_ids(&self, expr: &Expr<L>) -> Option<Vec<Id>> {
let nodes = expr.as_ref();
let mut new_ids = Vec::with_capacity(nodes.len());
for node in nodes {
Expand Down Expand Up @@ -1099,11 +1099,11 @@ impl<L: Language, N: Analysis<L>> EGraph<L, N> {
id
}

/// Checks whether two [`RecExpr`]s are equivalent.
/// Checks whether two [`Expr`]s are equivalent.
/// Returns a list of id where both expression are represented.
/// In most cases, there will none or exactly one id.
///
pub fn equivs(&self, expr1: &RecExpr<L>, expr2: &RecExpr<L>) -> Vec<Id> {
pub fn equivs(&self, expr1: &Expr<L>, expr2: &Expr<L>) -> Vec<Id> {
let pat1 = Pattern::from(expr1.as_ref());
let pat2 = Pattern::from(expr2.as_ref());
let matches1 = pat1.search(self);
Expand Down
8 changes: 4 additions & 4 deletions src/explain.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Symbol;
use crate::{
util::pretty_print, Analysis, EClass, ENodeOrVar, FromOp, HashMap, HashSet, Id, Language,
PatternAst, RecExpr, Rewrite, UnionFind, Var,
util::pretty_print, Analysis, EClass, ENodeOrVar, Expr, FromOp, HashMap, HashSet, Id, Language,
PatternAst, Rewrite, UnionFind, Var,
};
use saturating::Saturating;
use std::cmp::Ordering;
Expand Down Expand Up @@ -702,8 +702,8 @@ impl<L: Language + Display + FromOp> FlatTerm<L> {
expr
}

/// Convert this FlatTerm to a RecExpr.
pub fn get_recexpr(&self) -> RecExpr<L> {
/// Convert this FlatTerm to a Expr.
pub fn get_expr(&self) -> Expr<L> {
self.remove_rewrites().to_string().parse().unwrap()
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::cmp::Ordering;
use std::fmt::Debug;

use crate::util::HashMap;
use crate::{Analysis, EClass, EGraph, Id, Language, RecExpr};
use crate::{Analysis, EClass, EGraph, Expr, Id, Language};

/** Extracting a single [`RecExpr`] from an [`EGraph`].
/** Extracting a single [`Expr`] from an [`EGraph`].

```
use egg::*;
Expand Down Expand Up @@ -71,7 +71,7 @@ impl CostFunction<SymbolLang> for SillyCostFn {
}
}

let e: RecExpr<SymbolLang> = "(do_it foo bar baz)".parse().unwrap();
let e: Expr<SymbolLang> = "(do_it foo bar baz)".parse().unwrap();
assert_eq!(SillyCostFn.cost_rec(&e), 102.7);
assert_eq!(AstSize.cost_rec(&e), 4);
assert_eq!(AstDepth.cost_rec(&e), 2);
Expand Down Expand Up @@ -109,7 +109,7 @@ let _ = extractor.find_best(id);
Note that a particular e-class might occur in an expression multiple times.
This means that pathological, but nevertheless realistic cases
might overflow `usize` if you implement a cost function like [`AstSize`],
even if the actual [`RecExpr`] fits compactly in memory.
even if the actual [`Expr`] fits compactly in memory.
You might want to use [`saturating_add`](u64::saturating_add) to
ensure your cost function is still monotonic in this situation.
**/
Expand All @@ -128,12 +128,12 @@ pub trait CostFunction<L: Language> {
where
C: FnMut(Id) -> Self::Cost;

/// Calculates the total cost of a [`RecExpr`].
/// Calculates the total cost of a [`Expr`].
///
/// As provided, this just recursively calls `cost` all the way
/// down the [`RecExpr`].
/// down the [`Expr`].
///
fn cost_rec(&mut self, expr: &RecExpr<L>) -> Self::Cost {
fn cost_rec(&mut self, expr: &Expr<L>) -> Self::Cost {
let mut costs: HashMap<Id, Self::Cost> = HashMap::default();
for (i, node) in expr.as_ref().iter().enumerate() {
let cost = self.cost(node, |i| costs[&i].clone());
Expand All @@ -148,7 +148,7 @@ pub trait CostFunction<L: Language> {

```
# use egg::*;
let e: RecExpr<SymbolLang> = "(do_it foo bar baz)".parse().unwrap();
let e: Expr<SymbolLang> = "(do_it foo bar baz)".parse().unwrap();
assert_eq!(AstSize.cost_rec(&e), 4);
```

Expand All @@ -169,7 +169,7 @@ impl<L: Language> CostFunction<L> for AstSize {

```
# use egg::*;
let e: RecExpr<SymbolLang> = "(do_it foo bar baz)".parse().unwrap();
let e: Expr<SymbolLang> = "(do_it foo bar baz)".parse().unwrap();
assert_eq!(AstDepth.cost_rec(&e), 2);
```

Expand Down Expand Up @@ -220,11 +220,11 @@ where
extractor
}

/// Find the cheapest (lowest cost) represented `RecExpr` in the
/// Find the cheapest (lowest cost) represented `Expr` in the
/// given eclass.
pub fn find_best(&self, eclass: Id) -> (CF::Cost, RecExpr<L>) {
pub fn find_best(&self, eclass: Id) -> (CF::Cost, Expr<L>) {
let (cost, root) = self.costs[&self.egraph.find(eclass)].clone();
let expr = root.build_recexpr(|id| self.find_best_node(id).clone());
let expr = root.build_expr(|id| self.find_best_node(id).clone());
(cost, expr)
}

Expand Down
Loading
Loading