Fast Mutable arrays and a general question about IO

Jon Cast jcast@ou.edu
Thu, 01 May 2003 14:32:52 -0500


Ron de Bruijn <rondebruijn@yahoo.com> wrote:

> Hi there,
> 
> I have been busy for a couple of days now, to just get a bit more
> feeling on Haskell and I have read a bunch of tutorials, also about
> Monads and experimented of course. And now I want to use it for my
> real world problem. I think I understand the languageprinciples good
> enough, but I yet don't understand the syntax of all of the
> functioninterfaces. And that's my problem.
> 
> I want to create the fastest array as possible in Haskell

Why?  It's not always necessary to go for maximum speed; do you have a
reason for needing it in this case?

> and I understood that I should use this function(the function below
> that creates a mutable array). The only problem is that I don't kwow
> how to use it.
> 
> Constructing mutable arrays 
>  
> newArray :: (MArray a e m, Ix i) => (i, i) -> e -> m (a i e)

> Builds a new array, with every element initialised to undefined.

No; that's newArray_.  (newArray bounds x) builds an array with every
element initialized to x.

> Reading and writing mutable arrays 
>  
> readArray :: (MArray a e m, Ix i) => a i e -> i -> m e
> 
> Read an element from a mutable array 
>  
> writeArray :: (MArray a e m, Ix i) => a i e -> i -> e -> m () 
> Write an element in a mutable array 
> 
> I see it takes two arguments: a tuple of an index type (I use Int, so
> for example (0,1) 0 for the lower bound, 1 for the upper bound.),

I assume you're talking about newArray here, right?  If so, you're
correct.

> but the e (according to "GHC documentation" it says the "element
> type").  Well the type of my elements should be my own datatype
> myDataType, but I can't even get it done to create simple mutable
> array's of Int or Bool.

> How can I make it work with my own datatype myDataType (could you give
> some examples for the use of the below functions?)?

I think you mean `above'.  In any case, could you show us some code you
have that's not working?  That'll make it much easier for us to answer
your questions.

> Is it OK to say that in Haskell any value that is put from an extern
> source of the program (reading a file, reading stdin etc.) to a really
> pure function, must be in a do notation (essentially being a Monadic
> operation)?

Yes.  More specifically, they don't need to use do-notation (although if
you don't know why not, don't worry about it), but they do have to be
monadic.  Specifically, they have to use the IO monad.

> So to put it more concrete: In any Haskell program that does IO there
> is always some function that has this form:
> 
> do x<-someSource
>    putBoundVariableToSomeOtherFunctionThatDoesMonadicOperations
>      (restOfPureFunctionalProgramForExample x)

No.

> So it's not possible to write some function that does the following:
>
> f::Int
> f do x<-getChar   --suppose the Char is a number 
>      someSpecialReturnFunction x
>
> and than the result of the IO is only Int after some alterations of
> above code.

Correct.  (I don't think this is a big issue; a Haskell program is
required to execute in terms of a value `main' in the IO monad.)

<snip>

Jon Cast