2
$\begingroup$

In the console REPL, we can see that input is parsed in a certain way, that is not equivalent to simply calling ToExpression on the input string. From math.exe using Mathematica 13.1:

In[1]:= Print@"foo"\[NewLine]Print@"bar"
foo

In[2]:= DownValues[In][[1]]

Out[2]= HoldPattern[In[1]] :> Print["foo"]

In[3]:= ToExpression[InString[1]]
foo
bar

As we can see, the Print["bar"] is discarded when assigning In[1], but not when assigning InString[1], and calling ToExpression on InString[1] evaluates both print statements. What is the meaning of this, and is there a function analogous to ToExpression which reproduces the former behavior?

$\endgroup$
5
  • 1
    $\begingroup$ Use Print@"foo"; Print@"bar" instead. $\endgroup$ Nov 23, 2022 at 20:44
  • $\begingroup$ I understand perfectly well that using CompoundExpression is correct. However, my specific question is, why does the console REPL parse my particular example input string differently from calling ToExpression on InputString? $\endgroup$
    – Walhiskaz
    Nov 23, 2022 at 20:52
  • $\begingroup$ Because the input you type is not a string, but output of InString[1] is a string. $\endgroup$ Nov 23, 2022 at 20:58
  • $\begingroup$ I supposed I do not understand in precise enough detail how the main loop works. I was under the impression, based on the documentation of $PreRead, that input is given into the main loop as a string, the value of PreRead applied, and only then parsed into an expression for evaluation. $\endgroup$
    – Walhiskaz
    Nov 23, 2022 at 21:03
  • 1
    $\begingroup$ I do not get the same results as you. When I open the kernel from the command line I get this, where you see that ToExpression[InString[1]] only prints "foo" and not "bar", which makes sense - I've tried on Windows, Linux, and Mac and cannot reproduce $\endgroup$
    – Jason B.
    Nov 23, 2022 at 21:29

1 Answer 1

1
$\begingroup$

Input that is typed is not a string but output of InString[1] is a string.

So if you use FullForm:

In[28]:= FullForm[InString[1]]

Out[28]//FullForm= "Print@\"foo\"\\[NewLine]Print@\"bar\""

you see that the string generated by InString is a bit different than what you typed. Or more precisely it is the same but it is enclosed inside "" which original input is not.

$\endgroup$
3
  • $\begingroup$ I find this confusing, because if you define a value such as $PreRead = (Print[FullForm[#]]; #) &; , then you will see that it prints the value of input string, but only then evaluates the input, but it /only evaluates the first Print statement/, indicating that it is doing something to the value of InputString other than simply calling ToExpression, as it does to code passed on the -run switch from the command line. This seems surprising to me, but perhaps I'm naive. $\endgroup$
    – Walhiskaz
    Nov 23, 2022 at 21:16
  • $\begingroup$ Based on some experimentation, it seems the main loop may be doing something like this: InString[1]//(Function[x,SetDelayed[In[1],x],HoldAllComplete]@@Take[ToExpression[#,InputForm,HoldComplete],1];Out[1]=In[1])& $\endgroup$
    – Walhiskaz
    Nov 23, 2022 at 21:24
  • $\begingroup$ Your code with $PreRead works as expected, what is wrong with it? It prints the full-form of the input as a string and then evaluates the original input same way as it would do without $PreRead. $\endgroup$ Nov 23, 2022 at 21:27

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.