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

Move printing to Display and parsing to a new trait, FromOp. #86

Merged
merged 3 commits into from
May 4, 2021

Conversation

rosekunkel
Copy link
Contributor

@rosekunkel rosekunkel commented Apr 30, 2021

This fixes #83 by replacing Language::display_op with OpStr::fmt Display. It also moves Language::from_op_str to OpStr::from_str FromOp::from_op. Moving these two functions to a separate trait eliminates the need to give them default implementations which panic. define_language! has been updated to automatically implement OpStr Display and FromOp, so the transition should be seamless for people using the macro.

Additionally, this pull request adds a dependency on thiserror, a crate which provides a derive macro for std::error::Error, as this refactoring replaces several instances of String being used as an error type with structured error types implementing Error.

Let me know if there's anything that should be adjusted!

This removes `Language::from_op_str` and `Language::display_op`,
replacing them with `OpStr::from_str` and `OpStr::fmt`, respectively.
`define_language!` is also modified to implement this trait
automatically.
@mwillsey
Copy link
Member

Awesome! There's a lot of good stuff here. A couple thoughts:

  • Why not just have Language implement Display? We could still have a separate parsing trait.
  • I'm not ready for a thiserror dependency just yet, especially since we just have one error.

@mwillsey
Copy link
Member

mwillsey commented Apr 30, 2021

Ah, I see your thiserror changes are actually pretty widespread. I guess it's worth keeping then. thiserror is the future after all!

@rosekunkel
Copy link
Contributor Author

Good questions! Re: Display, do you mean that we should mandate that if a Language implements Display, it should just display the name of the operator?

As I've been thinking about this more, I'm wondering whether we really want to be using strings at all. Right now the way these string functions are used is for constructing and parsing sexps, so maybe it would make more sense if we had ToSexp and FromSexp traits instead.

Also, re: thiserror, it's just a convenience macro, so if we want to remove the dependency later we can just implement Error manually instead.

@mwillsey
Copy link
Member

mwillsey commented May 3, 2021

Yes, Display just prints the operator.

That's true, it's mostly used to print sexps, but there are couple places where it's nice to just print the operators, like in Dot.

@rosekunkel
Copy link
Contributor Author

Ok, I think that makes sense. I'll rework it into a trait for parsing and Display for printing the operator.

@rosekunkel rosekunkel changed the title Move printing and parsing to a new trait, OpStr. Move printing to Display and parsing to a new trait, FromOp. May 4, 2021
@rosekunkel
Copy link
Contributor Author

@mwillsey I made the changes you suggested -- let me know if there's anything else I should change.

@mwillsey mwillsey merged commit 3578fd4 into egraphs-good:main May 4, 2021
@mwillsey
Copy link
Member

mwillsey commented May 4, 2021

Nice work, thanks!

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.

Language::display_op should work like other formatting traits
2 participants