如果你违反了单子法则,你会发生什么? [英] What happens to you if you break the monad laws?

查看:22
本文介绍了如果你违反了单子法则,你会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译器或库中更原生"的部分(IO 或可以访问黑魔法和实现的函数)是否对这些定律做出假设?打破它们会导致不可能的事情发生吗?

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?

推荐答案

编译器不对规律做任何假设,但是,如果你的实例不遵守规律,它的行为就不会像一个 monad——它会做奇怪的事情,否则会在用户看来无法正常工作(例如丢弃值或以错误的顺序评估事物).

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).

此外,假设 monad 定律成立,您的用户可能进行的重构显然是不合理的.

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

这篇关于如果你违反了单子法则,你会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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