gpt4 book ai didi

haskell - Haskell monad 不强制执行的分类单子(monad)的身份是什么?

转载 作者:行者123 更新时间:2023-12-03 09:59:44 27 4
gpt4 key购买 nike

http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html

写道:

If you hadn't guessed, this is about monads as they appear in pure functional programming languages like Haskell. They are closely related to the monads of category theory, but are not exactly the same because Haskell doesn't enforce the identities satisfied by categorical monads.



这些是上面文字所说的身份吗?

Are monad laws enforced in Haskell?
return a >>= k  =  k a
m >>= return = m
m >>= (\x -> k x >>= h) = (m >>= k) >>= h

最佳答案

与链接问题的公认答案相比,请考虑此实例。

newtype List a = List [a] deriving (Functor, Applicative)

instance Monad List where
return _ = List []
m >>= f = List []

编译器会接受这个定义,但它不遵守单子(monad)定律。具体来说,尝试确认
m >>= return == m

两个 []List :
-- Correct
Prelude> [1,2,3] >>= return
[1,2,3]

-- Not correct, should be List [1,2,3]
Prelude> List [1,2,3] >>= return
List []

关于haskell - Haskell monad 不强制执行的分类单子(monad)的身份是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58852614/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com