适用于猫的逆变函子,适用,Monad等?

时间:2018-07-12 12:59:37

标签: scala functor scala-cats

我具有以下特征...

import cats._
import cats.implicits._

trait Preference[-A] {
  self =>

  def compare(a1: A, a2: A): Int

  final def ordering[A1 <: A]: Ordering[A1] = {
    new Ordering[A1] {
      def compare(a1: A1, a2: A1): Int = {
        self.compare(a1, a2)
      }
    }
  }

}


object Preference {

  implicit val contravariant: Contravariant[Preference] = {
    new Contravariant[Preference] {
      def contramap[A, B](fa: Preference[A])(f: B => A): Preference[B] = {
        new Preference[B] {
          def compare(b1: B, b2: B): Int = {
            fa.compare(f(b1), f(b2))
          }
        }
      }
    }
  }
}

我想为此特性定义ApplyApplicative,甚至可能定义Monad实例,但是所有这些类型类都是Functor的扩展。这些类型类的版本在Cats中是否存在逆函子?

1 个答案:

答案 0 :(得分:2)

Haskell中wpdb的对等等效项是DivisibleApplicative allows to define both of its methods (divide is contramap2, conquer is trivial)。不过,我不确定是否可以肯定cats.ContravariantMonoidal是猫的Divisible

对于Monoidal,Kmett说

  

There is no contravariant Monad-like. You need C -> C, not C -> C^op. The twist denies you the ability to build nice structure.

Are there contravariant monads?