State Monad, why not a tuple?

The State monad essentially works with a type state -> 'res * 'state which represents a computation that takes some initial state and produces result (together with a new value of the state).

The State monad essentially works with a type 'state -> 'res * 'state, which represents a computation that takes some initial state and produces result (together with a new value of the state). If you're asking whether it makes any difference whether we give some special name to this type (e.g. State) then the answer is it doesn't really matter. The only purpose of giving some special name to the type is that it makes the code more readable.

For example, let's look at the two possible type signatures of the following example: let foo n = state { let! M = getState() do! SetState(m + 1) return sprintf "Result: %d" (n * m) } // Using State type: val foo : int -> State // Using the underlying representation: val foo : int -> int -> int * state The first type signature more clearly says that we're writing function in some monad.

The second example is just a function that takes two int values. I think the main benefit of the first one is that you can more easily realize that the type can be used from other monadic computation (written using state { ... }). However, as I already noted, this isn't a technical requirement.

People probably use this style, because many monads come from Haskell, where monads are associated with the type (such as State) and not a computation builder (such as state), so it sounds like a good idea to define a new type for every monad in Haskell.

Thanks, Yes I have no intention of using the tuple version at all, I we were merely discussing it on IRC (##fsharp @ freenode) and none of us could see a technical reason for the State type. – thr Apr 8 '10 at 8:55.

The type of the monadic value in your example isn't just a tuple - it is a function returning tuple: 'state -> 'res * 'state If you're asking whether you can use just 'state * 'res as the type of the monadic computation, then the answer is no. That wouldn't work, because there is no way to (safely) implement the return operation, which would have to have the following type signature: // how would we get a value of type 'state in the implementation? Val return : 'a -> 'state * 'a.

Ah, yes, if the question is: should I use a single-tag Discriminated Union that carries a data value of type T, or should I just use T, then you can use either. In Haskell, you need to use a data tag with monads, since the Haskell do syntax infers the monad type based on value types (a tuple representation could be an instance of at most a single Monad). Whereas in F#, computation expressions are explicit about the monad type (e.g. State { ... } or async { ... } or whatever) so this restriction is not necessary, the same representation type could be used for multiple monads.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions