|
12 | 12 | import gt4py.next.ffront.field_operator_ast as foast |
13 | 13 | from gt4py import eve |
14 | 14 | from gt4py.eve import NodeTranslator, NodeVisitor, traits |
| 15 | +from gt4py.eve.extended_typing import MaybeNestedInTuple |
15 | 16 | from gt4py.next import common, errors |
16 | 17 | from gt4py.next.common import Dimension, DimensionKind, promote_dims |
17 | 18 | from gt4py.next.ffront import ( |
|
24 | 25 | from gt4py.next.ffront.foast_passes import utils as foast_utils |
25 | 26 | from gt4py.next.iterator import builtins |
26 | 27 | from gt4py.next.type_system import type_info, type_specifications as ts, type_translation |
| 28 | +from gt4py.next.utils import tree_map |
27 | 29 |
|
28 | 30 |
|
29 | 31 | OperatorNodeT = TypeVar("OperatorNodeT", bound=foast.LocatedNode) |
@@ -743,6 +745,117 @@ def visit_TupleExpr(self, node: foast.TupleExpr, **kwargs: Any) -> foast.TupleEx |
743 | 745 | new_type = ts.TupleType(types=[element.type for element in new_elts]) |
744 | 746 | return foast.TupleExpr(elts=new_elts, type=new_type, location=node.location) |
745 | 747 |
|
| 748 | + def _deduce_tuple_comprehension_target_type( |
| 749 | + self, |
| 750 | + target: MaybeNestedInTuple[foast.Symbol], |
| 751 | + element_type: ts.TypeSpec, |
| 752 | + **kwargs: Any, |
| 753 | + ) -> MaybeNestedInTuple[foast.Symbol]: |
| 754 | + """ |
| 755 | + Deduce the types of the loop target, e.g. `(a, b)` in `tuple(a + b for (a, b) in it)`. |
| 756 | +
|
| 757 | + Each target symbol starts out with a deferred type; revisiting it with `refine_type` |
| 758 | + replaces that by the constituent of `element_type` at the symbol's unpacking position. |
| 759 | + """ |
| 760 | + |
| 761 | + @tree_map(with_path_arg=True) |
| 762 | + def process_target(target_el: foast.Symbol, path: tuple[int, ...]) -> foast.Symbol: |
| 763 | + try: |
| 764 | + type_ = element_type |
| 765 | + for i in path: |
| 766 | + if not isinstance(type_, ts.TupleType) or len(type_.types) <= i: |
| 767 | + raise IndexError() |
| 768 | + type_ = type_.types[i] |
| 769 | + return self.visit(target_el, refine_type=type_, **kwargs) |
| 770 | + except IndexError: |
| 771 | + raise errors.DSLError( |
| 772 | + target_el.location, f"Cannot unpack non-iterable '{type_}' object." |
| 773 | + ) from None |
| 774 | + |
| 775 | + return process_target(target) |
| 776 | + |
| 777 | + def _deduce_tuple_comprehension_mapper( |
| 778 | + self, |
| 779 | + node: foast.TupleComprehension, |
| 780 | + target: MaybeNestedInTuple[foast.Symbol], |
| 781 | + element_type: ts.DataType, |
| 782 | + **kwargs: Any, |
| 783 | + ) -> foast.TupleComprehensionMapper: |
| 784 | + """ |
| 785 | + Deduce the per-element part, e.g. `a + b for (a, b)` in `tuple(a + b for (a, b) in it)`. |
| 786 | +
|
| 787 | + Refines the target symbols against `element_type` and then types the element |
| 788 | + expression with those symbols in scope (a fresh child symbol table). |
| 789 | + """ |
| 790 | + inner_kwargs = {**kwargs, "symtable": kwargs["symtable"].new_child()} |
| 791 | + new_target = self._deduce_tuple_comprehension_target_type( |
| 792 | + target, element_type, **inner_kwargs |
| 793 | + ) |
| 794 | + return foast.TupleComprehensionMapper( |
| 795 | + target=new_target, |
| 796 | + element_expr=self.visit(node.inner.element_expr, **inner_kwargs), |
| 797 | + location=node.location, |
| 798 | + ) |
| 799 | + |
| 800 | + def visit_TupleComprehension( |
| 801 | + self, node: foast.TupleComprehension, **kwargs: Any |
| 802 | + ) -> foast.TupleComprehension: |
| 803 | + # The target symbols are visited in `_deduce_tuple_comprehension_target_type`, |
| 804 | + # where their types are refined against the iterable's element type. |
| 805 | + target = node.inner.target |
| 806 | + iterable = self.visit(node.iterable, **kwargs) |
| 807 | + |
| 808 | + if isinstance(iterable.type, ts.TupleType): |
| 809 | + if len(iterable.type.types) == 0: |
| 810 | + raise errors.DSLError( |
| 811 | + iterable.location, |
| 812 | + "Cannot iterate over an empty tuple in a tuple comprehension.", |
| 813 | + ) |
| 814 | + if not all( |
| 815 | + isinstance(element_type, ts.DataType) for element_type in iterable.type.types |
| 816 | + ): |
| 817 | + raise errors.DSLError( |
| 818 | + iterable.location, |
| 819 | + "Tuple comprehension iterable elements must be data types.", |
| 820 | + ) |
| 821 | + |
| 822 | + element_types = cast(list[ts.DataType], iterable.type.types) |
| 823 | + # Only homogeneous iterables are supported, see ADR 0028. |
| 824 | + if not all(element_type == element_types[0] for element_type in element_types): |
| 825 | + raise NotImplementedError( |
| 826 | + "Tuple comprehensions over fixed-length tuples require all iterable " |
| 827 | + "elements to have the same type (see ADR 0028)." |
| 828 | + ) |
| 829 | + new_mapper = self._deduce_tuple_comprehension_mapper( |
| 830 | + node, target, element_types[0], **kwargs |
| 831 | + ) |
| 832 | + result = foast.TupleComprehension( |
| 833 | + inner=new_mapper, |
| 834 | + iterable=iterable, |
| 835 | + location=node.location, |
| 836 | + type=ts.TupleType(types=[new_mapper.element_expr.type for _ in element_types]), |
| 837 | + ) |
| 838 | + return result |
| 839 | + elif isinstance(iterable.type, ts.VarArgType): |
| 840 | + element_type = iterable.type.element_type |
| 841 | + new_mapper = self._deduce_tuple_comprehension_mapper( |
| 842 | + node, target, element_type, **kwargs |
| 843 | + ) |
| 844 | + element_expr = new_mapper.element_expr |
| 845 | + return_type = ts.VarArgType(element_type=element_expr.type) |
| 846 | + |
| 847 | + return foast.TupleComprehension( |
| 848 | + inner=new_mapper, |
| 849 | + iterable=iterable, |
| 850 | + location=node.location, |
| 851 | + type=return_type, |
| 852 | + ) |
| 853 | + else: |
| 854 | + raise errors.DSLError( |
| 855 | + iterable.location, |
| 856 | + f"Iterable in generator expression must be a tuple, got '{iterable.type}'.", |
| 857 | + ) |
| 858 | + |
746 | 859 | def visit_Call(self, node: foast.Call, **kwargs: Any) -> foast.Call: |
747 | 860 | new_func = self.visit(node.func, **kwargs) |
748 | 861 | new_args = self.visit(node.args, **kwargs) |
|
0 commit comments