[英]Cannot find Bind instances for Free Monads over Coyoneda when composing functions via Kleisli arrows in scalaz
預先感謝您的幫助
我嘗試通過 Kleisli 箭頭編寫 2 個函數。 這些函數接受 String 並生成 FreeC。 創建 kleisli 箭頭沒有問題,但編譯器抱怨它找不到。 為簡單起見,我將刪減一些代碼:
import scalaz._
import Scalaz._
import Free.FreeC
import Free._
import Kleisli._
trait AppCompose {
def lift[F[_], G[_], A](fa: F[A])(implicit I: Inject[F, G]): FreeC[G, A] =
Free.liftFC(I.inj(fa))
}
object BigBrother {
sealed trait Sensor[A]
case class Log(log: String) extends Sensor[Unit]
case class Filter(log: String) extends Sensor[String]
case class Secure(log: String) extends Sensor[String]
}
import BigBrother.Sensor
class BigBrother[F[_]](implicit I: Inject[Sensor,F]) extends AppCompose {
import BigBrother._
type FreeString[A] = FreeC[F,String]
def log(log: String) = lift(Log(log))
def filter(log: String) = lift(Filter(log))
def secure(log: String) = lift(Secure(log))
def filterAndSecure(phrase: String) = for {
f <- filter(phrase)
s <- secure(f)
} yield s
// kleisli composition attempt - alternative to filterAndSecure
val fk = kleisli[FreeString, String, String](filter _)
val sk = kleisli[FreeString, String, String](secure _)
val fAndS = fk >=> sk // this is where we have a compilation issue
}
出於某種原因,我得到的是這個編譯錯誤:
could not find implicit value for parameter b: scalaz.Bind[FreeString]
[error] val fAndS = sk >=> fk
感覺應該解決隱式問題,因為 FreeC 在實現 Bind 特性的 monad 實例中,我正在通過 import Free._ 導入所有 Free 隱式實例
我在這里錯過了什么?
先感謝您!
謝謝特拉維斯的幫助。 錯誤的類型聲明實際上是罪魁禍首之一。 在 scalaz 社區通過 google 群組提供的一些幫助下,這里的一些修補是答案:
class BigBrother[F[_]](implicit I: Inject[Sensor,F]) extends AppCompose {
import BigBrother._
def log(log: String) = lift(Log(log))
def filter(log: String) = lift(Filter(log))
def secure(log: String) = lift(Secure(log))
def filterAndSecure(phrase: String) = for {
f <- filter(phrase)
s <- secure(f)
} yield s
type CoyoF[A] = Coyoneda[F, A]
type FreeCoF[A] = Free[CoyoF,A]
implicit val MonadFreeSensor = Free.freeMonad[FreeCoF]
// kleisli composition attempt - alternative to filterAndSecure
val fk = kleisli[FreeCoF, String, String](filter _)
val sk = kleisli[FreeCoF, String, String](secure _)
val fAndS = fk >=> sk
}
關鍵是正確的類型聲明並為 FreeCoF 提供類型類 monad 實例隱式 val MonadFreeSensor = Free.freeMonad[FreeCoF]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.