restrict_environment function

Build a new function with a smaller environment