Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define /@-like operator

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?

like image 573
István Zachar Avatar asked Apr 17 '11 20:04

István Zachar


2 Answers

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.

  • The index page on Textual Input and Output.

  • MakeBoxes, and MakeExpression, and an example of their use.

  • PreRead

  • More drastically, one might use CellEvaluationFunction which can be used to do unusual things.

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

like image 114
Mr.Wizard Avatar answered Oct 20 '22 00:10

Mr.Wizard


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.

like image 34
István Zachar Avatar answered Oct 19 '22 23:10

István Zachar