[Haskell-cafe] Sequencing Operations in a Monad

SevenThunders mattcbro at earthlink.net
Fri Sep 14 20:50:58 EDT 2007

I have a matrix library written in C and interfaced into Haskell with a lot
of additional Haskell
support.  The C library of course has a lot of side effects and actually
ties into the BLAS libraries, thus at the present time, most of the
interesting calls are done in the IO monad.  I have no intention of
rewriting what I've done so far or using someone elses Matrix library. 
(Mine is tuned somewhat for my application).

I attempted to extend my Haskell matrix interface using type classes (real
and complex matrices) and have run into a conceptual problem.  I would like
to be able to use operator notation for matrix arithmetic.
R = Q * (A + B)

Unfortunately if I wrap my  matrix references in the IO monad, then at best
computations like 
S = A + B are themselves IO computations and thus whenever they are
'invoked' the computation ends up getting performed repeatedly contrary to
my intentions.  For example I might have some code like this,

let S = A += B in
       (r,c) <-  size S
       k  <- matindex S

code of this nature results in S being applied twice and if  the operator += 
has side effects, those side effects will be applied twice.  Even if there
are no side effects the computation will unnecessarily be applied twice. 
What I need is a way to force a single execution of the IO action without
losing the syntax sugar. 

 If you arrange the types to try to do all the operations inside the IO
monad you can't chain together more than 1 binary operation.  eg.

   S <- A + B
   Z <- Q * S


   S <-  Q * (A + B)

Are there any suggestions for this dilemma?  Am I using the wrong monad for
this task?
View this message in context: http://www.nabble.com/Sequencing-Operations-in-a-Monad-tf4446047.html#a12685983
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

More information about the Haskell-Cafe mailing list