I Hate IO

Ashley Yakeley ashley@semantic.org
Thu, 9 Aug 2001 03:53:23 -0700


At 2001-08-09 03:01, Simon Marlow wrote:

>Ok, I agree so far.  Are you suggesting the IO library should be
>changed? 

Yes.

>How?

Hmm... well the file part to use Word8s and not be stream-based. Perhaps 
something like this:

--
data OpenFile = ...

openFileReadOnly :: FilePath -> IO OpenFile
openFileReadWrite :: FilePath -> IO OpenFile
closeFile :: OpenFile -> IO ()

fileWritable :: OpenFile -> IO Bool

fileLength :: OpenFile -> IO Integer

setFileLength :: OpenFile -> Integer -> IO ()
-- fill with zeros if extending

readFileBlock :: OpenFile -> Integer -> Integer -> IO [Word8]
-- "readFileBlock file start length", return truncated array if past file 
end

writeFileBlock :: OpenFile -> Integer -> [Word8] -> IO ()
-- would extend file if writing past end
--

This covers ReadMode and ReadWriteMode. I don't really understand 
WriteMode, but depending on what operations that implies, that could be 
included too. AppendMode is by nature a weird sink-based thing and would 
have a separate interface:

--
data AppendableFile = ...

openFileAppendable :: FilePath -> IO AppendableFile
closeFileAppendable :: AppendableFile -> IO ()

appendToFile :: AppendableFile -> [Word8] -> IO ();
--

Of course, on some implementations the AppendableFile functions could be 
implemented with the OpenFile functions. In any case, network functions 
would not use the same datatypes at all. If you want to provide functions 
that work with different things that follow a single pattern, use classes.

-- 
Ashley Yakeley, Seattle WA