-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathevaluation.py
More file actions
340 lines (261 loc) · 9.34 KB
/
Copy pathevaluation.py
File metadata and controls
340 lines (261 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# -*- coding: utf-8 -*-
"""Evaluation Control
Mathics3 takes an expression that it is given, and evaluates it. Built \
into the evaluation are primitives that allow finer control over the \
process of evaluation in cases where it is needed.
"""
from mathics.core.atoms import Integer
from mathics.core.attributes import A_HOLD_ALL, A_HOLD_ALL_COMPLETE, A_PROTECTED
from mathics.core.builtin import Builtin, Predefined
from mathics.core.evaluation import (
MAX_RECURSION_DEPTH,
Evaluation,
set_python_recursion_limit,
)
class RecursionLimit(Predefined):
r"""
<url>
:WMA link:
https://reference.wolfram.com/language/ref/\$RecursionLimit.html</url>
<dl>
<dt>'\$RecursionLimit'
<dd>specifies the maximum allowable recursion depth after which a \
calculation is terminated.
</dl>
Calculations terminated by '\$RecursionLimit' return '\$Aborted':
>> a = a + a
: Recursion depth of 200 exceeded.
= $Aborted
>> $RecursionLimit
= 200
>> $RecursionLimit = x;
: Cannot set $RecursionLimit to x; value must be an integer between 20 and 512; use the MATHICS_MAX_RECURSION_DEPTH environment variable to allow higher limits.
>> $RecursionLimit = 512
= 512
>> a = a + a
: Recursion depth of 512 exceeded.
= $Aborted
"""
name = "$RecursionLimit"
value = 200
set_python_recursion_limit(value)
rules = {
"$RecursionLimit": str(value),
}
messages = {
"reclim": "Recursion depth of `1` exceeded.",
"limset": (
"Cannot set $RecursionLimit to `1`; "
"value must be an integer between 20 and %d; "
"use the MATHICS_MAX_RECURSION_DEPTH environment variable to allow higher limits."
)
% (MAX_RECURSION_DEPTH),
}
rules = {
"$RecursionLimit": str(value),
}
summary_text = "maximum recursion depth"
def evaluate(self, evaluation) -> Integer:
return Integer(self.value)
class IterationLimit(Predefined):
r"""
<url>:WMA link:https://reference.wolfram.com/language/ref/\$IterationLimit.html</url>
<dl>
<dt>'\$IterationLimit'
<dd>specifies the maximum number of times a reevaluation of an expression may happen.
</dl>
Calculations terminated by '\$IterationLimit' return '\$Aborted':
>> $IterationLimit
= 1000
"""
name = "$IterationLimit"
value = 1000
rules = {
"$IterationLimit": str(value),
}
messages = {
"itlim": "Iteration limit of `1` exceeded.",
"limset": (
"Cannot set $IterationLimit to `1`; "
"value must be an integer between 20 and Infinity."
),
}
rules = {
"$IterationLimit": str(value),
}
summary_text = "maximum number of iterations"
def evaluate(self, evaluation):
return Integer(self.value)
class Hold(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Hold.html</url>
<dl>
<dt>'Hold'[$expr$]
<dd>prevents $expr$ from being evaluated.
</dl>
>> Attributes[Hold]
= {HoldAll, Protected}
"""
attributes = A_HOLD_ALL | A_PROTECTED
summary_text = "prevent the evaluation"
class HoldComplete(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/HoldComplete.html</url>
<dl>
<dt>'HoldComplete'[$expr$]
<dd>prevents $expr$ from being evaluated, and also prevents \
'Sequence' objects from being spliced into argument lists.
</dl>
>> Attributes[HoldComplete]
= {HoldAllComplete, Protected}
"""
attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED
summary_text = "prevents the evaluation, including the upvalues"
class HoldForm(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/HoldForm.html</url>
<dl>
<dt>'HoldForm'[$expr$]
<dd>is equivalent to 'Hold[$expr$]', but prints as $expr$.
</dl>
>> HoldForm[1 + 2 + 3]
= 1 + 2 + 3
'HoldForm' has attribute 'HoldAll':
>> Attributes[HoldForm]
= {HoldAll, Protected}
"""
attributes = A_HOLD_ALL | A_PROTECTED
rules = {
"MakeBoxes[HoldForm[expr_], f_]": "MakeBoxes[expr, f]",
}
summary_text = "prevents the evaluation, prints just the expression"
class Evaluate(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Evaluate.html</url>
<dl>
<dt>'Evaluate'[$expr$]
<dd>forces evaluation of $expr$, even if it occurs inside a held argument or a 'Hold' form.
</dl>
Create a function $f$ with a held argument:
>> SetAttributes[f, HoldAll]
>> f[1 + 2]
= f[1 + 2]
'Evaluate' forces evaluation of the argument, even though $f$ has
the 'HoldAll' attribute:
>> f[Evaluate[1 + 2]]
= f[3]
>> Hold[Evaluate[1 + 2]]
= Hold[3]
>> HoldComplete[Evaluate[1 + 2]]
= HoldComplete[Evaluate[1 + 2]]
>> Evaluate[Sequence[1, 2]]
= Sequence[1, 2]
"""
rules = {
"Evaluate[Unevaluated[x_]]": "Unevaluated[x]",
"Evaluate[x___]": "x",
}
summary_text = "evaluate the element, disregarding Hold attributes"
class Unevaluated(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Unevaluated.html</url>
<dl>
<dt>'Unevaluated'[$expr$]
<dd>temporarily leaves $expr$ in an unevaluated form when it appears as a function argument.
</dl>
'Unevaluated' is automatically removed when function arguments are evaluated:
>> Sqrt[Unevaluated[x]]
= Sqrt[x]
In the following, the 'Length' value 4 because we do not evaluate the 'Plus':
>> Length[Unevaluated[1+2+3+4]]
= 4
'Unevaluated' has attribute 'HoldAllComplete':
>> Attributes[Unevaluated]
= {HoldAllComplete, Protected}
The 'Unevaluated[]' function call is kept in arguments of non-executed functions:
>> f[Unevaluated[x]]
= f[Unevaluated[x]]
In functions that have the 'Flat' property, 'Unevaluated[]' propagates into function's arguments:
>> Attributes[f] = {Flat};
>> f[a, Unevaluated[f[b, c]]]
= f[a, Unevaluated[b], Unevaluated[c]]
In 'Sequences' containing 'Unevaluated' functions:
>> g[a, Sequence[Unevaluated[b], Unevaluated[c]]]
= g[a, Unevaluated[b], Unevaluated[c]]
However, when surrounding a 'Sequence' by 'Unevaluated', no proliferation of 'Unevaluated[]' function calls occurs:
>> g[Unevaluated[Sequence[a, b, c]]]
= g[Unevaluated[Sequence[a, b, c]]]
"""
attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED
summary_text = "keep the element unevaluated, disregarding Hold attributes"
def eval(self, expr, evaluation: Evaluation):
"Unevaluated[expr_]"
# Note that because Unevaluated[] has the HoldAllComplete attribute, nothing
# in expr should have been evaluated leading up to this call, leaving
# methods like this to decide whether to evaluate or not. Of course, here
# we don't want evaluation.
# Setting the "elements" property for Unevaluated[expr] (note,
# not in the subexpression "expr") to be "fully evaluated"
# will further keep anything under "expr" form getting evaluated.
#
# It may seem odd that in this case we are saying something as "fully evaluated" to mean
# "don't evaluate". But you have a similar weirdness Unevaluated[5] means don't evaluate
# 5 but, 5 is already fully evaluated.
# Note that this isn't complete. In any functions which call Unevaluated that do not
# expect
evaluation.current_expression.elements_properties.elements_fully_evaluated = (
True
)
class ReleaseHold(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/ReleaseHold.html</url>
<dl>
<dt>'ReleaseHold'[$expr$]
<dd>removes any 'Hold', 'HoldForm', 'HoldPattern' or
'HoldComplete' head from $expr$.
</dl>
>> x = 3;
>> Hold[x]
= Hold[x]
>> ReleaseHold[Hold[x]]
= 3
>> ReleaseHold[y]
= y
"""
rules = {
"ReleaseHold[(Hold|HoldForm|HoldPattern|HoldComplete)[expr_]]": "expr",
"ReleaseHold[other_]": "other",
}
summary_text = "replace a Hold expression by its argument"
class Sequence(Builtin):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/Sequence.html</url>
<dl>
<dt>'Sequence'[$x_1$, $x_2$, ...]
<dd>represents a sequence of arguments to a function.
</dl>
'Sequence' is automatically spliced in, except when a function has attribute 'SequenceHold'
(like assignment functions).
>> f[x, Sequence[a, b], y]
= f[x, a, b, y]
>> Attributes[Set]
= {HoldFirst, Protected, SequenceHold}
>> a = Sequence[b, c];
>> a
= Sequence[b, c]
Apply 'Sequence' to a list to splice in arguments:
>> list = {1, 2, 3};
>> f[Sequence @@ list]
= f[1, 2, 3]
Inside 'Hold' or a function with a held argument, 'Sequence' is
spliced in at the first level of the argument:
>> Hold[a, Sequence[b, c], d]
= Hold[a, b, c, d]
If 'Sequence' appears at a deeper level, it is left unevaluated:
>> Hold[{a, Sequence[b, c], d}]
= Hold[{a, Sequence[b, c], d}]
"""
summary_text = (
"a sequence of arguments that will automatically be spliced into any function"
)
formats = {"Sequence[elems___]": "HoldForm[Sequence][elems]"}