JuliaDynamics / JuliaDynamics/LightSumTypes.jl

Function to perform branching automatically to solve inference failures

Open
#120 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
62
Forks
2
PR merge metrics
No merged PRs in 30d

Description

Currently inference fails inferring an abstract type

julia> using LightSumTypes

julia> @sumtype X(Bool, Int, Vector{Bool}, Vector{Int})

julia> xs = [X([1,2]), X(true)]

julia> Base.sum(x::X) = sum(variant(x));

julia> @code_warntype sum.(xs)
MethodInstance for (::var"##dotfunction#230#3")(::Vector{X})
  from (::var"##dotfunction#230#3")(x1) @ Main none:0
Arguments
  #self#::Core.Const(var"##dotfunction#230#3"())
  x1::Vector{X}
Body::AbstractVector
1 ─ %1 = Base.broadcasted(Main.sum, x1)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(sum), Tuple{Vector{X}}}
│   %2 = Base.materialize(%1)::AbstractVector
└──      return %2

but this is not fundamental, we would need a way to forward a function to its variant, without extracting the variant itself, adding something like this in the source code works:

function apply(f::Function, sumt)
   v = $LightSumTypes.unwrap(sumt)
   $(branchs(variants, :(return f(v))))
end

since

julia> Base.sum(x::X) = apply(sum, x);

julia> @code_warntype sum.(xs)
MethodInstance for (::var"##dotfunction#230#1")(::typeof(sum), ::Vector{X})
  from (::var"##dotfunction#230#1")(x1, x2) @ Main none:0
Arguments
  #self#::Core.Const(var"##dotfunction#230#1"())
  x1::Core.Const(sum)
  x2::Vector{X}
Body::Vector{Int64}
1 ─ %1 = Base.broadcasted(Main.apply, x1, x2)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(apply), Tuple{Base.RefValue{typeof(sum)}, Vector{X}}}
│   %2 = Base.materialize(%1)::Vector{Int64}
└──      return %2

only though for functions with a unique sumtype. I think with generated functions we could create an apply(f, args::Tuple) function which works for any mixture of sumtypes and normal types.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing unwrap, variants, and branchs implementations in the LightSumTypes source, then inspect how generated functions are currently used. The proposed behavior should support an apply(f, args::Tuple) path for mixtures of sum types and normal values, while making the shown sum.(xs) inference produce a concrete result type.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.