简体   繁体   中英

Avoid diverging implicit expansion on recursive MTL class

I am trying to make a composable state type in cats mtl using an Hlist and have defined a MonadState as follows

implicit def hlistStateMonad[M[_], S <: HList, S2]
(implicit S:Selector[S, S2], R:Replacer[S, S2, S2], M:MonadState[M, S]):MonadState[M, S2]

However this implicit definition is called to resolve implicit arg M even when S2 is not an HList, and the Selector and Replacer cannot resolve and it doesnt then look in the cats.mtl implicits

I have tried defining S2 <:!< HList (and =:!=), but this didnt help at all.

The testable code is shown below:

object Test {
  implicit def hlistStateMonad[M[_], S <: HList, S2](implicit S:Selector[S, S2], R:Replacer[S, S2, S2], M:MonadState[M, S]):MonadState[M, S2] =
    new MonadState[M, S2] {

      val monad: Monad[M] = M.monad

      def inspect[A](f: S2 => A):M[A] =
        M.inspect(s => f(S(s)))

      def modify(f: S2 => S2):M[Unit] =
        M.modify(s => R(s, f(S(s))).asInstanceOf[(S2, S)]._2)

      def get:M[S2] =
        M.inspect(S.apply)

      def set(s2: S2): M[Unit] =
        M.modify(s => R(s, s2).asInstanceOf[(S2, S)]._2)
    }
}

  test("Monad state resolution with HList") {

    type M[V] = State[Int :: String :: HNil, V]

    import cats.mtl.instances.all._
    import Test._

    val m = implicitly[MonadState[M, Int]]
  }

Adding the lookup for evidence that S2 isn't an HList (as you suggested) and looking up the MonadState before the Selector & Replacer seems to have done the trick for me.

Suspect it may have something to do with the compiler not binding S to anything - just knowing that it's upper bounded by HList. (but that's my best guess)

Hope this helps.

import cats.Monad
import cats.data.State
import cats.mtl.MonadState
import org.scalatest.{FlatSpec, Matchers}
import shapeless._
import shapeless.::
import shapeless.ops.hlist.{Replacer, Selector}
import cats.mtl.instances.all._

class Test extends FlatSpec with Matchers {

  implicit def hlistStateMonad[M[_], S <: HList, S2](
      implicit
      nEv: S2 <:!< HList,
      M: MonadState[M, S],
      S: Selector[S, S2],
      R: Replacer[S, S2, S2]): MonadState[M, S2] =
    new MonadState[M, S2] {

      val monad: Monad[M] = M.monad

      def inspect[A](f: S2 => A): M[A] =
        M.inspect(s => f(S(s)))

      def modify(f: S2 => S2): M[Unit] =
        M.modify(s => R(s, f(S(s))).asInstanceOf[(S2, S)]._2)

      def get: M[S2] =
        M.inspect(S.apply)

      def set(s2: S2): M[Unit] =
        M.modify(s => R(s, s2).asInstanceOf[(S2, S)]._2)
    }

  "Monad state resolution with HList" should "compile and maybe even run?" in {

    type M[V] = State[Int :: String :: HNil, V]

    val m = implicitly[MonadState[M, Int]]

    val res = for {
      a <- m.set(5)
      b <- m.get
    } yield b + 1

    res.run(1 :: "test" :: HNil).value._2 shouldBe 6
  }
}

Try

import cats.Monad
import cats.data.State
import cats.mtl.MonadState
import shapeless.{::, HList, HNil, Lazy}
import shapeless.ops.hlist.{Replacer, Selector}

import scala.language.higherKinds

object Test {
  implicit def hlistStateMonad[M[_], S <: HList, S2](implicit S:Selector[S, S2], R:Replacer[S, S2, S2], M:Lazy[MonadState[M, S]]):MonadState[M, S2] =
new MonadState[M, S2] {

    val monad: Monad[M] = M.value.monad

    def inspect[A](f: S2 => A):M[A] =
    M.value.inspect(s => f(S(s)))

    def modify(f: S2 => S2):M[Unit] =
    M.value.modify(s => R(s, f(S(s))).asInstanceOf[(S2, S)]._2)

    def get:M[S2] =
    M.value.inspect(S.apply)

    def set(s2: S2): M[Unit] =
    M.value.modify(s => R(s, s2).asInstanceOf[(S2, S)]._2)
  }
}

At least error changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM