Skip to content

Resolution of given instances for nested type constructors #15344

Open
@TimWSpence

Description

@TimWSpence

Compiler version

3.1.2

Minimized code

In scala-cli:

//> using scala "3.1.2"

trait Contra[F[_]]:
  extension [A] (fa: F[A])
    def contramap[B](f: B => A): F[B]

object Contra:
  given Contra[[x] =>> x => Unit] with
    extension [A] (f: A => Unit)
      def contramap[B](g: B => A): B => Unit = g.andThen(f)

trait Functor[F[_]]:
  extension [A] (fa: F[A])
    def contramap[B](f: A => B): F[B]

object Functor:
  given nested[F[_], G[_]](using F: Contra[F], G: Contra[G]): Functor[[x] =>> F[G[x]]] with
    extension [A](fga: F[G[A]])
      def map[B](f: A => B): F[G[B]] = F.contramap(fga)(ga => G.contramap(ga)(f))

val x: Functor[[x] =>> (x => Unit) => Unit] = Functor.nested[[x] =>> x => Unit, [x] =>> x => Unit]
summon[Functor[[x] =>> (x => Unit) => Unit]]

Output

[error] ./repro.sc:22:45: no implicit argument of type repro.Functor[[x] =>> (x => Unit) => Unit] was found for parameter x of method summon in object Predef
[error] summon[Functor[[x] =>> (x => Unit) => Unit]]
[error]                                             ^
Error compiling project (Scala 3.1.2, JVM)
Compilation failed

Expectation

My hope was that we should resolve the Functor instance for the nested type constructors here. From my experimentation this doesn't seem to work in scala 2 either so it possibly isn't a bug. Is this intended behaviour? (I can see that there's ambiguity at least in the case where there are more than 2 nested type constructors) If it is intended then are you able to suggest any workarounds? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions