def rm_id(isid, new=new):
 def ident_remove(expr):
  ' Remove identities '
  ids = list(map(isid, expr.args))
  if (sum(ids) == 0):
   return expr
  elif (sum(ids) != len(ids)):
   return new(expr.__class__, *[arg for (arg, x) in zip(expr.args, ids) if (not x)])
  else:
   return new(expr.__class__, expr.args[0])
 return ident_remove

def rm_id ( isid , new = new ) : def ident_remove ( expr ) : ids = list ( map ( isid , expr . args ) ) if ( sum ( ids ) == 0 ) : return expr elif ( sum ( ids ) != len ( ids ) ) : return new ( expr . __class__ , * [ arg for ( arg , x ) in zip ( expr . args , ids ) if ( not x ) ] ) else : return new ( expr . __class__ , expr . args [ 0 ] ) return ident_remove
create a rule to remove identities isid - fn :: x -> bool --- whether or not this element is an identity .