# On adjoints and custom postfix and infix operators

**URL:** <https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863>\
**Category:** Internals & Design\
**Created:** [August 3, 2018, 7:24am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863 "2018-08-03T07:24:25Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 3, 2018, 9:45am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/21 "2018-08-03T09:45:30Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/43):
>
> this won’t be solved until Julia can add self-defined operators by users, people may want more and this cannot always done by asking the parser to parse an operator.

If we place Julia in the (broad) Lisp family, this looks like a reasonable request. Eg for Common Lisp and Racket, the surface syntax is almost arbitrarily extensible.

However, even though it took me some time to appreciate it, I think Julia is much more careful about syntactic extensions. Eg macros require a `@`, so instead of blending seamlessly into the syntax like they do for other Lisps, the reader is warned that code transformations with non-standard evaluation etc can happen there.

Because of this, I would be cautious about packages customizing the syntax along other directions, too. The way things work at the moment, I can just read a piece of code and have a pretty good idea of the evaluation structure (or, if I see a `@`, be warned about having to look it up).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 3, 2018, 11:36am UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/22 "2018-08-03T11:36:24Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/43):
>
> Personally, I think this won’t be solved until Julia can add self-defined operators by users, people may want more and this cannot always done by asking the parser to parse an operator.

Julia already has this for infix operators, as long as you are willing to spell the operators in a certain way. There are an infinite number of user-defined operators available — dozens of Unicode operators, plus, in Julia 0.7, a large set of suffixes that you can append arbitrarily.

For more general spellings, see the discussion at [https://github.com/JuliaLang/julia/issues/16985](https://github.com/JuliaLang/julia/issues/16985)

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [August 3, 2018, 1:53pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/23 "2018-08-03T13:53:47Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/50):
>
> Julia already has this for infix operators, as long as you are willing to spell the operators in a certain way.

Unfortunately, the problem with this is that the order of operations is hard coded into the parser.

What is truly needed is the ability to specify an arbitrary order of operations, and to make meta-packages that provide certain standard sets of order of operation, so that mathematicians can create alternate symbologies for very different kinds of abstract algebras.

It has always bothered me that the Julia core devs are against the idea of making the order of operations user defined, so that it can be arbitrary.

This is a major limitation where other languages are going to overtake Julia.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 3, 2018, 2:04pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/24 "2018-08-03T14:04:58Z")

</div>

Are there any mainstream languages with user-defined operator precedence?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 3, 2018, 2:10pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/25 "2018-08-03T14:10:26Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/52):
>
> Are there any mainstream languages with user-defined operator precedence?

[Haskell](https://www.haskell.org/onlinereport/decls.html#fixity), but only for user-defined ``op`` infix operators. On the other hand, Haskell doesn’t allow Unicode math operators or even identifiers like `x̂`.

But I think it’s a stretch to call this a “major limitation” of Julia. Julia allows notation that is much closer to standard mathematics than any language I’m aware of because of the richness of the Unicode operator and identifier support.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 3, 2018, 2:11pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/26 "2018-08-03T14:11:12Z")

</div>

Yes, but what about _mainstream_ languages? :troll:

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [August 3, 2018, 2:18pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/27 "2018-08-03T14:18:31Z")

</div>

> **[Agda (programming language)](https://en.m.wikipedia.org/wiki/Agda_(programming_language))**
>
> Agda is a dependently typed functional programming language originally developed by Ulf Norell at Chalmers University of Technology with implementation described in his PhD thesis. The original Agda system was developed at Chalmers by Catarina Coquand in 1999. The current version, originally known as Agda 2, is a full rewrite, which should be considered a new language that shares a name and tradition.
> Agda is also a proof assistant based on the propositions-as-types paradigm, but unlike Coq, has...

But what’s the point of being mainstream? Hey, I get that most users probably dont want to deal with that, and it doesnt need to be turned on by default. But what’s wrong with having a switch that enables this feature if someone wants to get really wild and creative or obscure? Then you have to be forced to switch away from Julia to a proof-based language for that project.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 3, 2018, 2:29pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/28 "2018-08-03T14:29:10Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/55):
>
> But what’s wrong with having a switch that enables this feature if someone wants to get really wild and creative or obscure?

So much…

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [August 3, 2018, 2:44pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/29 "2018-08-03T14:44:31Z")

</div>

In fact, I would love to change the precedence of operations because I still think that the result:

```
6/2(2+1) = 1

```

is wrong 😅 However, IMHO, this is a very, very dangerous step. Everything can go wild if someone decides to do this and the debugging can be very difficult.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 3, 2018, 2:49pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/30 "2018-08-03T14:49:32Z")

</div>

Being able to read and review someone else’s code without having to know all of the possible global effects is much more important than being able to skip parenthesis in a language that is made for distributed package development and collaboration.

---

<div class="post-metadata">

**Author:** ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)\
**Post date:** [August 3, 2018, 3:11pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/31 "2018-08-03T15:11:43Z")

</div>

