[Haskell-cafe] Re: Handles with their IOMode in their type

Bas van Dijk v.dijk.bas at gmail.com
Wed Dec 9 16:51:31 EST 2009

On Tue, Dec 8, 2009 at 7:22 PM, Lee Houghton <gmane at asztal.net> wrote:
> I like this idea.


> A small observation, though:
>> stdin :: ReadModes ioMode => Handle ioMode
>> stdin = Handle SIO.stdin
> This allows writing to stdin by choosing ReadWriteMode as the ioMode. I
> think it would be better to have just
>> stdin :: Handle ReadMode
> *HandleExplicitIOMode> hPutStrLn (stdin :: Handle ReadWriteMode) "This
> shouldn't typecheck!"
> *** Exception: <stdin>: hPutStr: illegal operation (handle is not open for
> writing)
> This also shows another reason for stdin, stdout and stderr to be
> monomorphic:
>> hGetLine stdin
> <interactive>:1:0:
>    Ambiguous type variable `ioMode' in the constraint:
>      `ReadModes ioMode'
>        arising from a use of `hGetLine' at <interactive>:1:0-13
>    Probable fix: add a type signature that fixes these type variable(s)

Rightly spotted, thanks!

I will change the types to:

stdin :: Handle ReadMode
stdout :: Handle WriteMode
stderr :: Handle WriteMode

Or are there scenarios where people want to write to stdin or read
from stdout or stderr?

I think I will also rename the IOMode constructors to their
System.IO.IOMode equivalents. Then I will also have to rename the
ioMode types like so:

data IOMode ioMode where
    ReadMode  :: IOMode R
    WriteMode  :: IOMode W
    AppendMode  :: IOMode A
    ReadWriteMode :: IOMode RW

Then there are two decisions I still have to make:

1) How to name the module? What about: System.IO.ExplicitIOModes?

2) What to export? Do I only need to export the changed functions and
types like I have now:
http://hpaste.org/fastcgi/hpaste.fcgi/view?id=13782 or do I need to
export all the other things that System.IO exports. In the latter case
users have the advantage that they can just swap their import of
System.IO with System.IO.ExplicitIOModes

When I have time I will put this in a package and upload it to hackage.

Oh yes, final question: how to name this package?

What about explicit-iomodes?



More information about the Haskell-Cafe mailing list