# Define Tilde as a postfix operator

I have read a lot of posts in this forum on defining infix symbols like CircleDot and Tilde. The Help pages say Tilde is often used either as an infix or postfix operator, but I can find no example in the Mathematica documentation nor in this forum of how to define it as a postfix operator. I have several such operators I would like to define but I am unable to define even a very simple one.

It is straight-forward to define Tilde as an infix operator. For example, input:

Foo[expr_] := -expr
expr1_∼expr2_ := Foo[expr1]
x=.;
100∼x


-100


However, the operation I wish to define would be

expr1_∼:=Foo[expr1]
100∼


also with an answer of -100, but this is not accepted by Mathematica. I don't wish to invoke the Notations package. Is this possible to do? I hope the answer is trivial and, if so, I apologize for asking.

• Can you link to the help page that says that it can be used as a postfix operator? – Szabolcs Nov 21 '16 at 7:47
• I just spent an hour looking and I can't find it. I saw it again yesterday, just one tiny reference in the Documentation Center. The sentence said that it is usually used as an infix symbol but sometimes as postfix. I guess the upshot of this is that it is standard in Mathematica to easily define binary operators (using special symbols like Tilde) but not unary operators. I find that strange. I had posted here just to find out whether or not this can be done so that I would know whether or not to stop trying to do this. – matrixbud Nov 21 '16 at 16:29

It is not possible to make \[Tilde] postfix. It is not possible to define new operators with new parsing rules in Mathematica in such a way that they work generally. It is only possible to do it with the Notation package, but it will only work in the notebook interface, not in plain text source files or in terminal mode. Perhaps someone else will write an answer about that topic, as I don't use the Notation package. Instead I'll talk about how Mathematica parses plain text input.

a \[Tilde] b is hard-wired to parse to Tilde[a, b]. You can assign any definition to the symbol Tilde, but you cannot change how a \[Tilde] b is parsed. You cannot change the fact that it is infix, you cannot change its precedence, and you cannot change the fact that it parses directly to Tilde[a, b].

Note 1: Parsing means converting a textual representation of an expression into an in-memory data structure. It is separate from evaluation. Example: f@x doesn't evaluate to f[x]. It directly parses to the same in-memory data structure as f[x]. f@x and f[x] are indistinguishable by the evaluator, as it never even sees their textual form, only their internal representation.

Note 2: Perhaps it is unlucky to work with \[Tilde] because it looks very similar to ~, which has a completely different meaning: x ~f~ y is the same as f[x, y].

• @Szablocs, thank you for this clear explanation. A follow-on question, if you have time. If I select a non-hard-wired special symbol, is it possible to give it a postfix attribute and maybe a precedence level (not sure I am using the correct terminology) and then define it as unary operator? I am not locked in to Tilde although it happens to be the mathematical standard for one of the operations I have in mind. If so, is there a list of non-hardwired special symbols or a way to query a special symbol to know if it is hardwired? – matrixbud Nov 21 '16 at 19:50
• @matrixbud No, it is not possible to customize the parsing step. One reason is that there are multiple parsers for Mathematica: the kernel has one, the front end has a different one, even the Workbench has a Java implementation (I think). These all should ideally behave the same way, so customizing just one can't be allowed. (Yes, there are sometimes bugs stemming form the fact that they don't truly behave the same way...) – Szabolcs Nov 21 '16 at 19:59
• @matrixbud What you can do is restrict yourself to using a notebook interface. Things in the notebook are represented as "boxes". Search for this work in the documentation. You can customize how boxes are translated to Mathematica input or the reverse, see MakeExpression and MakeBoxes. This is not easy. The Notation package uses this feature to make it easier to set up new notations—this is what you want. But I am not experienced with this package. Start here and read the tutorials, also notation-package. – Szabolcs Nov 21 '16 at 20:01
• THAT answer is definitive. It explains a lot and I appreciate it. I'll just have to decide whether this feature is important enough to me to (1) learn the Notation package and (2) live with the overhead that comes with the package. – matrixbud Nov 21 '16 at 22:13
• I need to put in a feature request to add a few postfix symbols. Presumably the developers can coordinate the parsers. – matrixbud Nov 21 '16 at 22:34