[Haskell] State Transformers and Control.Monad.ST.Lazy

Tom Pledger tpledger at ihug.co.nz
Tue Jun 22 07:06:02 EDT 2004

Vivian McPhail wrote:

>>From the very helpful posts of John Hughes and others concerning 
>"Monadic Loops",  I've been advised to re-implement my neural net 
>simulator using lazy state threads to avoid crashes to which recursive 
>(and tail-recursive) monads lead.
>I had been using monad transformers to lift IO operations:
>data NeuralNet = NN { ... }
>data NNO a = NNO (NeuralNet -> (a,NeuralNet))
>data NNT m a = NNT (NeuralNet -> m (a,NeuralNet)) 
>following the example of a state transformer in "Functional Programming 
>with Overloading and Higher-Order Polymorphism".
>I am trying to reimplement with 
>data ST s a
>from Control.Monad.ST.Lazy and "Lazy Functional State Threads"
>however there is something I think I do not understand, that is the "s" in 
>the ST dataype.

The s is a safety mechanism, to ensure that you don't share an STRef or 
STArray between two state threads. It is usually 
instantiated/substituted where the runST function is applied. You don't 
supply a substitution for s; runST does. (The memoisation library which 
uses ST () is unusual in that respect.)

Normal use of ST for your neural net example would be:

    do nnRef <- newSTRef (NN { ... })  -- set the initial NeuralNet state
       --from here on, you pass nnRef around as a parameter

Control.Monad.State (as opposed to ST) may be a closer match for the NNO 
and NNT types.


More information about the Haskell mailing list