{---------------------------------------------------------------------

    Test d'utilisation du monad transformer ReaderT.

    mt 12.03.2006
   
---------------------------------------------------------------------}

module Main where

import Data.IORef            -- pour les variables mutables dans IO
import Control.Monad.Reader  -- pour le transformeur ReaderT


----------------------------------------------------------------------
-- code sans utilisation du ReaderT
----------------------------------------------------------------------

-- L'etat global a trimballer partout
-- voir newtype GlobalState

-- initialiser l'etat global
-- voir initGlobalState

-- une fonction ayant besoin de l'etat global
-- voir display'

-- le main initialize l'etat global et
-- le passe a une fonction.
main' :: IO()
main' = do
    gs <- initGlobalState
    display' gs


----------------------------------------------------------------------
-- code avec utilisation du ReaderT
--
-- copie d'une reponse a une question que j'ai pose
-- sur haskell@haskell.org :
--
--   Chris Kuklewicz  <haskell@list.mightyreason.com> à haskell 
--
--   If the things need to be more explicitly mutable you can
--   *) Create a bunch of IORefs (or MVar for thread safety)
--   *) Put them into some data structure (your own or perhaps tuples)
--   *) Use a ReaderT from Control.Monad.Reader, ReaderT YourData IO a
--
--   Then you can
--   *) lookup the IORef you need with using "asks"
--   *) read/write/update that mutable data
----------------------------------------------------------------------

-- my global state
newtype GlobalState = GlobalState
    { ref :: IORef Int }

-- initializes globals
initGlobalState = do
    i <- newIORef 1
    return (GlobalState i)

-- displays the state
display' gs = do
    i <- readIORef (ref gs)
    putStrLn (show i)

-- modifies the global state
inc' gs = do
     i <- readIORef (ref gs)
     writeIORef (ref gs) (i+1)

-- displays in the ReaderT
display :: ReaderT GlobalState IO ()
display = ReaderT (\gs -> display' gs)

-- modifies in the ReaderT
inc :: ReaderT GlobalState IO ()
inc = ReaderT (\gs -> inc' gs)

-- modifies then displays in the ReaderT
comp = do inc -- or " io inc' ", see below (**)
          -- how can I do IO here ?
          -- yes, I can "asks" to obtain the ref,
          -- but then ? how can I "readIORef" ?
          display

-- initilizes the global state then
-- performs the comp in the ReaderT
main = do
    gs <- initGlobalState
    runReaderT comp gs


-- maybe I can use something like this ?
-- maybe it already exists ?
io f = ReaderT (\gs -> f gs)

-- (**) inc can be rewritten as
inc'' = io inc'


-- is-it ok ? 'io' must always be used since the global state is a bunch of IORef.
-- in which case things would need to be more explicitly mutable ?


-- what's the advantage here ?
-- i can do this
main'' = do
    gs <- initGlobalState
    inc' gs
    display' gs
