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

Daniel Fischer daniel.is.fischer at web.de
Sat Jan 12 19:40:30 EST 2008


Am Sonntag, 13. Januar 2008 01:47 schrieb Brian Hurt:
> 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.

No, but you can use a newtype instead of data, so you have no run-time 
overhead.

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

It's already there:
Control.Monad.Identity

>
> Thanks,
> Brian
>
Cheers,
Daniel



More information about the Haskell-Cafe mailing list