Force evaluation of user-defined function












9












$begingroup$


Consider a function that sorts its arguments based on a function called order



Clear[f, order]
f[x_, y_] /; order[x] > order[y] := f[y, x]
order[1] = 1;
order[2] = 2;
expr1 = f[1, 2]
expr2 = f[2, 1]
(* f[1, 2] *)
(* f[1, 2] *)


However, let's imagine that we change the ordering function



order[1] = 2;
order[2] = 1;


Surprisingly, expr1 and expr2 still evaluate to f[1,2]



expr1
expr2
(* f[1, 2] *)
(* f[1, 2] *)


However, if we introduce f[1, 2] we obtain the correct result



f[1, 2]
(* f[2, 1] *)


Is there a way to force the reevaluation of expr to obtain f[2, 1]? I am looking for a function along the lines of



ForceEvaluation@expr1
ForceEvaluation@expr2
(* f[2, 1] *)
(* f[2, 1] *)









share|improve this question











$endgroup$








  • 1




    $begingroup$
    While I appreciate the accept, I'd recommend holding off for at least a few more hours. There are plenty of users who know more about Mathematica than I who might be able to provide a better answer, but once an answer is accepted many people will start skipping the question entirely.
    $endgroup$
    – eyorble
    Dec 19 '18 at 18:12










  • $begingroup$
    Thanks for the comment, since I am new I did not consider this. It is certainly an obscure behaviour of Mathematica, and it would be great to hear more about it!
    $endgroup$
    – AGim
    Dec 19 '18 at 18:17






  • 1




    $begingroup$
    Related, but not as minimal an example: (139461)
    $endgroup$
    – Mr.Wizard
    Dec 19 '18 at 21:26
















9












$begingroup$


Consider a function that sorts its arguments based on a function called order



Clear[f, order]
f[x_, y_] /; order[x] > order[y] := f[y, x]
order[1] = 1;
order[2] = 2;
expr1 = f[1, 2]
expr2 = f[2, 1]
(* f[1, 2] *)
(* f[1, 2] *)


However, let's imagine that we change the ordering function



order[1] = 2;
order[2] = 1;


Surprisingly, expr1 and expr2 still evaluate to f[1,2]



expr1
expr2
(* f[1, 2] *)
(* f[1, 2] *)


However, if we introduce f[1, 2] we obtain the correct result



f[1, 2]
(* f[2, 1] *)


Is there a way to force the reevaluation of expr to obtain f[2, 1]? I am looking for a function along the lines of



ForceEvaluation@expr1
ForceEvaluation@expr2
(* f[2, 1] *)
(* f[2, 1] *)









share|improve this question











$endgroup$








  • 1




    $begingroup$
    While I appreciate the accept, I'd recommend holding off for at least a few more hours. There are plenty of users who know more about Mathematica than I who might be able to provide a better answer, but once an answer is accepted many people will start skipping the question entirely.
    $endgroup$
    – eyorble
    Dec 19 '18 at 18:12










  • $begingroup$
    Thanks for the comment, since I am new I did not consider this. It is certainly an obscure behaviour of Mathematica, and it would be great to hear more about it!
    $endgroup$
    – AGim
    Dec 19 '18 at 18:17






  • 1




    $begingroup$
    Related, but not as minimal an example: (139461)
    $endgroup$
    – Mr.Wizard
    Dec 19 '18 at 21:26














9












9








9


1



$begingroup$


Consider a function that sorts its arguments based on a function called order



Clear[f, order]
f[x_, y_] /; order[x] > order[y] := f[y, x]
order[1] = 1;
order[2] = 2;
expr1 = f[1, 2]
expr2 = f[2, 1]
(* f[1, 2] *)
(* f[1, 2] *)


However, let's imagine that we change the ordering function



order[1] = 2;
order[2] = 1;


Surprisingly, expr1 and expr2 still evaluate to f[1,2]



expr1
expr2
(* f[1, 2] *)
(* f[1, 2] *)


However, if we introduce f[1, 2] we obtain the correct result



f[1, 2]
(* f[2, 1] *)


Is there a way to force the reevaluation of expr to obtain f[2, 1]? I am looking for a function along the lines of



ForceEvaluation@expr1
ForceEvaluation@expr2
(* f[2, 1] *)
(* f[2, 1] *)









share|improve this question











$endgroup$




Consider a function that sorts its arguments based on a function called order



Clear[f, order]
f[x_, y_] /; order[x] > order[y] := f[y, x]
order[1] = 1;
order[2] = 2;
expr1 = f[1, 2]
expr2 = f[2, 1]
(* f[1, 2] *)
(* f[1, 2] *)


However, let's imagine that we change the ordering function



order[1] = 2;
order[2] = 1;


Surprisingly, expr1 and expr2 still evaluate to f[1,2]



expr1
expr2
(* f[1, 2] *)
(* f[1, 2] *)


However, if we introduce f[1, 2] we obtain the correct result



f[1, 2]
(* f[2, 1] *)


Is there a way to force the reevaluation of expr to obtain f[2, 1]? I am looking for a function along the lines of



ForceEvaluation@expr1
ForceEvaluation@expr2
(* f[2, 1] *)
(* f[2, 1] *)






functions function-construction evaluation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '18 at 17:46







AGim

















asked Dec 19 '18 at 17:38









AGimAGim

465




465








  • 1




    $begingroup$
    While I appreciate the accept, I'd recommend holding off for at least a few more hours. There are plenty of users who know more about Mathematica than I who might be able to provide a better answer, but once an answer is accepted many people will start skipping the question entirely.
    $endgroup$
    – eyorble
    Dec 19 '18 at 18:12










  • $begingroup$
    Thanks for the comment, since I am new I did not consider this. It is certainly an obscure behaviour of Mathematica, and it would be great to hear more about it!
    $endgroup$
    – AGim
    Dec 19 '18 at 18:17






  • 1




    $begingroup$
    Related, but not as minimal an example: (139461)
    $endgroup$
    – Mr.Wizard
    Dec 19 '18 at 21:26














  • 1




    $begingroup$
    While I appreciate the accept, I'd recommend holding off for at least a few more hours. There are plenty of users who know more about Mathematica than I who might be able to provide a better answer, but once an answer is accepted many people will start skipping the question entirely.
    $endgroup$
    – eyorble
    Dec 19 '18 at 18:12










  • $begingroup$
    Thanks for the comment, since I am new I did not consider this. It is certainly an obscure behaviour of Mathematica, and it would be great to hear more about it!
    $endgroup$
    – AGim
    Dec 19 '18 at 18:17






  • 1




    $begingroup$
    Related, but not as minimal an example: (139461)
    $endgroup$
    – Mr.Wizard
    Dec 19 '18 at 21:26








1




1




$begingroup$
While I appreciate the accept, I'd recommend holding off for at least a few more hours. There are plenty of users who know more about Mathematica than I who might be able to provide a better answer, but once an answer is accepted many people will start skipping the question entirely.
$endgroup$
– eyorble
Dec 19 '18 at 18:12




$begingroup$
While I appreciate the accept, I'd recommend holding off for at least a few more hours. There are plenty of users who know more about Mathematica than I who might be able to provide a better answer, but once an answer is accepted many people will start skipping the question entirely.
$endgroup$
– eyorble
Dec 19 '18 at 18:12












$begingroup$
Thanks for the comment, since I am new I did not consider this. It is certainly an obscure behaviour of Mathematica, and it would be great to hear more about it!
$endgroup$
– AGim
Dec 19 '18 at 18:17




$begingroup$
Thanks for the comment, since I am new I did not consider this. It is certainly an obscure behaviour of Mathematica, and it would be great to hear more about it!
$endgroup$
– AGim
Dec 19 '18 at 18:17




1




1




$begingroup$
Related, but not as minimal an example: (139461)
$endgroup$
– Mr.Wizard
Dec 19 '18 at 21:26




$begingroup$
Related, but not as minimal an example: (139461)
$endgroup$
– Mr.Wizard
Dec 19 '18 at 21:26










1 Answer
1






active

oldest

votes


















11












$begingroup$

Stumbled across the solution to this in tutorial/ControllingInfiniteEvaluation in the documentation. The last paragraph says:




Some of the trickiest cases occur when you have rules that depend on complicated /; conditions (see "Putting Constraints on Patterns"). One particularly awkward case is when the condition involves a "global variable". The Wolfram Language may think that the evaluation is finished because the expression did not change. However, a side effect of some other operation could change the value of the global variable, and so should lead to a new result in the evaluation. The best way to avoid this kind of difficulty is not to use global variables in /; conditions. If all else fails, you can type Update[s] to tell the Wolfram Language to update all expressions involving s. Update tells the Wolfram Language to update absolutely all expressions.




The symbol to updated in this case is f, rather than order, but:



Clear[f, order]
f[x_, y_] /; order[x] > order[y] := f[y, x]
order[1] = 1;
order[2] = 2;
expr1 = f[1, 2];
expr2 = f[2, 1];
{expr1, expr2}



{f[1, 2], f[1, 2]}




order[1] = 2;
order[2] = 1;
Update[f];
{expr1, expr2}



{f[2, 1], f[2, 1]}




Clunky, but it works.






share|improve this answer









