
On Fri, 2010-01-01 at 15:07 -0800, Evan Laforge wrote:
You could say it's logically pure if you guarantee that no one else is modifying the DB while your program is running, and you are willing to completely abort on a network error, in which case there's a hack called unsafeInterleaveIO.
What do you mean by "completely abort"? Couldn't it simply raise an exception that could be handled in main?
However, I'm guessing no one is going to recommend actually doing that. The Prelude getContents function does that and it gets a lot of flak for poor error handling, not closing the handle when you want it to, etc. Talking over the network will have all those same problems.
Yes, when I saw the definition of getContents, I suspected problems were possible.
If you want to operate over a large structure and hide the fetching part, you can look into the iteratee stuff. Basically you would define a function that opens a socket, passes chunks of data to a passed in pure iteratee function, and closes the socket afterwards. If you have a random access Map then I can't think of anything more elegant than the standard imperative "lookup :: Key -> RemoteMap -> IO Val" possibly with caching. Put the reading strategy in an IO function, and the rest of the processing in passed-in pure functions.
Interesting. I wonder if it might be useful to approach it like a virtual memory system. Searching for a value that hasn't been loaded into the structure would raise an exception, which would then be caught, handled by using normal (safe) IO, and then the computation could be retried. Is that idea crazy, or does it have some merit? Regards, Jeff Davis