標準ライブラリでの Haskell の型クラス設定の多くの側面が気に入っているので、私は Scalaz を使用しています。しかし、まさにこれが私の現在の問題です。2 つの汎用パラメーターを持つ汎用データ構造があります。
case class Parser[T,A](f: T => (T,A))
Haskell では、Alternative
次のような型クラスを実装します。
newtype Parser t a = Parser { runParser :: t -> (t,a) }
instance Alternative (Parser t) where
...
しかし、Scala で同等のことを行うにはどうすればよいでしょうか? 私の知る限り、次のようなことはできません
object Parser {
implicit def ins_Alternative[T] = new Alternative[Parser[T]] {
// ...
}
}
これを行う方法を知っている人はいますか?事前にthx!
アップデート:
私はこれを見つけました:
implicit def eitherMonad[L]: Traverse[Either[L, ?]] with MonadError[Either[L, ?], L] with BindRec[Either[L, ?]] with Cozip[Either[L, ?]] =
new Traverse[Either[L, ?]] with MonadError[Either[L, ?], L] with BindRec[Either[L, ?]] with Cozip[Either[L, ?]] {
def bind[A, B](fa: Either[L, A])(f: A => Either[L, B]) = fa match {
case Left(a) => Left(a)
case Right(b) => f(b)
}
// ...
}
Scalaz ソース ( https://github.com/scalaz/scalaz/blob/series/7.3.x/core/src/main/scala/scalaz/std/Either.scala )。
これによると、私は次のようなものを書く必要があると思います
object Parser {
implicit def ins_Alternative[T] = new Alternative[Parser[T, ?]] {
// ...
}
}
?
タイプが不明であるため、コンパイルされません。