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?
Optional per se qualifies as a monad, despite some resistence in the Java 8 library team.
In FP we often loosely say things like "arrays are monads" or "maybe values are monadic" etc. However, speaking more strictly, it is not the values (like [1, 2, 3] , Nothing , or Just(6) ) that are monads, but the context (the Array or Maybe "namespace" as you put it).
A monad is just a wrapper around a type or data structure. So by 'wrapping' a class type (e.g. Integer) inside the Java Optional type, we add some functionality around it, like of(x) , isPresent() , orElse(c) and get() . To be more precise, a Monad always contains a bind() function, That translates to flatMap in Java.
Nothing. No one sees you break the law, there is no evidence against you, and/or the law is so trivial that no one is concerned that you broke it. You are seen breaking the law, it is reported, you are caught and convicted of the crime, and you are executed for committing that crime.
Monad Laws. Any class, to truly be a monad, is required to obey 3 laws: Left identity, applying the unit function to a value and then binding the resulting monad to function f is the same as calling f on the same value: let f be a function returning a monad, then bind(unit(value), f) === f(value).
If you are caught breaking the law you will be subject to whatever penalties the law you broke requires - ranging from a warning to jail or execution. It all depends on what specific law you broke and where you were when you broke it. Originally Answered: What will happen if I disobey the rules in society?
If guilty, you will be sentenced, and get fined, imprisoned or sanctioned according to the law and the severity of your crime. You’ve done it. You’ve built up a little cushion in your bank account — $1,000! It feels good, right? Those days of checking your account balance in a panic are behind you.
The monad laws are simply additional rules that instances are expected to follow, beyond what can be expressed in the type system. Insofar as Monad
expresses a programming pattern, the laws are part of that pattern. Such laws apply to other type classes as well: Monoid
has very similar rules to Monad
, and it's generally expected that instances of Eq
will follow the rules expected for an equality relation, among other examples.
Because these laws are in some sense "part of" the type class, it should be reasonable for other code to expect they will hold, and act accordingly. Misbehaving instances may thus violate assumptions made by client code's logic, resulting in bugs, the blame for which is properly placed at the instance, not the code using it.
In short, "breaking the monad laws" should generally be read as "writing buggy code".
I'll illustrate this point with an example involving another type class, modified from one given by Daniel Fischer on the haskell-cafe mailing list. It is (hopefully) well known that the standard libraries include some misbehaving instances, namely Eq
and Ord
for floating point types. The misbehavior occurs, as you might guess, when NaN is involved. Consider the following data structure:
> let x = fromList [0, -1, 0/0, -5, -6, -3] :: Set Float
Where 0/0
produces a NaN, which violates the assumptions about Ord
instances made by Data.Set.Set
. Does this Set
contain 0
?
> member 0 x True
Yes, of course it does, it's right there in plain sight! Now, we insert a value into the Set
:
> let x' = insert (0/0) x
This Set
still contains 0
, right? We didn't remove anything, after all.
> member 0 x' False
...oh. Oh, dear.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With