Flat attribute : example I don't understand












4














I am just beginning to learn about attributes of function in mathematica.



I saw the example "Flat". But there is something I don't get :



SetAttributes[fonction, Flat]

fonction[fonction[x]]

(*fonction[x]*)

fonction[x_] := x^2;

fonction[fonction[x]]

(*$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of fonction[x].

Hold[fonction[fonction[x]]]*)


Why do I have an error ? Shouldn't it returns me fonction[x]=x^2 because of the flat attribute ?










share|improve this question


















  • 2




    Look at the result from just fonction[x], you likely want to add the Attribute OneIdentity.
    – chuy
    11 hours ago


















4














I am just beginning to learn about attributes of function in mathematica.



I saw the example "Flat". But there is something I don't get :



SetAttributes[fonction, Flat]

fonction[fonction[x]]

(*fonction[x]*)

fonction[x_] := x^2;

fonction[fonction[x]]

(*$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of fonction[x].

Hold[fonction[fonction[x]]]*)


Why do I have an error ? Shouldn't it returns me fonction[x]=x^2 because of the flat attribute ?










share|improve this question


















  • 2




    Look at the result from just fonction[x], you likely want to add the Attribute OneIdentity.
    – chuy
    11 hours ago
















4












4








4


1





I am just beginning to learn about attributes of function in mathematica.



I saw the example "Flat". But there is something I don't get :



SetAttributes[fonction, Flat]

fonction[fonction[x]]

(*fonction[x]*)

fonction[x_] := x^2;

fonction[fonction[x]]

(*$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of fonction[x].

Hold[fonction[fonction[x]]]*)


Why do I have an error ? Shouldn't it returns me fonction[x]=x^2 because of the flat attribute ?










share|improve this question













I am just beginning to learn about attributes of function in mathematica.



I saw the example "Flat". But there is something I don't get :



SetAttributes[fonction, Flat]

fonction[fonction[x]]

(*fonction[x]*)

fonction[x_] := x^2;

fonction[fonction[x]]

(*$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of fonction[x].

Hold[fonction[fonction[x]]]*)


Why do I have an error ? Shouldn't it returns me fonction[x]=x^2 because of the flat attribute ?







attributes






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 12 hours ago









StarBucK

720212




720212








  • 2




    Look at the result from just fonction[x], you likely want to add the Attribute OneIdentity.
    – chuy
    11 hours ago
















  • 2




    Look at the result from just fonction[x], you likely want to add the Attribute OneIdentity.
    – chuy
    11 hours ago










2




2




Look at the result from just fonction[x], you likely want to add the Attribute OneIdentity.
– chuy
11 hours ago






Look at the result from just fonction[x], you likely want to add the Attribute OneIdentity.
– chuy
11 hours ago












2 Answers
2






active

oldest

votes


















4














The main point to understand here is that the kernel does reduce your fonction[fonction[x]] to fonction[x]. You get into troubles after this reduction has been made, when just fonction[x] is being evaluated, and because of a different reason.



To illustrate this, we can look at the Trace of the evaluation:



ClearAll[f];
SetAttributes[f, {Flat}];
f[x_] := x^2;
Trace[f[f[1]]][[1, 1 ;; 2]]
(*{f[1], f[1]^2}*)


As you can see, there is only one f here but still there is a recursion problem. We can just call f[1] and get the same issue.



To understand what has happened, let's just define:



ClearAll[f];
SetAttributes[f, {Flat}];
f[x_] := Hold[x];
f[1]
(*Returns Hold[f[1]]*)


The reason for this extra f in the output is that for a Flat symbol expressions f[x] and f[f[x]] are identical. So, when a pattern-matcher encounters f[1] it treats the expression as f[f[1]] and consequently substitutes f[1], not 1, instead of x in the rhs of the definition. The pattern matcher prefers f[f[1]] over f[1] when matching x_ to allow for matching a sequence of arguments as a whole:



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


Here the pattern matcher treated f[1, 2] as f[f[1, 2]] and replaced x by f[1, 2] accordingly.



As chuy has already mentioned in the comments, you can add OneIdentity attribute to a symbol. Then the pattern-mathcer will prefer f[1] over f[f[1]] when matching f[x_] if there is only one argument inside the expression:



