@@ -1378,7 +1378,11 @@ def evaluate(self, evaluation) -> typing.Union["Expression", "Symbol"]:
13781378 def evaluate_next (self , evaluation ) -> typing .Tuple ["Expression" , bool ]:
13791379 from mathics .builtin .base import BoxConstruct
13801380
1381+ up_cache_result = evaluation .cache_result
1382+ cache_result = up_cache_result
13811383 head = self ._head .evaluate (evaluation )
1384+ cache_result = cache_result and evaluation .cache_result
1385+ evaluation .cache_result = up_cache_result
13821386 attributes = head .get_attributes (evaluation .definitions )
13831387 leaves = self .get_mutable_leaves ()
13841388
@@ -1389,7 +1393,11 @@ def rest_range(indices):
13891393 for index in indices :
13901394 leaf = leaves [index ]
13911395 if leaf .has_form ("Evaluate" , 1 ):
1396+ up_cache_result = evaluation .cache_result
1397+ cache_result = up_cache_result
13921398 leaves [index ] = leaf .evaluate (evaluation )
1399+ cache_result = cache_result and evaluation .cache_result
1400+ evaluation .cache_result = up_cache_result
13931401
13941402 def eval_range (indices ):
13951403 for index in indices :
@@ -1405,11 +1413,18 @@ def eval_range(indices):
14051413 elif "System`HoldFirst" in attributes :
14061414 rest_range (range (0 , min (1 , len (leaves ))))
14071415 eval_range (range (1 , len (leaves )))
1416+ cache_result = cache_result and evaluation .cache_result
1417+ evaluation .cache_result = up_cache_result
14081418 elif "System`HoldRest" in attributes :
14091419 eval_range (range (0 , min (1 , len (leaves ))))
1420+ cache_result = cache_result and evaluation .cache_result
1421+ evaluation .cache_result = up_cache_result
14101422 rest_range (range (1 , len (leaves )))
14111423 else :
14121424 eval_range (range (len (leaves )))
1425+ cache_result = cache_result and evaluation .cache_result
1426+ evaluation .cache_result = up_cache_result
1427+
14131428 # rest_range(range(0, 0))
14141429
14151430 new = Expression (head )
@@ -1454,6 +1469,7 @@ def flatten_callback(new_leaves, old):
14541469 if "System`Listable" in attributes :
14551470 done , threaded = new .thread (evaluation )
14561471 if done :
1472+ evaluation .cache_result = cache_result
14571473 if threaded .sameQ (new ):
14581474 new ._timestamp_cache (evaluation )
14591475 return new , False
@@ -1481,6 +1497,7 @@ def rules():
14811497 for rule in rules ():
14821498 result = rule .apply (new , evaluation , fully = False )
14831499 if result is not None :
1500+ evaluation .cache_result = cache_result
14841501 if isinstance (result , BoxConstruct ):
14851502 return result , False
14861503 if result .sameQ (new ):
@@ -1504,11 +1521,19 @@ def rules():
15041521
15051522 new .unformatted = self .unformatted
15061523 new ._timestamp_cache (evaluation )
1524+ evaluation .cache_result = cache_result
15071525 return new , False
15081526
15091527 def evaluate_leaves (self , evaluation ) -> "Expression" :
1510- leaves = [leaf .evaluate (evaluation ) for leaf in self ._leaves ]
1528+ up_cache_result = evaluation .cache_result
1529+ cache_result = up_cache_result
1530+ leaves = []
1531+ for leaf in self ._leaves :
1532+ leaves .append (leaf .evaluate (evaluation ))
1533+ cache_result = cache_result and evaluation .cache_result
1534+ evaluation .cache_result = up_cache_result
15111535 head = self ._head .evaluate_leaves (evaluation )
1536+ evaluation .cache_result = cache_result and evaluation .cache_result
15121537 return Expression (head , * leaves )
15131538
15141539 def __str__ (self ) -> str :
0 commit comments