Actually you [can do this in Swift](https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#//appleref/swift/grammar/subscript-declaration) (see Operator Declaration and Precedence Group Declaration).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 3, 2018, 3:50pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/32 "2018-08-03T15:50:01Z")

</div>

Interesting! I’m going to prognosticate a bit and predict that actually using this for anything other than defining the operators that ship with the language is going to come to be considered an anti pattern and not something one ought to do when following best practices. It will be interesting to see if I’m right! In any case, it’s a stretch to say that user-defined operator precedence is a feature that a modern programming language clearly can’t succeed without.

---

<div class="post-metadata">

**Author:** ![airpmb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/airpmb/32/7826_2.png) [@airpmb](https://discourse.julialang.org/u/airpmb)\
**Post date:** [August 3, 2018, 3:51pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/33 "2018-08-03T15:51:02Z")

</div>

> [@Why \`eye\` has been deprecated?](https://discourse.julialang.org/t/why-eye-has-been-deprecated/12824/24):
>
> It seems you attributed that quote to the wrong person.

Apologies! I’m still learning how to quote properly in Discourse and it probably doesn’t help that I composed that post on my phone.

I’ve edited my message.

---

<div class="post-metadata">

**Author:** ![jekbradbury](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jekbradbury/32/953_2.png) [@jekbradbury](https://discourse.julialang.org/u/jekbradbury)\
**Post date:** [August 3, 2018, 5:25pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/34 "2018-08-03T17:25:01Z")

</div>

I think the main use is/is going to be defining operators that are “kind of like” existing operators and should have similar precedence. That is, if `•` is defined for matrix multiply, it can be given the precedence of `*` (which is always elementwise).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 3, 2018, 6:24pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/35 "2018-08-03T18:24:30Z")

</div>

> [@jekbradbury](#):
>
> I think the main use is/is going to be defining operators that are “kind of like” existing operators and should have similar precedence. That is, if `•` is defined for matrix multiply, it can be given the precedence of `*` (which is always elementwise).

Putting aside the fact that `*` is already matrix multiplication, there are _lots_ of allowed user-defined operators that are “kind of like `*`” and have the same precedence (in fact, infinitely many):

- Any of the other Unicode symbols [with the same precedence](https://github.com/JuliaLang/julia/blob/a882d9bf183fc93319002c9158f406f0755c202e/src/julia-parser.scm#L29): `∧ ⊗ ⊘ ⊙ ⊚ ⊛ ⊠ ⊡ ⊓ ∗ ∙ ∤ ⅋ ≀ ⊼ ⋄ ⋆ ⋇ ⋉ ⋊ ⋋ ⋌ ⋏ ⋒ ⟑ ⦸ ⦼ ⦾ ⦿ ⧶ ⧷ ⨇ ⨰ ⨱ ⨲ ⨳ ⨴ ⨵ ⨶ ⨷ ⨸ ⨻ ⨼ ⨽ ⩀ ⩃ ⩄ ⩋ ⩍ ⩎ ⩑ ⩓ ⩕ ⩘ ⩚ ⩜ ⩞ ⩟ ⩠ ⫛ ⊍ ▷ ⨝ ⟕ ⟖ ⟗`
- Just take `*` (or one of the above) and add one or more of any of the allowed operator suffixes in Julia 0.7, e.g. `*̂₁ᵃ′` is an operator with the same precedence as `*`.

Similarly for `+` etcetera.

(It seems a lot clearer to me to write `a *ₘ b` than `a `mytimes` b` for an operator that is kind of like `*`.)

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [August 3, 2018, 7:25pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/36 "2018-08-03T19:25:49Z")

</div>

If you are someone who doesn’t want to customize the operator precedence, it is very unlikely that you will ever encounter a scenario where you actually need it. However, there are real scenarios for some people where they want to be able to customize this. In those cases, you would be expected to have the necessary mathematical background before you would be able to contribute anything anyway, so I don’t see any issue with it, from a contributing point of view. If you want to work on code with abstract mathematics, background knowledge is needed regardless… so I don’t really see the extra steps as having a negative side effect.

By the way, my comment above about this being a major issue seems to be misinterpreted here. It is not major in the sense that it affects the “mainstream” status of the language. It is major in the sense that if you were to require this feature for a specific research, then another language has to be used. I’d rather stay within the Julia environment than to switch to another language only because operator precedence cannot be set.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 3, 2018, 8:08pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/37 "2018-08-03T20:08:31Z")

</div>

> [@chakravala](#):
>
> It is major in the sense that if you were to require this feature for a specific research, then another language has to be used.

I think you are using “require” in some sense that I’m not familiar with. I can’t imagine a research project “requiring” a programming language to use a particular spelling for a function call.

And if you do have a research project that “requires” particular spellings, it is quite likely that no existing programming language will satisfy you, because every language has certain keywords that cannot be renamed.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [August 3, 2018, 8:12pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/38 "2018-08-03T20:12:37Z")

</div>

> [@stevengj](#):
>
> allowed operator suffixes in Julia 0.7

@stevengj: what operator suffixes are allowed in Julia 0.7? I couldn’t find any documentation for it.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 3, 2018, 8:21pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/39 "2018-08-03T20:21:05Z")

</div>

> [@jandehaan](#):
>
> what operator suffixes are allowed in Julia 0.7

From [the manual](https://docs.julialang.org/en/latest/manual/variables/#Allowed-Variable-Names-1):

> Operators can also be suffixed with modifying marks, primes, and sub/superscripts, e.g. `+̂ₐ″` is parsed as an infix operator with the same precedence as `+` .

---

<div class="post-metadata">

**Author:** ![jekbradbury](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jekbradbury/32/953_2.png) [@jekbradbury](https://discourse.julialang.org/u/jekbradbury)\
**Post date:** [August 3, 2018, 10:34pm UTC](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863/40 "2018-08-03T22:34:27Z")

</div>

Whoops! I was replying to Stefan’s comment about Swift, where `*` is elementwise and the dot is used for matmul by at least one third-party library.

[Previous page](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863.md?page=1)

[Next page](https://discourse.julialang.org/t/on-adjoints-and-custom-postfix-and-infix-operators/12863.md?page=3)
