Search code examples
scalascalazkleislisemigroup

Why is there no >=> semigroup for A => M[A] in Scalaz?


This is a followup to my previous question

Kleisli defines two operators <=< (compose) and >=> (andThen). The >=> looks very natural for me and I don't understand how <=< can be useful.

Moreover, it looks like there is no >=> semigroup for A => M[A] but the <=< semigroup does exist.

What is the rationale behind it ?


Solution

  • compose (or <=<) is a little more natural when translating between point-free and non point-free styles. For example, if we have these functions:

    val f: Int => Int = _ + 1
    val g: Int => Int = _ * 10
    

    We get the following equivalences:

    scala> (f andThen g)(3) == g(f(3))
    res0: Boolean = true
    
    scala> (f compose g)(3) == f(g(3))
    res1: Boolean = true
    

    In the compose case the f and g are in the same order on both sides of the equation.

    Unfortunately Scala's type inference often makes andThen (or >=>) more convenient, and it tends to be more widely used than compose. So this is a case where mathematical conventions and the quirks of Scala's type inference system are at odds. Scalaz (not too surprisingly, given the culture of the project) chooses the math side.