Stream: helpdesk (published)

Topic: Coercing type inference to generate `Container{Union{A,B}}`


view this post on Zulip Brian Chen (Aug 20 2022 at 05:47):

...instead of Union{Container{A}, Container{B}}. Or in other words, getting discount parametric sum types without specifying the type parameters :stuck_out_tongue_wink:

To avoid the X-Y problem, here are the precise lines of code I'm thinking about with this question: https://github.com/FluxML/Flux.jl/pull/2005/files#diff-344cb9517a6c67cbcc6d110732e3f12cc908b8f4b5eeee46571218f5175d7e32R48-R66. After poking around I have not found a way to avoid return_type, so my hope is to nerd-snipe someone here into helping.

view this post on Zulip Mason Protter (Aug 20 2022 at 05:59):

I wonder if you could get away with a return type assertion here?

view this post on Zulip Mason Protter (Aug 20 2022 at 05:59):

have you tried that?

view this post on Zulip Brian Chen (Aug 20 2022 at 06:10):

You mean in lieu of the return_type call? Unfortunately I don't have a type to assert with if I drop that.

view this post on Zulip Sukera (Aug 20 2022 at 06:49):

not sure that transform is legal

view this post on Zulip Sukera (Aug 20 2022 at 06:49):

the outer Union may have a different memory layout than the inner union

view this post on Zulip Sukera (Aug 20 2022 at 06:50):

e.g. think of the layout of Union{Container{Int}, Container{Float64}} vs. Container{Union{Int, Float64}}

view this post on Zulip Sukera (Aug 20 2022 at 06:50):

those are two (three) totally different objects

view this post on Zulip Brian Chen (Aug 20 2022 at 14:09):

Yes, I'm aware of that. My point is that I can't even ask for the different Union coalescing behavior if I wanted to

view this post on Zulip Brian Chen (Aug 20 2022 at 14:11):

Not being able to reify unions generated by diverging -> converging control flow like this means that a lot of otherwise type stable functions are doomed to be type unstable once they're run through AD.

view this post on Zulip Mason Protter (Aug 20 2022 at 18:17):

Yeah, this is an interesting case. Looking with fresh eyes I see the problem. I think this is a case where I’d feel pretty shameless about using return_type

view this post on Zulip Mason Protter (Aug 20 2022 at 18:20):

I think this is a legitimate use case because so far as I can tell, you’re not modifying the behaviour based on the inference result, you’re just providing a more efficient smart container. If inference changes to give Any or any other wider type, this would still be correct, just slower


Last updated: Nov 22 2024 at 04:41 UTC