作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,我想手动证明 Maybe applicative 的组成定律是:
u <*> (v <*> w) = pure (.) <*> u <*> v <*> w
u <*> (v <*> w) [Left hand side of the law]
= (Just f) <*> (v <*> w) [Assume u ~ Just f]
= fmap f (v <*> w)
= fmap f (Just g <*> w) [Assume v ~ Just g]
= fmap f (fmap g w)
= fmap (f . g) w
pure (.) <*> u <*> v <*> w [Right hand side of the law]
= Just (.) <*> u <*> v <*> w
= fmap (.) u <*> v <*> w
= fmap (.) (Just f) <*> v <*> w [Replacing u with Just f]
= Just (f .) <*> v <*> w
= Just (f .) <*> Just g <*> w [Replacing v with Just g]
= fmap (f .) (Just g) <*> w
= Just (f . g) <*> w
= fmap (f . g) w
u
和
v
对于嵌入在
Just
中的一些功能数据构造函数继续我的证明。这可以接受吗?有没有更好的方法来证明这一点?
最佳答案
应用仿函数表达式只是某些仿函数上下文中的函数应用程序。因此:
pure f <*> pure a <*> pure b <*> pure c
-- is the same as:
pure (f a b c)
pure (.) <*> u <*> v <*> w == u <*> (v <*> w)
u = pure f
v = pure g
w = pure x
pure (.) <*> u <*> v <*> w
pure (.) <*> pure f <*> pure g <*> pure x
pure ((.) f g x)
pure ((f . g) x)
pure (f (g x))
pure f <*> pure (g x)
pure f <*> (pure g <*> pure x)
u <*> (v <*> w)
Maybe
我们知道
pure = Just
.因此,如果
u
,
v
和
w
是
Just
值然后我们知道组合定律成立。
Nothing
怎么办? ?我们知道:
Nothing <*> _ = Nothing
_ <*> Nothing = Nothing
Nothing
那么整个表达式变为
Nothing
(如果第一个参数是
undefined
则在第二种情况下除外)并且因为
Nothing == Nothing
法律仍然有效。
undefined
呢? (又名底部)值?我们知道:
(Just f) <*> (Just x) = Just (f x)
(Just f) <*> undefined
undefined <*> (Just x)
undefined <*> Nothing
Nothing
:
Nothing <*> undefined
关于haskell - 证明Maybe Applicative的组合律,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24129197/
我正在实现 ITU-T G.711 标准,我决定从 G.191 中的引用代码开始。 在 A-law 压缩上运行一些测试让我怀疑算法的正确性。 根据G.711,输入范围0-63应该位于第一段,步长为2。
我是一名优秀的程序员,十分优秀!