Monad法则解释 [英] Explanation of Monad laws

查看:136
本文介绍了Monad法则解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过对Haskell的温和介绍,有以下monad法。任何人都可以直观地解释它们的含义吗?

  return a>> = k = ka 
m>> = return = m
xs>> = return。 f = f map f xs
m>> =(\ x-> kx> = h)=(m> = k)> = h

这是我的尝试解释:


  1. 我们希望返回函数能够包装 a ,这样它的monadic特性就变得微不足道了。当我们将它绑定到一个函数时,没有单调效果,它应该只传递 a 到函数中。


  2. m 的解包输出传递给 return 来重新包装它。 monadic性质保持不变。所以它和原来的monad一样。


  3. 解包值传递给 f ,然后重新包装。 monadic性质保持不变。这是我们将普通函数转换为一元函数时的预期行为。


  4. 我没有这个定律的解释。但这确实表明monad必须是几乎联合的。

  5. >你的描述看起来不错。通常人们会谈到三条单子定律,你有1,2和4.你的第三条定律略有不同,我会在稍后讨论。

    对于这三个monad法,我发现使用Kleisli组合可以更直观地了解它们的意义:

       - 在Control.Monad中定义
    (> =>):: Monad m => (a - > m b) - > (b→m c)→> a - > m c
    mf> => n = \ x - > mf x>> = n

    现在这些法律可以写成:

      1)return> => mf = mf  - 左身份
    2)mf> => return = mf - 右标识
    4)(f> => g)> => h = f> => (g> => h) - 关联性



    <1>不会改变这个值,也不会在monad中做任何事情。



    <2>正确的身份定律 - 返回一个值不会改变值,也不会改变值



    4)相关性 - 一元组合是联想的(我喜欢KennyTM的回答)

    这两个身份法则基本上是一样的,但它们都是必需的,因为 return 应该在绑定操作符的两侧都有身份行为。



    现在是第三条法则。这个法则基本上说,当将函数提升到monad中时,Functor实例和Monad实例的行为方式都是相同的,并且它们都不是单向的。如果我没有弄错,那么当一个单子服从其他三个法则,并且Functor实例服从函子法则时,这种说法总是正确的。



    其中很多来自 Haskell Wiki Typeclassopedia 也是一个很好的参考。


    From a gentle introduction to Haskell, there are the following monad laws. Can anyone intuitively explain what they mean?

    return a >>= k             = k a
    m >>= return               = m
    xs >>= return . f          = fmap f xs
    m >>= (\x -> k x >>= h)    = (m >>= k) >>= h
    

    Here is my attempted explanation:

    1. We expect the return function to wrap a so that its monadic nature is trivial. When we bind it to a function, there are no monadic effects, it should just pass a to the function.

    2. The unwrapped output of m is passed to return that rewraps it. The monadic nature remains the same. So it is the same as the original monad.

    3. The unwrapped value is passed to f then rewrapped. The monadic nature remains the same. This is the behavior expected when we transform a normal function into a monadic function.

    4. I don't have an explanation for this law. This does say that the monad must be "almost associative" though.

    解决方案

    Your descriptions seem pretty good. Generally people speak of three monad laws, which you have as 1, 2, and 4. Your third law is slightly different, and I'll get to that later.

    For the three monad laws, I find it much easier to get an intuitive understanding of what they mean when they're re-written using Kleisli composition:

    -- defined in Control.Monad
    (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c
    mf >=> n = \x -> mf x >>= n
    

    Now the laws can be written as:

    1) return >=> mf = mf                  -- left identity
    2) mf >=> return = mf                  -- right identity
    4) (f >=> g) >=> h = f >=> (g >=> h)   -- associativity
    

    1) Left Identity Law - returning a value doesn't change the value and doesn't do anything in the monad.

    2) Right Identity Law - returning a value doesn't change the value and doesn't do anything in the monad.

    4) Associativity - monadic composition is associative (I like KennyTM's answer for this)

    The two identity laws basically say the same thing, but they're both necessary because return should have identity behavior on both sides of the bind operator.

    Now for the third law. This law essentially says that both the Functor instance and your Monad instance behave the same way when lifting a function into the monad, and that neither does anything monadic. If I'm not mistaken, it's the case that when a monad obeys the other three laws and the Functor instance obeys the functor laws, then this statement will always be true.

    A lot of this comes from the Haskell Wiki. The Typeclassopedia is a good reference too.

    这篇关于Monad法则解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