Skip to content

Recursive Types #3063

Closed
Closed
@tlively

Description

@tlively

The typed function reference proposal introduces directly recursive and mutually recursive types, and the possibilities for recursion are expanded in the original GC proposal, which @dcodeIO is currently working on prototyping in Binaryen. #3012 expands Binaryen's type system to support arbitrarily nested types introduced in the GC proposal, but it does not allow for the creation of recursive types. This issue sketches out my thinking on how we could build support for recursive types.

First of all, creating a recursive type requires that the type be available before its definition is constructed. That means we need a way to pre-allocate types, and because of mutual recursion, we will need to be able to pre-allocate an arbitrary number of types at once. The preallocated types should be in an uninitialized state that makes them unusable while their definitions are constructed. Then they should be "finalized" all at once, which should involve applying the type definitions then canonicalizing and de-duplicating the underlying interned type definitions, as happens today. Preallocated types that turn out to be duplicates should be freed and should not be leaked to the rest of the program.

Canonicalization of recursive types is going to be the tricky part. The way #3012 is currently written, only value_types as defined in the grammar of GC types are represented as Types, unlike in the WebAssembly text and binary formats, which give both value_types and def_types identifiers in the type section. That means we have to deduplicate slightly more complex structures, but doesn't change the fundamental problem much.

The state of the art for canonicalizing and checking equality of equi-recursive types like these seems to be described in https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.2276, which I have not yet had a chance to read. Should be fun, though!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions