scalascalazcomonad

What is a Cohoist in scalaz?


The scalaz defines a Cohoist:

  trait Cohoist[F[_[_], _]] extends ComonadTrans[F] {
    def cohoist[M[_], N[_]: Comonad](f: M ~> N): F[M, ?] ~> F[N, ?]
  }

where ComonadTrans is defined:

 trait ComonadTrans[F[_[_], _]] {
   def lower[G[_]: Cobind, A](a: F[G, A]): G[A]
 }

The question is how to treat this type? Can someone give an explanation in a few words or give an example?


Solution

  • ComonadTrans isn't really important to understanding cohoist, which is similar to a higher-order version of map.

    map can be reformulated, by flipping the arguments around, as

    [A, B](A => B) => (F[A] => F[B])
    

    In other words it lifts a function into F. ~> is just

    F ~> G
    [A]F[A] => G[A]
    

    With that you can expand the signature of cohoist

    [M[_], N[_]: Comonad]([A]M[A] => N[A]) => ([A]F[M, A] => F[N, A])
    

    (the two As cannot be combined and pulled to the initial tparam list; I don't want to go into detail here beyond saying "that would not work")

    So just like map, it lifts a function (M to N transformer) into F, making an "F of M" to "F of N" transformer.