Message Boards Message Boards

FrontEnd: puzzled by evaluation order vis-a-vis HoldAll, Defer,

Posted 12 years ago
I fear that I am so confused about this, that I will have trouble articulating the question!  I believe the crux of the question involves how the FrontEnd intervenes during evaluation, and whether the FrontEnd ignores HoldAll attributes of a function; I think I am looking for a FrontEndDefer function.  I'm primarily interested in getting this to work consistently---but would also like to learn a bit more about the FrontEnd.

(*postscript added after initial posting:  I'm now looking at Interpretation[__] as a way to achieve this. I'm hopeful to hear from  anyone has insight whether this will be fruitful or not*)

My goal here is to creating a modal dialog that displays and expression and then returns cells that contain the expression and the evaluated form of the expression. 

Here is an example which I have streamlined as much as I can, but is still a bit involved.  There are few examples of usage below which indicate the nature of my question.
 SetAttributes[dialogFunc, {HoldAll, HoldAllComplete}];
 
 dialogFunc[expr_] :=
  With[{thisNB = InputNotebook[]},
   DialogInput[
        Notebook[
     {
      Cell[
       BoxData[MakeBoxes[expr]], "Input"
      ],
     Cell[BoxData[
       ToBoxes[
        DefaultButton[
         DialogReturn[
          NotebookWrite[thisNB,
           {
            Cell[BoxData[ToBoxes[Defer@expr]], "Input"],
            Cell[BoxData[ToBoxes[Evaluate@expr]], "Output"]
            }
           ]
          ]
         ]
        ]
       ],
      "Input"]
     }
    ]
   ]
  ]

Here is an example of usage that does what I intended:
dialogFunc[Sin[Pi/2]]

Here are three examples that illustrate (I believe) how the FrontEnd ignores the HoldAll attribute and Defer:
dialogFunc[Manipulate[Sin[n Pi/2], {n, 1, 10, 1}]]
dialogFunc[Defer[Manipulate][Sin[n Pi/2], {n, 1, 10, 1}]] (*note that Defer only wraps up Manipulate*)
dialogFunc[Defer[Manipulate[Sin[n Pi/2], {n, 1, 10, 1}]]]


Finally, here are additional illustrative examples that indicate where the FrontEnd may be intervening and capturing some functions such as D and not others such as Sin
Clear[f,x];
dialogFunc[D[f[x], x]]
dialogFunc[Defer[D][f[x], x]]


I wish that I had a more efficient way to ask this question!
POSTED BY: W. Craig Carter
2 Replies
The output of Manipulate doesn't happen as a result of evaluation.  It happens as a result of formatting.  You can see, for example, that the output of

MakeBoxes[Manipulate[x, {x, 0, 1}]]

is a set of boxes for an interface, not for the input line.  That's the basic problem you're running into.  Similar things can happen with statements containing Graphics, Graphics3D, and various other interface constructs.  What you're wanting to do is to force an InputForm.  You could do this using ToString.  Here's a version of your code which does, I think, what you want:

SetAttributes[dialogFunc, {HoldAll, HoldAllComplete}];
dialogFunc[expr_] := With[{thisNB = InputNotebook[]},
   DialogInput[Notebook[{Cell[BoxData[ToString[Unevaluated@expr, InputForm]], "Input"],
     Cell[BoxData[ToBoxes[DefaultButton[DialogReturn[NotebookWrite[thisNB,        {Cell[BoxData[ToString[Unevaluated@expr, InputForm]], "Input"],
         Cell[BoxData[ToBoxes[expr]], "Output"]}]]]]], "Input"]}     ]]]
POSTED BY: John Fultz
Works like a charm! Thanks John
POSTED BY: W. Craig Carter
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract