State Transformer

Jorge Adriano jadrian@mat.uc.pt
Fri, 11 Jan 2002 22:56:24 +0000


> If I understand you correctly, you want global mutable variables, right?
> This is, I believe, only possible using IORef's and unsafePerformIO.

Kind of, I'm searching for the best approach to keep track of data in my 
algorithms without constantly changing signatures.
State monad as defined in the paper I mentioned "monads for the working 
haskell progarmmer" seemed the most elegant way. I saw ST in the Ghc and 
thought it was supposed to do the same... 
I never used mutable variables, but seems like it would prabably be the most 
efficient way to do it, as for more elegant, I don't know... I'm thinking 
about going for a simple State monad, no mutable variables... probably will 
face some efficiency problems though...

>
> Something like this:
> | xRef :: IORef Int
> | xRef = unsafePerformIO $
> |  newIORef 0
> | update_x :: IO ()
> | update_x = do
> |    x <- readIORef xRef
> |    writeIORef xRef (x+1)
>
> (Sorry for formatting, I'm using Outlook now :-)
>
> Of course this is not very functional, nice, safe or robust under compiler
> transformations (there probably should be a NOINLINE pragma there
> somewhere)

And I was trying to get away from unsafePerformIO... :-)


J.A.