Skip to content

Commit e92da0f

Browse files
mmaterarocky
authored andcommitted
fix test for SetDelayed. Move special case for Set with LHS a list away from the conditional.
1 parent 513bfc5 commit e92da0f

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

mathics/builtin/assignments/assignment.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,28 @@ class SetDelayed(Set):
146146
= -2 / 3
147147
We can use conditional delayed assignments to define \
148148
symbols with values conditioned to the context. For example,
149-
>> a/; b>0:= 3
149+
>> ClearAll[a,b]; a/; b>0:= 3
150150
Set $a$ to have a value of $3$ if certain variable $b$ is positive.\
151151
So, if this variable is not set, $a$ stays unevaluated:
152152
>> a
153153
= a
154154
If now we assign a positive value to $b$, then $a$ is evaluated:
155155
>> b=2; a
156156
= 3
157-
Notice however that if we assign an unconditional value to $a$, \
158-
this overrides the condition:
159-
>> a:=0; a/; b>1:= 3
160-
>> a
161-
= 0
162157
"""
163158

159+
# I WMA, if we assign a value without a condition on the LHS,
160+
# conditional values are never reached. So,
161+
#
162+
# Notice however that if we assign an unconditional value to $a$, \
163+
# this overrides the condition:
164+
# >> a:=0; a/; b>1:= 3
165+
# >> a
166+
# = 0
167+
#
168+
# In Mathics, this last line would return 3
169+
# """
170+
164171
operator = ":="
165172
attributes = A_HOLD_ALL | A_PROTECTED | A_SEQUENCE_HOLD
166173

mathics/builtin/assignments/internals.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,19 @@ def process_assign_format(self, lhs, rhs, evaluation, tags, upset):
612612
return count > 0
613613

614614

615+
def process_assign_list(self, lhs, rhs, evaluation, tags, upset):
616+
if not (
617+
rhs.get_head_name() == "System`List" and len(lhs.elements) == len(rhs.elements)
618+
): # nopep8
619+
evaluation.message(self.get_name(), "shape", lhs, rhs)
620+
return False
621+
result = True
622+
for left, right in zip(lhs.elements, rhs.elements):
623+
if not self.assign(left, right, evaluation):
624+
result = False
625+
return result
626+
627+
615628
def process_assign_makeboxes(self, lhs, rhs, evaluation, tags, upset):
616629
# FIXME: the below is a big hack.
617630
# Currently MakeBoxes boxing is implemented as a bunch of rules.
@@ -768,6 +781,7 @@ class _SetOperator:
768781
"System`DefaultValues": process_assign_definition_values,
769782
"System`DownValues": process_assign_definition_values,
770783
"System`Format": process_assign_format,
784+
"System`List": process_assign_list,
771785
"System`MakeBoxes": process_assign_makeboxes,
772786
"System`MessageName": process_assign_messagename,
773787
"System`Messages": process_assign_definition_values,
@@ -800,11 +814,10 @@ def assign_elementary(self, lhs, rhs, evaluation, tags=None, upset=False):
800814
def assign(self, lhs, rhs, evaluation):
801815
# lhs._format_cache = None
802816
defs = evaluation.definitions
803-
if lhs.get_head_name() == "System`List":
817+
if False and lhs.get_head_name() == "System`List":
804818
if not (rhs.get_head_name() == "System`List") or len(lhs.elements) != len(
805819
rhs.elements
806820
): # nopep8
807-
808821
evaluation.message(self.get_name(), "shape", lhs, rhs)
809822
return False
810823
else:

0 commit comments

Comments
 (0)