Challenge 3: Why Stack Order Changes What Happens to State on Error — Possible Solution ==================================================================== // With ExceptT e (StateT s IO) -- ExceptT is the OUTER layer, StateT // is INNER -- an error thrown partway through a sequence of state // updates still leaves the underlying StateT computation free to have // genuinely run and updated its state up to that point, because the // State layer sits UNDERNEATH the error-handling layer and isn't // itself aware that an "error" occurred at the ExceptT level above // it. In practice, this ordering commonly means: the state changes // already made before the error DO persist and are visible if you // inspect the final state afterward, even though the overall // computation reports a Left (an error) as its RESULT value. // // With StateT s (ExceptT e IO) -- StateT is the OUTER layer, ExceptT // is INNER -- the entire computation, state-threading included, is // running INSIDE the error-handling context. If an error is thrown // partway through, the ExceptT layer short-circuits the WHOLE // remaining computation, including whatever StateT was doing to // thread state through it. In practice, this ordering commonly means // state changes made before the error are effectively discarded along // with everything else, because the failure aborts the entire // state-threading computation from the point of failure onward, not // just the "logical result" being computed. // // The core reason this differs: each transformer's own effect only // "sees" and behaves consistently with the layers stacked BELOW it, // not above it. Which effect is "on the outside" determines whether a // short-circuiting error can also short-circuit (or leave untouched) // the effects that are nested inside it -- exactly the honest, // well-documented ordering sensitivity the chapter's own warn-box // describes. WHY THIS WORKS AS AN ANSWER ------------------------------ This correctly reasons about which effect wraps which in both orderings and traces the practical consequence for already-made state changes when an error occurs partway through, matching the chapter's own description of stack-order sensitivity as a real, well-documented concern rather than a beginner-only trap.