I am currently reading about the Alternative/MonadPlus typeclasses in wikibooks. It describes the difference very well. However, one puzzling part is the guard
function, which I am assuming, is used for "short-circuiting" a computation. (Am I right?)
The function guard
although defined in Control.Monad
has an Alternative
constraint, as following (link).
guard :: (Alternative f) => Bool -> f ()
guard True = pure ()
guard False = empty
But the above article, mentions that only the MonadPlus
is required to enforce the left zero and right zero laws (Hence the stronger claim).
mzero >>= f = mzero -- left zero
m >> mzero = mzero -- right zero
Given the purpose of the guard
function, shouldn't it be defined with a MonadPlus
constraint? Don't we need the stronger laws if guard
is supposed to "short-circuit" the computation? I am curious about the reason behind the specific design choice.
p.s.: I don't know what is a better way to describe the "cancelling the upfront computation" behavior other than the word "short-circuiting"?