1
$\begingroup$

How is a~b parsed and evaluated? I would expect an error, but it goes through without any complaints, yielding no apparent output. At first I thought it evaluated to Null, but then I realized every expression I've tried containing a~b (either directly or through something like %) gives no output whatsoever. Consider the following:

a~b
a~b//FullForm
Trace[a~b]
{A, a~b, B}
(a~b)~c

Each of the above (even input in separate cells) gives no output whatsoever.

Context: I accidentally typed A ~f[##,...]&~ B and was surprised by the result. (To be clear, I understand the precedence issue; what's surprising to me is the handling of (A~f[##,...])&.)

Edit 2

Thanks to Carl Woll (and later Szabolcs), I now know for sure that a~b is a syntax error. But my true problem was this: the front end didn't tell me it was a syntax error.

At Carl Woll's urging, I realized Messages[Syntax] returned

{HoldPattern[Syntax::bktwrn]:>"`1`" represents multiplication; use "`2`" to represent a function`4`.}

rather than the expected

{HoldPattern[Syntax::bktwrn]:>"`1`" represents multiplication; use "`2`" to represent a function`4`.,
HoldPattern[Syntax::tsntxi]:>"`1`" is incomplete; more input is needed.`4`}

I.e., Syntax::tsntxi was nowhere to be found!

Later I realized that in a fresh kernel, a~b is properly recognized as a syntax error. Before I restarted the kernel, I noticed something interesting: even Plot[x] failed to give me Plot::argr (Plot called with 1 argument; 2 arguments are expected) warning.

Reevaluating the notebook does not reproduce the error. There must have been some funny business somewhere in the session that later got erased. This is fortunate, as my problem is effectively resolved, even if my curiosity isn't.

It seems that somewhere in the (long) notebook I was working in, MMA stopped showing me certain messages, and stopped recognizing certain syntax errors. Without access to the session, it's nigh impossible to say more.

Lastly I offer an apology, for had I simply tested a~b in a fresh kernel, I wouldn't have wasted everyone's time trying to figure out the true problem. I sincerely thank everyone who viewed and thought about my problem, especially Carl Woll. At the very least, I learned some things about how MMA handles syntax errors in the process. (This is why I will accept Carl Woll's answer.) Of course, I also learned my lesson: always test in a fresh kernel!

Edit 1

At Carl Woll's request, here's raw call data:

Cell[BoxData[
 RowBox[{"a", "~", "b"}]], "Input",
 CellChangeTimes->{{3.69637095440485*^9, 3.6963709556847563`*^9}}]

And here's the output of CurrentValue[{StyleDefinitions, "Input"}]

{CellMargins -> {{66, 10}, {5, 10}}, 
 StyleKeyMapping -> {"=" -> "WolframAlphaShort", "*" -> "Item"}, 
 Evaluatable -> True, CellGroupingRules -> "InputGrouping", 
 TextClipboardType -> "InputText", StripStyleOnPaste -> True, 
 PageBreakWithin -> False, GroupPageBreakWithin -> False, 
 DefaultFormatType -> DefaultInputFormatType, 
 ContextMenu -> {MenuItem["Cu&t", "Cut"], MenuItem["&Copy", "Copy"], 
   MenuItem["&Paste", FrontEnd`Paste[After]], 
   Menu["Cop&y As", {MenuItem["Plain &Text", 
      FrontEnd`CopySpecial["PlainText"]], 
     MenuItem["&Input Text", FrontEnd`CopySpecial["InputText"]], 
     MenuItem["&LaTeX", 
      KernelExecute[ToExpression["FrontEnd`CopyAsTeX[]"]], 
      MenuEvaluator -> Automatic], 
     MenuItem["M&athML", 
      KernelExecute[ToExpression["FrontEnd`CopyAsMathML[]"]], 
      MenuEvaluator -> Automatic], Delimiter, 
     MenuItem["&Cell Expression", 
      FrontEnd`CopySpecial["CellExpression"]], 
     MenuItem["&Notebook Expression", 
      FrontEnd`CopySpecial["NotebookExpression"]]}], Delimiter, 
   MenuItem["&Evaluate Cell", "EvaluateCells"], 
   MenuItem["Evaluate &in Place", All], 
   MenuItem["Evaluate Initiali&zation Cells", 
    "EvaluateInitialization"], 
   Menu["C&onvert To", {MenuItem["&InputForm", 
      "SelectionConvert" -> InputForm], 
     MenuItem["&Raw InputForm", "SelectionConvert" -> RawInputForm], 
     MenuItem["&OutputForm", "SelectionConvert" -> OutputForm], 
     MenuItem["First Convert to BoxForm", 
      "MenuListConvertFormatTypes", MenuAnchor -> True]}], Delimiter, 
   MenuItem["Make &Hyperlink...", "CreateHyperlinkDialog"], 
   MenuItem["Insert Table/&Matrix...", "CreateGridBoxDialog"], 
   MenuItem["Insert &Special Character...", 
    FrontEndExecute[{FrontEnd`NotebookOpen["SpecialCharacters.nb"]}]],
    Delimiter, MenuItem["Check &Balance", "Balance"], 
   MenuItem["Di&vide Cell", "CellSplit"], 
   MenuItem["&Un/Comment", 
    KernelExecute[ToExpression["FE`toggleComment[]"]], 
    MenuEvaluator -> Automatic], Delimiter, 
   MenuItem["&Get Help", FrontEnd`SelectionHelpDialog[True]], 
   MenuItem["Why the Coloring?...", 
    FrontEndExecute[{FrontEnd`NotebookOpen[
       FrontEnd`FindFileOnPath["WhyTheColoring.nb", 
        "PrivatePathsSystemResources"]]}]], 
   MenuItem["Spea&k Selection", "SelectionSpeak"], Delimiter, 
   MenuItem["Toggle &Full Screen", 
    FrontEndExecute[
     FrontEnd`Value[FEPrivate`NotebookToggleFullScreen[]]]]}, 
 ShowAutoStyles -> True, ShowAutoConvert -> True, 
 "TranslationOptionsContentEnabled" -> Automatic, 
 "HyphenationOptionsHyphenationCharacter" -> "\[Continuation]", 
 AutoItalicWords -> {}, AutoQuoteCharacters -> {}, 
 PasteAutoQuoteCharacters -> {}, LanguageCategory -> "Input", 
 FormatType -> InputForm, NumberMarks -> True, 
 LinebreakAdjustments -> {0.85, 2, 10, 0, 1}, 
 CounterIncrements -> "Input", MenuSortingValue -> 1500, 
 MenuCommandKey -> "9", FontSize -> 13, FontWeight -> "DemiBold"}
$\endgroup$
  • $\begingroup$ Don't you get a little yellow box on a red bracket indicating a syntax error? $\endgroup$ – Carl Woll Feb 18 '17 at 1:14
  • $\begingroup$ @CarlWoll Nope, nothing. I'm in v11.0.0. $\endgroup$ – jjc385 Feb 18 '17 at 1:15
  • $\begingroup$ You get no output because it is a syntax error. I don't know why you don't get an indication of that in your version, though. $\endgroup$ – Carl Woll Feb 18 '17 at 1:16
  • $\begingroup$ @CarlWoll Does your version behave differently? $\endgroup$ – jjc385 Feb 18 '17 at 1:18
  • $\begingroup$ Could you include the underlying Cell structure (Cell | Show Expression or Shift Cmd E on a Mac), and the output of CurrentValue[{StyleDefinitions, "Input"}] as well what style sheet you're using? $\endgroup$ – Carl Woll Feb 18 '17 at 1:23
1
$\begingroup$

I only have a partial answer. The input a~b is not syntactically correct, and so the input is not evaluated. One way to see that it is not syntactically correct is to use:

ToExpression[RowBox[{"a","~","b"}],StandardForm]

ToExpression::esntx: Could not parse RowBox[{a,~,b}] as input.

$Failed

or

SyntaxQ["a~b"]

False

It should be equivalent to an input like 1/ which is incomplete. One signature of invalid syntax is that the line number does not go up when you evaluate the expression.

When an invalid syntax is encountered, the Cell should acquire an EmphasizeSyntaxErrors option, and below is the Cell expression that I see after using Cell | Show Expression to look at the cell:

Cell[BoxData[ RowBox[{"a", "~", "b"}]], "Input", CellChangeTimes->{{3.696372835096594*^9, 3.696372836088888*^9}}, EmphasizeSyntaxErrors->True]

The Cell given by the OP doesn't have this option, and so it doesn't get a red bracket. Further diagnosis is out of my realm, sorry.


It just occurred to me that one way to get the behavior the OP noticed is to do:

Off[Syntax::tsntxi]
$\endgroup$
  • $\begingroup$ +1 for teaching me a bit about syntax errors. Note that for me, your ToExpression example evaluates to $Failed but does not generate an error. $\endgroup$ – jjc385 Feb 18 '17 at 2:35
  • $\begingroup$ What does Syntax::tsntxi return? $\endgroup$ – Carl Woll Feb 18 '17 at 2:41
  • $\begingroup$ Syntax::tsntxi returns MessageName[Syntax,"tsntxi"]. Am I misinterpreting your question? $\endgroup$ – jjc385 Feb 18 '17 at 2:44
  • $\begingroup$ Perhaps this is useful: Messages[Syntax] returns {HoldPattern[Syntax::bktwrn]:>"`1`" represents multiplication; use "`2`" to represent a function`4`} -- Syntax::tsntxi is absent! $\endgroup$ – jjc385 Feb 18 '17 at 2:55
  • $\begingroup$ If you haven't already, please see edit 2 to the question. Thanks a lot for your help -- I'm somewhat ashamed that I didn't test in a fresh kernel before asking and wasting some of your time. Your answer helped a lot; hence the accept. $\endgroup$ – jjc385 Feb 19 '17 at 18:42
2
$\begingroup$

When there is a syntax error, a yellow [+] box will appear next to cell bracket after you try to evaluate:

enter image description here

If your window is very wide and you don't look on the right edge, you will not notice it.

Clicking it reveals the error:

enter image description here

A complete input would be a ~b~ c.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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