From 3b361f5c2247425a3ab3dc8602ab4a521d94a8a2 Mon Sep 17 00:00:00 2001 From: Isuru Fernando Date: Wed, 4 May 2022 00:07:05 -0500 Subject: [PATCH] fix map_comparison and map_substitution in IdentityMapper --- pymbolic/mapper/__init__.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pymbolic/mapper/__init__.py b/pymbolic/mapper/__init__.py index 2536de17..1ef44fe2 100644 --- a/pymbolic/mapper/__init__.py +++ b/pymbolic/mapper/__init__.py @@ -569,10 +569,12 @@ def map_bitwise_or(self, expr, *args, **kwargs): map_logical_and = map_bitwise_or def map_comparison(self, expr, *args, **kwargs): - return type(expr)( - self.rec(expr.left, *args, **kwargs), - expr.operator, - self.rec(expr.right, *args, **kwargs)) + left = self.rec(expr.left, *args, **kwargs) + right = self.rec(expr.right, *args, **kwargs) + if left is expr.left and right is expr.right: + return expr + + return type(expr)(left, expr.operator, right) def map_list(self, expr, *args, **kwargs): return [self.rec(child, *args, **kwargs) for child in expr] @@ -610,10 +612,13 @@ def map_common_subexpression(self, expr, *args, **kwargs): **expr.get_extra_properties()) def map_substitution(self, expr, *args, **kwargs): - return type(expr)( - self.rec(expr.child, *args, **kwargs), - expr.variables, - tuple([self.rec(v, *args, **kwargs) for v in expr.values])) + child = self.rec(expr.child, *args, **kwargs) + values = tuple([self.rec(v, *args, **kwargs) for v in expr.values]) + if child is expr.child and all([val is orig_val + for val, orig_val in zip(values, expr.values)]): + return expr + + return type(expr)(child, expr.variables, values) def map_derivative(self, expr, *args, **kwargs): child = self.rec(expr.child, *args, **kwargs)