8
$\begingroup$

Is there a way to convert an input expression to FullForm String?

Here is my attempt, which is almost there.

SetAttributes[fullFormString, HoldAll];
fullFormString[expr_] := ToString[FullForm[Unevaluated[expr]], OutputForm]

But Unevaluated appears in the string, which I don't want.

fullFormString[Integrate[α^2, {α, -3, 3}]]

"Unevaluated[Integrate[Power[\\[Alpha], 2], List[\\[Alpha], -3, 3]]]"

The escaped \ is correct. I just need the canonical way to make a FullForm string without the Unevaluated.

$\endgroup$
2
  • $\begingroup$ I suppose I could postprocess the output using StringReplace, but is there a better way? $\endgroup$
    – QuantumDot
    Commented Jul 27, 2017 at 15:05
  • $\begingroup$ Proposed duplicate: (70022) $\endgroup$
    – Mr.Wizard
    Commented Jul 28, 2017 at 11:11

3 Answers 3

10
$\begingroup$

You just put Unevaluated in the wrong spot:

SetAttributes[fullFormString, HoldAll];
fullFormString[expr_] := ToString[Unevaluated @ FullForm[expr]]

For your example:

fullFormString[Integrate[\[Alpha]^2, {\[Alpha], -3, 3}]]

"Integrate[Power[\[Alpha], 2], List[\[Alpha], -3, 3]]"

$\endgroup$
3
  • $\begingroup$ Oh that's very good. Do you know why Unevaluated is not stripped in FullForm? I thought Unevaluated simply sets the attributes of the outer function, rather than actually being processed by it. Also, would you have any recommendations about using your method vs @HenrikSchumacker's method (involving HoldForm)? $\endgroup$
    – QuantumDot
    Commented Jul 27, 2017 at 19:27
  • 2
    $\begingroup$ FullForm doesn't have DownValues, so the Unevaluated never gets stripped. I think Unevaluated is slightly better as it avoids evaluating anything, while the HoldForm approach evaluates the HoldForm object, which is inert because of it's Hold attributes, and the HoldForm approach relies on correct formatting of HoldForm when converting to OutputForm. For example, HoldForm doesn't disappear when converting to InputForm. $\endgroup$
    – Carl Woll
    Commented Jul 27, 2017 at 19:57
  • $\begingroup$ Ah I see; that makes sense. I also notice FullForm doesn't disappear when converting to InputForm either. It's stripped only when converting to OutputForm. I wish I understood this part of the evaluation process better. $\endgroup$
    – QuantumDot
    Commented Jul 27, 2017 at 20:05
7
$\begingroup$

Maybe this way?

SetAttributes[fullFormString, HoldAll];
fullFormString[expr_] := ToString[HoldForm[FullForm[expr]], OutputForm]
$\endgroup$
2
$\begingroup$

Or maybe this way?

SetAttributes[fullFormString, HoldAll];
fullFormString[expr_] := 
  StringReplace[
    ToString[FullForm[Unevaluated[expr]], OutputForm], 
    "Unevaluated[" ~~ Longest[u__] ~~ "]" -> u]

fullFormString[Integrate[α^2, {α, -3, 3}]]

"Integrate[Power[\\[Alpha], 2], List[\\[Alpha], -3, 3]]"

$\endgroup$
2
  • $\begingroup$ This works, but it's very similar to my "postprocessing" method mentioned in my comments. @Henrik's method seems to be working for all my cases. Is there really no built-in function to achieve my task? $\endgroup$
    – QuantumDot
    Commented Jul 27, 2017 at 16:25
  • $\begingroup$ @QuantumDot. Not that I know of. $\endgroup$
    – m_goldberg
    Commented Jul 27, 2017 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.