Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't reduce match types inside a type alias #13633

Closed
Odomontois opened this issue Sep 29, 2021 · 1 comment · Fixed by #13635
Closed

Can't reduce match types inside a type alias #13633

Odomontois opened this issue Sep 29, 2021 · 1 comment · Fixed by #13635

Comments

@Odomontois
Copy link

Compiler version

"3.1.0-RC1"

Minimized code

I'm very sorry, I couldn't minimize it further
The important part are definitions of Sum and Sum1. Sum contains a nearly meaningless match just to get things working, technically it should be equivalent to Sum1, also manual unfold of Plus1 definition works as well.

import scala.compiletime.{constValueTuple, constValue}

object Sums extends App:

  println(constValueTuple[Plus[(true, true, true), (true, true)]]) // works
  println(constValueTuple[Plus1[(true, true, true), (true, true)]]) // fails
  println(constValueTuple[
    Reverse[PlusLoop[Reverse[(true, true, true)], Reverse[(true, true)], false]]]
    ) // also works despite it's just an unfold of `Plus1` application

  type Plus[A <: Tuple, B <: Tuple] <: Tuple = (A, B) match
    case (EmptyTuple, EmptyTuple) => EmptyTuple
    case (a, b)                   => Reverse[PlusLoop[Reverse[A], Reverse[B], false]]

  type Plus1[A <: Tuple, B <: Tuple]         = Reverse[PlusLoop[Reverse[A], Reverse[B], false]]

  type ReverseLoop[A, XS <: Tuple] <: Tuple = A match {
    case EmptyTuple => XS
    case x *: xs    => ReverseLoop[xs, x *: XS]
  }

  type Reverse[A] = ReverseLoop[A, EmptyTuple]

  type PlusTri[A, B, C]                             = (A, B, C) match
    case (false, false, false)                                              => (false, false)
    case (true, false, false) | (false, true, false) | (false, false, true) => (false, true)
    case (true, true, false) | (true, false, true) | (false, true, true)    => (true, false)
    case (true, true, true)                                                 => (true, true)

  type Inc[A <: Tuple] <: Tuple                     = A match
    case EmptyTuple => true *: EmptyTuple
    case t *: as    =>
      t match
        case false => true *: as
        case true  => false *: Inc[as]

  type IncT[A <: Tuple, O <: Boolean] <: Tuple      = O match
    case false => A
    case true  => Inc[A]

  type PlusLoop[A <: Tuple, B <: Tuple, O] <: Tuple = (A, B) match
    case (EmptyTuple, EmptyTuple) =>
      O match
        case true  => (true *: EmptyTuple)
        case false => EmptyTuple
    case (EmptyTuple, B)          => IncT[B, O]
    case (A, EmptyTuple)          => IncT[A, O]
    case (a *: as, b *: bs)       =>
      PlusTri[a, b, O] match
        case (x, y) => y *: PlusLoop[as, bs, x]

Output

second println fails with

  cannot reduce inline match with
 scrutinee:  compiletime.erasedValue[
  
    playground.tint.Sums.Plus1[((true : Boolean), (true : Boolean), 
      (true : Boolean)
    ), ((true : Boolean), (true : Boolean))]
  
] : playground.tint.Sums.Plus1[((true : Boolean), (true : Boolean), (true : Boolean)
  )
, ((true : Boolean), (true : Boolean))]
 patterns :  case _:EmptyTuple
             case _:*:[t @ _, ts @ _]

Expectation

Plus1 application should work the same way as Plus

OlivierBlanvillain added a commit to dotty-staging/dotty that referenced this issue Sep 29, 2021
@OlivierBlanvillain
Copy link
Contributor

Thanks for the but report! The issue appears to be already fixed on master, so I made a PR to add your example as a regression test.

@OlivierBlanvillain OlivierBlanvillain linked a pull request Sep 29, 2021 that will close this issue
@anatoliykmetyuk anatoliykmetyuk added Spree Suitable for a future Spree and removed Spree Suitable for a future Spree labels Oct 15, 2021
nicolasstucki pushed a commit to dotty-staging/dotty that referenced this issue Jan 13, 2022
nicolasstucki added a commit that referenced this issue Jan 13, 2022
olsdavis pushed a commit to olsdavis/dotty that referenced this issue Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants