Proposal: System.Environment.getEnv should return Maybe

Henning Thielemann lemming at
Mon Mar 12 09:58:51 CET 2012

On Sun, 11 Mar 2012, Brandon Allbery wrote:

>       "exception" in what sense? Certainly not an exception in IO, since I do not like the
>       implicitness of exceptions in IO. No I think, Maybe is a perfect return type for 'lookup'.
>       What I want to say is, that Nothing is also an exceptional value.
> (So, you just tossed in <stdrant.h> C-style without looking at the context?)

You mean something like "Ceterum censeo Carthaginem esse delendam"? :-)

> The context is that getEnv currently throws an IO exception if the name isn't found, and we'd like it
> to be IO (Maybe String) because it's really a keyed store where absence of the key isn't really
> justification for an IO exception, but is a reasonable use for a Maybe.  It's the cranky IO exception
> that needs to go away, not the notion of an exceptional condition in general.

I replied to Conrad Parker who said "+1 to the general concept of not 
using Haskell exceptions in library code." and John Lato who said 
something similar. If they refer with "Haskell exceptions" to the 
exceptions that are implicit in the IO monad, then I support this 
proposal. That's the context I refered to.

If no IO exceptions for getEnv then two main alternatives remain: IO 
(Maybe String) and MaybeT IO String (or ErrorT e IO String or ExceptionalT 
e IO String). The switch between these two is simple - simpler than the 
one between IO exception and IO Maybe. The choice between them should 
depend on what we expect to be the most frequent case: If aborting a 
series of computations is the main use case, then the type should be 
MaybeT IO String. If choosing an alternative action in case of getEnv 
returning Nothing is the main use case, then the type might be better IO 
(Maybe String). But even in this case you could use mzero on MaybeT IO 
String. Both use cases seem to be reasonable to me. So what is the main 
use case?

Currently there is no one planning to use MaybeT in System.Environment, 
since this would mean to make base/haskell2010 depend on transformers. But 
an option might be to start a package where generally IO exceptions are 
replaced by MaybeT and its friends. Then we could leave System.Environment 
as it is.

More information about the Libraries mailing list