3

I did some experiments with Kittens (https://github.com/milessabin/kittens) and have issues with compiling my code. I receive the following error.

[error] ...danirey\scala\kittens\Kittens.scala:23: could not find implicit value for parameter instance: cats.Functor[danirey.scala.kittens.AdtDefns.Tree]
[error]     val funct = Functor[Tree]
[error]                        ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed

The complete File is as follows

package danirey.scala.kittens

/**
  * @author Dani
  */
import cats.Functor
import cats.syntax.AllSyntax
import cats.derived.functor._
import legacy._
import cats.derived.iterable.legacy._
import org.typelevel.discipline.scalatest.Discipline
import shapeless.cachedImplicit

object Kittens extends App {
  val ft = new FunctorExperiment()
  ft.print()
}

class FunctorExperiment extends AllSyntax {
  import AdtDefns._

  def print():Unit = {
    val funct = Functor[Tree]
    val tree: Tree[String] = Node(
        Leaf("Reto"),
        Node(
            Leaf("Sandra"),
            Leaf("Mike")
        )
    )

    println(funct.map(tree)(_.length))
  }
}

I have use the almost identical code in a ScalaTest which compiles without any issues.

package danirey.scala.kittens

import cats.Functor
import cats.syntax.AllSyntax
import cats.derived.functor._
import legacy._
import cats.derived.iterable.legacy._
import org.scalatest.FunSuite
import org.typelevel.discipline.scalatest.Discipline
import shapeless.cachedImplicit

/**
  * @author Dani
  */
class FunctorExperimentTest extends FunSuite with Discipline with AllSyntax {

  import AdtDefns._

  test("functors experiment") {
    val funct = Functor[Tree]

    val tree: Tree[String] = Node(
        Leaf("Reto"),
        Node(
            Leaf("Sandra"),
            Leaf("Mike")
        )
    )

    println(funct.map(tree)(_.length))
  }
}

My build.sbt looks as follows name := "shapeless-experiments"

version := "1.0-SNAPSHOT"

scalaVersion := "2.11.8"

exportJars := true

libraryDependencies ++= Seq(
  "com.chuusai" % "shapeless_2.11" % "2.3.0",
  "org.typelevel" % "kittens_2.11" % "1.0.0-M2",
  "org.scalatest"   %% "scalatest"      % "3.0.0-M7" % "test"
)

scalacOptions ++= Seq(
  "-feature",
  "-language:higherKinds",
  "-language:implicitConversions",
  "-unchecked"
)

The most interesting thing is, that it compiles as part of an incremental compile.

If I comment line number 16, 23 and 32, then execute "sbt compile", then remove the comments again and execute "sbt compile/package" it compiles and I can even execute the program. But as soon as I run "sbt clean", it will not compile anymore.

The AdtDefns Object is basically a copy of https://github.com/milessabin/kittens/blob/master/core/src/test/scala/cats/derived/adtdefns.scala The relevant part is

object AdtDefns {
  sealed trait Tree[T]
  final case class Leaf[T](t: T) extends Tree[T]
  final case class Node[T](l: Tree[T], r: Tree[T]) extends Tree[T]
}

PS: Would be nice if someone could create a tag for scala-kittens

Cœur
  • 37,241
  • 25
  • 195
  • 267
DaniRey
  • 61
  • 5
  • It looks like a compiler bug somewhere to me. My first reaction would be trying to rule out export hook, instead of `import cats.derived.functor._`, try `import cats.derived.MkFunctor._` directly. – KailuoWang May 04 '16 at 17:42
  • also 1.0.0-M3 (PR pending review) would be updated to scala 2.11.8 (with SI-2712 fix plugin), not sure if it's going to make an impact, but if this is a compiler bug... – KailuoWang May 04 '16 at 17:50
  • Thanks @KailuoWang for the hint. Unfortunately it didn't solve the problem. I'll wait for 1.0.0-M3 and then try it again together with the SI-2712 I guess no one would be interested in fixing a compiler bug in this area, when it will not occur with SI-2712 anyway. – DaniRey May 04 '16 at 19:05
  • to be clear, I am not sure if this is related to SI-2712. If there is any difference 1.0.0-M3 is gong to make in this issue, it would more likely be the 2.11.8 upgrade. – KailuoWang May 04 '16 at 19:11
  • Thanks for the Clarification. I will test again, as soon as 1.0.0-M3 is out. – DaniRey May 04 '16 at 19:46
  • FYI 1.0.0-M3 is out. – KailuoWang May 05 '16 at 20:03
  • @KailuoWang Thank you. Unfortunately the upgrade to 1.0.0-M3 didn't resolve the issue. Do you have any ideas how to further analysis this issue. – DaniRey May 07 '16 at 09:24
  • I am out of ideas (not many of them to begin with either). – KailuoWang May 09 '16 at 23:18
  • @KailuoWang I still have the feeling, that I made a very basic mistake, or have a misunderstanding of kittens, which leads to this error. Could you give me a pointer to a simple open-source project, which uses kittens as part of their implementation. Maybe I can find my error by looking at other peoples code. – DaniRey May 14 '16 at 06:27

1 Answers1

0

@DaniRey we use kittens in our projects but only the sequence part. I am not aware of any project that use kittens derivation. What's your user case?

KailuoWang
  • 1,304
  • 1
  • 12
  • 24
  • I don't have a specific use case - yet. I'm just eager to learn and understand new concepts. – DaniRey May 17 '16 at 20:02
  • @DaniRey I was thinking it might have something to day with the delayedInit in `App`, although I am not sure. If you want to use this in your real code I would say one work around could be adding an implicit derived instance in the companion object. – KailuoWang May 19 '16 at 16:55
  • Sorry for the late reply. I just changed the code to use a class with a `main` function instead of extending `App`, unfortunately it didn't change anything. Regarding the implicit derived instance. I'm sorry, but I'm not sure what to do. Do you suggest to create a companion object for FunctorExperiment? But then adding a implicit derived instance of what? – DaniRey May 22 '16 at 21:55
  • I was suggesting adding a companion object for `Tree` and add something like ``` implicit val f: Functor[Tree] = cats.derived.MkFunctor[Tree] ``` and in your tests, you don't need to import cats.derived any more. – KailuoWang May 23 '16 at 19:19
  • Thanks a lot for your patience. I tried to change the code as you suggested, but now I receive the following compile error `could not find implicit value for parameter mff: cats.derived.MkFunctor[danirey.scala.kittens.AdtDef ns.Tree]` I added the following companion object for Tree to adtdefns `object Tree { implicit val f: Functor[Tree] = cats.derived.MkFunctor[Tree] }` And simplified my Kittens object. The code is available at https://github.com/DaniRey/shapeless-experiments – DaniRey May 25 '16 at 22:02