12

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 ?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
faysou
  • 10,999
  • 3
  • 50
  • 125

1 Answers1

15

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 5
    +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. – Leonid Shifrin May 19 '15 at 15:59
  • @Leonid Thanks. I thought there was a distinction something like that. – Mr.Wizard May 19 '15 at 18:57
  • 1
    Formatted usage messages, set with SetUsage, will be displayed incorrectly in the autocompletion popup :-( – Szabolcs Oct 06 '15 at 10:46
  • 1
    @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? – Mr.Wizard Oct 07 '15 at 16:46