-
Notifications
You must be signed in to change notification settings - Fork 9
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
wip: use arena-style cache when simplifying Expressions (DO NOT MERGE) #276
Draft
Shadow53
wants to merge
6
commits into
simplify-by-hand
Choose a base branch
from
expression-arena
base: simplify-by-hand
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c6dd84
chore: test that similar calibrations with different sites are not fi…
MarquessV 0f95a7a
Merge remote-tracking branch 'origin/simplify-by-hand' into expressio…
Shadow53 b583c36
refactor: add a simplification cache
Shadow53 a3dce2d
refactor!: WIP simplify expressions by value
Shadow53 3993374
feat(lib): take ownership of Expression during simplification
Shadow53 e3c084a
refactor: avoid hashing in Expression PartialEq
Shadow53 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ use std::{ | |
#[cfg(test)] | ||
use proptest_derive::Arbitrary; | ||
|
||
mod simplification; | ||
//mod simplification; | ||
mod simplification_2; | ||
|
||
/// The different possible types of errors that could occur during expression evaluation. | ||
|
@@ -60,7 +60,7 @@ pub enum Expression { | |
Variable(String), | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct FunctionCallExpression { | ||
pub function: ExpressionFunction, | ||
pub expression: Box<Expression>, | ||
|
@@ -75,7 +75,7 @@ impl FunctionCallExpression { | |
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct InfixExpression { | ||
pub left: Box<Expression>, | ||
pub operator: InfixOperator, | ||
|
@@ -92,7 +92,7 @@ impl InfixExpression { | |
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct PrefixExpression { | ||
pub operator: PrefixOperator, | ||
pub expression: Box<Expression>, | ||
|
@@ -135,15 +135,15 @@ impl Hash for Expression { | |
"Infix".hash(state); | ||
operator.hash(state); | ||
match operator { | ||
InfixOperator::Plus | InfixOperator::Star => { | ||
// commutative, so put left & right in decreasing order by hash value | ||
let (a, b) = ( | ||
min_by_key(&left, &right, hash_to_u64), | ||
max_by_key(&left, &right, hash_to_u64), | ||
); | ||
a.hash(state); | ||
b.hash(state); | ||
} | ||
//InfixOperator::Plus | InfixOperator::Star => { | ||
// // commutative, so put left & right in decreasing order by hash value | ||
// let (a, b) = ( | ||
// min_by_key(&left, &right, hash_to_u64), | ||
// max_by_key(&left, &right, hash_to_u64), | ||
// ); | ||
// a.hash(state); | ||
// b.hash(state); | ||
//} | ||
_ => { | ||
left.hash(state); | ||
right.hash(state); | ||
|
@@ -181,7 +181,17 @@ impl Hash for Expression { | |
impl PartialEq for Expression { | ||
// Partial equality by hash value | ||
fn eq(&self, other: &Self) -> bool { | ||
hash_to_u64(self) == hash_to_u64(other) | ||
// hash_to_u64(self) == hash_to_u64(other) | ||
match (self, other) { | ||
(Self::Address(left), Self::Address(right)) => left == right, | ||
(Self::Infix(left), Self::Infix(right)) => left == right, | ||
(Self::Number(left), Self::Number(right)) => left == right, | ||
(Self::Prefix(left), Self::Prefix(right)) => left == right, | ||
(Self::FunctionCall(left), Self::FunctionCall(right)) => left == right, | ||
(Self::Variable(left), Self::Variable(right)) => left == right, | ||
(Self::PiConstant, Self::PiConstant) => true, | ||
_ => false, | ||
} | ||
} | ||
} | ||
|
||
|
@@ -274,9 +284,8 @@ impl Expression { | |
*self = Expression::Number(Complex64::from(PI)); | ||
} | ||
_ => { | ||
if let Ok(simpler) = simplification::run(self) { | ||
*self = simpler; | ||
} | ||
let temp = std::mem::replace(self, Expression::PiConstant); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really need to remember this trick 👍 |
||
*self = simplification_2::run(temp); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this is costly, but we put it in for #27. Are we deciding to no longer ensure that, e.g.,
1 + x == x + 1
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, should we just derive
Hash
,PartialEq,
andEq
and call it a day?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, I think this is the way to go. #27 only handles commutativity, but not other rules that lead to equality between expressions. Since we're simplifying expressions with these rules, enforcing some but not all in the hashing & equality implementations seems wrong and wasteful. I'll open a new PR.