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.

Type inference issue involve type alias and higher-kinded type

See original GitHub issue

As discussed in https://gitter.im/lampepfl/dotty?at=6033a71be634904e60b71e22

Compiler version

3.0.0-RC1

Minimized code

trait Functor[F[_]] {
  def map[A, B](fa: F[A]): F[B]
}

object data {

  opaque type OptionT[F[_], A] = F[Option[A]]

  extension [F[_], A](value: OptionT[F, A]) {

    def fold[B](default: => B)(f: A => B)(using F: Functor[F]): F[B] =
      F.map(value)(_.fold(default)(f))

    def cata[B](default: => B, f: A => B)(using x: Functor[F]): F[B] =
      fold(default)(f)(using x)
  }

}

Output

Compiler error
[error] -- [E050] Type Error: /Users/tim/dev/cheetahs/core/src/main/scala/io/github/timwspence/cheetahs/data.scala:30:11
[error] 30 |      F.map(value)(_.fold(default)(f))
[error]    |      ^^^^^^^^^^^^
[error]    |      method map in trait Functor does not take more parameters
[error] -- [E007] Type Mismatch Error: /Users/tim/dev/cheetahs/core/src/main/scala/io/github/timwspence/cheetahs/data.scala:33:29
[error] 33 |      fold(default)(f)(using x)
[error]    |                             ^
[error]    |Found:    (x : io.github.timwspence.cheetahs.Functor[F])
[error]    |Required: io.github.timwspence.cheetahs.Functor[F²]
[error]    |
[error]    |where:    F  is a type in method cata with bounds <: [_$3] =>> Any
[error]    |          F² is a type variable with constraint >: [X0] =>> io.github.timwspence.cheetahs.data.OptionT[F, X0] | F[X0] and <: [_$3] =>> Any

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
smartercommented, Mar 23, 2021

First, the compiler dealiases the second G.

Before we dealias, we should be compared the applied types as written, it looks like this is done but failing, looking around this seems to fix it:

diff --git compiler/src/dotty/tools/dotc/core/TypeComparer.scala compiler/src/dotty/tools/dotc/core/TypeComparer.scala
index 23cd8b25d25..46c3be62047 100644
--- compiler/src/dotty/tools/dotc/core/TypeComparer.scala
+++ compiler/src/dotty/tools/dotc/core/TypeComparer.scala
@@ -973,7 +973,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
       /** True if `tp1` and `tp2` have compatible type constructors and their
        *  corresponding arguments are subtypes relative to their variance (see `isSubArgs`).
        */
-      def isMatchingApply(tp1: Type): Boolean = tp1 match {
+      def isMatchingApply(tp1: Type): Boolean = tp1.widen match {
         case tp1 @ AppliedType(tycon1, args1) =>
           // We intentionally do not automatically dealias `tycon1` or `tycon2` here.
           // `TypeApplications#appliedTo` already takes care of dealiasing type

now running the tests to see if this break anything, but this looks promising 😃

2reactions
smartercommented, Feb 22, 2021

Minimized without the cats dependency:

trait Functor[F[_]]

object data {

  type OptionT[F[_], A] = F[Option[A]]

  def fold[F[_], A, B](value: OptionT[F, A])(f: Functor[F]): F[B] = ???

  def cata[F[_], A, B](value: OptionT[F, A])(f: Functor[F]): F[B] =
    fold(value)(f) // error
}

This compiles with Scala 2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Yet another Higher Kinded Types proposal in Typescript #44875
I am proposing that Higher order types be added in the truest since, that is, there is a strict hierarchy of type orders,...
Read more >
What are the limitations on inference of higher-kinded types in ...
Scala in general doesn't have trouble with higher-kinded types. This is one of several key features that distinguishes its type system from, ...
Read more >
Higher-Kinded Types in Dotty - Scala 3
In the new scheme, only classes (and traits) can have parameters and these are treated as equivalent to type members. Type aliases and...
Read more >
1598-generic_associated_types - The Rust RFC Book
"Higher-kinded types" is a vague term, conflating multiple language ... which looks a lot like the syntax for creating aliases for type constructors....
Read more >
Lightweight higher-kinded polymorphism
Higher-kinded polymorphism —i.e. abstraction over type con- ... Defining Unless in OCaml involves binding three modules. ... 1.1 The alias problem.
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