-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add ⟂ to infix operator precedence #24404
Conversation
Out of curiosity, what do you use them for? |
It's called the "over" and "under" operation for binary trees. Essentially, it combines 2 trees into 1, the over operation α ↗ β identifies the root of α with the leftmost leaf of β, while the under operation α ↖ β identifies the rightmost leaf of α with the root of β. It's a fairly standard operation, sometimes You can find more information in the references of my package Dendriform.jl |
Note that arrows use the precedence of "implies" so e.g. |
Thanks for clarifying, what needs to be done to avoid that? which of these lists should it be added to? |
I'm really not sure what precedence it should have (#23224 (comment)). I see the Arithmetree paper doesn't use exactly these operators, so that doesn't offer much guidance. Do these arrows have any other mathematical uses that might hint at their precedence (e.g. are they multiply-like or power-like)? Otherwise I'd probably just keep them in the "arrow" precedence level, and recommend parentheses. By the way, the READMEs for your julia packages are really nice!! |
In that paper the symbol for over and under is The way it's used in my package and in the math, it should be treated more like something of a multiplication or addition symbol. probably it should not be in the arrow category, I would prefer it to take precedence over order relations like
Thank you very much, it is still a work in progress and not completed (missing some clear documentations), but I'm gradually trying to make it exciting for visitors to explore and discover |
Maybe you can use EDIT: EDIT 2: |
@pabloferz Thanks for the suggestion, but I've already settled for these symbols in writing the paper months ago. Having them as infix operators in Julia is an after thought at this point. After thinking it over and taking a look at the other precedence lists, I figured it might actually fit into the |
Also, I'd like to ask what is the reasoning behind placing ⊣, ⊢ under the comparison group? Is there a specific need for that? In dendriform algebra for example, these operations would be analogous to addition, in fact the addition is the union of those two operations. Not that it needs to be changed, but if there is no particular reason for it being in the |
In computer science |
Alright, I see. That explains why those need parenthesis. One final thing is in the future I intend to use ⊥, but it has not been added to any list yet, so I went with the plus list for now. If that is alright, and the slanted arrows are alright, then this is finished. Extra Idea: instead of hard-coding these operators, what if Julia allowed users to define their own operators and their precedence using multiple dispatch? then one particular definition of an operator might have some kind of precedence or associativity, while a different defintion of it for another purpose would have a completely different precedence and associativity, but it's the same symbol. This would allow a great deal of flexibility, if it were possible, but would need type inference. |
That would require knowing types before code is even parsed, which is not really possible. There could be a separate mechanism for customizing parsing of operators within a certain file, but some people find that confusing, plus it makes it harder to print expressions or copy and paste code. |
You mean like minimum of a set? Where is that notation used? In my case, it is used to represent the middle sum operation in the tri-algebra (which generalizes the di-algebra from binary trees to any tree) You can find it's usage in the Arithmetree paper and other papers talking about tri-algebras. |
It's very commonly used in lattice theory and type theory (https://en.wikipedia.org/wiki/Up_tack). |
Then there's probably not a good argument for making it an op, so I removed the commit for that. Are the slanted arrows okay then? If mathematics is ever formalized into something like a programming language, I wonder if somebody will find a clever way of handling these symbols with so many different meanings. Now I'm beginning to gain a greater appreciation for natural language math and how flexible it is, that flexibility is not easily reproduced in a formal language. |
@JeffBezanson might this be possible: could we make the list of operator precedences mutable, so that packages could define their own operator precedences at run time? e.g. set_op_prec(:⊥, :plus) then Julia would throw an error or warning if two packages try to add the same operator to conflicting precedence levels. This would solve many of the issues associated with the static operator precedence Then the Base Julia precedence lists would only include the essential operators used by Base. If this was implemented, then it would also be much more clear to users how the precedence system actually works, because they helped define it as they are implementing their packages Please let me know if that is feasible, and if the changes in this PR ar okay as-is. |
As wiki says: "perpendicular symbol (⟂, \perp in LaTeX, U+27C2 in Unicode) is a binary relation symbol"
I am not sure if it brings nice solution to the problem. :-) |
Yes, so the Wouldn't it make sense for Julia to have a mutable operator precedence system though? This would make it much more useful as a formal specficiation language for pure mathematics, since any symbol could have so many different contexts and meanings. And if you disagree, then why not explain? |
What I disagree with is user-controlled operator precedence. That's definitely not going to happen. |
Custom operator precedence is definitely controversial, but it should not be thought of as mutable operator precedence. In other words, you do not want an imperative command that says "change the precedence of operator x to y", but rather a scoped construct that says "in this file or block, operator x has precedence y". That would allow different packages to use different precedence rules simultaneously without interference. However, that still has problems like that it's not possible to parse expressions without extra context info. |
\perp is a relation, so if we add it, it should go in the comparison precedence level. |
This is actually what I meant as a feature. I didn't mean that the precedence should be fully mutable. What I meant is that it should be possible to add an operator precedence after Julia has started. After you set it, it should not be changed obviously, so it should not be mutable aftewards. What I meant in this case is that it should be possible to add to the list, but not necessarily undo it after the package has loaded already. In this case, for most packages there would probably be a common system for defining the precedences. However, there might be the rare package that does not need to co-operate with other packages and would benefit from having completely different precedences. Could you explain further what is the technical difficulty with it? |
Hi, could we at least merge the |
@chakravala, you'll also have to remove Note that this is a breaking change and should be marked as such in the NEWS. |
Parsing (If only |
Done. However, those are not the same symbol, I added the julia> "⟘"=="⟂"
false |
NEWS.md
Outdated
@@ -31,7 +31,7 @@ New language features | |||
`@generated` and normal implementations of part of a function. Surrounding code | |||
will be common to both versions ([#23168]). | |||
|
|||
* Added `⟂` (perp) operator with comparison precedence ([#24404]). | |||
* Added `⟂` (\perp) `∥` (\parallel) operator with comparison precedence ([#24404]). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
∥
is already in the list of comparison operators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Sorry I initially led you astray on this.)
Would be good to add a test, e.g. @test Base.operator_precedence(:∥) == Base.operator_precedence(:⟂) == Base.operator_precedence(:<) |
In |
That is taken care of now. Thanks for checking it. |
test/parse.jl
Outdated
@@ -254,3 +254,6 @@ end | |||
end | |||
@test_throws ArgumentError parse(Complex{Int}, "3 + 4.2im") | |||
end | |||
|
|||
# added ⟂ to operator precedence (#24404) | |||
@test Base.operator_precedence(:∥) == Base.operator_precedence(:⟂) == Base.operator_precedence(:<) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Directly testing what the operator_precedence
function returns seems kind of pointless since that's what you defined it to be. Testing the result of a few parses would seem to be a better end-to-end test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like
@test Meta.parse("a ⟂ b ⟂ c") == Expr(:comparison, :a, :⟂, :b, :⟂, :c)
@test Meta.parse("a ⟂ b ∥ c") == Expr(:comparison, :a, :⟂, :b, :∥, :c)
?
NEWS.md
Outdated
@@ -31,6 +31,8 @@ New language features | |||
`@generated` and normal implementations of part of a function. Surrounding code | |||
will be common to both versions ([#23168]). | |||
|
|||
* Added `⟂` (\perp) operator with comparison precedence ([#24404]). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put backticks around \perp
src/flisp/julia_extensions.c
Outdated
@@ -84,7 +84,7 @@ static int is_wc_cat_id_start(uint32_t wc, utf8proc_category_t cat) | |||
|
|||
(wc >= 0x266f && | |||
(wc == 0x266f || wc == 0x27d8 || wc == 0x27d9 || // ♯, ⟘, ⟙ | |||
(wc >= 0x27c0 && wc <= 0x27c2) || // ⟀, ⟁, ⟂ | |||
(wc >= 0x27c0 && wc <= 0x27c2) || // ⟀, ⟁ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You only changed the comment, not the code. Need to change the upper limit to 0x27c1
.
fixed the test, news, and character range. |
based on the following Julia pull-requests JuliaLang/julia#24404 JuliaLang/julia#31604
based on the following Julia pull-requests JuliaLang/julia#24404 JuliaLang/julia#31604
Hi, since I am using
↗
,↖
as binary mathematical infix operators for a package, it would help if these were able to be used as infix. I'm not entirely sure if I added it into the right place.The goal is to be able to do something like
Please let me know if there is something additional that needs to be done to allow this.