what is GenSym's type?

92 views
Skip to first unread message

Hongbo Rong

unread,
Jul 8, 2015, 1:09:51 PM7/8/15
to juli...@googlegroups.com

In the latest Julia, there are often GenSym nodes. Here is an example. How to know the type (like Int64 instead of just Symbol) of such a node? Thanks.

 

julia> function f(A, x) v=Sparse.spmv(A, x); v = A*x end

f (generic function with 1 method)

 

julia> ast=code_typed(f, (SparseMatrixCSC, Vector))

1-element Array{Any,1}:

:($(Expr(:lambda, Any[:A,:x], Any[Any[:v],Any[Any[:A,Base.SparseMatrix.SparseMatrixCSC{Tv,Ti<:Integer},0],Any[:x,Array{T,1},0],Any[:v,Any,2]],Any[],Any[Array{T,1}]], :(begin  # none, line 1:

        v = ((top(getfield))(Sparse,:spmv)::Any)(A,x)::Any # line 1:

        GenSym(0) = A * x::Array{T,1}

        v = GenSym(0)

        return GenSym(0)

    end::Array{T,1}))))

 

julia> ast[1].args[3].args[4]

:(GenSym(0) = A * x::Array{T,1})

 

julia> ast[1].args[3].args[4].args[1]

GenSym(0)

 

julia> typeof(ast[1].args[3].args[4].args[1])

GenSym

 

julia> fieldnames(ast[1].args[3].args[4].args[1])

1-element Array{Symbol,1}:

:id

 

julia> ast[1].args[3].args[4].args[1].id

0

Oscar Blumberg

unread,
Jul 8, 2015, 1:13:56 PM7/8/15
to juli...@googlegroups.com
GenSym (probably badly named) are single assignment variables so the type of a gensym is the type of the RHS of its only declaration. If I understood the question correctly as "what is an upper bound of the type a gensym can hold at runtime" ?

Hongbo Rong

unread,
Jul 8, 2015, 1:21:50 PM7/8/15
to juli...@googlegroups.com
I see. Thanks, Oscar!
That means I have to scan the AST once in order to know the type. It would be great if you can add this type info directly into lambda, like what loca vars types are handled.
Reply all
Reply to author
Forward
0 new messages