[Haskell-cafe] Bool is not...safe?!

MarLinn monkleyon at gmail.com
Sat Jul 7 12:10:16 UTC 2018


> You might be interested in another kind of Boolean-like types: Commutative monads. A monad is called commutative if the order of actions does not matter, i.e. if the following always holds.
> do { x <- mx; y <- my; return f x y} == do {y <- my; x <- mx; return f x y}
> For example, Maybe is commutative, and [] is up to permutation. For such a monad m consider the type m (). Observe that Maybe () is isomorphic to Bool. Can we derive some operations generically? Indeed,
>
> true = return ()
> (&&) = liftM2 const
> (||) = mplus
> false = mzero

I don't think it's by definition yet, but surely by convention, that 
this is a restricted form of

true  = pure ()
(&&)  = (<*)
(||)  = (<|>)
false = empty

All of these functions require only an Applicative or an Alternative, 
and except for "true", they're all in the library.

Which shows another reason to think twice before using a bool: 
Applicatives can be a better fit. They can often carry the question 
behind the bool and the value this question is about in one convenient 
structure.

Cheers,
MarLinn



More information about the Haskell-Cafe mailing list