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

Improving precedence comparison. #1154

Merged
merged 2 commits into from
Nov 5, 2024
Merged

Conversation

mmatera
Copy link
Contributor

@mmatera mmatera commented Nov 4, 2024

This PR includes some fixes and improvings for handling operator precedence. In particular, as in WMA, avoids wrapping in parenthesis the second element of Rule[pat_, Function[...]].

* Improving handling for Infix, Prefix and Postfix format
@@ -16,7 +16,13 @@
from mathics.builtin.makeboxes import MakeBoxes
from mathics.builtin.options import options_to_rules
from mathics.core.atoms import Real, String
from mathics.core.builtin import BinaryOperator, Builtin, Operator
from mathics.core.builtin import (
BinaryOperator,
Copy link
Member

Choose a reason for hiding this comment

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

WIth the changes to use PostfixOperator and PrefixOperator (thank you very much), the "BinaryOperator" is no longer imported.

## PyPy: {{a -> (Function[{n}, 1 - C[1] + C[1] -1 ^ n])}}
## CPython: {{a -> (Function[{n}, 1 + C[1] -1 ^ n - C[1]])}
## PyPy: {{a -> Function[{n}, 1 - C[1] + C[1] -1 ^ n]}}
## CPython: {{a -> Function[{n}, 1 + C[1] -1 ^ n - C[1]]}

Geta "pure function" solution for a with two boundary conditions:
Copy link
Member

Choose a reason for hiding this comment

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

A small typo from previous: "Geta" -> "Get a"

attributes = A_SEQUENCE_HOLD | A_PROTECTED
grouping = "Right"
name = "Rule"
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for alphabetizing.

@@ -527,7 +527,7 @@ def lhs(expr):
return Expression(SymbolFormat, expr, Symbol(format))

def rhs(expr):
if expr.has_formf(SymbolInfix, None):
if expr.has_form(SymbolInfix, None):
Copy link
Member

Choose a reason for hiding this comment

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

Good catch!

) -> Optional[int]:
"""
compare the precedence of the element regarding a precedence value.
If both precedences are equivalent, return 0. If precedence of element
Copy link
Member

Choose a reason for hiding this comment

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

precedence is an integers or maybe fixed-point numbers. So here, "equivalent" is the same as "equals".

If both precedences are equivalent -> If the precedence is the same

Copy link
Member

Choose a reason for hiding this comment

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

If the precedence of element -> If the precedence of the first element

@@ -279,8 +307,8 @@ def format_expr(expr):

formatted = format_expr(expr) if isinstance(expr, EvalMixin) else None
if formatted is not None:
do_format = element_formatters.get(type(formatted), do_format_element)
result = do_format(formatted, evaluation, form)
do_format_fn = element_formatters.get(type(formatted), do_format_element)
Copy link
Member

Choose a reason for hiding this comment

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

thanks

@@ -1151,22 +1151,29 @@ def __init__(self, *args, **kwargs):

# Prevent pattern matching symbols from gaining meaning here using
# Verbatim
name = f"Verbatim[{name}]"
verbatim_name = f"Verbatim[{name}]"
Copy link
Member

Choose a reason for hiding this comment

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

thanks

@rocky
Copy link
Member

rocky commented Nov 5, 2024

LGTM - but please see comments.

@mmatera
Copy link
Contributor Author

mmatera commented Nov 5, 2024

@rocky, good. Then let's merge and iterate.

@mmatera mmatera merged commit c5a9227 into master Nov 5, 2024
13 checks passed
@mmatera mmatera deleted the improve_compare_precedence branch November 5, 2024 17:18
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