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

Generic analysis #2

Closed
wants to merge 6 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
18 changes: 10 additions & 8 deletions src/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The [`EGraph::dot`](EGraph::dot()) method creates `Dot`s.
# Example

```no_run
use egg::{*, rewrite as rw};
use egg::legacy::{*, rewrite as rw};

let rules = &[
rw!("mul-commutes"; "(* ?x ?y)" => "(* ?y ?x)"),
Expand Down Expand Up @@ -192,37 +192,39 @@ where
writeln!(f, " {}", line)?;
}

let classes = self.egraph.generate_class_nodes();

// define all the nodes, clustered by eclass
for class in self.egraph.classes() {
writeln!(f, " subgraph cluster_{} {{", class.id)?;
for (&id, class) in &classes {
writeln!(f, " subgraph cluster_{} {{", id)?;
writeln!(f, " style=dotted")?;
for (i, node) in class.iter().enumerate() {
writeln!(f, " {}.{}[label = \"{}\"]", class.id, i, node)?;
writeln!(f, " {}.{}[label = \"{}\"]", id, i, node)?;
}
writeln!(f, " }}")?;
}

for class in self.egraph.classes() {
for (&id, class) in &classes {
for (i_in_class, node) in class.iter().enumerate() {
let mut arg_i = 0;
node.try_for_each(|child| {
// write the edge to the child, but clip it to the eclass with lhead
let (anchor, label) = self.edge(arg_i, node.len());
let child_leader = self.egraph.find(child);

if child_leader == class.id {
if child_leader == id {
writeln!(
f,
// {}.0 to pick an arbitrary node in the cluster
" {}.{}{} -> {}.{}:n [lhead = cluster_{}, {}]",
class.id, i_in_class, anchor, class.id, i_in_class, class.id, label
id, i_in_class, anchor, id, i_in_class, id, label
)?;
} else {
writeln!(
f,
// {}.0 to pick an arbitrary node in the cluster
" {}.{}{} -> {}.0 [lhead = cluster_{}, {}]",
class.id, i_in_class, anchor, child, child_leader, label
id, i_in_class, anchor, child, child_leader, label
)?;
}
arg_i += 1;
Expand Down
55 changes: 29 additions & 26 deletions src/eclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,29 @@ use crate::*;
#[non_exhaustive]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub struct EClass<L, D> {
pub struct EClass<D> {
/// This eclass's id.
pub id: Id,
/// The equivalent enodes in this equivalence class.
pub nodes: Vec<L>,
/// The analysis data associated with this eclass.
///
/// Modifying this field will _not_ cause changes to propagate through the e-graph.
/// Prefer [`EGraph::set_analysis_data`] instead.
pub data: D,
/// The parent enodes and their original Ids.
pub(crate) parents: Vec<(L, Id)>,
/// The original Ids of parent enodes.
pub(crate) parents: Vec<Id>,
}

impl<L, D> EClass<L, D> {
/// Returns `true` if the `eclass` is empty.
pub fn is_empty(&self) -> bool {
self.nodes.is_empty()
}

/// Returns the number of enodes in this eclass.
pub fn len(&self) -> usize {
self.nodes.len()
}

/// Iterates over the enodes in this eclass.
pub fn iter(&self) -> impl ExactSizeIterator<Item = &L> {
self.nodes.iter()
}

/// Iterates over the parent enodes of this eclass.
pub fn parents(&self) -> impl ExactSizeIterator<Item = (&L, Id)> {
self.parents.iter().map(|(node, id)| (node, *id))
impl<D> EClass<D> {
/// Iterates over the non-canonical ids of parent enodes of this eclass.
pub fn parents(&self) -> impl ExactSizeIterator<Item = Id> + '_ {
self.parents.iter().copied()
}
}

impl<L: Language, D> EClass<L, D> {
impl<L: Language, D> EMClass<L, D> {
/// Iterates over the childless enodes in this eclass.
pub fn leaves(&self) -> impl Iterator<Item = &L> {
self.nodes.iter().filter(|&n| n.is_leaf())
self.iter().filter(|&n| n.is_leaf())
}

/// Asserts that the childless enodes in this eclass are unique.
Expand All @@ -64,4 +47,24 @@ impl<L: Language, D> EClass<L, D> {
);
}
}

/// The equivalent enodes in this equivalence class.
pub fn nodes(&self) -> &[L] {
&self.data.1
}

/// Returns `true` if the `eclass` is empty.
pub fn is_empty(&self) -> bool {
self.nodes().is_empty()
}

/// Returns the number of enodes in this eclass.
pub fn len(&self) -> usize {
self.nodes().len()
}

/// Iterates over the enodes in this eclass.
pub fn iter(&self) -> impl ExactSizeIterator<Item = &L> {
self.nodes().iter()
}
}
Loading
Loading