简体   繁体   English

Haskell 实例的适用法则证明

[英]Proofs of Applicative laws for haskell instances

Have all the Haskell instances of Applicative typeclass that we get with the Haskell platform been proved to satisfy all the Applicative laws?我们在 Haskell 平台上获得的 Applicative 类型类的所有 Haskell 实例是否都被证明满足所有 Applicative 定律? If yes, where do we find those proofs?如果是,我们在哪里可以找到这些证据?

The source code of Control.Applicative does not seem to contain any proof that the applicative laws for various instances do hold. Control.Applicative的源代码似乎不包含任何证据证明适用于各种情况的法律确实成立。 It just mentions that它只是提到

-- | A functor with application.
--
--Instances should satisfy the following laws:

It then just states the laws in the comments.然后它只是在评论中陈述法律。

I found a similar case for the instances of other typeclasses (Alternative and Monad) too.我也发现了其他类型类(Alternative 和 Monad)实例的类似情况。

Are the users of these libraries supposed to verify these laws by themselves?这些图书馆的用户是否应该自己验证这些法律?

But I was wondering whether the rigorous proofs of these laws have been given elsewhere by the developers?但我想知道这些定律的严格证明是否由开发人员在其他地方给出?

Again, I am aware that a rigorous proof of Applicate (or Monad) laws for IO Monad, which involves talking with outside world, in general, may well be very much complex.同样,我知道 IO Monad 的应用(或 Monad)定律的严格证明,通常涉及与外部世界的对话,可能非常复杂。

Thanks.谢谢。

Yes, the burden of proof is entirely on the library writers.是的,举证责任完全在图书馆作者身上。 There are some examples of implementations that violate these laws.有一些违反这些法律的实施示例。 The canonical example of a law violation is ListT , which doesn't obey the monad laws for most base monads (see examples ).违反法律的典型示例是ListT ,它不遵守大多数基础 monad 的 monad 定律(参见示例)。 This gives very buggy behavior and nobody really uses ListT as a result.这给出了非常错误的行为,结果没有人真正使用ListT

I'm pretty sure most of these kinds of proofs haven't been etched in stone in a standard place.我很确定这些证据中的大多数都没有在标准的地方刻在石头上。 Most of the proofs have simply been repeated and checked to death by various curious members of the community so after a while we know which implementations do and do not satisfy their laws.大多数证明只是被社区中各种好奇的成员简单地重复和检查到死,所以一段时间后我们知道哪些实现满足和不满足他们的法律。

To give a concrete example, when I write my pipes library, I have to prove that my pipes satisfy the Category laws, but I just keep these proofs in a text file or an hpaste for future record if somebody requests them.举一个具体的例子,当我编写我的pipes库时,我必须证明我的pipes满足Category法律,但如果有人要求,我只是将这些证明保存在文本文件或 hpaste 中以备将来记录。 Including them in the source is not really feasible because they can get very long, especially for associativity laws.将它们包含在源中并不是真正可行的,因为它们会变得很长,尤其是对于结合律。

However, I think a good practice might be to include, when possible, machine-checked proofs in the original repositories so that users can refer to them as necessary.但是,我认为一个好的做法可能是在可能的情况下在原始存储库中包含机器检查的证明,以便用户可以在必要时参考它们。

有出色的库检查器,可提供 QuickCheck 属性来检查法律。

The experimental library ghc-proofs allows you to use the compiler to verify such laws for you:实验性库ghc-proofs允许您使用编译器为您验证此类定律:

app_law_2 a b (c :: Succs a) = pure (.) <*> a <*> b <*> c
                           === a <*> (b <*> c)

It only works in a few cases, such as the one described in my blog post , and is best seen as an experiment, not a ready tool.它仅适用于少数情况,例如我的博客文章中描述的情况,最好将其视为实验,而不是现成的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM