def subst(s, x):
if isinstance(x, list):<EOL><INDENT>return [subst(s, xi) for xi in x]<EOL><DEDENT>elif isinstance(x, tuple):<EOL><INDENT>return tuple([subst(s, xi) for xi in x])<EOL><DEDENT>elif not isinstance(x, Expr):<EOL><INDENT>return x<EOL><DEDENT>elif is_var_symbol(x.op):<EOL><INDENT>return s.get(x, x)<EOL><DEDENT>else:<EOL><INDENT>return Expr(x.op, *[subst(s, arg) for arg in x.args])<EOL><DEDENT>
Substitute the substitution s into the expression x.
    >>> subst({x: 42, y:0}, F(x) + y)
    (F(42) + 0)