Julia: ๋˜ ๋‹ค๋ฅธ ๋ถˆ๋ฒ• ์ง€์‹œ

์— ๋งŒ๋“  2017๋…„ 04์›” 17์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: JuliaLang/julia

https://github.com/JuliaLang/julia/issues/21243 ๊ณผ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค

type SpanningTree{V, E}
end

immutable TreePath{V, E}
    source::V
    source_to_lca::Vector{E}
end

function path{V, E}(src::V, target::V, tree::SpanningTree{V, E})
    TreePath(src, E[])
end

type RigidBody{T<:Number}
end

type Joint{T<:Number}
end

num_velocities(itr) = error("bad")
num_velocities{T}(p::TreePath{RigidBody{T}, Joint{T}}) = 0

type Mechanism{T<:Number}
    bodies::Vector{RigidBody{T}}
    tree::SpanningTree{RigidBody{T}, Joint{T}}

    function (::Type{Mechanism{T}}){T<:Number}(rootBody::RigidBody{T}; bla = 0)
        tree = SpanningTree{RigidBody{T}, Joint{T}}()
        new{T}([rootBody], tree)
    end
end

Mechanism{T}(rootBody::RigidBody{T}; kwargs...) = Mechanism{T}(rootBody; kwargs...)
bodies(mechanism::Mechanism) = mechanism.bodies
path(mechanism::Mechanism, from::RigidBody, to::RigidBody) = path(from, to, mechanism.tree)

function foo()
    mechanism = Mechanism(RigidBody{Float64}())
    b = first(bodies(mechanism))
    p = path(mechanism, b, b)
    nvpath = num_velocities(p)
end

foo()

--inline=no ๊ฒฐ๊ณผ๋Š”

signal (4): Illegal instruction: 4
while loading /Users/twan/code/julia/RigidBodyDynamics/v0.6/RigidBodyDynamics/perf/runbenchmarks.jl, in expression starting on line 43
foo at /Users/twan/code/julia/RigidBodyDynamics/v0.6/RigidBodyDynamics/perf/runbenchmarks.jl:40
unknown function (ip: 0x3151a23ff)
do_call at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/interpreter.c:75
eval at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/interpreter.c:242
jl_interpret_toplevel_expr at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/interpreter.c:34
jl_toplevel_eval_flex at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/toplevel.c:577
jl_parse_eval_all at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/ast.c:873
jl_load at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/toplevel.c:616 [inlined]
jl_load_ at /Users/osx/buildbot/slave/package_osx10_9-x64/build/src/toplevel.c:623
include_from_node1 at ./loading.jl:539
jlcall_include_from_node1_18834 at /Applications/Julia-0.6.app/Contents/Resources/julia/lib/julia/sys.dylib (unknown line)
include at ./sysimg.jl:14
jlcall_include_1043 at /Applications/Julia-0.6.app/Contents/Resources/julia/lib/julia/sys.dylib (unknown line)
process_options at ./client.jl:305
_start at ./client.jl:371
jlcall__start_19032 at /Applications/Julia-0.6.app/Contents/Resources/julia/lib/julia/sys.dylib (unknown line)
true_main at /Applications/Julia-0.6.app/Contents/Resources/julia/bin/julia (unknown line)
main at /Applications/Julia-0.6.app/Contents/Resources/julia/bin/julia (unknown line)
Allocations: 848930 (Pool: 847706; Big: 1224); GC: 0
Illegal instruction: 4

--inline=no ์—†์œผ๋ฉด ๋” ๊ฑฑ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ERROR: LoadError: bad
Stacktrace:
 [1] foo() at /Users/twan/code/julia/RigidBodyDynamics/v0.6/RigidBodyDynamics/perf/runbenchmarks.jl:40
 [2] include_from_node1(::String) at ./loading.jl:539
 [3] include(::String) at ./sysimg.jl:14
 [4] process_options(::Base.JLOptions) at ./client.jl:305
 [5] _start() at ./client.jl:371
while loading /Users/twan/code/julia/RigidBodyDynamics/v0.6/RigidBodyDynamics/perf/runbenchmarks.jl, in expression starting on line 43

versioninfo() :

Julia Version 0.6.0-pre.beta.132
Commit 52607b07f6 (2017-04-14 14:39 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin13.4.0)
  CPU: Intel(R) Core(TM) i7-3820QM CPU @ 2.70GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, ivybridge)
bug inference

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

julia> a=Core.Inference.return_type(path, Tuple{Any, RigidBody{T} where T<:Number, RigidBody{T} where T<:Number})
TreePath{_,Joint{T<:Number}} where _

julia> Core.Inference.has_free_typevars(a)
true

julia> typeintersect(a, TreePath{RigidBody{T},Joint{T}} where T)
Union{}

path ์˜ ์œ ์ถ”๋œ ๋ฐ˜ํ™˜ ์œ ํ˜•์—๋Š” ๋ฐ”์ธ๋”ฉ๋˜์ง€ ์•Š์€ typevar๊ฐ€ ์—†์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, where T ๊ฐ€ ๋ˆ„๋ฝ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งž์Šต๋‹ˆ๊นŒ?

๋ชจ๋“  3 ๋Œ“๊ธ€

julia> a=Core.Inference.return_type(path, Tuple{Any, RigidBody{T} where T<:Number, RigidBody{T} where T<:Number})
TreePath{_,Joint{T<:Number}} where _

julia> Core.Inference.has_free_typevars(a)
true

julia> typeintersect(a, TreePath{RigidBody{T},Joint{T}} where T)
Union{}

path ์˜ ์œ ์ถ”๋œ ๋ฐ˜ํ™˜ ์œ ํ˜•์—๋Š” ๋ฐ”์ธ๋”ฉ๋˜์ง€ ์•Š์€ typevar๊ฐ€ ์—†์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, where T ๊ฐ€ ๋ˆ„๋ฝ๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งž์Šต๋‹ˆ๊นŒ?

๊ฐ์†Œ๋œ ์žฌํ˜„:

julia> function foo{V, E}(::V, ::Pair{V, E})
           E
       end
foo (generic function with 1 method)

julia> code_warntype(foo, Tuple{Ref, Pair{Ref{T},Ref{T}} where T<:Number})
Variables:
  #self#::#foo
  #unused#<strong i="6">@_2</strong>::Any
  #unused#<strong i="7">@_3</strong>::Any

Body:
  begin 
      return $(Expr(:static_parameter, 2))
  end::Type{Ref{T<:Number}}

IIUC, ์ด๊ฒƒ์€ Type{Ref{T}} where T <: Number ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋‘ ๋ถ„ ๋ชจ๋‘ ๋น ๋ฅธ ๋‹ต๋ณ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