8000 Handling recursive types automatically · Issue #602 · chalk-lab/Mooncake.jl · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Handling recursive types automatically #602
Open
@yebai

Description

@yebai

A promising suggestion by @MilesCranmer (part of #594 (comment))

So I think to handle recursive types automatically, you would ultimately need to have a second argument for tangent_type (actually I suppose it should be a callee of tangent_type so it doesn't have the constraints of a generated function) in the form of a type cache. Something like:

function _resolve_tangent_type(::Type{P}, cache=IdDict{Any,Any}) where {P}

This cache would need to be passed to downstream calls. It's kind of similar to your existing cache-like mechanisms, only here it would be for types rather than values.

Then what you would do is have a special sentinel struct for catching recursions:

struct UnderAssembly end
const UNDER_ASSEMBLY = UnderAssembly()

so that in your tangent_type call, you could have something like:

cached_type = get(cache, P, nothing)

if cached_type === nothing
    cache[P] = UNDER_ASSEMBLY
    
    result = #= Regular code =#

    cache[P] = result  # Indicate it is finished
    return result
elseif cached_type === UNDER_ASSEMBLY

    ######################################
    #= This means we have a recursion!! =#
    ######################################
    
    result = #= handle of recursive structs =#

    cache[P] = result  # Regular caching
    return result
else
    return cached_type  # Already computed
end

Now, you want this to be a local cache rather than global, to prevent any world age problems. Which is why I think having a second argument to tangent_type makes the most sense.

This can follow on #600 which means you would do all of this in the codegen step, so you wouldn't get any allocations at runtime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0