refactor[next]: parameterize element enumeration in 'tree_map' - #2931
Open
tehrengruber wants to merge 2 commits into
Open
tehrengruber wants to merge 2 commits into
tehrengruber wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Traversing type trees with
tree_maponly worked becausets.TupleTypeandts.NamedCollectionTypeimplemented__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_mapnow takes two keyword-only parameters:collection_elements(defaultiter) decomposes a collection into its elements, the dual ofresult_collection_constructor.collection_keys(default an unboundeditertools.count()) supplies the path components forwith_path_arg, so elements can be addressed by something other than their position.Together with
collection_typethese are the three facets of a traversable collection: membership, decomposition and reconstruction.Call sites that spelled out
collection_type=ts....now go throughtype_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 emptyTupleTypeis no longer falsy, which fixes theif node.type:/assert node.typechecks in type inference that were meant as "is the type set".The motivation for this PR is the introduction of the
VarArgTypein #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.