
Folks, Dean Herrington points out (see msg below from the ghc-bugs list) that GHC's instance of Show on Handles is incorrect because it is impure. This is because it includes parts of the Handle that are mutable, such as the buffering state. I've changed GHC's implementation so that it now displays just the filename from the Handle (this is the the only immutable part of the Handle). This raises another question though: should there be an IO operation for displaying the more interesting parts of a Handle? I propose: System.IO.hShow :: Handle -> IO String Comments? Cheers, Simon -----Original Message----- From: glasgow-haskell-bugs-admin@haskell.org [mailto:glasgow-haskell-bugs-admin@haskell.org] On Behalf Of Dean Herington Sent: 15 July 2003 19:35 To: glasgow-haskell-bugs@haskell.org Subject: showing Handle is impure The `shows` function for type `Handle` is inappropriately impure. In my opinion, showing a handle should merely identify it as a handle and distinguish it from other handles. The information currently provided by showing a handle should be provided with some other function; that function should be in the IO monad. swan(102)% ghci ___ ___ _ / _ \ /\ /\/ __(_) / /_\// /_/ / / | | GHC Interactive, version 5.04.3, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. Loading package base ... linking ... done. Loading package haskell98 ... linking ... done. Prelude> :m IO Prelude IO> h <- openFile "/dev/null" ReadMode Prelude IO> h {loc=/dev/null,type=readable,binary=False,buffering=block (8192)} Prelude IO> c <- hGetContents h Prelude IO> h {closed} _______________________________________________ Glasgow-haskell-bugs mailing list Glasgow-haskell-bugs@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

I've changed GHC's implementation so that it now displays just the filename from the Handle (this is the the only immutable part of the Handle). This raises another question though: should there be an IO operation for displaying the more interesting parts of a Handle? I propose:
System.IO.hShow :: Handle -> IO String
Sounds good. I'm a little surprised that there always is a filename though - I thought (but haven't bothered to check) that Handles were used for anything that can contain a file descriptor: files, pipes, network connections, etc. No big deal but what is the output in these cases? (The file descriptor number suggests itself - immutable though not necessarily unique.) -- Alastair

I've changed GHC's implementation so that it now displays just the filename from the Handle (this is the the only immutable part of the Handle).
I share Alastair's concern about the possibility that a handle may not always contain a filename. I think it would be cleaner to assume that the only immutable part of a handle is its identity. And it is important that the identity be shown (that is, as by `show`). I think we should we specify that `show`ing a `Handle` gives: "Handle " ++ handleId or "(Handle " ++ handleId ++ ")" depending in the usual way on the current precedence, and where `handleId` is implementation-defined but required to identify the handle uniquely within the current process.
This raises another question though: should there be an IO
operation for displaying the more interesting parts of a Handle? I propose:
System.IO.hShow :: Handle -> IO String
Sounds good.
I agree. I am content to leave the form of output implementation-defined. -- Dean
participants (3)
-
Alastair Reid
-
Dean Herington
-
Simon Marlow