ClearAll[f];
SetAttributes[f, {Flat, OneIdentity}];
f[x_] := Hold[x];
f[1]
f[1, 2]
(*Returns Hold[1] and Hold[f[1, 2]]*)


Note, however, that OneIdentity attribute will not save your form recursion when there are more than one argument: f[1, 2] will be matched as f[f[1, 2]], f[1, 2] will be squared, f[1, 2]^2, and the f[1, 2] inside the square will again be matched as f[f[1, 2]]. So, basically, use Flat attribute only for symbols which really stand for some associative operators or you are likely to get into trouble.



ClearAll[f];
SetAttributes[f, {Flat, OneIdentity}];
f[x_] := x^2;
f[1]
f[1, 2]
(*1
$RecursionLimit::reclim2 bla-bla-bla
Hold[f[1, 2]^2]
*)





share|improve this answer































    1














    The following example may help:



    SetAttributes[f, Flat];
    Hold[f[f[x]]] /. HoldPattern[f[x_]] :> x^2


    The result is:



    Hold[f[f[x]]^2]


    To see what's happening, we may run



    MatchQ[f[a, b], f[_]]


    The result is True. Thus we see that f[a,b] is identified as f[f[a,b]]. This is what the attribute Flat does to a function.






    share|improve this answer





















      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%2f188643%2fflat-attribute-example-i-dont-understand%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      The main point to understand here is that the kernel does reduce your fonction[fonction[x]] to fonction[x]. You get into troubles after this reduction has been made, when just fonction[x] is being evaluated, and because of a different reason.



      To illustrate this, we can look at the Trace of the evaluation:



      ClearAll[f];
      SetAttributes[f, {Flat}];
      f[x_] := x^2;
      Trace[f[f[1]]][[1, 1 ;; 2]]
      (*{f[1], f[1]^2}*)


      As you can see, there is only one f here but still there is a recursion problem. We can just call f[1] and get the same issue.



      To understand what has happened, let's just define:



      ClearAll[f];
      SetAttributes[f, {Flat}];
      f[x_] := Hold[x];
      f[1]
      (*Returns Hold[f[1]]*)


      The reason for this extra f in the output is that for a Flat symbol expressions f[x] and f[f[x]] are identical. So, when a pattern-matcher encounters f[1] it treats the expression as f[f[1]] and consequently substitutes f[1], not 1, instead of x in the rhs of the definition. The pattern matcher prefers f[f[1]] over f[1] when matching x_ to allow for matching a sequence of arguments as a whole:



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


      Here the pattern matcher treated f[1, 2] as f[f[1, 2]] and replaced x by f[1, 2] accordingly.



      As chuy has already mentioned in the comments, you can add OneIdentity attribute to a symbol. Then the pattern-mathcer will prefer f[1] over f[f[1]] when matching f[x_] if there is only one argument inside the expression:



      ClearAll[f];
      SetAttributes[f, {Flat, OneIdentity}];
      f[x_] := Hold[x];
      f[1]
      f[1, 2]
      (*Returns Hold[1] and Hold[f[1, 2]]*)


      Note, however, that OneIdentity attribute will not save your form recursion when there are more than one argument: f[1, 2] will be matched as f[f[1, 2]], f[1, 2] will be squared, f[1, 2]^2, and the f[1, 2] inside the square will again be matched as f[f[1, 2]]. So, basically, use Flat attribute only for symbols which really stand for some associative operators or you are likely to get into trouble.



      ClearAll[f];
      SetAttributes[f, {Flat, OneIdentity}];
      f[x_] := x^2;
      f[1]
      f[1, 2]
      (*1
      $RecursionLimit::reclim2 bla-bla-bla
      Hold[f[1, 2]^2]
      *)





      share|improve this answer




























        4














        The main point to understand here is that the kernel does reduce your fonction[fonction[x]] to fonction[x]. You get into troubles after this reduction has been made, when just fonction[x] is being evaluated, and because of a different reason.



        To illustrate this, we can look at the Trace of the evaluation:



        ClearAll[f];
        SetAttributes[f, {Flat}];
        f[x_] := x^2;
        Trace[f[f[1]]][[1, 1 ;; 2]]
        (*{f[1], f[1]^2}*)


        As you can see, there is only one f here but still there is a recursion problem. We can just call f[1] and get the same issue.



        To understand what has happened, let's just define:



        ClearAll[f];
        SetAttributes[f, {Flat}];
        f[x_] := Hold[x];
        f[1]
        (*Returns Hold[f[1]]*)


        The reason for this extra f in the output is that for a Flat symbol expressions f[x] and f[f[x]] are identical. So, when a pattern-matcher encounters f[1] it treats the expression as f[f[1]] and consequently substitutes f[1], not 1, instead of x in the rhs of the definition. The pattern matcher prefers f[f[1]] over f[1] when matching x_ to allow for matching a sequence of arguments as a whole:



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


        Here the pattern matcher treated f[1, 2] as f[f[1, 2]] and replaced x by f[1, 2] accordingly.



        As chuy has already mentioned in the comments, you can add OneIdentity attribute to a symbol. Then the pattern-mathcer will prefer f[1] over f[f[1]] when matching f[x_] if there is only one argument inside the expression:



        ClearAll[f];
        SetAttributes[f, {Flat, OneIdentity}];
        f[x_] := Hold[x];
        f[1]
        f[1, 2]
        (*Returns Hold[1] and Hold[f[1, 2]]*)


        Note, however, that OneIdentity attribute will not save your form recursion when there are more than one argument: f[1, 2] will be matched as f[f[1, 2]], f[1, 2] will be squared, f[1, 2]^2, and the f[1, 2] inside the square will again be matched as f[f[1, 2]]. So, basically, use Flat attribute only for symbols which really stand for some associative operators or you are likely to get into trouble.



        ClearAll[f];
        SetAttributes[f, {Flat, OneIdentity}];
        f[x_] := x^2;
        f[1]
        f[1, 2]
        (*1
        $RecursionLimit::reclim2 bla-bla-bla
        Hold[f[1, 2]^2]
        *)





        share|improve this answer


























          4












          4








          4






          The main point to understand here is that the kernel does reduce your fonction[fonction[x]] to fonction[x]. You get into troubles after this reduction has been made, when just fonction[x] is being evaluated, and because of a different reason.



          To illustrate this, we can look at the Trace of the evaluation:



          ClearAll[f];
          SetAttributes[f, {Flat}];
          f[x_] := x^2;
          Trace[f[f[1]]][[1, 1 ;; 2]]
          (*{f[1], f[1]^2}*)


          As you can see, there is only one f here but still there is a recursion problem. We can just call f[1] and get the same issue.



          To understand what has happened, let's just define:



          ClearAll[f];
          SetAttributes[f, {Flat}];
          f[x_] := Hold[x];
          f[1]
          (*Returns Hold[f[1]]*)


          The reason for this extra f in the output is that for a Flat symbol expressions f[x] and f[f[x]] are identical. So, when a pattern-matcher encounters f[1] it treats the expression as f[f[1]] and consequently substitutes f[1], not 1, instead of x in the rhs of the definition. The pattern matcher prefers f[f[1]] over f[1] when matching x_ to allow for matching a sequence of arguments as a whole:



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


          Here the pattern matcher treated f[1, 2] as f[f[1, 2]] and replaced x by f[1, 2] accordingly.



          As chuy has already mentioned in the comments, you can add OneIdentity attribute to a symbol. Then the pattern-mathcer will prefer f[1] over f[f[1]] when matching f[x_] if there is only one argument inside the expression:



          ClearAll[f];
          SetAttributes[f, {Flat, OneIdentity}];
          f[x_] := Hold[x];
          f[1]
          f[1, 2]
          (*Returns Hold[1] and Hold[f[1, 2]]*)


          Note, however, that OneIdentity attribute will not save your form recursion when there are more than one argument: f[1, 2] will be matched as f[f[1, 2]], f[1, 2] will be squared, f[1, 2]^2, and the f[1, 2] inside the square will again be matched as f[f[1, 2]]. So, basically, use Flat attribute only for symbols which really stand for some associative operators or you are likely to get into trouble.



          ClearAll[f];
          SetAttributes[f, {Flat, OneIdentity}];
          f[x_] := x^2;
          f[1]
          f[1, 2]
          (*1
          $RecursionLimit::reclim2 bla-bla-bla
          Hold[f[1, 2]^2]
          *)





          share|improve this answer














          The main point to understand here is that the kernel does reduce your fonction[fonction[x]] to fonction[x]. You get into troubles after this reduction has been made, when just fonction[x] is being evaluated, and because of a different reason.



          To illustrate this, we can look at the Trace of the evaluation:



          ClearAll[f];
          SetAttributes[f, {Flat}];
          f[x_] := x^2;
          Trace[f[f[1]]][[1, 1 ;; 2]]
          (*{f[1], f[1]^2}*)


          As you can see, there is only one f here but still there is a recursion problem. We can just call f[1] and get the same issue.



          To understand what has happened, let's just define:



          ClearAll[f];
          SetAttributes[f, {Flat}];
          f[x_] := Hold[x];
          f[1]
          (*Returns Hold[f[1]]*)


          The reason for this extra f in the output is that for a Flat symbol expressions f[x] and f[f[x]] are identical. So, when a pattern-matcher encounters f[1] it treats the expression as f[f[1]] and consequently substitutes f[1], not 1, instead of x in the rhs of the definition. The pattern matcher prefers f[f[1]] over f[1] when matching x_ to allow for matching a sequence of arguments as a whole:



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


          Here the pattern matcher treated f[1, 2] as f[f[1, 2]] and replaced x by f[1, 2] accordingly.



          As chuy has already mentioned in the comments, you can add OneIdentity attribute to a symbol. Then the pattern-mathcer will prefer f[1] over f[f[1]] when matching f[x_] if there is only one argument inside the expression:



          ClearAll[f];
          SetAttributes[f, {Flat, OneIdentity}];
          f[x_] := Hold[x];
          f[1]
          f[1, 2]
          (*Returns Hold[1] and Hold[f[1, 2]]*)


          Note, however, that OneIdentity attribute will not save your form recursion when there are more than one argument: f[1, 2] will be matched as f[f[1, 2]], f[1, 2] will be squared, f[1, 2]^2, and the f[1, 2] inside the square will again be matched as f[f[1, 2]]. So, basically, use Flat attribute only for symbols which really stand for some associative operators or you are likely to get into trouble.



          ClearAll[f];
          SetAttributes[f, {Flat, OneIdentity}];
          f[x_] := x^2;
          f[1]
          f[1, 2]
          (*1
          $RecursionLimit::reclim2 bla-bla-bla
          Hold[f[1, 2]^2]
          *)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 8 hours ago

























          answered 11 hours ago









          Anton.Sakovich

          49628




          49628























              1














              The following example may help:



              SetAttributes[f, Flat];
              Hold[f[f[x]]] /. HoldPattern[f[x_]] :> x^2


              The result is:



              Hold[f[f[x]]^2]


              To see what's happening, we may run



              MatchQ[f[a, b], f[_]]


              The result is True. Thus we see that f[a,b] is identified as f[f[a,b]]. This is what the attribute Flat does to a function.






              share|improve this answer


























                1














                The following example may help:



                SetAttributes[f, Flat];
                Hold[f[f[x]]] /. HoldPattern[f[x_]] :> x^2


                The result is:



                Hold[f[f[x]]^2]


                To see what's happening, we may run



                MatchQ[f[a, b], f[_]]


                The result is True. Thus we see that f[a,b] is identified as f[f[a,b]]. This is what the attribute Flat does to a function.






                share|improve this answer
























                  1












                  1








                  1






                  The following example may help:



                  SetAttributes[f, Flat];
                  Hold[f[f[x]]] /. HoldPattern[f[x_]] :> x^2


                  The result is:



                  Hold[f[f[x]]^2]


                  To see what's happening, we may run



                  MatchQ[f[a, b], f[_]]


                  The result is True. Thus we see that f[a,b] is identified as f[f[a,b]]. This is what the attribute Flat does to a function.






                  share|improve this answer












                  The following example may help:



                  SetAttributes[f, Flat];
                  Hold[f[f[x]]] /. HoldPattern[f[x_]] :> x^2


                  The result is:



                  Hold[f[f[x]]^2]


                  To see what's happening, we may run



                  MatchQ[f[a, b], f[_]]


                  The result is True. Thus we see that f[a,b] is identified as f[f[a,b]]. This is what the attribute Flat does to a function.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 11 hours ago









                  Wen Chern

                  33118




                  33118






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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.


                      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%2f188643%2fflat-attribute-example-i-dont-understand%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

                      Quarter-circle Tiles

                      build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                      Mont Emei