[Haskell-beginners] given these types (HFuse), can I share state?

Jon Dowland jon+haskell-beginners at dowland.me
Wed Mar 28 16:32:26 CEST 2012


Hi,

Firstly, this is my first post to this list, I consider myself a Haskell
beginner but this question might be a bit esoteric so please let me know if I'm
better directing this elsewhere.

I'm writing a small fuse-based filesystem[1] using hfuse[2].  I get to write
functions to a defined type.  One of these is 'open', for when the user opens a
file; another is 'read', for when a user invokes the read syscall. Here are the
type signatures:

  fuseOpen :: FilePath -> OpenMode -> OpenFileFlags -> IO (Either Errno fh)

  fuseRead :: FilePath -> fh -> ByteCount -> FileOffset -> IO (Either Errno ByteString)

In the case of my filesystem, for a typical work pattern of open, read, read, read…;
there's an amount of setup that is required up front (possibly combining two or more
files via patches; uncompressing files, etc.) which can be quite expensive.

With my current implementation, I am having to do this work in the read function[3],
because I can't figure out a way of doing it in the open function and sharing the
state with subsequent reads.  I think this is likely to be a performance issue and
I'd prefer to have it done once, in the open function.

I know that there are various 'state' monads, although I've never used them. In
my case the signature for my functions is fixed, but the 'return' type is the
IO monad.  Is there any possibility for me to hide/share state 'through' the IO
monad?


Thanks for your help,


[1] http://jmtd.net/software/rdifffs/
[2] http://hackage.haskell.org/packages/archive/HFuse/0.2.4/doc/html/System-Fuse.html
[3] the work takes place within 'rdiffIncrementReadFile', here:
    https://github.com/jmtd/rdifffs/blob/master/rdifffs.lhs#L558


-- 
Jon Dowland




More information about the Beginners mailing list