Fwd: Re: Yet Another Monad Tutorial

From: Derek Elkins
"blaat blaat" wrote:
[...]
However, given that observation, the fact that IO is a monad I find to be a _rather arbitrary design decision_; why not define a small term language which may be passed to the OS?
That would be confusing to use or restrictive (unless done exceedingly well perhaps), it would be awkward to define, and it would pretty much require arbitrary design decisions (what should be included in this language? what are it's semantics? etc).
Why not have (some limited) introspection on programs which we build?
It would be round-about in both implementation and use to do this. Nevertheless, you can easily view the IO monad as an imperative language -embedded- in Haskell. Even when writing mostly imperative code Haskell gives you much more flexibility and protection than many imperative languages, especially the more popular ones.
[...]
Are we missing out on usefull stuff?
No, but if we were, it'd be trivial to test/get it with an appropriate top-level runTheProgram IO computation definable in Haskell today.
Trivial to test I fully agree with; however, to the missing out on usefull stuff I am not convinced. Please allow me to continue my trolling. To use the IO monad for impure calculations is, of course, a design decision. It is arbitrary to the point that I have the freedom not to agree with it; but hey, even I know the thing works ;-). The key point to my email was that doing impure IO possibly has little to do with a monadic style of programming but with the fact that we can functionally construct a description of an impure program which is evaluated _only_ when delivered as a sole result of a Haskell computation. I find that somewhat odd; but not to the point I cannot live with it ;-) About the indirectness issue. The benefit of choosing a monad is clear - the evaluation/rewrite of an action corresponds directly with an impure subroutine call. Moreover, the action can be discarded and, as you pointed out, the description of these impure subroutine calls are trivially embedded directly into Haskell - giving typesafety as a reward. It is both effective and efficient. Great.... Hail to the monad! Hmmm, uhm, but let's think further... What did we lose? Well, introspection comes to mind. A monad is strange in the sense that, actually, there does not seem to be any reason to compute its value since we cannot possibly get anything out of it. In case of IO, only the haskell interpreter can. And, introspection is a nice thing to have, especially for compiler writers. Although I think the benefits in Haskell would be limited, an application might be that I could write my own promotion functions on my own programs. Think of the following though experiment. What if I would have a function "unparse:IO a->String" which gives the textual representation of the specification of the object of type IO a? Clearly, IO is not a monad anymore. Moreover, I don't think it would break the purity of Haskell. And surely, in case of the IO monad, we can think of better manners to break a program down. Hmm, I am rambling. The point is, I think there is no reason to discard all alternatives to monads; I believe that central in the IO-issue is not the fact that we are using monads for IO - but that we can purely create impure programs which do the IO for us ;-), and that those do not have to be of the monadic kind (with all respect to the baffling ingenuity of the invention). Cheers, l4t3r _________________________________________________________________ Protect your PC - get McAfee.com VirusScan Online http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963

Think of the following though experiment. What if I would have a function "unparse:IO a->String" which gives the textual representation of the specification of the object of type IO a? Clearly, IO is not a monad anymore. Moreover, I don't think it would break the purity of Haskell. And surely, in case of the IO monad, we can think of better manners to break a program down.
Why would such a function cause IO to cease being a monad? There's no reason such a function couldn't be written, given an appropriate implementation of IO, and an implementation that is able to print a representation of a function (not too difficult for an interpreter). Imagine adding "deriving Show" to the data types IO and SysCall suggested by Ross Paterson in <20030813092707.GA1151@soi.city.ac.uk>. --KW 8-)

... Hmmm, uhm, but let's think further...
What did we lose? Well, introspection comes to mind. A monad is strange in the sense that, actually, there does not seem to be any reason to compute its value since we cannot possibly get anything out of it. In case of IO, only the haskell interpreter can. And, introspection is a nice thing to have, especially for compiler writers. Although I think the benefits in Haskell would be limited, an application might be that I could write my own promotion functions on my own programs. what do you mean by "you can't get anything out of it"?, i.e. what can you get out of (3::Int)? what do you mean by "introspection", from the text bellow it would seem
Think of the following though experiment. What if I would have a function "unparse:IO a->String" which gives the textual representation of the specification of the object of type IO a?
hello, sorry for asking so many questions :-) blaat blaat wrote: that you mean some sort of reflection, where a program can examine itself? such programs tend to be hard to understand. there are many other functions that cannot be written in haskell (and often there are good reasons for not allowing them). again what you point out is not specific to monads or IO in any way. consider a function: "unparse :: (Int -> Int) -> String" that given a function will produce its definition. you cannot write such a function in haskell. Clearly, IO is not a
monad anymore. Moreover, I don't think it would break the purity of Haskell. And surely, in case of the IO monad, we can think of better manners to break a program down. being able to write such a function has nothing to do with IO being a monad or not. to be a monad you need a suitable definitions for 'return' and '>>='. adding _more_ functions, does not cange anything.
Hmm, I am rambling. The point is, I think there is no reason to discard all alternatives to monads; I believe that central in the IO-issue is not the fact that we are using monads for IO - but that we can purely create impure programs which do the IO for us ;-), and that those do not have to be of the monadic kind (with all respect to the baffling ingenuity of the invention). you are quite right that there are alternatives to monadic IO. in fact haskell used to use stream-based IO. you are also right in that IO being a monad is not the central issue contibuting to "purity". what ensures "purity" is the fact that the type system keeps track of IO operations (and similarly stateful computations in the case of ST). this is in contrast with ML (or C) for example, where an expression of type 'int' can print to the screen, so the type system is unaware of side-effects.
bye iavor -- ================================================== | Iavor S. Diatchki, Ph.D. student | | Department of Computer Science and Engineering | | School of OGI at OHSU | | http://www.cse.ogi.edu/~diatchki | ==================================================
participants (3)
-
blaat blaat
-
Iavor Diatchki
-
Keith Wansbrough