diff --git a/crates/pyrefly_types/src/type_level_dsl.rs b/crates/pyrefly_types/src/type_level_dsl.rs index ebaa2f9cdb..35ee202875 100644 --- a/crates/pyrefly_types/src/type_level_dsl.rs +++ b/crates/pyrefly_types/src/type_level_dsl.rs @@ -122,6 +122,14 @@ pub enum TypeShapeDslInputDomain { NamedInts, } +/// The domain accepted by scalar and sequence narrowing operations. Boolean conditions are +/// validated separately against `Flag[bool]`. +pub fn type_shape_dsl_narrowable_flag_domain() -> FlagDomain { + FlagDomain::of(FlagMember::Int) + .join(FlagDomain::of(FlagMember::IntTuple)) + .join(FlagDomain::of(FlagMember::NoneType)) +} + impl TypeShapeDslInputDomain { /// Whether a parameter in this domain may be used as a value in `result` after narrowing. pub fn can_use_as( @@ -135,6 +143,44 @@ impl TypeShapeDslInputDomain { && narrowing.proves_not_none()) } + /// Which integer domain a parameter in this domain belongs to after `narrowing`, if any. + pub fn integer_domain( + self, + narrowing: TypeShapeDslParameterNarrowing, + ) -> Option { + if self.can_use_as(TypeShapeDslDomain::Int, narrowing) { + return Some(DslIntegerDomain::Dimension); + } + let Self::Flag(domain) = self else { + return None; + }; + let is_flag_int = if narrowing == TypeShapeDslParameterNarrowing::Integer { + // Any domain accepted by `dsl.is_int_value(...)` (`int | tuple[int, ...] | None` or a + // subset like `tuple[int, ...] | None`) may be used as `Flag[int]` inside the narrowed + // branch; if `int` wasn't in the declared union, that branch is simply dead at runtime. + domain.is_subset_of(type_shape_dsl_narrowable_flag_domain()) + } else { + narrowing != TypeShapeDslParameterNarrowing::IsNone + && domain == FlagDomain::of(FlagMember::Int) + }; + is_flag_int.then_some(DslIntegerDomain::Flag) + } + + fn shared_integer_domain( + parameter_domains: &[Self], + uses: &[TypeShapeDslParameterUse], + ) -> Option { + let [first, rest @ ..] = uses else { + return None; + }; + let domain = parameter_domains[first.parameter].integer_domain(first.narrowing)?; + rest.iter() + .all(|use_| { + parameter_domains[use_.parameter].integer_domain(use_.narrowing) == Some(domain) + }) + .then_some(domain) + } + /// Whether a value from this domain can be passed to a helper parameter in `expected`. fn can_forward_to(self, expected: Self) -> bool { match (self, expected) { @@ -1717,6 +1763,25 @@ impl DslStaticKind { } } + /// Parameter uses for a bare or narrowed parameter value that can join with a dimension or + /// `Flag[int]` integer expression once parameter annotations are checked. + fn integer_parameter_uses(&self) -> Option> { + match self { + Self::UnknownParameters(parameters) => Some(parameter_uses( + parameters, + TypeShapeDslParameterNarrowing::Unnarrowed, + )), + Self::ValueSet { sources, kinds } + if !sources.has_non_parameter_values() + && kinds & FLAG_NONE == 0 + && kinds & FLAG_INT != 0 => + { + sources.parameter_uses() + } + _ => None, + } + } + fn join(self, other: Self) -> Option { match (self, other) { (left, right) if left == right => Some(left), @@ -3843,8 +3908,12 @@ impl<'a, F: Fn(&Expr) -> Option> DslValidator<'a, F> { let args = &call.arguments.args; let parameter_origins = self.validate_int_tuple_expression(&args[0], flow)?; let axis = self.slot(&args[1], flow)?; + if let DslStaticKind::DeferredInteger(index) = flow.kinds[axis] { + self.resolve_deferred_integer(index, DslIntegerDomain::Flag)?; + } let axis_parameters = match &flow.kinds[axis] { kind if kind.unnarrowed_parameter_origins().is_some() => kind.parameter_uses(), + DslStaticKind::DeferredInteger(_) => None, DslStaticKind::ValueSet { kinds, .. } if *kinds & !(FLAG_INT | FLAG_SEQUENCE | FLAG_NONE) == 0 => { @@ -4119,7 +4188,7 @@ impl<'a, F: Fn(&Expr) -> Option> DslValidator<'a, F> { let (when_true, when_false) = self.validate_condition(&if_expr.test, flow)?; let when_true = self.validate_assignment_value(&if_expr.body, &when_true)?; let when_false = self.validate_assignment_value(&if_expr.orelse, &when_false)?; - let Some(kind) = when_true.join(when_false) else { + let Some(kind) = self.join_value_kinds(when_true, when_false)? else { return Err(TypeShapeDslDefinitionError { range: if_expr.range, message: "conditional expression branches must have the same value domain", @@ -4556,8 +4625,9 @@ impl<'a, F: Fn(&Expr) -> Option> DslValidator<'a, F> { DslStaticKind::ValueSet { sources, kinds: FLAG_INT, - } if sources.all_prove(TypeShapeDslParameterNarrowing::Integer) - && sources.non_parameter_values_are(FLAG_INT) => + } if sources.non_parameter_values_are(FLAG_INT) + && (sources.all_prove(TypeShapeDslParameterNarrowing::Integer) + || sources.has_non_parameter_values()) => { Some(DeferredIntegerClassification { default_domain: DslIntegerDomain::Flag, @@ -4566,8 +4636,11 @@ impl<'a, F: Fn(&Expr) -> Option> DslValidator<'a, F> { } DslStaticKind::ValueSet { sources, kinds } if kinds & FLAG_NONE == 0 - && sources.all_prove(TypeShapeDslParameterNarrowing::NonNone) - && sources.non_parameter_values_are(FLAG_INT) => + && ((sources + .all_prove(TypeShapeDslParameterNarrowing::NonNone) + && sources.non_parameter_values_are(FLAG_INT)) + || (!sources.has_non_parameter_values() + && kinds & FLAG_INT != 0)) => { Some(DeferredIntegerClassification { default_domain: DslIntegerDomain::Dimension, @@ -5505,6 +5578,110 @@ impl<'a, F: Fn(&Expr) -> Option> DslValidator<'a, F> { Ok(()) } + fn join_value_kinds( + &mut self, + left: DslStaticKind, + right: DslStaticKind, + ) -> Result, TypeShapeDslDefinitionError> { + Ok(match (left, right) { + (DslStaticKind::DeferredInteger(left), DslStaticKind::DeferredInteger(right)) => Some( + DslStaticKind::DeferredInteger(self.merge_deferred_integers(left, right)?), + ), + (DslStaticKind::DeferredInteger(index), DslStaticKind::Dimension) + | (DslStaticKind::Dimension, DslStaticKind::DeferredInteger(index)) => { + self.resolve_deferred_integer(index, DslIntegerDomain::Dimension)?; + Some(DslStaticKind::Dimension) + } + (DslStaticKind::Dimension, other) | (other, DslStaticKind::Dimension) + if let Some(uses) = other.integer_parameter_uses() => + { + // Syntax validation runs with `parameter_domains == None` before annotations are + // resolved; revalidation with `Some(domains)` checks that the parameter actually + // belongs to the dimension domain. + self.parameter_domains + .is_none_or(|domains| { + TypeShapeDslInputDomain::shared_integer_domain(domains, &uses) + == Some(DslIntegerDomain::Dimension) + }) + .then_some(DslStaticKind::Dimension) + } + (DslStaticKind::DeferredInteger(index), other) + | (other, DslStaticKind::DeferredInteger(index)) + if let Some(uses) = other.integer_parameter_uses() => + { + let kind_for_domain = |domain, uses| match domain { + DslIntegerDomain::Dimension => DslStaticKind::Dimension, + // Retain `parameter_uses` alongside `non_parameter_kinds: FLAG_INT` so the + // merged Flag local stays input-only and cannot be used as a literal dimension. + DslIntegerDomain::Flag => DslStaticKind::ValueSet { + sources: DslValueSources { + parameter_uses: uses, + non_parameter_kinds: FLAG_INT, + }, + kinds: FLAG_INT, + }, + }; + // `previous` is `Some` when an earlier use already locked the deferred integer's + // domain before this branch merge. + let (root, previous) = self.deferred_integer_domain(index); + match self.parameter_domains { + None => Some(match previous { + None => DslStaticKind::DeferredInteger(root), + Some(domain) => kind_for_domain(domain, uses), + }), + Some(domains) => { + match TypeShapeDslInputDomain::shared_integer_domain(domains, &uses) { + Some(domain) if previous.is_none_or(|prev| prev == domain) => { + self.resolve_deferred_integer(index, domain)?; + Some(kind_for_domain(domain, uses)) + } + _ => None, + } + } + } + } + ( + DslStaticKind::DeferredInteger(index), + flag @ DslStaticKind::ValueSet { + kinds: FLAG_INT, .. + }, + ) + | ( + flag @ DslStaticKind::ValueSet { + kinds: FLAG_INT, .. + }, + DslStaticKind::DeferredInteger(index), + ) => { + self.resolve_deferred_integer(index, DslIntegerDomain::Flag)?; + Some(flag) + } + (left, right) => { + left.join(right) + .filter(|joined| match (joined, self.parameter_domains) { + // A `ValueSet` cannot mix `Flag` parameters with `Int` / `Int | None` (or + // shape) parameters; rejecting mixed sources here keeps 3+ branch merges + // independent of branch order. + (DslStaticKind::ValueSet { sources, .. }, Some(domains)) => { + let all_flag = sources.parameter_uses.iter().all(|use_| { + matches!(domains[use_.parameter], TypeShapeDslInputDomain::Flag(_)) + }); + let all_int_like = + sources.non_parameter_kinds & !(FLAG_INT | FLAG_NONE) == 0 + && sources.parameter_uses.iter().all(|use_| { + matches!( + domains[use_.parameter], + TypeShapeDslInputDomain::Value(TypeShapeDslDomain::Int) + | TypeShapeDslInputDomain::OptionalInt + ) + }); + all_flag || all_int_like + } + _ => true, + }) + } + }) + } + fn merge_flows( &mut self, flows: Vec, @@ -5523,38 +5700,12 @@ impl<'a, F: Fn(&Expr) -> Option> DslValidator<'a, F> { if result.assigned[slot] { let left = result.kinds[slot].clone(); let right = flow.kinds[slot].clone(); - let kind = match (left, right) { - ( - DslStaticKind::DeferredInteger(left), - DslStaticKind::DeferredInteger(right), - ) => DslStaticKind::DeferredInteger( - self.merge_deferred_integers(left, right)?, - ), - (DslStaticKind::DeferredInteger(index), DslStaticKind::Dimension) - | (DslStaticKind::Dimension, DslStaticKind::DeferredInteger(index)) => { - self.resolve_deferred_integer(index, DslIntegerDomain::Dimension)?; - DslStaticKind::Dimension - } - ( - DslStaticKind::DeferredInteger(index), - flag @ DslStaticKind::ValueSet { - kinds: FLAG_INT, .. - }, - ) - | ( - flag @ DslStaticKind::ValueSet { - kinds: FLAG_INT, .. - }, - DslStaticKind::DeferredInteger(index), - ) => { - self.resolve_deferred_integer(index, DslIntegerDomain::Flag)?; - flag - } - (left, right) => left.join(right).ok_or(TypeShapeDslDefinitionError { + let kind = self.join_value_kinds(left, right)?.ok_or( + TypeShapeDslDefinitionError { range, message: "all continuing branch assignments to a local must have the same value domain", - })?, - }; + }, + )?; result.kinds[slot] = kind; } } @@ -9212,11 +9363,7 @@ mod tests { }; let int = TypeShapeDslInputDomain::Value(TypeShapeDslDomain::Int); let optional_int = TypeShapeDslInputDomain::OptionalInt; - let broad_flag = TypeShapeDslInputDomain::Flag( - FlagDomain::of(FlagMember::Int) - .join(FlagDomain::of(FlagMember::IntTuple)) - .join(FlagDomain::of(FlagMember::NoneType)), - ); + let broad_flag = TypeShapeDslInputDomain::Flag(type_shape_dsl_narrowable_flag_domain()); for actual in [optional_int, broad_flag] { assert_eq!( diff --git a/pyrefly/lib/alt/type_level_dsl.rs b/pyrefly/lib/alt/type_level_dsl.rs index ab403f9e6a..3003a451d6 100644 --- a/pyrefly/lib/alt/type_level_dsl.rs +++ b/pyrefly/lib/alt/type_level_dsl.rs @@ -19,6 +19,7 @@ use pyrefly_types::known_class::KnownClass; use pyrefly_types::quantified::QuantifiedKind; use pyrefly_types::quantified::TypeVarLike; use pyrefly_types::shaped_array::is_int_tuples_type; +use pyrefly_types::type_level_dsl::DslIntegerDomain; use pyrefly_types::type_level_dsl::ParsedTypeShapeDslFunction; use pyrefly_types::type_level_dsl::ResolvedTypeShapeDslFunction; use pyrefly_types::type_level_dsl::StructurallyValidatedTypeShapeDslFunction; @@ -37,6 +38,7 @@ use pyrefly_types::type_level_dsl::TypeShapeDslParameterUse; use pyrefly_types::type_level_dsl::TypeShapeDslProgramError; use pyrefly_types::type_level_dsl::TypeShapeDslReturnKind; use pyrefly_types::type_level_dsl::TypeShapeDslSlotReturnKind; +use pyrefly_types::type_level_dsl::type_shape_dsl_narrowable_flag_domain; use pyrefly_types::type_var::FlagDomain; use pyrefly_types::type_var::FlagMember; use pyrefly_types::type_var::Restriction; @@ -70,14 +72,6 @@ impl TypeFormContext<'_> { } } -/// The domain accepted by scalar and sequence narrowing operations. Boolean conditions are -/// validated separately against `Flag[bool]`. -fn type_shape_dsl_narrowable_flag_domain() -> FlagDomain { - FlagDomain::of(FlagMember::Int) - .join(FlagDomain::of(FlagMember::IntTuple)) - .join(FlagDomain::of(FlagMember::NoneType)) -} - fn type_shape_dsl_representable_flag_domain() -> FlagDomain { type_shape_dsl_narrowable_flag_domain() .join(FlagDomain::of(FlagMember::Bool)) @@ -131,12 +125,8 @@ fn type_shape_dsl_comparison_domain( .is_none_or(|domain| domain == FlagDomain::of(FlagMember::Int)) && uses.is_some_and(|uses| { uses.iter().all(|use_| { - matches!(parameter_domains[use_.parameter()], TypeShapeDslInputDomain::Flag(domain) - if (domain == FlagDomain::of(FlagMember::Int) - && use_.narrowing() == TypeShapeDslParameterNarrowing::Unnarrowed) - || (use_.narrowing() == TypeShapeDslParameterNarrowing::Integer - && domain.contains(FlagMember::Int) - && domain.is_subset_of(type_shape_dsl_narrowable_flag_domain()))) + parameter_domains[use_.parameter()].integer_domain(use_.narrowing()) + == Some(DslIntegerDomain::Flag) }) }) }; @@ -544,20 +534,9 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { .. } => { let valid = uses.iter().all(|use_| { - let narrowing = use_.narrowing(); parameter_domains[use_.parameter()] - .can_use_as(TypeShapeDslDomain::Int, narrowing) - || match parameter_domains[use_.parameter()] { - TypeShapeDslInputDomain::Flag(domain) - if narrowing == TypeShapeDslParameterNarrowing::Integer => - { - domain.is_subset_of(type_shape_dsl_narrowable_flag_domain()) - } - TypeShapeDslInputDomain::Flag(domain) => { - domain == FlagDomain::of(FlagMember::Int) - } - _ => false, - } + .integer_domain(use_.narrowing()) + .is_some() }); (!valid).then_some("`@shape_transform` dimension arithmetic operands must be annotated as `Int`, as `Int | None` narrowed to exclude `None`, or as a compatible integer Flag") } @@ -706,33 +685,32 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { .. } => { let valid = uses.iter().all(|use_| { - match parameter_domains[use_.parameter()] { - TypeShapeDslInputDomain::Flag(domain) => match required { - TypeShapeDslFlagValueKind::Int - if use_.narrowing() - == TypeShapeDslParameterNarrowing::Integer => - { - domain.is_subset_of(type_shape_dsl_narrowable_flag_domain()) - } - TypeShapeDslFlagValueKind::Int => { - domain == FlagDomain::of(FlagMember::Int) - } - TypeShapeDslFlagValueKind::String => { - domain.contains(FlagMember::Str) - && domain.is_subset_of(type_shape_dsl_optional_flag_domain( - FlagMember::Str, - )) - } - TypeShapeDslFlagValueKind::Sequence - if use_.narrowing() - != TypeShapeDslParameterNarrowing::Unnarrowed => - { - domain.is_subset_of(type_shape_dsl_narrowable_flag_domain()) - } - TypeShapeDslFlagValueKind::Sequence => { - domain.is_subset_of(FlagDomain::of(FlagMember::IntTuple)) - } - }, + match (required, parameter_domains[use_.parameter()]) { + (TypeShapeDslFlagValueKind::Int, input_domain) => { + input_domain.integer_domain(use_.narrowing()) + == Some(DslIntegerDomain::Flag) + } + ( + TypeShapeDslFlagValueKind::String, + TypeShapeDslInputDomain::Flag(domain), + ) => { + domain.contains(FlagMember::Str) + && domain.is_subset_of( + type_shape_dsl_optional_flag_domain(FlagMember::Str), + ) + } + ( + TypeShapeDslFlagValueKind::Sequence, + TypeShapeDslInputDomain::Flag(domain), + ) if use_.narrowing() + != TypeShapeDslParameterNarrowing::Unnarrowed => + { + domain.is_subset_of(type_shape_dsl_narrowable_flag_domain()) + } + ( + TypeShapeDslFlagValueKind::Sequence, + TypeShapeDslInputDomain::Flag(domain), + ) => domain.is_subset_of(FlagDomain::of(FlagMember::IntTuple)), _ => false, } }); diff --git a/pyrefly/lib/test/shape_dsl.rs b/pyrefly/lib/test/shape_dsl.rs index 6670503f0f..9e7f3c2c27 100644 --- a/pyrefly/lib/test/shape_dsl.rs +++ b/pyrefly/lib/test/shape_dsl.rs @@ -14382,3 +14382,394 @@ def g(x): pass "#, ); + +testcase!( + test_type_shape_dsl_integer_branch_and_conditional_joins, + shape_extensions_env(), + r#" +from typing import assert_type +import shape_extensions.dsl as dsl +from shape_extensions import Elements, Flag, Int, IntTuple, IntVar, type_shape_dsl_function + +class Array[Shape: IntTuple]: ... +class DimBox[D: Int]: ... + +@type_shape_dsl_function +def flag_slice_helper(shape: IntTuple, axis: int) -> IntTuple: + return shape[:axis] + +@type_shape_dsl_function +def optional_flag_helper(shape: IntTuple, axis: int | None) -> IntTuple: + if dsl.is_int_value(axis): + return shape[:axis] + return shape + +@type_shape_dsl_function +def int_helper(d: Int) -> Int: + return d + 1 + +@type_shape_dsl_function +def optional_int_helper(d: Int | None, fallback: Int) -> Int: + if d is None: + return fallback + return d + 1 + +@type_shape_dsl_function +def expand_dims_if_else(shape: IntTuple, axis: int) -> IntTuple: + output_rank = len(shape) + 1 + if axis < 0 - output_rank or axis >= output_rank: + return dsl.Invalid("expand_dims axis out of bounds") + if axis < 0: + norm_axis = axis + output_rank + else: + norm_axis = axis + return dsl.IntTuple( + ( + 1 + if index == norm_axis + else shape[index if index < norm_axis else index - 1] + for index in range(output_rank) + ) + ) + +@type_shape_dsl_function +def expand_dims_conditional(shape: IntTuple, axis: int | tuple[int, ...] | None) -> IntTuple: + if dsl.is_int_value(axis): + output_rank = len(shape) + 1 + norm_axis = axis + output_rank if axis < 0 else axis + if norm_axis < 0 or norm_axis >= output_rank: + return dsl.Invalid("expand_dims axis out of bounds") + return dsl.concat(dsl.concat(shape[:norm_axis], dsl.IntTuple((1,))), shape[norm_axis:]) + return dsl.IntTuple.gradual() + +@type_shape_dsl_function +def reduce_normalized_axis(shape: IntTuple, axis: int) -> IntTuple: + rank = len(shape) + norm_axis = axis if axis >= 0 else axis + rank + if norm_axis < 0 or norm_axis >= rank: + return dsl.Invalid("axis out of bounds") + return dsl.axis_reduce(shape, norm_axis, False, False, False, False) + +@type_shape_dsl_function +def forward_normalized_flag(shape: IntTuple, axis: int, use_optional: bool) -> IntTuple: + if axis < 0: + norm_axis = axis + len(shape) + else: + norm_axis = axis + if use_optional: + return optional_flag_helper(shape, norm_axis) + return flag_slice_helper(shape, norm_axis) + +@type_shape_dsl_function +def choose_flag_arithmetic_or_literal(shape: IntTuple, axis: int) -> IntTuple: + norm_axis = axis + len(shape) if axis < 0 else 0 + return shape[norm_axis:] + +@type_shape_dsl_function +def offset_after_normalize(shape: IntTuple, axis: int, via_helper: bool) -> Int: + norm_axis = axis + len(shape) if axis < 0 else axis + out = norm_axis + 1 + if via_helper: + return int_helper(out) + return out + +@type_shape_dsl_function +def pad_or_keep(d: Int, shape: IntTuple, mode: int) -> Int: + if mode == 0: + out = shape[0] + elif mode == 1: + out = d + else: + out = d + 2 + return out + +@type_shape_dsl_function +def pad_or_keep_conditional(d: Int, pad: bool) -> Int: + out = d + 2 if pad else d + return out + +@type_shape_dsl_function +def forward_merged_dim(d: Int, pad: bool, use_optional: bool) -> Int: + if pad: + out = d + 2 + else: + out = d + if use_optional: + return optional_int_helper(out, d) + return int_helper(out) + +@type_shape_dsl_function +def optional_extent(shape: IntTuple, n: Int | None) -> Int: + extent = 2 * (shape[-1] - 1) if n is None else n + return extent + +@type_shape_dsl_function +def concrete_branch_dimension(d: Int, n: Int | None) -> IntTuple: + if dsl.is_concrete_int(d): + first = d + else: + first = d + 1 + if dsl.is_concrete_int(n): + second = n + else: + second = d + 2 + return dsl.IntTuple((first, second)) + +@type_shape_dsl_function +def conditional_both_deferred(a: Int, b: Int, choose: bool) -> Int: + out = (a + 1) if choose else (b + 2) + return out + +@type_shape_dsl_function +def dead_is_int_value_branch(shape: IntTuple, axes: tuple[int, ...] | None) -> IntTuple: + if dsl.is_int_value(axes): + norm = axes + len(shape) if axes < 0 else axes + return shape[:norm] + return shape + +@type_shape_dsl_function +def redundant_not_none_flag_int(shape: IntTuple, axis: int) -> IntTuple: + if axis is not None: + if axis < 0: + return shape[1:] + return shape[:1] + return shape + +def apply_expand_if_else[S: IntTuple, A: Flag[int]](x: Array[S], axis: A) -> Array[expand_dims_if_else(S, A)]: ... +def apply_expand_conditional[S: IntTuple, A: Flag[int | tuple[int, ...] | None]](x: Array[S], axis: A = None) -> Array[expand_dims_conditional(S, A)]: ... +def apply_reduce[S: IntTuple, A: Flag[int]](x: Array[S], axis: A) -> Array[reduce_normalized_axis(S, A)]: ... +def apply_forward_flag[S: IntTuple, A: Flag[int], Opt: Flag[bool]](x: Array[S], axis: A, use_optional: Opt) -> Array[forward_normalized_flag(S, A, Opt)]: ... +def apply_flag_or_literal[S: IntTuple, A: Flag[int]](x: Array[S], axis: A) -> Array[choose_flag_arithmetic_or_literal(S, A)]: ... +def apply_offset_after_norm[S: IntTuple, A: Flag[int], H: Flag[bool]](x: Array[S], axis: A, via_helper: H) -> DimBox[offset_after_normalize(S, A, H)]: ... +def apply_pad_or_keep[D: Int, S: IntTuple, M: Flag[int]](d: DimBox[D], x: Array[S], mode: M) -> DimBox[pad_or_keep(D, S, M)]: ... +def apply_pad_conditional[D: Int, P: Flag[bool]](d: DimBox[D], pad: P) -> DimBox[pad_or_keep_conditional(D, P)]: ... +def apply_forward_dim[D: Int, P: Flag[bool], Opt: Flag[bool]](d: DimBox[D], pad: P, use_optional: Opt) -> DimBox[forward_merged_dim(D, P, Opt)]: ... +def apply_optional_extent[S: IntTuple, N: Int | None = None](x: Array[S], n: N = None) -> DimBox[optional_extent(S, N)]: ... +def apply_optional_symbolic[S: IntTuple, N: IntVar](x: Array[S], n: DimBox[Int[N]]) -> DimBox[optional_extent(S, Int[N])]: ... +def apply_concrete_branch[D: Int, N: Int | None = None](d: DimBox[D], n: N = None) -> Array[concrete_branch_dimension(D, N)]: ... +def apply_both_deferred[A: Int, B: Int, C: Flag[bool]](a: DimBox[A], b: DimBox[B], choose: C) -> DimBox[conditional_both_deferred(A, B, C)]: ... +def apply_dead_is_int[S: IntTuple, A: Flag[tuple[int, ...] | None]](x: Array[S], axes: A = None) -> Array[dead_is_int_value_branch(S, A)]: ... +def apply_redundant_not_none[S: IntTuple, A: Flag[int]](x: Array[S], axis: A) -> Array[redundant_not_none_flag_int(S, A)]: ... + +def check[N: IntVar, M: IntVar, Mid: IntTuple]( + x: Array[IntTuple[3, 5]], + sym_x: Array[IntTuple[N, *Elements[Mid], 5]], + d4: DimBox[Int[4]], + dn: DimBox[Int[N]], + dm: DimBox[Int[M]], + d_gradual: DimBox[Int], + broad_axis: int, + broad_axes: tuple[int, ...], + broad_bool: bool, +) -> None: + assert_type(apply_expand_if_else(x, 1), Array[IntTuple[3, 1, 5]]) + assert_type(apply_expand_if_else(x, -1), Array[IntTuple[3, 5, 1]]) + assert_type(apply_expand_if_else(x, broad_axis), Array[IntTuple]) + apply_expand_if_else(x, 3) # E: expand_dims axis out of bounds + + assert_type(apply_expand_conditional(x, 1), Array[IntTuple[3, 1, 5]]) + assert_type(apply_expand_conditional(x, -1), Array[IntTuple[3, 5, 1]]) + assert_type(apply_expand_conditional(x, (0, 1)), Array[IntTuple]) + assert_type(apply_expand_conditional(x), Array[IntTuple]) + + assert_type(apply_reduce(x, 0), Array[IntTuple[5]]) + assert_type(apply_reduce(x, -1), Array[IntTuple[3]]) + apply_reduce(x, 2) # E: axis out of bounds + + assert_type(apply_forward_flag(x, -1, False), Array[IntTuple[3]]) + assert_type(apply_forward_flag(x, 1, True), Array[IntTuple[3]]) + assert_type(apply_flag_or_literal(x, -1), Array[IntTuple[5]]) + assert_type(apply_flag_or_literal(x, 1), Array[IntTuple[3, 5]]) + assert_type(apply_offset_after_norm(x, -1, False), DimBox[Int[2]]) + assert_type(apply_offset_after_norm(x, 0, True), DimBox[Int[2]]) + + assert_type(apply_pad_or_keep(dn, x, 0), DimBox[Int[3]]) + assert_type(apply_pad_or_keep(d4, x, 1), DimBox[Int[4]]) + assert_type(apply_pad_or_keep(dn, x, 1), DimBox[Int[N]]) + assert_type(apply_pad_or_keep(dn, x, 2), DimBox[Int[N + 2]]) + assert_type(apply_pad_or_keep(d_gradual, x, 1), DimBox[Int]) + + assert_type(apply_pad_conditional(dn, True), DimBox[Int[N + 2]]) + assert_type(apply_pad_conditional(dn, False), DimBox[Int[N]]) + assert_type(apply_pad_conditional(dn, broad_bool), DimBox[Int]) + + assert_type(apply_forward_dim(d4, True, False), DimBox[Int[7]]) + assert_type(apply_forward_dim(dn, False, True), DimBox[Int[N + 1]]) + + assert_type(apply_optional_extent(x), DimBox[Int[8]]) + assert_type(apply_optional_extent(sym_x), DimBox[Int[8]]) + assert_type(apply_optional_extent(x, 9), DimBox[Int[9]]) + assert_type(apply_optional_symbolic(x, dn), DimBox[Int[N]]) + + assert_type(apply_concrete_branch(d4, 7), Array[IntTuple[4, 7]]) + assert_type(apply_concrete_branch(dn, None), Array[IntTuple[N + 1, N + 2]]) + + assert_type(apply_both_deferred(dn, dm, True), DimBox[Int[N + 1]]) + assert_type(apply_both_deferred(dn, dm, False), DimBox[Int[M + 2]]) + + assert_type(apply_dead_is_int(x, (0,)), Array[IntTuple[3, 5]]) + assert_type(apply_dead_is_int(x), Array[IntTuple[3, 5]]) + assert_type(apply_dead_is_int(x, broad_axes), Array[IntTuple]) + + assert_type(apply_redundant_not_none(x, 1), Array[IntTuple[3]]) + assert_type(apply_redundant_not_none(x, -1), Array[IntTuple[5]]) + assert_type(apply_redundant_not_none(x, broad_axis), Array[IntTuple]) +"#, +); + +testcase!( + test_type_shape_dsl_invalid_integer_branch_and_conditional_joins, + shape_extensions_env(), + r#" +import shape_extensions.dsl as dsl +from shape_extensions import Int, IntTuple, type_shape_dsl_function + +@type_shape_dsl_function +def int_helper(d: Int) -> Int: + return d + +@type_shape_dsl_function +def flag_helper(shape: IntTuple, axis: int) -> IntTuple: + return shape[:axis] + +@type_shape_dsl_function +def unused_dimension_and_flag(shape: IntTuple, axis: int, choose: bool) -> IntTuple: + if choose: # E: all continuing branch assignments to a local must have the same value domain + mixed = shape[0] + else: + mixed = axis + return shape + +@type_shape_dsl_function +def conditional_dimension_and_flag(shape: IntTuple, axis: int, choose: bool) -> IntTuple: + mixed = shape[0] if choose else axis # E: conditional expression branches must have the same value domain + return shape + +@type_shape_dsl_function +def unused_deferred_dim_and_flag(d: Int, axis: int, choose: bool) -> Int: + if choose: + mixed = d + 1 # E: Flag operation requires a compatible Flag parameter + else: + mixed = axis + return d + +@type_shape_dsl_function +def mixed_parameter_alias_then_deferred(d: Int, axis: int, c1: bool, c2: bool) -> Int: + picked = d if c1 else axis + out = (d + 1) if c2 else picked # E: conditional expression branches must have the same value domain + return d + +@type_shape_dsl_function +def preresolved_dimension_and_flag(shape: IntTuple, axis: int, choose: bool) -> IntTuple: + rank = len(shape) + _ = dsl.IntTuple((rank,)) + if choose: # E: all continuing branch assignments to a local must have the same value domain + mixed = rank + else: + mixed = axis + return shape + +@type_shape_dsl_function +def preresolved_flag_and_dimension(shape: IntTuple, d: Int, choose: bool) -> IntTuple: + rank = len(shape) + _ = shape[rank] + if choose: # E: all continuing branch assignments to a local must have the same value domain + mixed = rank + else: + mixed = d + return shape + +@type_shape_dsl_function +def return_merged_flag_local(axis: int, choose: bool) -> Int: + if choose: + norm = axis + 1 + else: + norm = axis + return norm # E: Flag values are input-only and cannot be returned + +@type_shape_dsl_function +def tuple_element_merged_flag_local(axis: int, choose: bool) -> IntTuple: + if choose: + norm = axis + 1 + else: + norm = axis + return dsl.IntTuple((norm,)) # E: IntTuple elements must be annotated as `Int` + +@type_shape_dsl_function +def forward_merged_flag_to_int_helper(axis: int, choose: bool) -> Int: + if choose: + norm = axis + 1 + else: + norm = axis + return int_helper(norm) # E: DSL helper argument domains are incompatible + +@type_shape_dsl_function +def axis_reduce_with_merged_dimension(shape: IntTuple, d: Int, choose: bool) -> IntTuple: + if choose: + extent = d + 1 + else: + extent = d + return dsl.axis_reduce(shape, extent, False, False, False, False) # E: `dsl.axis_reduce` axis must be a Flag integer, tuple, or None + +@type_shape_dsl_function +def forward_merged_dimension_to_flag_helper(shape: IntTuple, d: Int, choose: bool) -> IntTuple: + if choose: + extent = d + 1 + else: + extent = d + return flag_helper(shape, extent) # E: DSL helper argument domains are incompatible + +@type_shape_dsl_function +def unnarrowed_optional_int_branch(d: Int, n: Int | None, choose: bool) -> Int: + if choose: # E: all continuing branch assignments to a local must have the same value domain + out = d + 1 + else: + out = n + return d + +@type_shape_dsl_function +def unnarrowed_optional_flag_conditional(axis: int, opt: int | None, choose: bool) -> IntTuple: + out = axis + 1 if choose else opt # E: conditional expression branches must have the same value domain + return dsl.IntTuple(()) + +@type_shape_dsl_function +def non_integer_flag_branch(d: Int, label: str, choose: bool) -> Int: + if choose: # E: all continuing branch assignments to a local must have the same value domain + out = d + 1 + else: + out = label + return d + +@type_shape_dsl_function +def is_none_narrowed_flag_int_local(d: Int, p: int, choose: bool) -> Int: + x = p if choose else 1 + if x is None: + return d + x # E: dimension arithmetic operands must be annotated as `Int`, as `Int | None` narrowed to exclude `None`, or as a compatible integer Flag + return d + +@type_shape_dsl_function +def flag_branches_then_dimension(shape: IntTuple, d: Int, e: int, c1: bool, c2: bool) -> IntTuple: + if c1: # E: all continuing branch assignments to a local must have the same value domain + out = e + 1 + elif c2: + out = e + else: + out = d + return shape + +@type_shape_dsl_function +def dimension_then_flag_branches(shape: IntTuple, d: Int, e: int, c1: bool, c2: bool) -> IntTuple: + if not c1 and not c2: # E: all continuing branch assignments to a local must have the same value domain + out = d + elif c1: + out = e + 1 + else: + out = e + return shape + +@type_shape_dsl_function +def bool_literal_and_dimension(shape: IntTuple, d: Int, choose: bool) -> IntTuple: + out = True if choose else d # E: conditional expression branches must have the same value domain + return shape +"#, +); diff --git a/website/docs/tensor-shapes.mdx b/website/docs/tensor-shapes.mdx index b60b7b256b..d528f26351 100644 --- a/website/docs/tensor-shapes.mdx +++ b/website/docs/tensor-shapes.mdx @@ -153,7 +153,7 @@ def expand_dims_shape(shape: IntTuple, axis: int) -> IntTuple: if axis < 0: norm_axis = axis + output_rank else: - norm_axis = axis + 0 + norm_axis = axis return dsl.IntTuple( ( 1