Why ∀ is an invalid character?

It seems ∀ (\forall) is not allowed for some reason:

julia> const ∀ = for
ERROR: syntax: invalid character "∀" near column 7

Why is that? Is it just because it’s not on one of the character lists in the parser source code?

https://github.com/JuliaLang/julia/issues/19012

1 Like

TLDR so that we can decide how they should work without breaking anything.

6 Likes

But the character is still “disallowed”. What prevents it from being available? I don’t want any special syntax parsing for it, just to be allowed for general use.

You can break pretty much anything, so it’s not a 100% sound reason:

julia> const + = -
- (generic function with 192 methods)

julia> 1+1
0
1 Like

Yea but we will not redefine + to - in Julia base.
If was allowed in e.g. variable names, then code like a∀b = 1 would break if we decide we want to introduce as an operator.

6 Likes

Incidentally,

const <...anything...> = for

will not work as for is a keyword and the parser will expect the relevant forms after it.

7 Likes

This is specially important. You are trying to use a const variable as it was a C macro, you want that any reference to that token/name is replaced by the code in the right-hand side in any context. This would never work. You can use a macro to do this replacement for you in a restrict scope and will need to be :∀ (probably).

2 Likes

You need to use string macros here or a special repl mode if you want this syntax.

using ReplMaker
using REPL: LineEdit

iscomplete(x) = true
function iscomplete(ex::Expr)
    if ex.head == :incomplete
        false
    else
        true
    end
end

function valid_code(s)
    input = String(take!(copy(LineEdit.buffer(s))))
    iscomplete(parse_forall(input))
end

function parse_forall(s::String)
    Meta.parse(replace(s, "∀" => "for"))
end
julia> initrepl(parse_forall,
                prompt_text="∀ julia> ",
                prompt_color = :blue,
                start_key=')',
                mode_name="∀_mode",
                valid_input_checker=valid_code)
REPL mode ∀_mode initialized. Press ) to enter and backspace to exit.

∀ julia> [x^2 + 1 ∀ x ∈ -3:3]
7-element Array{Int64,1}:
 10
  5
  2
  1
  2
  5
 10

∀ julia> ∀ x ∈ 1:4
             @show x + 1
         end
x + 1 = 2
x + 1 = 3
x + 1 = 4
x + 1 = 5
11 Likes