- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
背景语境:
从数学上讲,我可以看到需要关联性以保持简单而不依赖顺序。我遇到的示例 monad 的所有实现(博客、书籍等)似乎总是有效。似乎只是简单地使用 map, flatMap
(Scala) 或 fmap, >>=
(Haskell) 就可以使事情成为一个有效的 monad。
据我所知,这并不完全正确,但无法通过失败案例提出一个反例来说明法律的“必要性”。
Wadler's paper提到了不正确实现的可能性:
Haskell Wiki提到以下内容:
The third law is a kind of associativity law for
>>=
. Obeying the three laws ensures that the semantics of the do-notation using the monad will be consistent.Any type constructor with return and bind operators that satisfy the three monad laws is a monad. In Haskell, the compiler does not check that the laws hold for every instance of the Monad class. It is up to the programmer to ensure that any Monad instance they create satisfies the monad laws.
问题:
do
符号有何影响?最佳答案
这是一个结合性失败的非 monad 示例:
{-# LANGUAGE DeriveFunctor #-}
import Control.Monad
newtype ListIO a = L { runListIO :: IO [a] }
deriving Functor
instance Applicative ListIO where
pure x = L $ return [x]
(<*>) = ap
instance Monad ListIO where
(L m) >>= f = L $ do
xs <- m
concat <$> mapM (runListIO . f) xs
如果满足结合性,这两个 do
block 将是等价的
act1 :: ListIO Int
act1 = do
L (pure [1,2,3])
do L (putStr "a" >> return [10])
L (putStr "b" >> return [7])
act2 :: ListIO Int
act2 = do
do L (pure [1,2,3])
L (putStr "a" >> return [10])
L (putStr "b" >> return [7])
但是,运行操作会产生不同的输出:
main :: IO ()
main = do
runListIO act1 -- ababab
putStrLn ""
runListIO act2 -- aaabbb
return ()
总的来说,结合律的验证可能很困难。当然,可以编写测试,但确保关联性的理想方法是编写定律的数学证明。在某些情况下,等式推理足以证明结合性。有时候,我们也需要归纳。
关于haskell - 违反结合律的不正确 monad 的例子是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60768312/
我正在实现 ITU-T G.711 标准,我决定从 G.191 中的引用代码开始。 在 A-law 压缩上运行一些测试让我怀疑算法的正确性。 根据G.711,输入范围0-63应该位于第一段,步长为2。
我是一名优秀的程序员,十分优秀!