framework for composing monads?

Manuel M. T. Chakravarty chak@cse.unsw.edu.au
Mon, 19 Feb 2001 13:58:09 +1100


Elke Kasimir <elke.kasimir@catmint.de> wrote,

> (Moving to haskell cafe...)
> 
> On 18-Feb-2001 Manuel M. T. Chakravarty wrote:
> >> It is even acceptable for me to manage the state in C -
> >> independent of the API design - but then some time there 
> >> will be the question: Why do I always say that that Haskell 
> >> is the better programming language, when I'm
> >> really doing all the tricky stuff in C?...
> > 
> > Sure - therefore, I proposed to use `IORef's rather than C
> > routines. 
> 
> Thanks for the hint! 
> 
> I took a look at them and now have some questions:
> 
> a) It is clear that I need some C-link to access the cli/odbc lib.
> Up to now I planned to use Haskell Direct for this. Except of this, I want
> to stick to Haskell 98 and seek for maximal portability. 

I am all for portable code, too.

> Practically, this raises the question of wether nhc and hbc support hslibs
> or else I can provide a substitute for IORef's for these compilers.

nhc does supports `IORef's (they come in the module
IOExtras).  I am not sure whether H/Direct works with nhc,
though.  Sigbjorn should be able to answer this.

> b) What I finally need is "hidden state". My first attempt to get one 
> using IORefs is:
> 
> > import IOExts
> >
> > state :: IORef Int
> > state = unsafePerformIO $ newIORef 0
> >
> > main = seq state $ do
> >                   writeIORef state 1
> >                   currstate <- readIORef state
> >                   putStr (show currstate)
> 
> Is this the right way?

Yes, except that you want to have

  {-# NOINLINE state #-}

too.  Wouldn't be nice if ghc were to choose to inline
`state', would it? ;-)

Cheers,
Manuel