21
$\begingroup$

For some functions in Mathematica it is important to use RuleDelayed. One prominent example is the EvaluationMonitor for functions like NMinimize

NMinimize[x^4 - 3 x^2 - x, x, EvaluationMonitor :> Print[x]]

Here it is inevitable to use RuleDelayed because otherwise Print[x] would be evaluated too soon.

Question: Is there a comprehensive list of options which require RuleDelayed?

Btw, the reason why I need this list is to support this correctly in the IntelliJ Plugin for Mathematica:

enter image description here

$\endgroup$
9
  • $\begingroup$ Oh, yes. It's the complement of the comprehensive list of all options with the comprehensive subset that requires a non-delayed rule. I'm still trying to find any of those too. $\endgroup$ Commented May 23, 2013 at 4:21
  • 1
    $\begingroup$ maybe there is an enourmous corpus of mathematica code somewhere (like that wolfram website i've seen), where you could extract these kinds of things semi-exhaustively? Just throwing that out as an idea. $\endgroup$
    – amr
    Commented May 23, 2013 at 5:23
  • 1
    $\begingroup$ I have a feeling there are also those options that can work with either of Rule[] or RuleDelayed[], depending on the circumstances. $\endgroup$ Commented May 23, 2013 at 5:25
  • 1
    $\begingroup$ @J.M. You can always trick Mathematica like here NMinimize[x^4 - 3 x^2 - x, x, EvaluationMonitor -> Hold[Print[x]]] but I think especially in the monitor options the user almost always wants :>. In many other cases it probably doesn't hurt to use :> in preference to ->. $\endgroup$
    – halirutan
    Commented May 23, 2013 at 5:41
  • 1
    $\begingroup$ I tend to think that evaluation control is the user's responsibility. That said, it's a noble goal to offer hints where appropriate. I would just add that it's probably best for the resulting list to be subject to some human discretion before inclusion in the final product since there are quite a few scenarios where the "usual" or default choice is not applicable. $\endgroup$ Commented May 23, 2013 at 13:49

3 Answers 3

7
$\begingroup$

One possible way to get some candidates is to test all Options of all System` symbols

extractRuleDelayedOptions[symbol_String] := 
 With[{opts = Options @@ MakeExpression[symbol]},
  Cases[opts, (p_ :> _) :> p]
];

Union@Flatten[extractRuleDelayedOptions /@ Names["System`*"]]

This produces the following list

{"Compiler", "CompilerWarnings", "Device", "Graphics", "Obsolete", 
"Packing", "Spelling", "Symbolics", "UserName", 
BoxForm`ActionFunction, Assumptions, BackgroundAppearance, 
ButtonFunction, ByteOrdering, CachedValue, CellDynamicExpression, 
CharacterEncoding, CompilationTarget, CoordinatesToolOptions, 
CounterFunction, DefaultFont, Deinitialization, DisplayFunction, 
DistributedContexts, DOSTextFormat, DynamicModuleParent, 
DynamicModuleValues, FormatType, FrontEndDynamicExpression, 
Initialization, NotebookDynamicExpression, NumberMarks, Path, 
PerformanceGoal, TextStyle, TimeZone, UnsavedVariables, 
UntrackedVariables}

which is not satisfactory mainly because EvaluationMonitor and StepMonitor which require RuleDelayed are not included. Who knows what further options we missed.

The problem with this approach is as follows: It doesn't tell you what Options require :> when they are used inside a function call. Here :> acts to make the standard values work.

Example: Path for instance should always be just a string and not require :>. The reason why in the default setting :> was utilized is because the default settings is

Path :> $Path

Therefore, the value of $Path is not evaluated until the rule is used. If -> would have been used here, the default option would have the value of $Path at the moment of definition, and not at the moment of its usage.

$\endgroup$
6
  • $\begingroup$ Initialization and TrackedSymbols came to my mind immediately. The former is in your list, the latter isn't. $\endgroup$ Commented May 23, 2013 at 5:37
  • $\begingroup$ @SjoerdC.deVries Yes, I should have thought about this too since I used it quite often lately. $\endgroup$
    – halirutan
    Commented May 23, 2013 at 5:43
  • $\begingroup$ Monitor-like options (such as NMinimize) are warped by Rule (not RuleDelayed) and set to None as default. When monitoring something, we'll have to type RuleDelayed. In such cases, the auto-completion of Mathematica 's front-end is wrong.(v9.0.0 in Windows7) $\endgroup$
    – luyuwuli
    Commented May 23, 2013 at 9:54
  • $\begingroup$ I would like to do this: funcs = ToExpression /@ Flatten@(Names["System" <> # <> "*"] & /@ CharacterRange["A", "Z"]);Select[funcs, MemberQ[Options[#][[;; , 1]], Monitor | EvaluationMonitor | StepMonitor] &]` . But I only pick up the monitor one, possibly missing other RuleDelayed cases. $\endgroup$
    – luyuwuli
    Commented May 23, 2013 at 9:58
  • $\begingroup$ I also noticed that RuleDelayed is added when using the auto-completion of CompilationTarget in Compile. Although both of Rule and RuleDelayed will work, the default should be Rule I think. Is this a small bug? $\endgroup$
    – luyuwuli
    Commented May 23, 2013 at 10:05
7
$\begingroup$

According to @amr 's suggestion, I scanned every .nb files under the Mathematica\9.0\Documentation\English\ directory - which is fairly fast even on my outdated PC. This is what I got:

wsc = (WhitespaceCharacter ...);
prePtn = "RowBox[" ~~ wsc ~~ "{" ~~ wsc ~~ "\"";
inPtn = "\"" ~~ wsc ~~ "," ~~ wsc ~~ "\"";

rdOptExtractor = Function[testfile,
   Module[{fileStr},
    fileStr = Import[testfile, "String"];
    Function[ruleType,
        StringCases[fileStr, 
         Shortest[
           prePtn ~~ opt : Except[WhitespaceCharacter] .. ~~ inPtn ~~ 
            ruleOpr : ruleType ~~ "\""] :> opt]
        ] /@ {":>", "\[RuleDelayed]", "\\[RuleDelayed]"} // Flatten // Union
    ] ];

filelist = 
  FileNames["*.nb",{"D:\\Wolfram Research\\Mathematica\\9.0\\Documentation\\English\\"}, ∞];

Length[filelist]

10416

AbsoluteTiming[res = Union[Flatten[rdOptExtractor /@ filelist]];]

{504.27584,Null}

Cases[
 Union[Quiet[Check[
      ToExpression[#],
      StringReplace[#, "\\\"\\<" ~~ opt : __ ~~ "\\>\\\"" :> opt]
      ] & /@ res]],
 a_ /; MemberQ[{String, Symbol}, Head[a]]]
{"-", "+", "10!", "4!", "7!", "a", "b", "back", "c", "dialog1", 
"Drag", "\\<\\\"E1\\\"\\>", "\\<\\\"E2\\\"\\>", "E2", 
"\\<\\\"E3\\\"\\>", "end", "EvaluationMonitor", "Event", 
"EventAction", "halfway", "In", "Invariants", 
"\\<\\\"listHead\\\"\\>", "listHead", "MethodMonitor", 
"MouseClicked", "MouseDown", "MouseDragged", "MouseUp", "Off", "On", 
"one", "\\<\\\"opt2\\\"\\>", "Out", "Pinch", "PositionVariables", 
"print1", "Reset", "ReturnKeyDown", "start", "Subsubtitle", 
"Subtitle", "Title", "two", "WindowClose", Assumptions, b, 
bookmarkName, ButtonData, ButtonFunction, CellEpilog, CellProlog, 
CharacterEncoding, DateFunction, Deinitialization, DialogProlog, 
DialogSymbols, DisplayFunction, DistributedContexts, e, Epilog, 
EvaluationMonitor, EventAction, FontFamily, FontSize, FormatType, g1, 
g2, g3, g4, GeneratedParameters, Gradient, Initialization, 
InterpretationFunction, List, NotebookDynamicExpression, NumberMarks, 
ParameterVariables, PassEventsUp, Path, PerformanceGoal, PlotLabel, 
Polygon, Prolog, RegionFunction, StepMonitor, TextStyle, TimeZone, 
Tooltip, TrackedSymbols, UnsavedVariables, WordBoundary, x, y}

Of cause some elements in the above list are irrelevant, but unfortunately I can't figure out a good way to filter them out.

Remaining work:

I didn't scan .m files. Also there are still many corpus under Mathematica\9.0\AddOns\ and Mathematica\9.0\SystemFiles\, which are worth looking-into.

Hope this helps.

$\endgroup$
2
  • $\begingroup$ +1 This is what I tried first, although I just used fgrep and searched only in the ReferencePages sub-folder. Like you I wasn't able to figure out a good criterion for selecting the relevant ones. $\endgroup$
    – halirutan
    Commented May 23, 2013 at 9:56
  • $\begingroup$ @halirutan Thanks! btw if reading the file as Notebook expressions I think it would be possible/easier to get inheritance relationships between options and their sub-options. It's so sad pattern matching is much slower in that case.. $\endgroup$
    – Silvia
    Commented May 23, 2013 at 10:01
4
$\begingroup$

Let us start a community wiki answer where we collect possible :>-options because I think this question got enough attention by many of the experienced users to tell there isn't an easy way to get such a list automatically.

The reason for that is that the choice of the rule type is not fixed. You can almost always use :> instead of -> and as I have shown in the comments even the other way around is possible

NMinimize[x^4 - 3 x^2 - x, x, EvaluationMonitor -> Hold[Print[x]]]

The question here is: If options would be inserted automatically like in the Mathematica front end, which ones should utilize :>?. Please append more options below

{
 EvaluationMonitor, EventAction, Initialization, 
 InterpretationFunction, NotebookDynamicExpression, 
 StepMonitor, TrackedSymbols, UnsavedVariables, UntrackedVariables,

 CellDynamicExpression,

 (*GestureHandler: *)
 "Drag", "Pinch",

 (*EventHandler: *)
 PassEventsUp, "MouseClicked", "MouseDown", "MouseDragged", "MouseMoved", "MouseUp",
 "KeyDown", "ReturnKeyDown", "EscapeKeyDown", "LeftArrowKeyDown", "RightArrowKeyDown",
 "UpArrowKeyDown", "DownArrowKeyDown", "MenuCommand", "WindowClose",

 CellEpilog, CellProlog, DateFunction(*?*), Deinitialization, DialogProlog, DialogSymbols,
 Gradient(*evaluates only with the specific numerical values of variables that are needed*)
}
$\endgroup$
3
  • $\begingroup$ For some reason, I seem to recall 9.0.0 inserting a lot more :> than -> even for options that normally don't need them (e.g. PlotRange) $\endgroup$
    – rm -rf
    Commented May 27, 2013 at 3:36
  • $\begingroup$ @rm-rf Not under OSX. I have an old installation here and this doesn't do it. $\endgroup$
    – halirutan
    Commented May 27, 2013 at 3:41
  • $\begingroup$ Also CellDynamicExpression. (but not FrontEndDynamicExpression?) And maybe all events under EventHandler. $\endgroup$
    – Silvia
    Commented May 27, 2013 at 17:36

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.