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

Split mathics builtin patterns #1123

Merged
merged 20 commits into from
Oct 7, 2024
Merged

Split mathics builtin patterns #1123

merged 20 commits into from
Oct 7, 2024

Conversation

mmatera
Copy link
Contributor

@mmatera mmatera commented Oct 4, 2024

Just split this long module into smaller parts.

@rocky
Copy link
Member

rocky commented Oct 5, 2024

@mmatera have the changes quiesced to the point that I should pull this branch and try it?

@mmatera
Copy link
Contributor Author

mmatera commented Oct 5, 2024

@rocky, sure. This is just what it is.

@rocky
Copy link
Member

rocky commented Oct 6, 2024

Let's follow the organization in the docs. So Rules and Patterns is called Patterns and under than are Basic Pattern Objects and Composite Patterns, etc.

@mmatera
Copy link
Contributor Author

mmatera commented Oct 6, 2024

In the guide of the link, rules are not included, except as a link to other guide named "Rules & Patterns" . But ok, let's follow this organization as far as it is possible.

@rocky
Copy link
Member

rocky commented Oct 6, 2024

In the guide of the link, rules are not included, except as a link to other guide named "Rules & Patterns" . But ok, let's follow this organization as far as it is possible.

Yeah, I realized things are neither clear cut nor optimal. Possibly Rules would be in its own section as a sibling of Patterns.

Following the existing organization does have the advantage that -- within a given section like Rules, or Pattern Matching Functions -- it is usually easy to see what is covered and what is missing, (for those functions that have a strong affinity to only one section which does occur quite a bit.)

@rocky
Copy link
Member

rocky commented Oct 6, 2024

Much better - thanks!

Some additional things that would make this closer to the current style.

  • Order Builtin classes alphabetically
  • subclass ABC in _XXX classes
  • Be on the lookout for eval_ functions that should get moved to mathics.eval. For example: create_rules -> eval_Replace.
  • I would add a comment at the bottom for those missing Builtin functions of the corresponding section. For example, KeyValuePattern and OrderlessPatternSequence for the Composite Patterns module.



# TODO: disentangle me
def create_rules(
Copy link
Member

Choose a reason for hiding this comment

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

This is really an eval function and should be moved to mathics.eval.rule and probably called eval_Replace().

Copy link
Member

Choose a reason for hiding this comment

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

Will be addressed in PR.

@mmatera
Copy link
Contributor Author

mmatera commented Oct 7, 2024

I think now this is ready.

>> Replace[x[x[y]], x -> z, All]
= x[x[y]]

Heads can be replaced using the Heads option
Copy link
Member

Choose a reason for hiding this comment

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

Heads option -> 'Heads' option:

Copy link
Member

Choose a reason for hiding this comment

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

Will be addressed in next PR

>> Replace[x[x[y]], x -> z, {1}, Heads -> True]
= z[x[y]]

You can use Replace as an operator
Copy link
Member

Choose a reason for hiding this comment

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

":" at the end of the line

"""

messages = {
"reps": "`1` is not a valid replacement rule.",
Copy link
Member

Choose a reason for hiding this comment

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

This line can be deleted if should have been "rep". And then the message call below in 255 should be adjusted.

Copy link
Member

Choose a reason for hiding this comment

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

BTW I haven't been able to check this since I can't get to galecia

Copy link
Member

Choose a reason for hiding this comment

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

Will be addressed in next PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW I haven't been able to check this since I can't get to galecia

Yes, it seems there is a power outage in the building where galatea is located.

def eval_levelspec(self, expr, rules, ls, evaluation, options):
"Replace[expr_, rules_, Optional[Pattern[ls, _?LevelQ], {0}], OptionsPattern[Replace]]"
try:
rules, ret = create_rules(rules, expr, "Replace", evaluation)
Copy link
Member

@rocky rocky Oct 7, 2024

Choose a reason for hiding this comment

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

create_rules -> eval_Replace in mathics.eval.pattern

Copy link
Member

Choose a reason for hiding this comment

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

will be addressed in next PR

from mathics.eval.rules import Dispatch, create_rules

# This tells documentation how to sort this module
sort_order = "mathics.builtin.rules-and-patterns.rules"
Copy link
Member

@rocky rocky Oct 7, 2024

Choose a reason for hiding this comment

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

sort order no longer matches title def...

Copy link
Member

Choose a reason for hiding this comment

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

Will be fixed in PR

@rocky
Copy link
Member

rocky commented Oct 7, 2024

It is definitely much better! And closer to our current practice. There are still a number of small changes.

If you want me to make them, I will do.

@mmatera
Copy link
Contributor Author

mmatera commented Oct 7, 2024

@rocky , I have implemented almost all your suggestions. Fell free to add the changes you consider useful.

the rules are applied.

>> dispatchrule = Dispatch[{rule}];
>> a + F[F[x ^ 2]] //. dispatchrule
Copy link
Member

Choose a reason for hiding this comment

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

It looks like this test triggers a bug in the code!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. Hopefully, it is fixed in 07d1768. However, probably we need to go over Dispatch and create_rules again.

@mmatera
Copy link
Contributor Author

mmatera commented Oct 7, 2024

Well, finally all that got broken, and what we discovered was broken seems to work now.

See review comments form `plit_mathics_builtin_patterns`.  Additionally, it looks like `sort_order` for  sections has been fixed.

Co-authored-by: Juan Mauricio Matera <[email protected]>
@rocky
Copy link
Member

rocky commented Oct 7, 2024

LGTM now. I see more corrects that should go in. But will do that in due time.

@rocky rocky merged commit a71bbd6 into master Oct 7, 2024
11 checks passed
@rocky rocky deleted the split_mathics_builtin_patterns branch October 7, 2024 13:17
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.

2 participants