title: Proper implementation for a 2 type parameters Functor in Scala
body: <p>I saw this question several times on SO, but no matter how hard I try, I can't make the following code compile. The goal is to implement an <code>Functor</code> implementation for a simpler <code>Reader</code> (the code is <a href="https://github.com/FrancisToth/FP" rel="nofollow noreferrer">here</a>):</p>
  
  <pre><code>  trait Functor[F[_]] {
      def fmap[A, B](fa: F[A])(f: A =&gt; B): F[B]
    }
  
    implicit class FunctorOps[F[_]: Functor, A](self: F[A]) {
      def fmap[B](f: A =&gt; B): F[B] = implicitly[Functor[F]].fmap(self)(f)
    }
  
    case class Reader[A, B](run: A =&gt; B)
    type ReaderF[X] = ({ type L[A] = Reader[X, A] })
  
    implicit def readerFunctors[E]: Functor[ReaderF[E]#L] = 
      new Functor[ReaderF[E]#L] {
         override def fmap[A, B](fa: Reader[E, A])(f: A =&gt; B): Reader[E, B] = 
            Reader(e =&gt; f(fa.run(e)))
      }
  
    val foo = Reader[String, Int](_ =&gt; 42)
  
    foo.fmap(_ + 1) // does not compile
  </code></pre>
  
  <p>I tried to bypass the implicit mechanism with the following:</p>
  
  <pre><code>FunctorOps(foo).fmap(_ + 1)
  </code></pre>
  
  <p>but this outputs the following compilation error:</p>
  
  <pre><code>Error:(82, 23) type mismatch;
   found   : com.fp.Scratchpad.Reader[String,Int]
   required: ?F[?A]
  Note that implicit conversions are not applicable because they are ambiguous:
   both method ArrowAssoc in object Predef of type [A](self: A)ArrowAssoc[A]
   and method Ensuring in object Predef of type [A](self: A)Ensuring[A]
   are possible conversion functions from com.fp.Scratchpad.Reader[String,Int] to ?F[?A]
    FunctorOps(foo).fmap(_ + 1)
  </code></pre>
  
  <p>Thank you in advance for your help.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Just to make sure my FunctorOps is right, I created a functor instance for <code>Id</code>:</p>
  
  <pre><code>case class Id[A](value: A)
  implicit val idF: Functor[Id] = new Functor[Id] {
    override def fmap[A, B](fa: Id[A])(f: A =&gt; B): Id[B] = Id(f(fa.value))
  }
  
  val id = Id(42)
  id.fmap(_ + 1) // compiles
  </code></pre>
  
  <p>So the problem does not come from the <code>FunctorOps</code> implicit class. I suspect Scala to have a real hard time with type lambdas...</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I tried to simplify the problem but without success:</p>
  
  <pre><code>  trait Functor[F[_]] {
      def map[A, B](x: F[A])(f: A =&gt; B): F[B]
    }
  
    implicit class Ops[F[_], A](fa: F[A])(implicit F: Functor[F]) {
      def map[B](f: A =&gt; B): F[B] = F.map(fa)(f)
    }
  
    type FF[A] = ({ type F[B] = A =&gt; B })
  
    implicit def ff[E]: Functor[FF[E]#F] = new Functor[FF[E]#F] {
      override def map[A, B](x: E =&gt; A)(f: A =&gt; B): E =&gt; B = e =&gt; f(x(e))
    }
  
    val f: String =&gt; Int = _ =&gt; 42
  
    val value: Functor[FF[String]#F] = ff[String]
    val ops = new Ops[FF[String]#F, Int](f)(value)
  
    // These compile
    ops.map(_ + 1)("")
    value.map(f)(_ + 1)("")
  
    // This not
    f.map(_ + 1)
  </code></pre>