From: Jim Pryor
Date: Sun, 21 Nov 2010 18:43:30 +0000 (-0500)
Subject: week9 tweak
X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=3c0613a82cd81a58fc9143383f4f6cdfe55c6098
week9 tweak
Signed-off-by: Jim Pryor
---
diff --git a/week9.mdwn b/week9.mdwn
index 85cbdef0..dbabb6d3 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_box >>= fun _ -> get_state >>= (fun cur_state -> ...)
+ some_existing_state_monad_box >>= fun _ -> get_state >>= (fun cur_store -> ...)
-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 box. 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 box. We also wrap that store as *our own value*, which can be retrieved by further operations in the `... >>= ...` chain, such as `(fun cur_store -> ...)`.
The other operation for the State monad will be to update the existing store to a new one. This operation looks like this:
@@ -427,11 +427,11 @@ If we want to stick this in a `... >>= ...` chain, we'll need to prefix it with
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_box >>= fun _ -> get_state >>= (fun cur_state -> set_state (cur_state + 1) >>= ...
+ some_existing_state_monad_box >>= fun _ -> get_state >>= (fun cur_store -> set_state (cur_store + 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.
+We can of course define more complex functions that perform the `get_state >>= (fun cur_store -> set_state (cur_store + 1)` as a single operation.
-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.
+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 a static encoding of some computation on a store, which encoding is used as a box wrapped around a value of type `'a`. (And also it's a burrito.)
State monadic **operations** (type `'a -> 'b state`, what appears anywhere in the middle or end of a `... >>= ... >>= ...` chain) are operations that generate new State monad boxes, 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.