6

enter image description here

In[1] and In[3] are identical but the output is different.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Chen Stats Yu
  • 4,986
  • 2
  • 24
  • 50
  • I note that after these steps, VariationalMethods has not been loaded according to $Packages, and the attributes of VariationalD is an empty list (in particular, doesn't contain Stub). It has no Down, Up, Sub or OwnValues. As far as I can tell, Messages is the only thing that has been changed. – Patrick Stevens Aug 09 '15 at 15:45
  • 1
    The symbol creation is being hooked somehow. In[1] is equivalent to Information["VariationalMethods\VariationalD"](note the symbol given as a string), and *not* the same asIn[2]`, which actually creates the symbol during parse. But I am not sure how this mechanism actually works. – Oleksandr R. Aug 09 '15 at 18:35

2 Answers2

3

? name is a special input form with nonstandard parsing behavior, just like >>> as explained here.

When you write a line starting with ? the item following it is not a Symbol, contrary to appearances. Instead it is a String with implicit delimiters. This is not simply a matter of a hold attribute. For example HoldComplete[a^] is incomplete syntax and cannot be entered, yet:

?a^

Information::nomatch: No symbol matching a^ found. >>

Using the same method as for the linked question we can take a look at parsing itself:

parseString[s_String, prep : (True | False) : True] := 
  FrontEndExecute[FrontEnd`UndocumentedTestFEParserPacket[s, prep]]

parseString["?a^"]

parseString["HoldComplete[a^]"]
{BoxData[RowBox[{"?", "a^"}]], StandardForm}

{BoxData[RowBox[{"HoldComplete", "[", RowBox[{"a", "^"}], "]"}]], StandardForm}

Observe that in the first case "a^" remains an undivided String whereas in the section it is parsed into a RowBox.

We can look at the next step in evaluation by using MakeBoxes:

MakeExpression @ "?name"
HoldComplete[Information["name", LongForm -> False]]

Note that the first argument of Information is the String "name" and not the Symbol name.

So know you know that your ? name input form actually becomes:

 Information["VariationalMethods`VariationalD", LongForm -> False]

And indeed this behaves just the same. But why does this say "No symbol matching" in a fresh kernel while this does not?:

Information[VariationalMethods`VariationalD, LongForm -> False]

Consider the way that DeclarePackage works:

You can use DeclarePackage to tell Mathematica automatically to load a particular package when any of the symbols defined in it are used.

DeclarePackage["ErrorBarPlots`", "ErrorListPlot"]

The String "ErrorListPlot" does not count as the use of the Symbol ErrorListPlot as explained in the documentation for Stub:

  • Symbols with the Stub attribute are created by DeclarePackage.

  • A symbol is considered "used" if its name appears explicitly, not in the form of a string.

  • Names["nameform"] and Attributes["nameform"] do not constitute "uses" of a symbol.

Therefore the implicit string form of ?name does not constitute a use of name and the package is not loaded, resulting in the nomatch message.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • Well, except that the usage message is actually from Usage.m and not VariationalMethods.m (check them; they are different). Also: Remove["test\"]; DeclarePackage["test`", "sym"]; Print[{#, Attributes[#]}] & /@ Names["test`"];. The symbol does exist in the symbol table afterDeclarePackage` (as it must, to have an attribute). – Oleksandr R. Aug 10 '15 at 18:10
  • Is this behaviour of ? documented anywhere? I can't find it, and it's really really counterintuitive… – Patrick Stevens Aug 11 '15 at 15:06
  • @Oleksandr Pardon me, but I'm feeling a bit obtuse and I don't understand the point you are making. Would you mind bluntly stating the significance of your observations? – Mr.Wizard Aug 12 '15 at 01:47
  • @Patrick I don't believe this is documented as such other than what I quoted here but the parsing difference follows from the nonstandard input when ? starts a "line." Which aspect do you find most counter-intuitive? – Mr.Wizard Aug 12 '15 at 01:49
  • @Mr.Wizard for me, the fact that the input is nonstandard at all. Before seeing this post I'd only ever seen ? as a unary operator shorthand for Information, which took a Symbol and returned its Information. That's the simplest hypothesis to explain all the examples I've ever seen in the docs. It's therefore a very large violation of expectation to discover that in fact it isn't an operator at all, but something which directly alters the parsing step, and so not really part of the language at all, but part of the system. That's the crux: ? is part of the WSystem, not the WLang. – Patrick Stevens Aug 12 '15 at 07:38
  • @Patrick Ah, I see now, and I agree. Unlike Put or Get which say something like expr>>filename is equivalent to expr>>"filename". The double quotes can be omitted if the file name is of the form specified in "Operator Input Forms" the documentation for Information does not make clear that anything nonstandard is taking place. I'll look harder and see if I can find any mention of this. – Mr.Wizard Aug 12 '15 at 08:58
  • 1
    To put it bluntly, then: while your observations are completely correct and accurate, they don't apply to this situation, because the package is not being autoloaded. Only the usage message is, and not even from the package itself, but from a different file (Usage.m). To assign attribute (Stub) does create the symbol, but VariationalMethods\VariationalD` does not exist in the symbol table at all here. I don't know what the mechanism is (I did not go through the whole trace), but it doesn't seem to be the one you describe. – Oleksandr R. Aug 12 '15 at 10:42
  • @Oleksandr Okay, thank you. I'll have to give this a closer look when I have time. As I wrote to someone else recently: thanks for keeping me honest. – Mr.Wizard Aug 12 '15 at 13:46
2

I do not think this a bug, but more of a peculiarity of paclet manager. This sort of thing happens when a package isn't loaded and a function it defines is mentioned. This what I think happened.

At the time you evaluated In[1], VariationalD was an undefined symbol, because the VariationalMethods package wasn't loaded. Which is what the message you got told you.

Evaluating Information didn't load the package, but it triggered the packet manager to get the paclets it needs. So the next time you evaluated Information, you got what you expected.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • Can you give more detail about this mechanism? Autoloaded definitions are usually dealt with by assigning the Stub attribute or by giving them some definition that loads the package when they are first referenced, which requires the symbol at least to exist. But here there is no pre-existing definition at all and the symbol does not exist in the symbol table. So how does the paclet manager hook the creation of this symbol to load the package? – Oleksandr R. Aug 09 '15 at 18:33
  • @OleksandrR. Stub causes the symbol not to be found too, but Attributes["symbolName"] will still show Stub. This appears to behave in a very similar way except Stub is not explicitly shown. – Szabolcs Aug 09 '15 at 20:26
  • @OleksandrR. I don't really have any insight in the mechanism. However, VariationalMethods is not an auto-loaded package. So not sure why you are bringing autoload into the discussion. – m_goldberg Aug 09 '15 at 21:06
  • @Szabolcs to have attributes, a symbol has to at least exist in the symbol table. VariationalMethods\VariationalD` doesn't, although at least the FE seems to know enough to offer autocomplete/highlighting for it. @m_goldberg although the whole package is not autoloaded, at the very least the messages are. – Oleksandr R. Aug 09 '15 at 21:53