From: Jim Pryor
Date: Sun, 21 Nov 2010 18:36:17 +0000 (-0500)
Subject: week9 tweak
X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=2040369451e2dce6e7e23a815ab1d861752e9869
week9 tweak
Signed-off-by: Jim Pryor
---
diff --git a/week9.mdwn b/week9.mdwn
index 41de4762..a05c132c 100644
--- a/week9.mdwn
+++ b/week9.mdwn
@@ -412,9 +412,9 @@ With the Reader monad, we also had some special-purpose operations, beyond its g
This passes through the current store unaltered, and also returns a copy of the store as its value. We can use this operation like this:
- some_existing_state_monad_value >>= fun _ -> get_state >>= (fun cur_state -> ...)
+ some_existing_state_monad_box >>= fun _ -> get_state >>= (fun cur_state -> ...)
-The `fun _ ->` part here discards the value wrapped by `some_existing_state_monad_value`. We're only going to pass through, unaltered, whatever *store* is generated by that monadic value. We also wrap that store as *our own value*, which can be retrieved by further operations in the `... >>= ...` chain, such as `(fun cur_state -> ...)`.
+The `fun _ ->` part here discards the value wrapped by `some_existing_state_monad_box`. We're only going to pass through, unaltered, whatever *store* is generated by that monadic value. We also wrap that store as *our own value*, which can be retrieved by further operations in the `... >>= ...` chain, such as `(fun cur_state -> ...)`.
The other operation for the State monad will be to update the existing store to a new one. This operation looks like this:
@@ -423,21 +423,21 @@ The other operation for the State monad will be to update the existing store to
If we want to stick this in a `... >>= ...` chain, we'll need to prefix it with `fun _ ->` too, like this:
- some_existing_state_monad_value >>= fun _ -> set_state 100 >>= ...
+ some_existing_state_monad_box >>= fun _ -> set_state 100 >>= ...
-In this usage, we don't care what value is wrapped by `some_existing_state_monad_value`. We don't even care what store it generates, since we're going to replace that store with our own new store. A more complex kind of `set_state` operation might insert not just some constant value as the new store, but rather the result of applying some function to the existing store. For example, we might want to increment the current store. Here's how we could do that:
+In this usage, we don't care what value is wrapped by `some_existing_state_monad_box`. We don't even care what store it generates, since we're going to replace that store with our own new store. A more complex kind of `set_state` operation might insert not just some constant value as the new store, but rather the result of applying some function to the existing store. For example, we might want to increment the current store. Here's how we could do that:
- some_existing_state_monad_value >>= fun _ -> get_state >>= (fun cur_state -> set_state (cur_state + 1) >>= ...
+ some_existing_state_monad_box >>= fun _ -> get_state >>= (fun cur_state -> set_state (cur_state + 1) >>= ...
We can of course define more complex functions that perform the `get_state >>= (fun cur_state -> set_state (cur_state + 1)` as a single operation.
-In general, a State monadic **value** (type `'a state`, what appears at the start of a `... >>= ... >>= ...` chain) is an operation that accepts some starting store as input---where the store might be simple as it is here, or much more complex---and returns a value plus a possibly modified store. This can be thought of as an encoding of some operation on a store serving as a box wrapped around a value.
+In general, a State monadic **box** (type `'a state`, what appears at the start of a `... >>= ... >>= ...` chain) is an operation that accepts some starting store as input---where the store might be simple as it is here, or much more complex---and returns a value plus a possibly modified store. This can be thought of as an encoding of some operation on a store serving as a box wrapped around a value.
State monadic **operations** (type `'a -> 'b state`, what appears anywhere in the middle or end of a `... >>= ... >>= ...` chain) are operations that generate new State monadic values, based on what value was wrapped by the preceding elements in the `... >>= ... >>= ...` chain. The computations on a store that these encode (which their values may or may not be sensitive to) will be chained in the order given by their position in the `... >>= ... >>= ...` chain. That is, the computation encoded by the first element in the chain will accept a starting store s0 as input, and will return (a value and) a new store s1 as output, the next computation will get s1 as input and will return s2 as output, the next computation will get s2 as input, ... and so on.
To get the whole process started, the complex computation so defined will need to be given a starting store. So we'd need to do something like this:
- let computation = some_state_monadic_value >>= operation >>= operation
+ let computation = some_state_monadic_box >>= operation >>= operation
in computation initial_store;;