should Kleisli contravariant on A? #2749
Kleisli[F[_], A, B] is supposed to be abstraction of
from what I observed, it will be much easier to let scala compiler find out what A should be
for instance, if I would like to choose from two Kleisli
contravariant on -A
case class Kleisli[F[_], -A, B](run: A => F[B]) def first[A, B, C, F[_]](a: Kleisli[F, A, B], b: Kleisli[F, A, C]) = a trait A1 trait A2 trait A12 extends A1 with A2 first(Kleisli((a: A1) => Some(a)), Kleisli((b:A2)=>Some(b))) res7: Kleisli[Some, A2 with A1, A1] = ...
compiler can infer the correct type of the input of Kleisli should be
to achieve the same thing without
def first[A1, A2, A12, B, C, F[_]](a: Kleisli[F, A1, B], b: Kleisli[F, A2, C]) (implicit ev: A12<:<A1):Kleisli[F, A12, B] = Contravariant[Kleisli[F, ?,B]].contramap(a)(ev)
@ first(Kleisli((a: A1) => Some(a)), Kleisli((b:A2)=>Some(b))) cmd14.sc:1: type mismatch; found : Nothing <:< Nothing required: A12 <:< ammonite.$sess.cmd9.A1 val res14 = first(Kleisli((a: A1) => Some(a)), Kleisli((b:A2)=>Some(b))) ^
the compiler can't infer A12 for contramap unless I explicitly tell the compiler that I need
@ first(Kleisli((a: A1) => Some(a)), Kleisli((b:A2)=>Some(b))) : Kleisli[Some, A12, A1] res14: Kleisli[Some, A12, A1] = Kleisli(scala.Function1$$Lambda$35/695682681@1e3c4c12)
The text was updated successfully, but these errors were encountered:
Hi @jcouyang I wonder if you could help me with some pointers on the issue you raised.
Although I understand the explanation above, and experienced it when composing Kleisli, I am looking for the formal definition of things.
What is the machanism that enable to compiler to infer
I wonder if i can find those rules somewhere in the specification so that thing does not sound magic to me and is predictable.
hi @Maatary I think it is the other way around, A1 is upper bound of