Evaluating Expr Error: UndefVarError: = not defined

With a hand-crafted Expr, I can initialize a new variable like so:

julia> a = :(x=1)
:(x = 1)

julia> eval(a)
1

julia> x
1

julia> 

However, if I evaluate a comparably simple Expr generated from the Reduce algebra library I get the following problematic behavior:

julia> begin
       using Reduce
       a = :(2*(x+y))
       b = :(2*x + 2*y)
       # solve returns a tuple of solutions
       c_expr = Algebra.solve(:($a / $b == c), :(c))[1]
       println(typeof(c_expr))
       println(c_expr)
       eval(c_expr)
       println(c)
       end
Expr
c = 1
ERROR: UndefVarError: = not defined
Stacktrace:
 [1] top-level scope at none:0
 [2] eval at ./boot.jl:328 [inlined]
 [3] eval(::Expr) at ./client.jl:404
 [4] top-level scope at none:9

julia> 
ERROR: LoadError: TypeError: in Expr, expected Symbol, got Expr
Stacktrace:
 [1] Expr(::Any, ::Vararg{Any,N} where N) at ./boot.jl:221
 [2] top-level scope at none:0
in expression starting at /Users/alex/dev/spectral/derp.jl:8
help?> eval
search: eval evalfile evallhseq @eval @evalpoly @evallhseq @off_evallhseq bytesavailable readavailable

  eval(expr)

We can see it’s an Expr, and we can see it is c = 1. This seems completely analogous to the working input above. What gives?
Thanks

It seems this here is the problem

julia> c_expr = Algebra.solve(:($a/$b == c),:c)[1]
:(c = 1)

julia> dump(ans)
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol =
    2: Symbol c
    3: Int64 1

julia> :(c=1) |> dump
Expr
  head: Symbol =
  args: Array{Any}((2,))
    1: Symbol c
    2: Int64 1

Probably due to the fact that the Julia language has changed at some point, this can be fixed.

Call dump on the expression. How things print can be deceiving.

Edit: nvm, sniped.

1 Like

The bug has now been fixed, this shouldn’t be a problem anymore.

1 Like

Confirmed, it works, thanks, @chakravala !