1

I need a function of type Monad m => m [a] -> [m a], which works just as sequence does, but in reverse.

I've tried searching for any function with this type signature on Hoogle, but there doesn't seem to exist any. Is it impossible in general? Should I be avoiding this type of construction to begin with?

Nicolás Kim
  • 121
  • 4
  • 2
    The answer below (you can't do this) is probably about the best we can say; but this smells like an [XY problem](http://meta.stackexchange.com/q/66377). Perhaps you can open another question discussing how you came to the conclusion that you want this operation; then we may be able to suggest an alternative that helps with what you actually want to do. – Daniel Wagner May 16 '15 at 01:07
  • @user3237465 Good eye! – Nicolás Kim May 16 '15 at 06:23
  • @DanielWagner You're right. I managed to work around it. Thanks for the advice! – Nicolás Kim May 16 '15 at 06:25

1 Answers1

8

There can be no such function, because not every monad admits a kind of "exit function" m a -> a. IO exemplifies this: if you have an IO a, you cannot remove the IO constructor (except via unsafePerformIO which you probably don't want to use).

The monad typeclass functions

return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b

each have an m a after the rightmost arrow, so without more constraints on your function, the m constructor cannot be removed from around the list [a].

Alexander Vieth
  • 876
  • 6
  • 9