0

I've been developing a custom function that serves to simplify the body of other Function's in Mathematica. Here is my function definition:

FSimplifyFunction[f : HoldPattern[Function[vars : _Symbol, body_, atts___]], opts : OptionsPattern[]] := 
Block[{vars}, Function @@ FullSimplify[List @@ f, opts]]

This function appears to perform as intended for my needs. However, I am encountering a warning stating: "vars appears twice in a nested scoping construct." I understand that this is due to my usage of vars as both a pattern variable in Function[vars, ...] and a local variable in Block[{vars}, ...].

Despite the warning, the code seems to operate as I anticipate. Should this warning be a cause for concern? Is there an alternative way to structure my code to avoid this warning while preserving functionality? I welcome any insights.

Note: Changing the name of vars within the Block affects its behavior, leading to unintended consequences when the same variable name is used globally. To ensure proper scoping and protect the function from global values, we must stick to the original definition of vars.

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
  • 1
    Change the name of one of the vars? What is the one in the Block doing anyway? – MarcoB Aug 01 '23 at 01:29
  • Hi, @Marco! If I modify the name of vars within the Block, a problem arises when vars is assigned as a global variable, which is a scenario I am trying to prevent. – E. Chan-López Aug 01 '23 at 01:44
  • 1
    Do you need to worry about non-vars parameters being evaluated, as in a = 4; FSimplifyFunction[Function[x, x + a]]? – Michael E2 Aug 01 '23 at 02:47
  • 1
    Somewhat similar here: https://stackoverflow.com/a/6236264 – Michael E2 Aug 01 '23 at 03:13
  • 1
    This construct is used here: https://mathematica.stackexchange.com/questions/2793/making-a-symbols-new-definitions-be-tried-before-all-previously-defined-ones/2795#2795 – Michael E2 Aug 01 '23 at 03:15
  • Thank you, @MichaelE2! I need to carefully read through the references you've shared. Indeed, I need to prevent the evaluation of variables on which the function depends, as in the case of Function[x, x + a]. – E. Chan-López Aug 01 '23 at 03:35

0 Answers0