Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inferrability failure #2

Closed
timholy opened this issue Jul 6, 2017 · 4 comments
Closed

Inferrability failure #2

timholy opened this issue Jul 6, 2017 · 4 comments

Comments

@timholy
Copy link
Contributor

timholy commented Jul 6, 2017

julia> using ComputedFieldTypes

julia> @computed type Mine{N}
           x::NTuple{N,Int}
           y::NTuple{2^N,Int}

           function Mine{N}(x::NTuple{N,Int}) where N
               y = ntuple(identity, Val{2^N})
               new(x, y)
           end
       end

julia> @code_warntype Mine{2}((1,2))
Variables:
  #self#::Any
  x::Tuple{Int64,Int64}
  y::Any

Body:
  begin 
      y::Any = (Main.ntuple)(Main.identity, (Core.apply_type)(Main.Val, $(Expr(:invoke, MethodInstance for power_by_squaring(::Int64, ::Int64), :(Base.power_by_squaring), 2, :($(Expr(:static_parameter, 1))))))::Type{Val{_}} where _)::Any # line 7:
      SSAValue(0) = (Core.apply_type)(Main.Mine, $(Expr(:static_parameter, 1)), $(Expr(:invoke, MethodInstance for power_by_squaring(::Int64, ::Int64), :(Base.power_by_squaring), 2, :($(Expr(:static_parameter, 1))))))::Type{Mine{2,_}} where _
      return $(Expr(:new, SSAValue(0), :((Base.convert)((Core.fieldtype)(SSAValue(0), :x)::Type{#s45} where #s45<:Tuple{Int64,Int64}, x)::Tuple), :((Base.convert)((Core.fieldtype)(SSAValue(0), :y)::Type{#s45} where #s45<:(Tuple{Vararg{Int64,_}} where _), y))))
  end::Mine{2,_} where _

It's not helped if one changes the ntuple call to ntuple(identity, 2^N).

@timholy
Copy link
Contributor Author

timholy commented Jul 6, 2017

Hmm, fulltype is also not inferrable:

julia> @code_warntype fulltype(Mine{3})
Variables:
  #self#::ComputedFieldTypes.#fulltype
  #unused#::Any

Body:
  begin 
      return (Core.apply_type)(Main.Mine, $(Expr(:static_parameter, 1)), $(Expr(:invoke, MethodInstance for power_by_squaring(::Int64, ::Int64), :(Base.power_by_squaring), 2, :($(Expr(:static_parameter, 1))))))::Type{Mine{3,_}} where _
  end::Type{Mine{3,_}} where _

A potential workaround is OK for now but may not in the future.

@vtjnash
Copy link
Owner

vtjnash commented Jul 6, 2017

It's going to be difficult to teach inference that ^ is computable. Anyways, the purpose of this package is not to make uninferrable things magically computed. The purpose of this package is simply to demonstrate how JuliaLang/julia#18466 would look and behave if implemented in base.

@vtjnash vtjnash closed this as completed Jul 6, 2017
@timholy
Copy link
Contributor Author

timholy commented Jul 6, 2017

Got it, I imagined there was more @pure magic going on...but it doesn't seem like there is.

@vtjnash
Copy link
Owner

vtjnash commented Jul 6, 2017

It seems like it is going to be difficult to teach inference that ^ is pure. The closest I can suggest right now is to use Base.@pure pure_pow(x, y) = x^y.

This package is only intended as a demonstration of what JuliaLang/julia#18466 would look like if implemented in Base. It's not intended as a mechanism to make uninferrable functions magically computable. (which is pretty much the same discussion as happened with mauro3/SimpleTraits.jl#39, if you wish to revisit the issue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants