JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.43k stars 5.45k forks source link

Add more unary operators #18139

Open cstjean opened 8 years ago

cstjean commented 8 years ago

Julia has an impressive list of binary operators, but the unary operators are quite lonely: (+ - ! ¬ ~ |<:| |>:| √ ∛ ∜), and most of them have existing meanings. Having a lot of operators makes it easier to define nice-looking DSLs. I can't think of too many mathematical unary operators, except maybe DAGGER † (can we have post-fix unary operators?) so perhaps we could grab ~random unicode characters (☼💣❖) and call them unary?

StefanKarpinski commented 8 years ago

I'm confused by the thinking here. Why would we want to make some random set of characters into unary operators?

JeffBezanson commented 8 years ago

There are a couple postfix unary operators, namely A' and A.'.

To add more, there needs to be an argument that a character has been used as an operator e.g. in some subfield, or that its only reasonable use would be as an operator, etc. It can't just be random characters.

cstjean commented 8 years ago

The sunshine suggestion was fanciful, but there aren't a whole lot of clean unary operators in mathematical notation. Most of them are diacritics or some form of brackets, and neither of those are suitable for Julia. Could we at least get the Hermitian operators † and ⊹?

Perhaps that's a different problem, but the way MacroTools handles variables: @capture(ex, f_(args__) = body_) would also be cleaner if there was a unary operator to denote "This is a variable in a pattern matching algorithm"

JeffBezanson commented 8 years ago

Could we at least get the Hermitian operators † and ⊹?

Those are both currently invalid characters in julia, so yes this definitely seems possible. has unicode category Sm, the same as most other mathematical operators, so that's an easy case to make. is punctuation, but let's see what others think. If it gets a few other votes I think we can add this.

andyferris commented 8 years ago

is frequently used in superscript position... is there some way of doing this in Unicode?

tkelman commented 8 years ago

I think the way is "petition the Unicode committee and hope it gets included in Unicode N+1" ?

cstjean commented 8 years ago

@TotalVerb For what, variables in pattern-matching? $ is for plugging in values. MacroTools does the inverse: it extracts parts of an expression tree.

I want to build a tree with variables in it, and manipulate the tree. Right now it looks like tree = Node(1, 2, Node(5, Var(:x))). I would prefer tree = Node(1, 2, Node(5, ⨹:x)). I can use a macro in this particular case, so it's not the end of world. If I wanted a concise binary operation though, I would have plenty of unused, exotic operators to choose from ⨩ ⨪ ⨫ ⨬ ⨭ ⨮ ⨹ ⨺ ⩁ ⩂ ⩅ ⩊ ⩌ ⩏ ⩐ ⩒ ⩔ ⩖ ⩗ ⩛

jebej commented 7 years ago

Is there any way to make the dagger operator parsable as a variable name? Right now it's just invalid.

JeffBezanson commented 7 years ago

Yes, definitely, but we would need to decide on its syntax --- i.e. does it parse as an identifier, or some kind of operator?

jebej commented 7 years ago

Personally, I would use dagger as an identifier (ie part of a variable name). The complex transpose already has a symbol for it so it makes little sense to just have † do the same operation.

The exact use case I have is to store the result of something like this:

a†a = a'*a 
StefanKarpinski commented 7 years ago

That seems like a nice usage well aligned with how we already allow superscript integers and such.

stevengj commented 6 years ago

Also, the most sensible meaning for a postfix operator would be adjoint, and we already have a postfix adjoint operator.

chakravala commented 6 years ago

Hi, I didn't know about this issue but I found it after posting on discourse

I think it would be really great if people could define their own unary operators. Unary operators aren't so uncommon in mathematics.

Perhaps, a special character could be used in conjunction in order to help the parser determine that it is a unary operator. For example, `! could be factorial so that one couuld do 5`! and then one could define their own unary operator, each marked by `. Another thing could be to have both left-handed and right-handed unary operators.

For example factorial would be right-handed as in 5`! but if it was defined as a left-handed it could be written as !`5.

Not saying that ` is the right character for this, but it's just an idea that some sort of syntax like that might help in clarifying what is a unary operator and what is not, to help enable people to define their own unary operators for whatever they need.

stevengj commented 6 years ago

@chakravala, the discussion of custom unary operators is very similar to the discussion of custom infix operators — see #16985. It's a bit of a rat's nest when you get into syntax details.

jebej commented 6 years ago

I would like to add \dagger to be a valid part of a variable name. Where would I do that?

stevengj commented 6 years ago

@jebej, you would modify here.

jebej commented 6 years ago

Should I just add it at the end of line for primes?

(wc >= 0x2032 && wc <= 0x2037) || wc == 0x2057 || wc == 0x2020 ||
rjkat commented 5 years ago

FWIW I’d like to have and from Kleene algebra, but can also see the sense in keeping the number of operators to a minimum.

StefanKarpinski commented 5 years ago

The ship has definitely sailed on "keeping the number of operators to a minimum"—I don't think that's ever going to be the stance. I think that if the case can be made for any operator having a standard syntax and precedence that fits in with the way Julia is parsed, we're happy to support it. The issue with unary operators is more that they're a bit weird syntactically than that we don't want more operators.

kauesena commented 2 years ago

I think a postfix unary operator to implement transpose or permutedims is really missing. A superscript T or t is almost universally adopted as notation for transposition without conjugation in mathematical texts. However, the Unicode symbol is currently allowed as part of variable names, and making it the operator would be a breaking change... (One I'd happily support.) But perhaps there is another good symbol which isn't yet allowed which could be used.

Jollywatt commented 1 year ago

A superscript T or t is almost universally adopted as notation for transposition

@kauesena By the way, we can currently do this:

julia> var"'ᵀ"(a) = transpose(a)

julia> [im, 1]'ᵀ
1×2 transpose(::Vector{Complex{Int64}}) with eltype Complex{Int64}:
 0+1im  1+0im

Discussion