@@ -140,7 +140,7 @@ def eval_list(
140140 return result
141141
142142 def eval (
143- self , rules : Expression , evaluation : Evaluation
143+ self , rules : BaseElement , evaluation : Evaluation
144144 ) -> OptionalType [BaseElement ]:
145145 """Dispatch[rules_]"""
146146 if not isinstance (rules , Expression ):
@@ -489,6 +489,8 @@ def eval_list(
489489 return result
490490
491491
492+ # rocky: I don't know why, but this class seems to need a lot of
493+ # things that handled differently from normal practice.
492494class Rule_ (InfixOperator ):
493495 """
494496
@@ -510,20 +512,39 @@ class Rule_(InfixOperator):
510512 """
511513
512514 attributes = A_SEQUENCE_HOLD | A_PROTECTED
515+
516+ # For some mysterious reason, the below does not work and
517+ # we have to do argument checking as code in eval().
518+ # eval_error = Builtin.generic_argument_error
519+ # expected_args = 2
520+
513521 grouping = "Right"
514522 name = "Rule"
515523 needs_verbatim = True
516524
517- # FIXME: if we remove this we have problems.
518- # We should be able to get this from JSON.
525+ # FIXME: We should be able to get the operator from the JSON operator tables.
526+ # However, if we remove the operator class variable assignment, is we have problems.
527+ # Run test/format/test_format.py::test_makeboxes_text for details.
519528 operator = "->"
529+
520530 summary_text = "a replacement rule"
521531
522- def eval_rule (self , elems , evaluation ):
532+ def eval (self , elems , evaluation : Evaluation ):
523533 """Rule[elems___]"""
534+
524535 num_parms = len (elems .get_sequence ())
525536 if num_parms != 2 :
526537 evaluation .message ("Rule" , "argrx" , "Rule" , Integer (num_parms ), Integer2 )
538+
539+ # "Rule" is a somewhat generic term in WMA and thus Mathics3.
540+ # Rule semantics and its implementation change depending on the context that the Rule appears in.
541+ # Inside a Set, RuleDelayed, or ReplaceAll expression, the left-hand side of a Rule is a pattern.
542+ # But inside an Association or an Option, it is a key-value pair, with no pattern matching applied
543+ # to the left-hand side.
544+ # As a result, at this point we don't know which context Rule[] might appear, so we have to return
545+ # "None" which keeps the Expression the same. Returning "elems" which you might think would be the
546+ # same thing, is not correct, since the Head symbol "Rule" can get replaced with "Sequence" symbol
547+ # coming into this code.
527548 return None
528549
529550
0 commit comments