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

Fix ExtractReport generation and extracting in sub-graph #205

Merged
merged 6 commits into from
Aug 29, 2023

Conversation

saulshanabrook
Copy link
Member

@saulshanabrook saulshanabrook commented Aug 23, 2023

In the switch to using an extract action over an extract command in #176, the saving of the extracted expressions in a report was lost.

This PR restores that behavior and also refactors the ExtractReport to be an enum. In #176, the extraction logic was also changed, so that if passed in a non-zero number of variants, it would no longer perform a normal cost-based extraction as well. So now the report captures these two cases as distinct.

This PR also fixes printing the extracted expression in the simplify command, which was broken in #195.

// Extract an expression from the current state, returning the cost, the extracted expression and some number
// of other variants, if variants is not zero.
pub fn extract_expr(&mut self, e: Expr, num_variants: usize) -> Result<ExtractReport, Error> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was previously used in a number of places before #176 and was now only being used in the un-tested Output command.

@mwillsey
Copy link
Member

Do we want the report to be an enum? I feel that the single and multiple extraction case could be handled uniformly, no?

@saulshanabrook
Copy link
Member Author

saulshanabrook commented Aug 23, 2023

Do we want the report to be an enum? I feel that the single and multiple extraction case could be handled uniformly, no?

After #176, the logic for the extraction now seems disjoint between the variants and the best. If there are variants, then there is no "best" expression extracted, and vice versa:

egglog/src/typecheck.rs

Lines 879 to 909 in 9e53038

if variants == 0 {
let (cost, expr) = self.extract(
values[0],
&mut termdag,
self.proof_state
.type_info
.sorts
.get(&values[0].tag)
.unwrap(),
);
let extracted = termdag.to_string(&expr);
log::info!("extracted with cost {cost}: {}", extracted);
self.print_msg(extracted);
} else {
if variants < 0 {
panic!("Cannot extract negative number of variants");
}
let extracted =
self.extract_variants(values[0], variants as usize, &mut termdag);
log::info!("extracted variants:");
let mut msg = String::default();
msg += "(\n";
assert!(!extracted.is_empty());
for expr in extracted {
let str = termdag.to_string(&expr);
log::info!(" {}", str);
msg += &format!(" {}\n", str);
}
msg += ")";
self.print_msg(msg);
}

Whereas previously, if you extracted some variants, a best was also extracted.

So I thought now an enum is appropriate?

@saulshanabrook
Copy link
Member Author

On another note, I noticed that simplify is not working with this new scheme, due to it running the extraction in a sub-egraph.

As a solution, I am considering changing from a saved "report" to a callback on the EGraph which is called during extraction.

Any thoughts on that?

@mwillsey
Copy link
Member

mwillsey commented Aug 23, 2023 via email

otherwise it wont be preserved when after (pop) in de-sugared simplify
@saulshanabrook
Copy link
Member Author

What simplify are we talking about?

The simplify command, i.e. like for this example:

(datatype Expr
  (Num i64))

(let x (Num 1))
(simplify (run) x)

I’m not sure what the callback would do.

Instead of adding a report, it would call a callback on every extraction, so that from Python, we could still see what was extracted.

I opted not to do this, due to the added complexity, and instead copy reports from children to parents when popping.

@saulshanabrook saulshanabrook changed the title Add back working extract report Fix ExtractReport generation and extracting in sub-graph Aug 23, 2023
Copy link
Member

@oflatt oflatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a good change to me!

@saulshanabrook saulshanabrook requested a review from a team as a code owner August 29, 2023 18:14
@saulshanabrook saulshanabrook requested review from ajpal and oflatt and removed request for a team August 29, 2023 18:14
Copy link
Member

@oflatt oflatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a good change, and fixes a bug in preserving messages across push/pop.
Had a small nit before we merge.

src/lib.rs Outdated Show resolved Hide resolved
@oflatt oflatt merged commit 192a703 into egraphs-good:main Aug 29, 2023
3 checks passed
@saulshanabrook saulshanabrook deleted the add- branch August 29, 2023 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants