功能组合,Kleisli箭头和Monadic法则

Mic*_*ael 17 monads functional-programming scala kleisli

阅读本文后,我理解>=>(Kleisli arrow)只是一个组合函数的高阶函数,它返回"monadic values".例如:

val f: A => M[B] = ...
val g: B => M[C] = ...

val h: A => M[C] = f >=> g // compose f and g with Kleisli arrow

它看起来像是一个简单的"简单"函数组合(即返回简单值的纯函数):

val f: A => B = ...
val g: B => C = ...

val h = f andThen g; // compose f and g

现在我猜这个"简单"的构图andThen符合某些规律

  • 身份:f andThen g == gg andThen f == g身份的功能:f[A](a:A):A = a
  • 相关性:(f1 andThen f2) andThen f3 == f1 andThen (f2 andThen f3)

现在我的问题是:

  • 是否>=>遵守这些法律,其中的身份f(a:A) = M[a].unit(a)
  • 我们能否从这些法律中得出一元法则?这些法律和一元法律是否相同?

phi*_*ler 11

你在这里得到的是这种结构作为一个类别的直接后果.

  1. 是的,他们确实符合.并且它们符合确实是他们被称为Kleisli的原因,因为Kleisli箭头加上类型形成了monad 的Kleisli类别(每个monad引起).这也是为什么这样unit称呼它:它是Kleisli箭头组成的单位.
  2. 是的,它们可以衍生出来.使用转换(f <=< g) x = f =<< (g x)(其中<=<andThen,并且=<<很可能是像flip(bind)斯卡拉).可以在此处找到推导的确切步骤.