Quote expressions

I can’t understand True and False outputs

julia> x=:(1+2);

julia> e₁=quote quote $x end end
quote
    #= REPL[2]:1 =#
    $(Expr(:quote, quote
    #= REPL[2]:1 =#
    $(Expr(:$, :x))
end))
end

julia> eval(e₁)
quote
    #= REPL[2]:1 =#
    1 + 2
end

julia> eval(eval(e₁))
3

julia> e₂=quote 1+2 end
quote
    #= REPL[5]:1 =#
    1 + 2
end

julia> eval(e₂)
3

julia> eval(e₁)==e₂
false

julia> eval(eval(e₁))==eval(e₂)
true

julia> eval(e₁)== quote
         #= REPL[2]:1 =#
         1 + 2
       end
false

julia> eval(e₁)
quote
    #= REPL[2]:1 =#
    1 + 2
end

julia> Expr(:quote,:(1+2))==:($(Expr(:quote, :(1 + 2))))
true

All quoted expressions contain a reference to the source line of their definition. Here is a simpler example:

julia> a = quote 1 + 2 end
quote
    #= REPL[1]:1 =#
    1 + 2
end

julia> b = quote 1 + 2 end
quote
    #= REPL[2]:1 =#
    1 + 2
end

julia> a == b
false

julia> quote 1 + 2 end == quote 1 + 2 end
true

So if the quoted expressions are defined on the same line they are indeed equal, but if they are defined on different lines, like a and b or your eval(e₁) and e₂, then the essential part (the inner expression) is the same, but not the complete expression objects.

On the other hand your eval(eval(e₁)) and eval(e₂) both evaluate to 3 and 3 equals 3, so everything is fine.

2 Likes