Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between StateT s (ExceptT e m) and ExceptT e (StateT s m)?

Monad transformers are tricky, and I'm not sure (= don't have good intuition) which one should go on top.

like image 500
phadej Avatar asked Aug 05 '15 17:08

phadej


1 Answers

StateT s (ExceptT e m)

This says:

  • Start with m
  • Add exceptions to that
  • Add state to that

Now, 'adding exceptions' means your actions can terminate in two ways: either with a normal return value, or with an exception.

'Adding state' means that an extra bit of state output gets included in the normal return values.

So in StateT s (ExceptT e m), you only get a result state if there is no exception.

On the other hand,

ExceptT e (StateT s m)

says:

  • Start with m
  • Add state to that
  • Add exceptions to that

'Adding state' means that an extra bit of state output gets included in the return values of m.

But now, your added exceptions get added as an alternative return value inside the StateT monad. So you always get a state output, and then you may get a normal return value or you may get an exception along with it.

like image 188
Jonathan Cast Avatar answered Sep 27 '22 19:09

Jonathan Cast