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

Condition[...] expressions as second element in RuleDelayed behaviour not compatible with WMA #1213

Open
mmatera opened this issue Dec 8, 2024 · 6 comments

Comments

@mmatera
Copy link
Contributor

mmatera commented Dec 8, 2024

Description

In WMA, rules of the form pat:>Condition[a,cond] are treated as Condition[pat, cond]:>a when applied to an expression. Mathics-core does not reproduce this behavior. Notice that this does not happen with ->.

Now, when Mathics execute a (Delayed) assignment instruction f[x_]:= g[x], the rule f[x_]:>g[x] is stored into the definition of f. On the other hand, for conditional assignments like f[x_]:=x/;x==2, in Mathics, we reformat the rule to obtain the right result. Now, if we fix the logic in handling these kind of rules, we can then replace the behavior of the assignment.

How to Reproduce

For example WMA, if we apply the rule f[x_]:>x/;x==2 to the expression f[3], the interpreter understands the same as we apply the rule f[x_]/;x==2:>x, i.e., check that f[3] matches with the pattern f[x_], and then if x satisfies the condition. If it does, replaces the expression by the replacement (in this case, 2).

In Mathics, the interpreter understands that f[x_]:>x/;x==2 matches with f[3], and then f[3]/. f[x_]/;x==2:>x results in 3/;3==2.

Your Environment

This is independent on the environment.

Workarounds

Put the Condition expression in the pattern instead of the replacement.

Priority

Low?

Additional context

This also affects how expressions are stored in Assignements (Set, SetDelayed, etc...)

@mmatera
Copy link
Contributor Author

mmatera commented Dec 8, 2024

This is related with #1209

@mmatera mmatera changed the title Condition[...] expressions as second element in Rule and RuleDelayed behaviour not compatible with WMA Condition[...] expressions as second element in RuleDelayed behaviour not compatible with WMA Dec 8, 2024
@rocky
Copy link
Member

rocky commented Dec 8, 2024

As a workaround, can one implement this by adding a rewriting rule?

@mmatera
Copy link
Contributor Author

mmatera commented Dec 8, 2024

As a workaround, can one implement this by adding a rewriting rule?

I think so

@rocky
Copy link
Member

rocky commented Dec 8, 2024

As a workaround, can one implement this by adding a rewriting rule?

I think so

Ok. Then this provides a simple solution. First, let's write this outside of Mathics3. Then we can add it as a Rule inside Mathics3 replacing whatever code we currently have.

@mmatera
Copy link
Contributor Author

mmatera commented Dec 8, 2024

In any case, it would be a partial solution: the real issue becomes apparent in functions that do an introspection of the rules in DownValues. That is what is mentioned in #1209.

@rocky
Copy link
Member

rocky commented Dec 8, 2024

Ok. If you are up for it, we can start with a partial solution if it is simple, and gets tests for the partial solution, which ensures more correct behavior.

After the next release, we can discuss and expand as necessary.

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

No branches or pull requests

2 participants