Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type inference fails on unicode operator

Tags:

julia

Here is a simplified example:

odot(A::Matrix, B::Matrix) = A * B 

# using unicode alias ⊙
odot(A::Matrix, B::Vector) = reshape(A ⊙ reshape(B,size(A)), length(B))
⊙ = odot

julia> @code_warntype rand(3,3) ⊙ rand(9)
Variables:
  #self#::#odot
  A::Array{Float64,2}
  B::Array{Float64,1}

Body:
  begin 
      return (Main.reshape)((A::Array{Float64,2} ⊙ $(Expr(:invoke, LambdaInfo for reshape(::Array{Float64,1}, ::Tuple{Int64,Int64}), :(Main.reshape), :(B), :((Core.tuple)((Base.arraysize)(A,1)::Int64,(Base.arraysize)(A,2)::Int64)::Tuple{Int64,Int64}))))::Any,(Base.arraylen)(B::Array{Float64,1})::Int64)::Any
  end::Any

# using odot
odot(A::Matrix, B::Vector) = reshape(odot(A,reshape(B,size(A))), length(B))

julia> @code_warntype rand(3,3) ⊙ rand(9)
Variables:
  #self#::#odot
  A::Array{Float64,2}
  B::Array{Float64,1}
  TS::Type{Float64}

Body:
  begin 
      SSAValue(0) = $(Expr(:invoke, LambdaInfo for reshape(::Array{Float64,1}, ::Tuple{Int64,Int64}), :(Main.reshape), :(B), :((Core.tuple)((Base.arraysize)(A,1)::Int64,(Base.arraysize)(A,2)::Int64)::Tuple{Int64,Int64})))
      # meta: location REPL[1] odot 1
      # meta: location linalg/matmul.jl * 128
      TS::Type{Float64} = $(QuoteNode(Float64)) # line 129:
      SSAValue(1) = (Core.tuple)((Base.arraysize)(A::Array{Float64,2},1)::Int64,(Base.arraysize)(SSAValue(0),2)::Int64)::Tuple{Int64,Int64}
      SSAValue(2) = (Core.ccall)(:jl_new_array,(Core.apply_type)(Core.Array,Float64,2)::Type{Array{Float64,2}},(Core.svec)(Core.Any,Core.Any)::SimpleVector,Array{Float64,2},0,SSAValue(1),0)::Array{Float64,2}
      # meta: pop location
      # meta: pop location
      SSAValue(4) = $(Expr(:invoke, LambdaInfo for gemm_wrapper!(::Array{Float64,2}, ::Char, ::Char, ::Array{Float64,2}, ::Array{Float64,2}), :(Base.LinAlg.gemm_wrapper!), SSAValue(2), 'N', 'N', :(A), SSAValue(0)))
      SSAValue(3) = (Core.tuple)((Base.arraylen)(B::Array{Float64,1})::Int64)::Tuple{Int64}
      return $(Expr(:invoke, LambdaInfo for reshape(::Array{Float64,2}, ::Tuple{Int64}), :(Base.reshape), SSAValue(4), SSAValue(3)))
  end::Array{Float64,1}

I'm wondering why type inference fails on the unicode ⊙ in the first case.

like image 540
Gnimuc Avatar asked Mar 21 '17 04:03

Gnimuc


1 Answers

When defining a function in the variable form, you need to make sure you declare it as a constant in order for it to have the correct type information as a global.

const ⊙ = odot
like image 81
Chris Rackauckas Avatar answered Sep 19 '22 09:09

Chris Rackauckas