def merge_transforms(exp):
 if isinstance(exp, (Var, Removed)):
  return exp
 elif isinstance(exp, (Reduced, Sorted, Transformed)):
  prev = merge_transforms(exp.sub)
  if isinstance(prev, (Reduced, Sorted, Transformed)):
   B = exp.var.compute_value
   assert isinstance(B, Lookup)
   A = B.variable.compute_value
   assert isinstance(A, Lookup)
   new_var = DiscreteVariable(exp.var.name, values=exp.var.values, ordered=exp.var.ordered, compute_value=merge_lookup(A, B))
   assert isinstance(prev.sub, Var)
   return Transformed(prev.sub, new_var)
  else:
   assert (prev is exp.sub)
   return exp
 else:
  raise TypeError

def merge_transforms ( exp ) : if isinstance ( exp , ( Var , Removed ) ) : return exp elif isinstance ( exp , ( Reduced , Sorted , Transformed ) ) : prev = merge_transforms ( exp . sub ) if isinstance ( prev , ( Reduced , Sorted , Transformed ) ) : B = exp . var . compute_value assert isinstance ( B , Lookup ) A = B . variable . compute_value assert isinstance ( A , Lookup ) new_var = DiscreteVariable ( exp . var . name , values = exp . var . values , ordered = exp . var . ordered , compute_value = merge_lookup ( A , B ) ) assert isinstance ( prev . sub , Var ) return Transformed ( prev . sub , new_var ) else : assert ( prev is exp . sub ) return exp else : raise TypeError
merge consecutive removed .