10

I would like to set up the following custom notation in Mathematica 7.

This notation is not particularly useful in itself, so please do not suggest existing alternatives, or point out that this only saves a few keystrokes.

I want to know if and how it may be done.


At present, one may enter

f = #2 + #^2 / #3 & @@ # & ;

f[ {a, b, c} ]

Out[]= b + a^2 / c

Where the inner function #^2 / #3 + #2 & is Apply'd to the first argument.


I would like to implement the syntax

f = #2 + #^2 / #3 @@& ;

and have it behave exactly the same. That is, @@& to represent a Function that is automatically applied to its first argument.

It will need to have the same binding as the & symbol.


It is preferable that this is done with the Notations package, to whatever extent that is possible, rather than manual MakeBoxes, for the sake of ease in setting up similar notations, even though the use of Notations is more difficult to communicate via text.

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • 1
    A word of caution about using custom notation is that it may cause problems if you try to integrate your code into a package, because parsing mechanism differs between .nb and .m files -- https://groups.google.com/d/msg/comp.soft-sys.math.mathematica/wABDm0h1EHg/CHHrGkdfbcIJ – Yaroslav Bulatov Mar 14 '11 at 22:37

1 Answers1

9

You can't do this with an operator syntax of your own invention (like @@&). Mathematica just doesn't have the capability to modify the language grammar at runtime like that.

You can get at least partway there with the Notation package, but you have to use a symbol that has no meaning in Mathematica, and possibly most of the way there with one of the operators without built-in meanings, but most (if any) of them don't bind as postfix operators.

Here, for example, I'll use the Notations package to define the \[Wolf] character as an admittedly pseudo-postfix operator in place of @@&:

In[1]:= Needs["Notation`"]

In[2]:= Notation[x_ \[Wolf] \[DoubleLongLeftRightArrow] (x_ @@ # &)]

In[3]:=  f=#2+#^2/#3& \[Wolf]
Out[3]= (#2+#1^2/#3&) \[Wolf]

In[4]:= f[{a,b,c}]
Out[4]= b+a^2/c

I'll include a screenshot too since this involves notation:

example operator hackery

Where this approach may fail is in the fact that you can't set an operator precedence for an arbitrary symbol like \[Wolf]. You can instead use one of the meaningless operators I linked to above, but those also have a fixed precedence that can't be changed.

If you found PrecedenceForm in the documentation you might get a brief false hope, but as the docs say, it only effects printing and not evaluation.

HTH!

Michael Pilat
  • 6,480
  • 27
  • 30
  • Yes, that does help: I won't waste time trying to do the impossible. This is a bad example, but if I have ideas for syntax extensions that might be generally useful, that cannot be implemented with the Notations package, who should I write to? (Obviously Mathematica is not open-source, and I understand that.) – Mr.Wizard Mar 14 '11 at 22:51
  • Also, can you give a little explanation for what `Symbolize` does and does not, because I was originally thinking I could `Symbolize` `@@&`. – Mr.Wizard Mar 14 '11 at 22:57
  • You can always submit suggestions via technical support at support@wolfram.com – Michael Pilat Mar 14 '11 at 22:59
  • 1
    @Mr. You can also send suggestions to WRI using their [feedback page](http://www.wolfram.com/support/contact/email/?topic=Feedback). – WReach Mar 14 '11 at 23:01
  • 1
    Folding the Wolf operator is considered animal cruelty. – Dr. belisarius Mar 14 '11 at 23:18
  • If one was really determined (to break Mma ;-) could one hack this with $PreRead? It appears that `@@ &` does parse, and then that `RowBox` structure could be manipulated. – Mr.Wizard Mar 14 '11 at 23:23
  • Potentially, or maybe with `$SyntaxHandler`, if you live entirely in the text Kernel, but the FrontEnd has its own parser that creates box forms of the input and `@@&` is not preserved as a single construct, if it parses at all. Either way, the consequences might not be entirely predictable even if they work most of the time. – Michael Pilat Mar 15 '11 at 01:01
  • Also, I'm not an expert on the `Notation` package, but concerning `Symbolize`, my understanding is that it lets you treat certain kinds of box structures that wouldn't ordinarily be a valid symbol in vanilla Mathematica as a symbol for input. Operators and symbols aren't the same thing in Mathematica, of course. – Michael Pilat Mar 15 '11 at 01:03
  • @MichaelPilat - If you get the $SyntaxHandler method work let us know. Symbolize is unfortunately rather restrictive in what can be turned into a symbol in my understanding. I could not get something like <*> to be treated as a symbol. – Davorak Nov 09 '12 at 07:10