[ALGOM7] maybem breaks the monad laws. Created: 15/Sep/12 Updated: 05/Feb/14 Resolved: 05/Feb/14 

Status:  Closed 
Project:  algo.monads 
Component/s:  None 
Affects Version/s:  None 
Fix Version/s:  None 
Type:  Defect  Priority:  Major 
Reporter:  Seth J. Gold  Assignee:  Konrad Hinsen 
Resolution:  Completed  Votes:  0 
Labels:  None 
Description 
One of the monad laws is that (mbind (mresult v) f) should be the same as (f v). However, this is not the case in maybem: user=> (withmonad maybem (mbind (mresult nil) nil?)) user=> (nil? nil) The crux of the problem is that in algo.monad's maybem, there is no way to wrap a nil in a Justlike container. 
Comments 
Comment by Seth J. Gold [ 15/Sep/12 3:27 PM ] 
Just realized that that demonstration doesn't actually work, because the function passed to mbind is supposed to return a monadic value. Here's a better one: user=> (withmonad maybem (mbind (mresult nil) (comp mresult nil?))) 
Comment by Bruno Tavares [ 07/Jun/13 8:51 PM ] 
I don't think this would be a valid example. Here is an example in Haskell ; Clojure example ;; Both examples will fail Those errors would not be possible to happen with Haskell's type system. The following snippet shows it evaluates properly the monadic bind law for valid operations (let [value 1 
Comment by Konrad Hinsen [ 05/Feb/14 5:20 AM ] 
The Clojure implementation of maybem is based on the convention that nil is not a value, but a marker for "not a value". That's a very common convention in Clojure, so from a practical point of view this is the most useful way to implement maybem. There are other options, and everyone is invited to implement their personal preference and use it in place of maybem. For example, a monadic value could be either an empty list or a list containing a single value. Note that the Haskell approach of setting up a special type for the maybem container is not an option in a dynamically typed language. Whatever you choose to represent "no value" can be created outside of the monad and thus be used to violate the monad laws. It must be understood that the monad laws are valid only subject to the conventions of the specific monad. 