question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Another GADT typechecking error

See original GitHub issue

Compiler version

3.0.2-RC1

Minimized code

class Module[*[_, _]] {
  sealed trait Box[A]

  case class Box2x2[A, B, C, D](value: (A * B) * (C * D)) extends Box[(A * B) * (C * D)]

  def unbox2[X, Y](box: Box[X * Y]): (X * Y) =
    box match {
      case Box2x2(v) => v // we know that v has type ((a * b) * (c * d))
                          // for some types a, b, c, d,
                          // and that ((a * b) * (c * d)) = (X * Y),
                          // so v does have the expected return type (X * Y)
    }
}

Output

% ~/scala3-3.0.2-RC1/bin/scalac boxes.scala 
-- [E007] Type Mismatch Error: boxes.scala:8:24 --------------------------------
8 |      case Box2x2(v) => v // we know that v has type ((a * b) * (c * d))
  |                        ^
  |       Found:    (v : A$1 * B$1 * (C$1 * D$1))
  |       Required: X * Y
  |
  |       where:    * is a type in class Module with bounds <: [_, _] =>> Any

longer explanation available when compiling with `-explain`
1 error found

Expectation

The code should typecheck, as per the explanation in the code comment.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
dwijnandcommented, Jul 15, 2021

#fixed-in-scala2 😛

$ pbpaste > boxes.scala
$ m boxes.scala
class Module[*[_, _]] {
  sealed trait Box[A]

  case class Box2x2[A, B, C, D](value: (A * B) * (C * D)) extends Box[(A * B) * (C * D)]

  def unbox2[X, Y](box: Box[X * Y]): (X * Y) =
    box match {
      case Box2x2(v) => v // we know that v has type ((a * b) * (c * d))
                          // for some types a, b, c, d,
                          // and that ((a * b) * (c * d)) = (X * Y),
                          // so v does have the expected return type (X * Y)
    }
}
$ scalac boxes.scala
$
1reaction
LPTKcommented, Jul 17, 2021

Yes, though you actually need to phrase it in a way that’s a bit of more general:

abstract class Eq[A, B]:
  type Ev >: B | A <: A & B

def toEqF[A, B, F[_]](eq: Eq[A, B]): Eq[F[A], F[B]] =
  new Eq[F[eq.Ev], F[eq.Ev]]{}

(This Ev member of course still works for the original problem in this issue.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scope error with locally abstract type when GADT constructor ...
I've updated the question with an example showing that there are other constructors with other shapes, so that using two locally abstract types ......
Read more >
Complete and Decidable Type Inference for GADTs - Microsoft
The type checking problem for GADTs is decidable (CH03; SP07). However, type inference turns out to be extremely difficult. The.
Read more >
Configuration - golangci-lint
If no configuration file has been found, GolangCI-Lint will try to find one in your home directory. To see which config file is...
Read more >
Building robust Streamlit apps with type-checking
Embracing type-checking will let you avoid such errors. Like other forms of static analysis, type-checking is usually performed by a tool—a ...
Read more >
A Complete Guide to Linting Go Programs - freshman.tech
Go already ventures farther than most other programming languages by bundling gofmt ... errcheck - Detect unchecked errors in Go programs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found