-
-
Notifications
You must be signed in to change notification settings - Fork 48
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
Assignments with (named) Patterns in the lhs #898
Comments
Notice that for builtin rules, we have a hack to pass the original expression (see for example |
Let me see if I can simplify the demonstration of the bug. In WMA: In[1]:= expr:F[x_] := G[HoldForm[expr]]
In[2]:= ?F
Out[2]= Global`F
expr:F[x_] := G[HoldForm[expr]] In Mathics3: In[1]:= expr:F[x_] := G[HoldForm[expr]]
Out[1]= None
In[2]:= ?F
Out[2]= Null
(* This above is wrong . Global`F should have the definition: expr:F[x_] := G[HoldForm[expr]] *) |
It works too. However, it is also instructive to evaluate In WMA,
In Mathics
so the rule seems to be stored in some place, but in a wrong way... |
Now that I think I understand the problem, it looks to me that fixing this involves:
A "disentangled" PR then should do just this, and no more. And once this is fixed, I imagine it will work properly when used inside the docstring of an evaluation method for a built-in function. Given that right now this does not work in these kinds of docstrings, it is not surprising that they are not currently used inside the code docstrings. In future PR, we can start introducing these possibly, simplifying code. |
Probably a first step would be to improve the and in the Assignment evaluation, when Pattern is in the lhs, and a tag is not given, set the tag based on the second argument. |
Description
Assignments with named patterns in the LHS are not properly handled.
Expected behavior
Consider an assignment expression of the form
expr:F[x_,y_]:=G[HoldForm[expr]]
In WMA,
In Mathics,
In a similar way, in builtin symbols, docstring patterns of
eval_
methods, of the patternsexpr:Expr[...]
are not interpreted as named patterns but actually produce an error.Additional context
This kind of pattern is used for example in CellsToTeX` to produce messages without requiring to rebuild an expression. For example,
here,
functionCall
is used instead of rebuilding the expression:Another more interesting reason to fix this issue is related with a discussion we have some time ago with @rocky, regarding the reason for returning
None
when a builtineval_
method "fails".Suppose that you have an eval method like this:
To avoid returning
None
we could writebut this adds a lot of overhead both in rebuilding the expression and, in the rewrite- evaluation loop, to compare the expressions with sameQ.
Instead, if we could use a named pattern, we could write
which avoids the overhead of rebuilding the expression, and makes the
SameQ
evaluation faster (because if is decided by anis
comparison, instead of a recursive comparison of leaves).Note aside: WMA rewrite-evaluation loop do not check "SameQ" but check something like the
is
to decide if the evaluation continues. For example, in WMANotice that in the case of the definition of
F
, we reach the iteration limit because the result of evaluatingF[1,2]
is different from the original expression. In mathics, on the other hand,because the recursion is finished by the costly
sameQ
evaluation.In any case, named patterns in the doctest strings do not work either.
The text was updated successfully, but these errors were encountered: