You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: blog/posts/2019-09-07-demystifying-monadbasecontrol.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -182,30 +182,30 @@ instance MonadBaseControl b m => MonadBaseControl b (StateT s m) where
182
182
183
183
1. Start by examining the definitions of `InputState` and `OutputState`. Are they what you expected? You’d be forgiven for expecting the following:
184
184
185
-
```haskell
186
-
typeInputState (StateTsm) =s
187
-
typeOutputState (StateTsm) =s
188
-
```
185
+
```haskell
186
+
typeInputState (StateTsm) =s
187
+
typeOutputState (StateTsm) =s
188
+
```
189
189
190
-
Afterall, that’s what we wrote in the table, isn’t it?
190
+
Afterall, that’s what we wrote in the table, isn’t it?
191
191
192
-
However, if you give it a try, you’ll find it doesn’t work. `InputState` and `OutputState` must capture the state of the *entire* monad, not just a single transformer layer, so we have to combine the `StateT s` state with the state of the underlying monad.In the simplest case we get
192
+
However, if you give it a try, you’ll find it doesn’t work. `InputState` and `OutputState` must capture the state of the *entire* monad, not just a single transformer layer, so we have to combine the `StateT s` state with the state of the underlying monad.In the simplest case we get
193
193
194
-
```haskell
195
-
InputState (StateT s IO) = (s, ())
196
-
```
194
+
```haskell
195
+
InputState (StateT s IO) = (s, ())
196
+
```
197
197
198
-
which is boring, but in a more complex case, we need to get something like this:
198
+
which is boring, but in a more complex case, we need to get something like this:
199
199
200
-
```haskell
201
-
InputState (StateT s (ReaderTIO)) = (s, (r, ()))
202
-
```
200
+
```haskell
201
+
InputState (StateT s (ReaderTIO)) = (s, (r, ()))
202
+
```
203
203
204
-
Therefore, `InputState (StateT s m)` combines `s` with `InputState m` in a tuple, and `OutputState` does the same.
204
+
Therefore, `InputState (StateT s m)` combines `s` with `InputState m` in a tuple, and `OutputState` does the same.
205
205
206
206
2.Moving on, take a look at `captureInputState`and`closeOverInputState`.Just as `InputState` and `OutputState` capture the state of the entire monad, these functions need to be inductive in the same way.
207
207
208
-
`captureInputState` acquires the current state using `get`, and it combines it with the remaining monadic state using `lift captureInputState`.`closeOverInputState` uses the captured state to peel off the outermost `StateT` layer, then calls `closeOverInputState` recursively to peel off the rest of them.
208
+
`captureInputState` acquires the current state using `get`, and it combines it with the remaining monadic state using `lift captureInputState`.`closeOverInputState` uses the captured state to peel off the outermost `StateT` layer, then calls `closeOverInputState` recursively to peel off the rest of them.
209
209
210
210
3.Finally, `restoreOutputState` restores the state of the underlying monad stack, then restores the `StateT` state, ensuring everything ends up back the way it’s supposed to be.
0 commit comments