Skip to content

Commit ed98034

Browse files
committed
adding documentation
1 parent b74729f commit ed98034

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

CHANGES.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ Bugs
5050

5151
# ``0`` with a given precision (like in ```0`3```) is now parsed as ``0``, an integer number.
5252
#. ``RandomSample`` with one list argument now returns a random ordering of the list items. Previously it would return just one item.
53-
#. Rules of the form ``pat->Condition[expr, cond]`` are handled as in WL. The same works for nested `Condition` expressions.
53+
#. Rules of the form ``pat->Condition[expr, cond]`` are handled as in WL. The same also works for nested `Condition` expressions. In particular, the comparison between two Rules with the same pattern but an iterated ``Condition`` expressionare considered equal if the conditions are the same.
54+
5455

5556
Enhancements
5657
++++++++++++

mathics/builtin/assignments/assignment.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,21 @@ class SetDelayed(Set):
170170
'Condition' ('/;') can be used with 'SetDelayed' to make an
171171
assignment that only holds if a condition is satisfied:
172172
>> f[x_] := p[x] /; x>0
173+
>> f[x_] := p[-x]/; x<-2
173174
>> f[3]
174175
= p[3]
175176
>> f[-3]
176-
= f[-3]
177-
It also works if the condition is set in the LHS:
177+
= p[3]
178+
>> f[-1]
179+
= f[-1]
180+
Notice that the LHS is the same in both definitions, but the second
181+
does not overwrite the first one.
182+
183+
To overwrite one of these definitions, we have to assign using the same condition:
184+
>> f[x_] := Sin[x] /; x>0
185+
>> f[3]
186+
= Sin[3]
187+
In a similar way, the condition can be set in the LHS:
178188
>> F[x_, y_] /; x < y /; x>0 := x / y;
179189
>> F[x_, y_] := y / x;
180190
>> F[2, 3]

mathics/core/definitions.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -802,10 +802,15 @@ def get_tag_position(pattern, name) -> Optional[str]:
802802

803803

804804
def insert_rule(values, rule) -> None:
805+
rhs_cond = getattr(rule, "rhs_conditions", [])
805806
for index, existing in enumerate(values):
806807
if existing.pattern.sameQ(rule.pattern):
807-
del values[index]
808-
break
808+
# Check for coincidences in the rhs conditions,
809+
# it they are there.
810+
existing_rhs_cond = getattr(existing, "rhs_conditions", [])
811+
if existing_rhs_cond == rhs_cond:
812+
del values[index]
813+
break
809814
# use insort_left to guarantee that if equal rules exist, newer rules will
810815
# get higher precedence by being inserted before them. see DownValues[].
811816
bisect.insort_left(values, rule)

mathics/core/rules.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,9 @@ def __init__(self, pattern, replace, delayed=True, system=False) -> None:
251251
while replace.has_form("System`Condition", 2):
252252
replace, cond = replace.elements
253253
conds.append(cond)
254-
self.rhs_conditions = sorted(conds)
255-
self.strip_replace = replace
254+
255+
self.rhs_conditions = sorted(conds)
256+
self.strip_replace = replace
256257

257258
def do_replace(self, expression, vars, options, evaluation):
258259
replace = self.replace if self.rhs_conditions == [] else self.strip_replace

0 commit comments

Comments
 (0)