Handles with their IOMode in their type

Could not get to sleep tonight so I got up and hacked this together: http://hpaste.org/fastcgi/hpaste.fcgi/view?id=13782 Does something like this already exist on hackage: If not, I may turn this into a package and upload it tomorrow. Comments, criticism and patches are welcome. regards, Bas

On 08/12/2009 03:54, Bas van Dijk wrote:
Could not get to sleep tonight so I got up and hacked this together:
http://hpaste.org/fastcgi/hpaste.fcgi/view?id=13782
Does something like this already exist on hackage:
If not, I may turn this into a package and upload it tomorrow.
Comments, criticism and patches are welcome.
regards,
Bas
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)

On Tue, Dec 8, 2009 at 7:22 PM, Lee Houghton
I like this idea.
Thanks
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? regards, Bas

On Dec 9, 2009, at 16:51 , Bas van Dijk wrote:
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?
These situations *do* come up; the controlling terminal for a program is open read/write on all 3 file descriptors initially, ands programs like more/less/pg rely on this and do their I/O on stdout. Additionally, on *BSD pipes are actually socketpairs and therefore bidirectional, and a small number of programs rely on this. But I would not do that by default, just expose a way for programs to make it so if they wish. (Hm, just discovered that the System.Posix.IO wrapper for fcntl() does *not* expose the part of the result of F_GETFL that reports the open mode. Boo hiss.) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Wed, Dec 9, 2009 at 6:00 PM, Brandon S. Allbery KF8NH < allbery@ece.cmu.edu> wrote:
On Dec 9, 2009, at 16:51 , Bas van Dijk wrote:
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?
These situations *do* come up; the controlling terminal for a program is open read/write on all 3 file descriptors initially, ands programs like more/less/pg rely on this and do their I/O on stdout. Additionally, on *BSD pipes are actually socketpairs and therefore bidirectional, and a small number of programs rely on this.
I was surprised to hear this, so I did some fact checking: http://books.google.com/books?id=rHyMRyDEG3gC&pg=PA39&lpg=PA39&dq=posix+write+to+stdin&source=bl&ots=vHsgioIR8J&sig=PPXTzuwuuxyx_peCnuSNVmE220I&hl=en&ei=o6cgS-DxJ5S0sgPSl82kBQ&sa=X&oi=book_result&ct=result&resnum=3&ved=0CA8Q6AEwAjgK#v=onepage&q=&f=false Looks like you're telling the truth. Learn something new every time I read Haskell-Cafe :) Jason
participants (4)
-
Bas van Dijk
-
Brandon S. Allbery KF8NH
-
Jason Dagit
-
Lee Houghton