Skip to content

Commit 63855b9

Browse files
committed
Add id to make
Closes #315 Closes #352
1 parent 0e5c466 commit 63855b9

6 files changed

Lines changed: 17 additions & 10 deletions

File tree

src/egraph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ impl<L: Language, N: Analysis<L>> EGraph<L, N> {
10371037
let class = EClass {
10381038
id,
10391039
nodes: vec![enode.clone()],
1040-
data: N::make(self, &original),
1040+
data: N::make(self, &original, id),
10411041
parents: Default::default(),
10421042
};
10431043

@@ -1345,7 +1345,7 @@ impl<L: Language, N: Analysis<L>> EGraph<L, N> {
13451345
while let Some(class_id) = self.analysis_pending.pop() {
13461346
let node = self.nodes[usize::from(class_id)].clone();
13471347
let class_id = self.find_mut(class_id);
1348-
let node_data = N::make(self, &node);
1348+
let node_data = N::remake(self, &node, class_id);
13491349
let class = self.classes.get_mut(&class_id).unwrap();
13501350

13511351
let did_merge = self.analysis.merge(&mut class.data, node_data);

src/language.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ impl Analysis<SimpleMath> for ConstantFolding {
745745
egg::merge_max(to, from)
746746
}
747747
748-
fn make(egraph: &mut EGraph<SimpleMath, Self>, enode: &SimpleMath) -> Self::Data {
748+
fn make(egraph: &mut EGraph<SimpleMath, Self>, enode: &SimpleMath, _id: Id) -> Self::Data {
749749
let x = |i: &Id| egraph[*i].data;
750750
match enode {
751751
SimpleMath::Num(n) => Some(*n),
@@ -785,7 +785,7 @@ pub trait Analysis<L: Language>: Sized {
785785
/// The per-[`EClass`] data for this analysis.
786786
type Data: Debug;
787787

788-
/// Makes a new [`Analysis`] data for a given e-node.
788+
/// Makes a new [`Analysis`] data for a given e-node that will go into the given e-class.
789789
///
790790
/// Note the mutable `egraph` parameter: this is needed for some
791791
/// advanced use cases, but most use cases will not need to mutate
@@ -795,7 +795,14 @@ pub trait Analysis<L: Language>: Sized {
795795
/// Doing so will create an infinite loop.
796796
///
797797
/// Note that `enode`'s children may not be canonical
798-
fn make(egraph: &mut EGraph<L, Self>, enode: &L) -> Self::Data;
798+
fn make(egraph: &mut EGraph<L, Self>, enode: &L, id: Id) -> Self::Data;
799+
800+
/// Same as [`Analysis::make`], but called during rebuilding.
801+
///
802+
/// By default, it just calls `make`.
803+
fn remake(egraph: &mut EGraph<L, Self>, enode: &L, id: Id) -> Self::Data {
804+
Self::make(egraph, enode, id)
805+
}
799806

800807
/// An optional hook that allows inspection before a [`union`] occurs.
801808
/// When explanations are enabled, it gives two ids that represent the two particular terms being unioned, not the canonical ids for the two eclasses.
@@ -862,7 +869,7 @@ pub trait Analysis<L: Language>: Sized {
862869

863870
impl<L: Language> Analysis<L> for () {
864871
type Data = ();
865-
fn make(_egraph: &mut EGraph<L, Self>, _enode: &L) -> Self::Data {}
872+
fn make(_egraph: &mut EGraph<L, Self>, _enode: &L, _id: Id) -> Self::Data {}
866873
fn merge(&mut self, _: &mut Self::Data, _: Self::Data) -> DidMerge {
867874
DidMerge(false, false)
868875
}

src/rewrite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ where
250250
/// fn merge(&mut self, to: &mut Self::Data, from: Self::Data) -> DidMerge {
251251
/// merge_min(to, from)
252252
/// }
253-
/// fn make(egraph: &mut EGraph, enode: &Math) -> Self::Data {
253+
/// fn make(egraph: &mut EGraph, enode: &Math, _id: Id) -> Self::Data {
254254
/// let get_size = |i: Id| egraph[i].data;
255255
/// AstSize.cost(enode, get_size)
256256
/// }

tests/lambda.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl Analysis<Lambda> for LambdaAnalysis {
7575
})
7676
}
7777

78-
fn make(egraph: &mut EGraph, enode: &Lambda) -> Data {
78+
fn make(egraph: &mut EGraph, enode: &Lambda, _id: Id) -> Data {
7979
let f = |i: &Id| egraph[*i].data.free.iter().cloned();
8080
let mut free = HashSet::default();
8181
match enode {

tests/math.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub struct ConstantFold;
5050
impl Analysis<Math> for ConstantFold {
5151
type Data = Option<(Constant, PatternAst<Math>)>;
5252

53-
fn make(egraph: &mut EGraph, enode: &Math) -> Self::Data {
53+
fn make(egraph: &mut EGraph, enode: &Math, _id: Id) -> Self::Data {
5454
let x = |i: &Id| egraph[*i].data.as_ref().map(|d| d.0);
5555
Some(match enode {
5656
Math::Constant(c) => (*c, format!("{}", c).parse().unwrap()),

tests/prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl Analysis<Prop> for ConstantFold {
2525
})
2626
}
2727

28-
fn make(egraph: &mut EGraph, enode: &Prop) -> Self::Data {
28+
fn make(egraph: &mut EGraph, enode: &Prop, _id: Id) -> Self::Data {
2929
let x = |i: &Id| egraph[*i].data.as_ref().map(|c| c.0);
3030
let result = match enode {
3131
Prop::Bool(c) => Some((*c, c.to_string().parse().unwrap())),

0 commit comments

Comments
 (0)