Skip to content

refactor[next]: parameterize element enumeration in 'tree_map' - #2931

Open
tehrengruber wants to merge 2 commits into
mainfrom
tehrengruber/parameterize-tree-map-elements
Open

tehrengruber wants to merge 2 commits into
mainfrom
tehrengruber/parameterize-tree-map-elements

Conversation

@tehrengruber

@tehrengruber tehrengruber commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Traversing type trees with tree_map only worked because ts.TupleType and ts.NamedCollectionType implemented __iter__/__len__ — container protocols bolted onto plain data classes solely to serve the traversal. Make the enumeration of a node's children a parameter of the traversal instead, so the dunders can be removed.

tree_map now takes two keyword-only parameters:

  • collection_elements (default iter) decomposes a collection into its elements, the dual of result_collection_constructor.
  • collection_keys (default an unbounded itertools.count()) supplies the path components for with_path_arg, so elements can be addressed by something other than their position.

Together with collection_type these are the three facets of a traversable collection: membership, decomposition and reconstruction.

Call sites that spelled out collection_type=ts.... now go through type_info.tree_map_type, which is additionally usable as a (parametrized) decorator so that the older sites can adopt it.

Removing __len__ also means an empty TupleType is no longer falsy, which fixes the if node.type: / assert node.type checks in type inference that were meant as "is the type set".

The motivation for this PR is the introduction of the VarArgType in #2833. Adding __len__ and __iter__ to it would have given rather strange semantics, which motivated this cleanup here.

AI disclaimer: This code was written with the help of AI tools. Tests have only been reviewed very coarsely.

Traversing type trees with `tree_map` only worked because
`ts.TupleType` and `ts.NamedCollectionType` implemented
`__iter__`/`__len__` — container protocols bolted onto plain data
classes solely to serve the traversal. Make the enumeration of a
node's children a parameter of the traversal instead, so the dunders
can be removed.

`tree_map` now takes two keyword-only parameters:

- `collection_elements` (default `iter`) decomposes a collection into
  its elements, the dual of `result_collection_constructor`.
- `collection_keys` (default an unbounded `itertools.count()`) supplies
  the path components for `with_path_arg`, so elements can be addressed
  by something other than their position.

Together with `collection_type` these are the three facets of a
traversable collection: membership, decomposition and reconstruction.

Call sites that spelled out `collection_type=ts....` now go through
`type_info.tree_map_type`, which is additionally usable as a
(parametrized) decorator so that the older sites can adopt it.

Removing `__len__` also means an empty `TupleType` is no longer falsy,
which fixes the `if node.type:` / `assert node.type` checks in type
inference that were meant as "is the type set".
…-elements

# Conflicts:
#	src/gt4py/next/iterator/type_system/type_synthesizer.py
#	src/gt4py/next/type_system/type_specifications.py

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant