Skip to content

Commit

Permalink
Combine cases of Tuple.Zip disjoint from (h1 *: t1, h2 *: t2) (#2…
Browse files Browse the repository at this point in the history
…1287)

If we reach the second case of `Zip[T1 <: Tuple, T2 <: Tuple]`, then we
know `(T1, T2)` is disjoint from `(NonEmptyTuple, NonEmptyTuple)`, from
which we can conclude at least one of the two is an `EmptyTuple`.

Addressing #19175
  • Loading branch information
EugeneFlesselle authored Nov 18, 2024
2 parents 6a7d5d3 + 14bf406 commit 43d74c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
9 changes: 3 additions & 6 deletions library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,12 @@ object Tuple {
}

/** Given two tuples, `A1 *: ... *: An * At` and `B1 *: ... *: Bn *: Bt`
* where at least one of `At` or `Bt` is `EmptyTuple` or `Tuple`,
* returns the tuple type `(A1, B1) *: ... *: (An, Bn) *: Ct`
* where `Ct` is `EmptyTuple` if `At` or `Bt` is `EmptyTuple`, otherwise `Ct` is `Tuple`.
* where at least one of `At` or `Bt` is `EmptyTuple`,
* returns the tuple type `(A1, B1) *: ... *: (An, Bn) *: EmptyTuple`.
*/
type Zip[T1 <: Tuple, T2 <: Tuple] <: Tuple = (T1, T2) match {
case (h1 *: t1, h2 *: t2) => (h1, h2) *: Zip[t1, t2]
case (EmptyTuple, _) => EmptyTuple
case (_, EmptyTuple) => EmptyTuple
case _ => Tuple
case _ => EmptyTuple
}

/** Converts a tuple `(F[T1], ..., F[Tn])` to `(T1, ... Tn)` */
Expand Down
20 changes: 20 additions & 0 deletions tests/pos/tuple-zip.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

import scala.Tuple.Zip

type A
type B
type C

def Test =

summon[Zip[A *: B *: C *: EmptyTuple, C *: B *: A *: EmptyTuple] =:= (A, C) *: (B, B) *: (C, A) *: EmptyTuple]

summon[Zip[A *: B *: EmptyTuple, C *: B *: A *: EmptyTuple] =:= (A, C) *: (B, B) *: EmptyTuple]
summon[Zip[A *: B *: C *: EmptyTuple, C *: B *: EmptyTuple] =:= (A, C) *: (B, B) *: EmptyTuple]

summon[Zip[A *: B *: C *: Tuple, C *: B *: A *: Tuple] =:= (A, C) *: (B, B) *: (C, A) *: Zip[Tuple, Tuple]]
summon[Zip[A *: B *: C *: Tuple, C *: B *: A *: Tuple] <:< (A, C) *: (B, B) *: (C, A) *: Tuple]

summon[Zip[A *: B *: Tuple, C *: B *: A *: Tuple] =:= (A, C) *: (B, B) *: Zip[Tuple, A *: Tuple]]
summon[Zip[A *: B *: NonEmptyTuple, C *: B *: A *: Tuple] =:= (A, C) *: (B, B) *: Zip[NonEmptyTuple, A *: Tuple]]
summon[Zip[A *: B *: EmptyTuple, C *: B *: A *: Tuple] =:= (A, C) *: (B, B) *: EmptyTuple]

0 comments on commit 43d74c8

Please sign in to comment.