On Wed, Jun 25, 2014 at 12:33 PM, Mateusz Kowalczyk <fuuzetsu@fuuzetsu.co.uk> wrote:
On 06/25/2014 05:54 AM, Rafael Almeida wrote:
> Hello,
>
> You guys have been great taking my questions. Thank you. Now I have another
> one :)
>
> What can go wrong if I make an Monad instance but don't follow Monad rules
> (identity and associativity)? Sure it would be misleading for someone using
> the non-conforming class. They may make code that assume those laws,
> although they don't hold. However, could it make the compiler generate bad
> code

The compiler makes no assumption (at least that I know of) that laws are
followed. It should never generate ‘bad’ code (whatever that might mean)
anyway.

The compiler makes assumptions about associativity when de-sugaring do-notation.  If the monad laws aren't followed, it's possible for these two blocks to show different behavior (given that a,b,c are all values of the misbehaved Monad instance):

> do { a; b; c }

> a >> b >> c

I think everyone can agree that this is surprising, at the very least.  Although it's not the compiler that's generating bad code here.

John L.