Re: Fast Mutable arrays and a general question about IO

Ron de Bruijn
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

Why? It's not always necessary to go for maximum speed; do you have a reason for needing it in this case?
Almost any operation in my program works on array's and before the program terminates, there have been an awful lot of operations on it. If there is an other kind of array that mutates and doesn't create a copy of it when changing a value and isn't more than 1,5 times as slow as an array in C++ then that should also be ok. The program would run in a scale of hours or days. And if it's twice as fast, than I could calculate two times as much.
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.
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.
Well I don't have really code, because I want to begin programming it. I will restate my goal: I want to be able to read, update and create arrays in Haskell of type myDataType(suppose my constructor function is Con Int String Int on a way that's fast, preferabily the fastest method.
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.
I know it's also possible to use (>>=) and a function that works on values of the bound variable.
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.
Why not? Greets Ron __________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com

Hello. I am having two problems. I have a program that reads two texts file
and writes a new text with its info. So I have done so that if main file
doesn't exist I just output an error string but my problem is when it comes
to the second file. This file is not exactly needed so if it is not there,
the program should just ignore the file. I tried doing that but if the
second file is there I also get an error saying that it couldn;t find the
file. So is it possible to check whether a file exists or not outputting and
empty string if not or the actual data from it if so without giving an error
and making the program not run??
For example
main = do
mainfile <- readFile "xxxxx.txt"
secondfile <- readMyFile "yyyy.txt"
writeFile "test.txt" mainfile++secondfile
readMyFile file = do
list <- readFile file
if list == "" then return "" else return list
Apparently this doesn't work and if file doesn't exist it still gives me an
error.
Another problem I ran into is with IO[String] and [String].
I have a function like
foo = do
nf <- readFile "xxx.txt"
return (lines nf)
Then when I try using foo like a [String] in other programs I run into type
error. How can I turn this IO[String] into a [String] so I don't actually
have to open this file in the main and then use it as an argument of another
function running "lines' on main method????
Best Regards
Alex
----- Original Message -----
From: "Ron de Bruijn"
Why? It's not always necessary to go for maximum speed; do you have a reason for needing it in this case?
Almost any operation in my program works on array's and before the program terminates, there have been an awful lot of operations on it. If there is an other kind of array that mutates and doesn't create a copy of it when changing a value and isn't more than 1,5 times as slow as an array in C++ then that should also be ok. The program would run in a scale of hours or days. And if it's twice as fast, than I could calculate two times as much.
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.
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.
Well I don't have really code, because I want to begin programming it.
I will restate my goal: I want to be able to read, update and create arrays in Haskell of type myDataType(suppose my constructor function is Con Int String Int on a way that's fast, preferabily the fastest method.
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.
I know it's also possible to use (>>=) and a function that works on values of the bound variable.
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.
Why not?
Greets Ron
__________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Fri, 2 May 2003 23:06:36 +0200
"Alexandre Weffort Thenorio"
Hello. I am having two problems.
http://www.haskell.org/tutorial/io.html conveniently answers both. Read the rest of it too.

Thanks I read the whole thing and I got what you mean but I solved the
problem with doesFileExist function from Directory library (Read other mail)
although this makes me wonder one thing: How do you know the name of the
error you are getting to put it aside and give a proper error handling just
for that error?? For example out of the page you gave me we have:
getChar' :: IO Char
getChar' = getChar `catch` eofHandler where
eofHandler e = if isEofError e then return '\n' else ioError e
Here the author knows what is the name of the error he wants to put aside
(isEofError) but how can I find out the name of an error that I am getting.
For example when I was getting the problem with file not existing I was
getting:
Action: openFile
Reason: No such file or directory
File: ./nagrarom.txt
But no error name. I tried catching the error and printing out the error
name but was'n't really successfull:
main: catch (do
readFile "xxx.txt") (\e -> putStrLn (show e))
And what I got was
"does not exist"
Again thanks.
Best Regards
NooK
----- Original Message -----
From: "Derek Elkins"
On Fri, 2 May 2003 23:06:36 +0200 "Alexandre Weffort Thenorio"
wrote: Hello. I am having two problems.
http://www.haskell.org/tutorial/io.html conveniently answers both. Read the rest of it too.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sat, 3 May 2003 00:35:49 +0200
"Alexandre Weffort Thenorio"
And what I got was
"does not exist"
and the selector for that is isDoesNotExist. There are only so many IOErrors, and they are more or less self-explanatory.
participants (4)
-
Alexandre Weffort Thenorio
-
Derek Elkins
-
Jon Cast
-
Ron de Bruijn