7

I would like to define a new operator of the form x /==> y, where the operator /==> is treated as e.g. the /@ operator of Map, and is translated to MyFunction[x, y]. There is one important aspect: I want the resulting operator to behave in the frontend like any two-bit operator does, that is, the two characters (a Divide and a DoubleLongRightArrow) should be connected together, no syntax coloration should appear, and they are to be selected together when clicked, so precedence must be set. Also, I'd rather avoid using the Notation` package. As a result, I'd like to see something like this:

In[11]:= FullForm[x/\[DoubleLongRightArrow]y]

Out[11]//FullForm= MyFunction[x,y]

Does anyone have an idea how to achieve this?

István Zachar
  • 1,343
  • 2
  • 13
  • 31
  • 1
    In other words, you also would like this to work in a stand-alone kernel, i.e. without front-end ? In this case you might want to write your own `$SyntaxHandler`, which would take a string that Mathematica considers to have a syntax error. This might be tricky, though. – Sasha Apr 17 '11 at 21:31
  • 1
    Hello Istvan! Can not help with this one, but let me welcome you to StackOverflow! I hope someone else here will have a good answer. – Leonid Shifrin Apr 17 '11 at 21:38
  • 1
    Hi István, welcome to StackOverflow! A useful welcoming message which I borrow from belisarius: Allow me to welcome you to StackOverflow and remind three things we usually do here: 1) As you receive help, try to give it too answering questions in your area of expertise 2) Read the FAQs 3) When you see good Q&A, vote them up using the gray triangles, as the credibility of the system is based on the reputation that users gain by sharing their knowledge. Also remember to accept the answer that better solves your problem, if any, by pressing the checkmark sign – Sjoerd C. de Vries Apr 17 '11 at 21:39
  • Thank you, will try to grow up for the community and credibility. – István Zachar Apr 18 '11 at 22:06

2 Answers2

6

The Notation Package is perhaps the closest to doing this kind of thing, but according to the response to my own question of a similar nature, what you want is unfortunately not practical.

Don't let this stop you from trying however, as you will probably learn new things in the process. The Notation Package and the the functions that underpin it are far from useless.

You may also find the replies to this question informative.


There are a number of functions that are useful for manual implementation of syntax changes. Rather than try to write my own help file for these, I will direct you to the official pages on these functions. After reading them, please ask any focused questions you have, or for help with implementing specific ideas. I or others here should be able to either answer your question, show you how to do something, or explain why it is not readily possible.

There are more, and I will try to extend this list later. (others are welcome to edit this post)

Community
  • 1
  • 1
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • @István Zachar, specifically, you'll want to look at this `Notation` [tutorial](http://reference.wolfram.com/mathematica/Notation/tutorial/NotationSymbolizeAndInfixNotation.html) as it discusses essentially what you want to do, although the combination of the three operators may cause you issues. – rcollyer Apr 18 '11 at 12:32
  • Thanks for the tips on the Notation package, but that is like a black box to me. I neither would know what happens internally (I really don't want to reverse engineer it) nor could I modify it to my needs, or develop it further. That is why I would go by the bare Mathematica-basics, if possible. If not, then I avoid using multicharacter operators at all. – István Zachar Apr 18 '11 at 22:04
  • @István Zachar, I don't think even the Notation package will do what you want, so please don't be disappointed if you do not get an answer that meets your request. In a few minutes I will update my answer with some further thoughts about manual syntax manipulation. – Mr.Wizard Apr 18 '11 at 22:49
2

Thanks to Mr.Wizard's links, I've found the only example in the documentation on how to parse new operators (the gplus example in Low-Level Input). According to this example, here is my version for the new operator PerArrow. Please comment/critize on the code below:

In[1]:= PerArrow /: MakeBoxes[PerArrow[x_, y_], StandardForm] := 
  RowBox[{MakeBoxes[x, StandardForm], 
    RowBox[{AdjustmentBox["/", BoxMargins -> -.2], 
      AdjustmentBox["\[DoubleLongRightArrow]", BoxMargins -> -.1]}], 
    MakeBoxes[y, StandardForm]}];

MakeExpression[
   RowBox[{x_, "/", RowBox[{"\[DoubleLongRightArrow]", y_}]}], 
   StandardForm] := 
  MakeExpression[RowBox[{"PerArrow", "[", x, ",", y, "]"}], 
   StandardForm];

In[3]:= PerArrow[x, y]

Out[3]= x /\[DoubleLongRightArrow] y

In[4]:= x /\[DoubleLongRightArrow]y

Out[4]= x /\[DoubleLongRightArrow] y

In[5]:= FullForm[x /\[DoubleLongRightArrow]y]

Out[5]//FullForm= \!\(\*
TagBox[
StyleBox[
RowBox[{"PerArrow", "[", 
RowBox[{"x", ",", "y"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)

For sake of clarity, here is a screenshot as well: new operator

Since the operator is not fully integrated, further concerns are:

  • the operator is selected weird when clicked (DoubleLongRightArrow with y instead of with /).
  • accordingly, the parsing part requires the DoubleLongRightArrow to be RowBox-ed with y, otherwise it yields syntax error
  • syntax coloration (at In[4] and In[5])
  • it prints weird if inputted directly (notice the large gaps at In[4] and In[5])

Now, I can live with these, though it would be nice to have some means to iron out all the minor issues. I guess all these boil down to basically some even lower-level syntax handler, which does not now how to group the new operator. Any idea on how to tackle these? I understand that Cell has a multitude of options which might come handy (like CellEvaluationFunction, ShowAutoStyles and InputAutoReplacements) though I'm again clueless here.

István Zachar
  • 1,343
  • 2
  • 13
  • 31
  • I had forgotten about ShowAutoStyles. I'll have to think about how that might be used. I still suspect that what you want is not completely possible, based on the response I got to my own question from Michael Pilat, who works for Wolfram Research. Nevertheless, it may be possible to fake one or more of the additional behavior points you request. – Mr.Wizard Apr 19 '11 at 15:35