$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "387"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188175%2fforce-evaluation-of-user-defined-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    11












    $begingroup$

    Stumbled across the solution to this in tutorial/ControllingInfiniteEvaluation in the documentation. The last paragraph says:




    Some of the trickiest cases occur when you have rules that depend on complicated /; conditions (see "Putting Constraints on Patterns"). One particularly awkward case is when the condition involves a "global variable". The Wolfram Language may think that the evaluation is finished because the expression did not change. However, a side effect of some other operation could change the value of the global variable, and so should lead to a new result in the evaluation. The best way to avoid this kind of difficulty is not to use global variables in /; conditions. If all else fails, you can type Update[s] to tell the Wolfram Language to update all expressions involving s. Update tells the Wolfram Language to update absolutely all expressions.




    The symbol to updated in this case is f, rather than order, but:



    Clear[f, order]
    f[x_, y_] /; order[x] > order[y] := f[y, x]
    order[1] = 1;
    order[2] = 2;
    expr1 = f[1, 2];
    expr2 = f[2, 1];
    {expr1, expr2}



    {f[1, 2], f[1, 2]}




    order[1] = 2;
    order[2] = 1;
    Update[f];
    {expr1, expr2}



    {f[2, 1], f[2, 1]}




    Clunky, but it works.






    share|improve this answer









    $endgroup$


















      11












      $begingroup$

      Stumbled across the solution to this in tutorial/ControllingInfiniteEvaluation in the documentation. The last paragraph says:




      Some of the trickiest cases occur when you have rules that depend on complicated /; conditions (see "Putting Constraints on Patterns"). One particularly awkward case is when the condition involves a "global variable". The Wolfram Language may think that the evaluation is finished because the expression did not change. However, a side effect of some other operation could change the value of the global variable, and so should lead to a new result in the evaluation. The best way to avoid this kind of difficulty is not to use global variables in /; conditions. If all else fails, you can type Update[s] to tell the Wolfram Language to update all expressions involving s. Update tells the Wolfram Language to update absolutely all expressions.




      The symbol to updated in this case is f, rather than order, but:



      Clear[f, order]
      f[x_, y_] /; order[x] > order[y] := f[y, x]
      order[1] = 1;
      order[2] = 2;
      expr1 = f[1, 2];
      expr2 = f[2, 1];
      {expr1, expr2}



      {f[1, 2], f[1, 2]}




      order[1] = 2;
      order[2] = 1;
      Update[f];
      {expr1, expr2}



      {f[2, 1], f[2, 1]}




      Clunky, but it works.






      share|improve this answer









      $endgroup$
















        11












        11








        11





        $begingroup$

        Stumbled across the solution to this in tutorial/ControllingInfiniteEvaluation in the documentation. The last paragraph says:




        Some of the trickiest cases occur when you have rules that depend on complicated /; conditions (see "Putting Constraints on Patterns"). One particularly awkward case is when the condition involves a "global variable". The Wolfram Language may think that the evaluation is finished because the expression did not change. However, a side effect of some other operation could change the value of the global variable, and so should lead to a new result in the evaluation. The best way to avoid this kind of difficulty is not to use global variables in /; conditions. If all else fails, you can type Update[s] to tell the Wolfram Language to update all expressions involving s. Update tells the Wolfram Language to update absolutely all expressions.




        The symbol to updated in this case is f, rather than order, but:



        Clear[f, order]
        f[x_, y_] /; order[x] > order[y] := f[y, x]
        order[1] = 1;
        order[2] = 2;
        expr1 = f[1, 2];
        expr2 = f[2, 1];
        {expr1, expr2}



        {f[1, 2], f[1, 2]}




        order[1] = 2;
        order[2] = 1;
        Update[f];
        {expr1, expr2}



        {f[2, 1], f[2, 1]}




        Clunky, but it works.






        share|improve this answer









        $endgroup$



        Stumbled across the solution to this in tutorial/ControllingInfiniteEvaluation in the documentation. The last paragraph says:




        Some of the trickiest cases occur when you have rules that depend on complicated /; conditions (see "Putting Constraints on Patterns"). One particularly awkward case is when the condition involves a "global variable". The Wolfram Language may think that the evaluation is finished because the expression did not change. However, a side effect of some other operation could change the value of the global variable, and so should lead to a new result in the evaluation. The best way to avoid this kind of difficulty is not to use global variables in /; conditions. If all else fails, you can type Update[s] to tell the Wolfram Language to update all expressions involving s. Update tells the Wolfram Language to update absolutely all expressions.




        The symbol to updated in this case is f, rather than order, but:



        Clear[f, order]
        f[x_, y_] /; order[x] > order[y] := f[y, x]
        order[1] = 1;
        order[2] = 2;
        expr1 = f[1, 2];
        expr2 = f[2, 1];
        {expr1, expr2}



        {f[1, 2], f[1, 2]}




        order[1] = 2;
        order[2] = 1;
        Update[f];
        {expr1, expr2}



        {f[2, 1], f[2, 1]}




        Clunky, but it works.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 19 '18 at 18:05









        eyorbleeyorble

        5,1031826




        5,1031826






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematica Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188175%2fforce-evaluation-of-user-defined-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Plaza Victoria

            Puebla de Zaragoza

            Musa