Btw your code for substitution is not correct because it is not "capture avoiding" !!! May I suggest switching to the De Bruijn handling of binders?