[Haskell-cafe] the trivial monad- thoughts and a question

Brian Hurt bhurt at spnz.org
Sat Jan 12 19:47:40 EST 2008


So, I've been playing around with what I call the trivial monad:

module TrivialMonad where

data TrivialMonad a = M a

recover :: TrivialMonad a -> a
recover (M x) = x

instance Monad TrivialMonad where

 	(M x) >>= f = f x
 	(M x) >> f = f
 	return x = M x
 	fail s = undefined

This is actually a surprisingly usefull little sucker- it allows you to 
"demonadify" code.  Which is usefull when you have some places where you 
want to use the code within a monad, and some places where you don't.  You 
write the base bit of code monadically, and then demonadify it as needed.

The first question I have is it is possible to implement this guy without 
wrapping the value in a constructor?  What I'd like to do is replace the:
 	data TrivialMonad a = M a
with something like:
 	type TrivialMonad a = a
and then be able to junk the recover function.

The second question I have is: is there any hope of getting something like 
this into the standard library?

Thanks,
Brian



More information about the Haskell-Cafe mailing list