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

标签 haskell monads category-theory

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/

相关文章:

haskell - 有没有更好的方式来表达这种类型?

haskell - 如何从不纯方法中返回纯值

scala - 与try..catch相比,scala.util.Try有什么优势?

haskell - 每个自由单子(monad)超过 ???仿函数产生一个共同点?

haskell - 仿函数是 for (a -> b) -> (f a -> f b),什么是 for (Category c) => c a b -> c (f a) (f b)?

haskell - 所有 Haskell 仿函数都是内仿函数吗?

haskell - 如何在 [maybe string] 上使用 fmap 所以添加的字符串在 Just 之前?

haskell - Haskell 中将函数从 LET 迁移到 WHERE

haskell - 为什么 GHC 在这里推断出单态类型,即使禁用了 MonomorphismRestriction?

c++ - 修改不可变子结构