1616"""
1717
1818
19+ from mathics .core .atoms import Integer1 , IntegerM1
1920from mathics .core .attributes import A_HOLD_FIRST , A_PROTECTED , A_READ_PROTECTED
2021from mathics .core .builtin import InfixOperator , PostfixOperator , PrefixOperator
22+ from mathics .core .evaluation import Evaluation
23+ from mathics .core .systemsymbols import SymbolPlus
24+ from mathics .eval .assignments .assign_binaryop import eval_inplace_op
2125
2226
23- class AddTo (InfixOperator ):
27+ class InplaceInfixOperator :
28+ grouping = "Right"
29+ operator_symbol = SymbolPlus
30+ increment_symbol = Integer1
31+ returns_updated_value : bool = True
32+
33+ def eval (self , expr , evaluation : Evaluation ):
34+ """%(name)s[expr_]"""
35+ return eval_inplace_op (
36+ self ,
37+ expr ,
38+ self .operator_symbol ,
39+ self .increment_symbol ,
40+ self .returns_updated_value ,
41+ evaluation ,
42+ )
43+
44+
45+ class AddTo (InfixOperator , InplaceInfixOperator ):
2446 """
2547 <url>:WMA link:https://reference.wolfram.com/language/ref/AddTo.html</url>
2648
@@ -38,16 +60,27 @@ class AddTo(InfixOperator):
3860 """
3961
4062 attributes = A_HOLD_FIRST | A_PROTECTED
41- grouping = "Right"
4263 operator = "+="
4364
44- rules = {
45- "x_ += dx_" : "x = x + dx" ,
46- }
65+ operator_symbol = SymbolPlus
66+ return_before_value : bool = True
4767 summary_text = "add a value and assigns that returning the new value"
4868
49-
50- class Decrement (PostfixOperator ):
69+ def eval (self , expr , increment , evaluation : Evaluation ):
70+ """%(name)s[expr_, increment_]"""
71+ return eval_inplace_op (
72+ self ,
73+ expr ,
74+ self .operator_symbol ,
75+ increment ,
76+ self .return_before_value ,
77+ evaluation ,
78+ )
79+
80+
81+ # Decrement has Infix properties for evaluation purposes, but Postfix
82+ # properties for operator association.
83+ class Decrement (InplaceInfixOperator , InfixOperator , PostfixOperator ):
5184 """
5285 <url>:WMA link
5386 :https://reference.wolfram.com/language/ref/Decrement.html</url>
@@ -59,26 +92,43 @@ class Decrement(PostfixOperator):
5992 <dd>decrements $x$ by 1, returning the original value of $x$.
6093 </dl>
6194
62- >> a = 5;
63- X> a--
95+ >> a = 5; a--
6496 = 5
65- X > a
97+ > > a
6698 = 4
99+
100+ Decrement a numerical value:
101+
102+ >> a = 1.6; a--; a
103+ = 0.6
104+
105+ Decrement all values in a list:
106+
107+ >> a = {1, 3, 5}
108+ = {1, 3, 5}
109+
110+ >> a--; a
111+ = {0, 2, 4}
112+
113+ Compare with <url>:PreDecrement:
114+ /doc/reference-of-built-in-symbols/assignments/in-place-binary-assignment-operator/predecrement
115+ </url> which returns the value before updating, and <url>:Increment:
116+ /doc/reference-of-built-in-symbols/assignments/in-place-binary-assignment-operator/increment
117+ </url> which goes the other way.
67118 """
68119
69- operator = "--"
70120 attributes = A_HOLD_FIRST | A_PROTECTED | A_READ_PROTECTED
121+ increment_symbol = IntegerM1
122+ operator = "--"
123+ operator_symbol = SymbolPlus
71124
72- rules = {
73- "x_--" : "Module[{t=x}, x = x - 1; t]" ,
74- }
75-
125+ returns_updated_value = False
76126 summary_text = (
77127 "decreases the value by one and assigns that returning the original value"
78128 )
79129
80130
81- class DivideBy (InfixOperator ):
131+ class DivideBy (InplaceInfixOperator , InfixOperator ):
82132 """
83133 <url>:WMA link:https://reference.wolfram.com/language/ref/DivideBy.html</url>
84134
@@ -105,7 +155,7 @@ class DivideBy(InfixOperator):
105155 summary_text = "divide a value and assigns that returning the new value"
106156
107157
108- class Increment (PostfixOperator ):
158+ class Increment (InplaceInfixOperator , InfixOperator , PostfixOperator ):
109159 """
110160 <url>:WMA link:
111161 https://reference.wolfram.com/language/ref/Increment.html</url>
@@ -117,34 +167,90 @@ class Increment(PostfixOperator):
117167 <dd>increments $x$ by 1, returning the original value of $x$.
118168 </dl>
119169
120- >> a = 2;
121- >> a++
170+ >> a = 1; a++
171+ = 1
172+ >> a
122173 = 2
174+
175+ Increment a numeric value:
176+
177+ >> a = 1.5; a++
178+ = 1.5
179+
123180 >> a
124- = 3
181+ = 2.5
182+
183+ Increment a symbolic value:
184+
185+ >> y = 2 x; y++; y
186+ = 1 + 2 x
187+
188+ Increment all values in a list:
189+
190+ >> x = {1, 3, 5}
191+ = {1, 3, 5}
192+
193+ x++; x
194+ = {2, 4, 6}
195+
125196 Grouping of 'Increment', 'PreIncrement' and 'Plus':
126197 >> ++++a+++++2//Hold//FullForm
127198 = Hold[Plus[PreIncrement[PreIncrement[Increment[Increment[a]]]], 2]]
128- """
129199
130- operator = "++"
131- attributes = A_HOLD_FIRST | A_PROTECTED | A_READ_PROTECTED
200+ Compare with <url>:PreIncrement:
201+ /doc/reference-of-built-in-symbols/assignments/in-place-binary-assignment-operator/preincrement
202+ </url> which returns the value before update.
132203
133- rules = {
134- "x_++" : (
135- "Module[{Internal`IncrementTemporary = x},"
136- " x = x + 1;"
137- " Internal`IncrementTemporary"
138- "]"
139- ),
140- }
204+ #> Clear[a, x, y]
205+ """
141206
207+ attributes = A_HOLD_FIRST | A_PROTECTED | A_READ_PROTECTED
208+ increment_symbol = Integer1
209+ operator = "++"
210+ operator_symbol = SymbolPlus
211+ returns_updated_value : bool = False
142212 summary_text = (
143213 "increases the value by one and assigns that returning the original value"
144214 )
145215
146216
147- class PreIncrement (PrefixOperator ):
217+ class PreDecrement (InplaceInfixOperator , PrefixOperator ):
218+ """
219+ <url>:WMA link:
220+ https://reference.wolfram.com/language/ref/PreDecrement.html</url>
221+
222+ <dl>
223+ <dt>'PreDecrement[$x$]'
224+
225+ <dt>'--$x$'
226+ <dd>decrements $x$ by 1, returning the new value of $x$.
227+ </dl>
228+
229+ '--$a$' is equivalent to '$a$ = $a$ - 1':
230+ >> a = 2;
231+ >> --a
232+ = 1
233+ >> a
234+ = 1
235+
236+ Compare with <url>:Decrement:
237+ /doc/reference-of-built-in-symbols/assignments/in-place-binary-assignment-operator/decrement
238+ </url> which returns the updated value, and <url>:Increment:
239+ /doc/reference-of-built-in-symbols/assignments/in-place-binary-assignment-operator/increment
240+ </url> which goes the other way.
241+
242+ #> Clear[a]
243+ """
244+
245+ attributes = A_HOLD_FIRST | A_PROTECTED | A_READ_PROTECTED
246+ increment_symbol = IntegerM1
247+ operator = "--"
248+ operator_symbol = SymbolPlus
249+ returns_updated_value : bool = True
250+ summary_text = "decrease the value by one and assigns that returning the new value"
251+
252+
253+ class PreIncrement (InplaceInfixOperator , PrefixOperator ):
148254 """
149255 <url>:WMA link:
150256 https://reference.wolfram.com/language/ref/PreIncrement.html</url>
@@ -156,50 +262,46 @@ class PreIncrement(PrefixOperator):
156262 </dl>
157263
158264 '++$a$' is equivalent to '$a$ = $a$ + 1':
159- >> a = 2;
265+ >> a = 2
266+ = 2
267+
160268 >> ++a
161269 = 3
270+
162271 >> a
163272 = 3
164- """
165273
166- attributes = A_HOLD_FIRST | A_PROTECTED | A_READ_PROTECTED
167- operator = "++"
274+ PreIncrement a numeric value:
168275
169- rules = {
170- "++x_" : "x = x + 1" ,
171- }
276+ >> a + 1.6
277+ = 4.6
172278
173- summary_text = "increase the value by one and assigns that returning the new value"
174279
280+ PreIncrement a symbolic value:
175281
176- class PreDecrement (PrefixOperator ):
177- """
178- <url>:WMA link:
179- https://reference.wolfram.com/language/ref/PreDecrement.html</url>
282+ #> Clear[x, y]
180283
181- <dl>
182- <dt>'PreDecrement[$x$]'
284+ >> y = x; ++y
285+ = 1 + x
183286
184- <dt>'--$x$'
185- <dd>decrements $x$ by 1, returning the new value of $x$.
186- </dl>
287+ >> y
288+ = 1 + x
187289
188- '--$a$' is equivalent to '$a$ = $a$ - 1':
189- >> a = 2;
190- >> --a
191- = 1
192- >> a
193- = 1
290+ Compare with <url>:Increment:
291+ /doc/reference-of-built-in-symbols/assignments/in-place-binary-assignment-operator/increment
292+ </url> which returns the updated value, and <url>:PreDecrement:
293+ /doc/reference-of-built-in-symbols/assignments/in-place-binary-assignment-operator/predecrement
294+ </url> which goes the other way.
295+
296+ #> Clear[a, x, y]
194297 """
195298
196- operator = "--"
197299 attributes = A_HOLD_FIRST | A_PROTECTED | A_READ_PROTECTED
300+ operator = "++"
301+ operator_symbol = SymbolPlus
302+ return_before_value : bool = False
198303
199- rules = {
200- "--x_" : "x = x - 1" ,
201- }
202- summary_text = "decrease the value by one and assigns that returning the new value"
304+ summary_text = "increase the value by one and assigns that returning the new value"
203305
204306
205307class SubtractFrom (InfixOperator ):
@@ -218,6 +320,8 @@ class SubtractFrom(InfixOperator):
218320 = 8
219321 >> a
220322 = 8
323+
324+ #> Clear[a]
221325 """
222326
223327 attributes = A_HOLD_FIRST | A_PROTECTED
@@ -245,6 +349,8 @@ class TimesBy(InfixOperator):
245349 = 20
246350 >> a
247351 = 20
352+
353+ #> Clear[a]
248354 """
249355
250356 operator = "*="
0 commit comments