10
$\begingroup$

I see in the Mathematica install a Macros` package. In windows the paths is for example:

C:\Program Files\Wolfram Research\Mathematica\10.1\SystemFiles\Components\Macros

I read sometimes that Mathematica lacks proper Macro support like in lisp but is this an attempt to do similar things like in lisp ? Could there be any practical use from this package in the daily lives of mere mortal MM programmers ?

$\endgroup$
13
$\begingroup$

I am not really familiar with LISP macros but from what little I think I know these do not appear to be strongly related. Rather they appear to be utility functions for function authors; it seems that a number of recently added internal top-level definitions use them. Some of them clearly do use meta-programming type expansions so perhaps they are closer to what you want than I think.

I have been meaning for some while to look through these and see if I could figure out what they do and which of them are generally useful. I guess this is as good a time as any to start.

For future reference here are the public package symbols in 10.1:

names = Names["Macros`*"]

Output:

{"Macros`AbortFailure", "Macros`BlockSetOptionValues", "Macros`CatchFailure",
"Macros`CheckFailure", "Macros`ConditionalRHS", "Macros`DeclareMacro",
"Macros`FailOnMessages", "Macros`HoldSequence", "Macros`IgnoreFailure",
"Macros`InactivateFull", "Macros`InactiveSymbol", "Macros`MessageNames",
"Macros`OptionValuePatterns", "Macros`OptionValues", "Macros`Panic",
"Macros`ParseInactives", "Macros`ReleaseHoldSequence", "Macros`SetArgumentCount",
"Macros`SetUsage", "Macros`ThrowFailure", "Macros`ToFailure", "Macros`UnevaluatedLHS",
"Macros`Verify", "Macros`VerifyFalse", "Macros`VerifyTrue", "Macros`$FailRHS",
"Macros`$FailureScope", "Macros`$FailureScopeStack", "Macros`$MacroHead"}

One of these has a usage message:

?Macros`SetUsage

SetUsage[f , "usage "] attaches a usage message to f in which the special symbols \$ and \$$ can be used as they are in DocuTools.

SetUsage[f , "Subscript[usage, 1] ", "Subscript[usage, 2] ", …] concatenates several usage messages to f , each of which will show as one line when displayed with ?.

Since I am not a DocuTools user this is rather enigmatic to me but a peek at the definition of SetUsage shows that its core function is:

GeneralUtilities`PrintDefinitions @ Macros`Macros`PackagePrivate`tolinearsyntax

Macros`Macros`PackagePrivate`tolinearsyntax[string_] := 
  StringReplace[
   string, {"->" -> "\[Rule]", "'" -> "\"", 
    w : (LetterCharacter ..) ~~ "$" ~~ i : DigitCharacter | LetterCharacter :> 
     ToString[Style[Subscript[w, Style[i, "TR"]], "TI"], StandardForm] <> 
      "\[VeryThinSpace]", 
    w : (LetterCharacter ..) ~~ "$(" ~~ Shortest[i___] ~~ ")" :> 
     ToString[Style[Subscript[w, Style[i, "TR"]], "TI"], StandardForm] <> 
      "\[VeryThinSpace]", 
    w : (LetterCharacter ..) ~~ "$" :> 
     ToString[Style[w, "TI"], StandardForm] <> "\[VeryThinSpace]", 
    "$$" -> ToString[Style["…", "TR"]]}];

So it seems that $$ simply becomes an ellipsis while $ is used for quick subscripts:

Macros`SetUsage[foo, "foo[bar$1, baz$1] does bar$1 to baz$1 and $$"];

?foo

enter image description here

As I dig through this package in the coming days I shall add descriptions of my understanding of them, assuming someone has not done this before me.

$\endgroup$
  • 5
    $\begingroup$ +1. I would add to this that Lisp-style macros are not possible without introducing a separate "read-time", that is, a stage where code has been read (or is being read), but not yet executed, and where one could attach some hooks. In fact, my project on fine-grained modules was meant to provide such read-time (among other things), and I was particularly having in mind Lisp-style macro support. Hopefully, I will move that along some time soon. $\endgroup$ – Leonid Shifrin May 19 '15 at 15:59
  • $\begingroup$ @Leonid Thanks. I thought there was a distinction something like that. $\endgroup$ – Mr.Wizard May 19 '15 at 18:57
  • 1
    $\begingroup$ Formatted usage messages, set with SetUsage, will be displayed incorrectly in the autocompletion popup :-( $\endgroup$ – Szabolcs Oct 6 '15 at 10:46
  • 1
    $\begingroup$ @Szabolcs So it seems. :-/ I see that I never extended this answer as I intended too. (I write that far too often I fear.) As I suppose you've noticed I am taking a break from Mathematica (software and site) as I was feeling burned out and I don't want my participation to become a drudgery. I can't remember the present knowledge of the auto-completion popup behavior; don't some System symbols have formatting problems there too? $\endgroup$ – Mr.Wizard Oct 7 '15 at 16:46

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.