[Haskell-cafe] develop new Haskell shell?

Brian Hulley brianh at metamilk.com
Thu May 11 18:05:14 EDT 2006

Donn Cave wrote:
> On Wed, 10 May 2006, Donald Bruce Stewart wrote:
>> Funny this should come up. We've just had several submissions to
>> work on a functional shell for the google summer of code.
>> Here's a bit of a summary of what's been done in Haskell I prepared a
>> while back.
>> http://www.cse.unsw.edu.au/~pls/thesis-topics/functionalshell.html
> My background is more shells than FP, so I'm not sure what to make
> of this - if we're talking about doing things for educational
> purposes, or serious attempts to make a viable alternative to ...
> something.

Here is an example of the kind of thing you could have with a pure 
interactive Haskell shell:

newtype FileName = FileName String    -- for example
newtype DirName = DirName String

newtype Shell a = Shell (IO a) deriving (Monad, MonadIO)

ls :: Shell [FileName]
cat :: FileName -> [FileName] -> Shell ()

-- run a command with the current directory set to DirName
withDir :: DirName -> Shell a -> Shell a

-- catenate all files in a specified directory

catenate outputFile dir = withDir dir $
                                            ls >>= cat outputFile

Of course the above could no doubt be improved but surely it is already far 
easier to understand and much more powerful than the idiosyncratic text 
based approach used in UNIX shells (including rc). To extend the example 
further, with a simple function to split a filename into a name and an 
extension, we could rename all .txt files to .hs files with:

split :: FileName -> (String, String)
unsplit :: (String, String) -> FileName
rename :: FileName -> FileName -> Shell ()

rename extFrom extTo files = do
          let candidates = filter (\(_,ext) -> ext==extFrom) (map split 
          mapM_ (\f@(n,_) -> rename (unsplit f) (unsplit (n, extTo))) 

%           ls >>= rename "txt" "hs"

So although the above may be educational, I think it would also be truly 
useful and practical. It would also save a lot of trouble if everything was 
done in Haskell instead of using all those UNIX commands, because then 
people would only need to learn one language to be able to do anything with 
their computing environment.

Regards, Brian. 

More information about the Haskell-Cafe mailing list