Stream: helpdesk (published)

Topic: ✔ Unknown unicode character '⇑'


view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 12:49):

I'm trying to use the \Uparrow unicode, but I get an error saying unknown unicode character '⇑'. The same happens for \Downarrow, but it works on the Left and Right cases. Both the Up and Down arrows are listed in the Unicode input documentation for Julia... Any ideas on what the problem might be?

view this post on Zulip Mason Protter (Feb 09 2024 at 13:06):

They're reserved characters

view this post on Zulip Mason Protter (Feb 09 2024 at 13:07):

Meaning that when the parser was written, we weren't sure if a good use-case for them would later arise, and so we made it so they can't be used as identifiers until we know what the possible use-case is

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:08):

So I can't use these unicodes?

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:11):

I was looking for an easy to use alternative to represent left, right, up and down. But I'm already using the normal arrows

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:11):

Is it possible to use named functions as binary operators?

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:13):

Like in Haskell :

x `myf` y

view this post on Zulip Mason Protter (Feb 09 2024 at 13:18):

You could open a PR adding them to https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm#L13

view this post on Zulip Mason Protter (Feb 09 2024 at 13:18):

I doubt it would be very controversial

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:19):

You mean adding \Uparrow and \Downarrow?

view this post on Zulip Mason Protter (Feb 09 2024 at 13:19):

yes

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:19):

What would be the controversy?

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:19):

Isn't having more unicode available better?

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:19):

What would be the downside?

view this post on Zulip Mason Protter (Feb 09 2024 at 13:20):

You can also attach unicode prefixes or suffixes to the regular arrow operators to make new versions of them:

julia> (a, b) = (a, b);

julia> ↑ᵃ(a, b) = (b, a);

julia> 1  2
(1, 2)

julia> 1 ↑ᵃ 2
(2, 1)

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:20):

This is a great idea! Had not thought about it.

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:20):

I'll use this. Thanks, @Mason Protter

view this post on Zulip Mason Protter (Feb 09 2024 at 13:20):

Davi Sales Barreira said:

What would be the downside?

the downside I mentioned before. Someone might want that operator to live at a different precedence level or be for something else. But I doubt it'd be a problem in this case, since the pre-arrow category is pretty clear.

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:21):

Ohh, now I undertand.

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:21):

The problem is that the precedence is "fixed".

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:22):

But then I'm confused again. I thought \Uparrow was unusable.

view this post on Zulip Mason Protter (Feb 09 2024 at 13:23):

Davi Sales Barreira said:

Like in Haskell :

x `myf` y

Yeah, there is a hack to do this:

struct InfixFunction <: Function
    name::Symbol
    operator::Function
end

InfixFunction(operator::Function) = InfixFunction(gensym(), operator)

(infix::InfixFunction)(arg₁, arg₂) = infix.operator(arg₁, arg₂)

function Base.show(io::IO, infix::InfixFunction)
    n_methods = length(methods(infix.operator))
    _methods = n_methods == 1 ? "method" : "methods"
    name = infix.name

    println(io, "$name (generic infix function with $n_methods $_methods)")
end

Base.display(infix::InfixFunction) = show(infix)

function Base.:|(arg₁, infix::InfixFunction)
    return InfixFunction(arg₂ -> infix.operator(arg₁, arg₂))
end

Base.:|(infix::InfixFunction, arg₂) = infix.operator(arg₂)

macro infix(operator::Symbol)
    return quote
        $operator::Function

        function Base.:|(arg₁, infix::typeof($operator))
            return $InfixFunction(arg₂ -> infix(arg₁, arg₂))
        end

        Base.:|(infix::typeof($operator), arg₂) = infix(arg₂)

        @info "$($operator) has been infixified"

        $operator
    end |> esc
end

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:24):

Mason Protter said:

Davi Sales Barreira said:

Like in Haskell :

x `myf` y

Yeah, there is a hack to do this:

struct InfixFunction <: Function
    name::Symbol
    operator::Function
end

InfixFunction(operator::Function) = InfixFunction(gensym(), operator)

(infix::InfixFunction)(arg₁, arg₂) = infix.operator(arg₁, arg₂)

function Base.show(io::IO, infix::InfixFunction)
    n_methods = length(methods(infix.operator))
    _methods = n_methods == 1 ? "method" : "methods"
    name = infix.name

    println(io, "$name (generic infix function with $n_methods $_methods)")
end

Base.display(infix::InfixFunction) = show(infix)

function Base.:|(arg₁, infix::InfixFunction)
    return InfixFunction(arg₂ -> infix.operator(arg₁, arg₂))
end

Base.:|(infix::InfixFunction, arg₂) = infix.operator(arg₂)

macro infix(operator::Symbol)
    return quote
        $operator::Function

        function Base.:|(arg₁, infix::typeof($operator))
            return $InfixFunction(arg₂ -> infix(arg₁, arg₂))
        end

        Base.:|(infix::typeof($operator), arg₂) = infix(arg₂)

        @info "$($operator) has been infixified"

        $operator
    end |> esc
end

Any downside on using this?

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:24):

It looks like a very nice feature to have.

view this post on Zulip Mason Protter (Feb 09 2024 at 13:24):

julia> myf(x, y) = x => y
myf (generic function with 1 method)

julia> @infix myf
[ Info: myf has been infixified
myf (generic function with 1 method)

julia> 1 |myf| 2
1 => 2

view this post on Zulip Mason Protter (Feb 09 2024 at 13:25):

Davi Sales Barreira said:

But then I'm confused again. I thought \Uparrow was unusable.

I'm not sure what you mean. It's currently not useable because it's not registered in the parser. If you add it to the parser as a registered infix operator, it will become usable.

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:26):

hmm. I can add it to the parsers in my code?

view this post on Zulip Mason Protter (Feb 09 2024 at 13:27):

No

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:27):

oh, ok

view this post on Zulip Mason Protter (Feb 09 2024 at 13:27):

You would need to modify julia itself

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:27):

yeah, I'm not doing that.

view this post on Zulip Mason Protter (Feb 09 2024 at 13:27):

it's a very easy PR, you literally would just add it to that list

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:27):

Yep.

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:27):

I guess I can add the PR.

view this post on Zulip Mason Protter (Feb 09 2024 at 13:28):

Here's an example of what a PR for that would look like: https://github.com/JuliaLang/julia/pull/49623/files

view this post on Zulip Mason Protter (Feb 09 2024 at 13:29):

And I think you wouldn't even need to do the latex completion stuff because it's already registered for latex completions

view this post on Zulip Mason Protter (Feb 09 2024 at 13:29):

Oh wait, I just realized we use JuliaSyntax.jl now, not the scheme parser :face_palm:

view this post on Zulip Mason Protter (Feb 09 2024 at 13:34):

So you would instead change this: https://github.com/JuliaLang/JuliaSyntax.jl/blob/main/src/kinds.jl#L147

view this post on Zulip Davi Sales Barreira (Feb 09 2024 at 13:43):

Thanks!

view this post on Zulip Notification Bot (Feb 09 2024 at 23:00):

Davi Sales Barreira has marked this topic as resolved.


Last updated: Nov 22 2024 at 04:41 UTC