如果你打破了monad的法律,你会怎么样? [英] What happens to you if you break the monad laws?

查看:146
本文介绍了如果你打破了monad的法律,你会怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译器或库的更多本地部分(可以访问黑魔法和实现的IO或函数)做出关于这些规则的假设吗?打破他们会不会发生吗?



或者他们只是表达一种编程模式 - 即,唯一的人,你会讨厌打破他们是谁使用你的代码,并不希望你这么粗心。

解决方案

编译器不做任何假设的法律,然而,如果你的实例不遵守法律,它将不会像一个monad - 它会做奇怪的事情,否则看起来对你的用户不能正确工作(例如丢弃值,或以错误的顺序评估事情)。 / p>

此外,您的用户可能会假设monad法律保留的重构显然不会很好。


Do the compiler or the more "native" parts of the libraries (IO or functions that have access to black magic and the implementation) make assumptions about these laws? Will breaking them cause the impossible to happen?

Or do they just express a programming pattern -- ie, the only person you'll annoy by breaking them are people who use your code and didn't expect you to be so careless?

解决方案

The compiler doesn't make any assumptions about the laws, however, if your instance does not obey the laws, it will not behave like a monad -- it will do strange things and otherwise appear to your users to not work correctly (e.g. dropping values, or evaluating things in the wrong order).

Also, refactorings your users might make assuming the monad laws hold will obviously not be sound.

这篇关于如果你打破了monad的法律,你会怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