[Haskell-cafe] directory tree?

David Roundy droundy at darcs.net
Fri Jun 22 16:46:18 EDT 2007


On Fri, Jun 22, 2007 at 01:19:01PM -0700, Chad Scherrer wrote:
> Haskell is great at manipulating tree structures, but I can't seem to
> find anything representing a directory tree. A simple representation
> would be something like this:
> 
> data Dir = Dir {dirName :: String, subDirectories :: [Dir], files :: [File]}
> data File = File {fileName :: String, fileSize :: Int}
> 
> Maybe these would need to be parametrized to allow a function
> splitting files by extension or that kind of thing. Anyway, the whole
> idea would be to abstract as much of the file stuff out of the IO
> monad as possible.
> 
> I haven't used the "Scrap Your Boilerplate" stuff yet, but it seems
> like that could fit in here naturally to traverse a Dir and make
> changes at specified points.
> 
> The only problem I can see (so far) with the approach is that it might
> make "big changes" to the directory tree too easy to make. I'm not
> sure immediately how to deal with that, or if the answer is just to
> post a "be careful" disclaimer.
> 
> So, what do you think? Do you know of any work in this direction? Is
> there a way to make "dangerous 1-liners" safe? Is there a fundamental
> flaw with the approach I'm missing?

Darcs does this sort of thing (see SlurpDirectory.lhs in the source code),
which can be pretty nice, but you really only want to use it for read-only
purposes.  Early versions of darcs made modifications directly to Slurpies
(these directory tree data structures), which kept track of what changes
had been made, and then there was a "write modifications" IO functions that
actually made the changes.  But this was terribly fragile, since ordering
of changes had to be kept track of, etc.  The theory was nice, that we'd be
able to make the actual changes all at once (only write once to each file,
for example, each file had a dirty bit), but in practice we kept running
into trouble.  So now we just use this for read-only purposes, for which it
works fine, although it still can be scary, if users modify the directories
while we're looking at them (but this is scary regardless...).

Nowadays we've got a (moderately) nice little monad DarcsIO, which allows
us to do file/directory IO operations on either Slurpies or disk, or
various other sorts of virtualized objects.  Someday I'd like to write an
industrial strength version of this monad (which addresses more than just
darcs' needs).
-- 
David Roundy
Department of Physics
Oregon State University


More information about the Haskell-Cafe mailing list