Composing two substitutions, say
and
, produces a new
substitution,
, which when applied to an expression, say
, results in the same expression as applying first
and
then
to
. That is,
For example, if we think about first applying the
substitution given above, then the substitution
, we can see that any instance of variable
will first be
replaced by
when
is applied. This becomes
when
is applied, so the net result is that
goes to
. The variable
would becomes
which is changed to
by
, and
becomes
which is unchanged by
. Furthermore, the
variable
which is untouched by
, is replaced by
when
is applied. So the resulting composition
.
Formally, we can define composition of substitutions by saying that if we have
and
the composition is found by creating
and then removing any pairs of the form
.