Skip to content

Commit ad6b87a

Browse files
authored
Merge pull request #601 from Mathics3/documenting_assignment
Adding comments and tests for Assignment
2 parents d88ab76 + a728701 commit ad6b87a

File tree

5 files changed

+266
-63
lines changed

5 files changed

+266
-63
lines changed

CHANGES.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. contents::
1+
.. contents::
22

33
CHANGES
44
=======
@@ -41,6 +41,8 @@ Internals
4141
#. Operator name to unicode or ASCII comes from Mathics scanner character tables.
4242
#. ``eval*`` methods in `Builtin` classes are considerer as synonyms of ``apply*`` methods.
4343
#. Modularize and improve the way in which `Builtin` classes are selected to have an associated `Definition`.
44+
#. `_SetOperator.assign_elementary` was renamed as `_SetOperator.assign`. All the special cases are not handled by the `_SetOperator.special_cases` dict.
45+
4446

4547

4648
Bugs

mathics/builtin/assignments/assignment.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,30 @@ class SetDelayed(Set):
144144
= 2 / 3
145145
>> F[-3, 2]
146146
= -2 / 3
147+
We can use conditional delayed assignments to define \
148+
symbols with values conditioned to the context. For example,
149+
>> ClearAll[a,b]; a/; b>0:= 3
150+
Set $a$ to have a value of $3$ if certain variable $b$ is positive.\
151+
So, if this variable is not set, $a$ stays unevaluated:
152+
>> a
153+
= a
154+
If now we assign a positive value to $b$, then $a$ is evaluated:
155+
>> b=2; a
156+
= 3
147157
"""
148158

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+
149171
operator = ":="
150172
attributes = A_HOLD_ALL | A_PROTECTED | A_SEQUENCE_HOLD
151173

@@ -203,7 +225,7 @@ def apply(self, f, lhs, rhs, evaluation):
203225
return
204226

205227
rhs = rhs.evaluate(evaluation)
206-
self.assign_elementary(lhs, rhs, evaluation, tags=[name])
228+
self.assign(lhs, rhs, evaluation, tags=[name])
207229
return rhs
208230

209231

@@ -228,7 +250,7 @@ def apply(self, f, lhs, rhs, evaluation):
228250
evaluation.message(self.get_name(), "sym", f, 1)
229251
return
230252

231-
if self.assign_elementary(lhs, rhs, evaluation, tags=[name]):
253+
if self.assign(lhs, rhs, evaluation, tags=[name]):
232254
return SymbolNull
233255
else:
234256
return SymbolFailed

0 commit comments

Comments
 (0)