fixed substitute_term for lambda applications