3 ms·
Pretending that modern type systems provide anything near an adequate control of effects is misleading at best. Controlled side effects are an excellent goal; t
by pron 11y ago
Pretending that modern type systems provide anything near an adequate control of effects is misleading at best. Controlled side effects are an excellent goal; the way Haskell (or the pure-FP approach) goes about achieving that goal not only falls short of the target (which is OK) but may be going off in the wrong direction altogether.
For example, one of the important things about side effects is not only their existence or lack thereof, but their ordering (e.g. you must write to a socket after you open it and before you close it). While enforcing ordering is possible with PFP type systems (to a certain extent), it gets very complicated very fast. On the other hand, other ways of reasoning about effects and their ordering -- e.g. by debugging and profiling -- are made harder, rather than easier, by the PFP approach.
Personally, I believe a better approach is imperative-functional, namely inline (i.e. non-monadic) side-effects, but made explicit with good effect systems. Sadly, we're not yet at a point where this is possible and easy to do.
I strongly believe monads are not a good way of representing side effects, and to understand why you only need to look at how hard it is to compose monads and how difficult to follow are the ways of composing them (monad transformers).
- Chris_Newton 11y agoPretending that modern type systems provide anything near an adequate control of effects is misleading at best. Controlled side effects are an excellent goal I completely agree. But, as I suspect you’d agree, it’s not an easy problem even to specify the behaviour we would ideally like to see here. For example, one of the important things about side effects is not only their existence or lack thereof, but their ordering (e.g. you must write to a socket only after you open it and only before you close it). When I’m designing software, I tend to think of effects at three distinct levels: can effects happen at all, what relative timing (i.e., ordering) do they need to have, and what absolute timing do they need to have? The most basic question is whether certain effects or effects related to a certain resource happen at all. This includes distinguishing pure functions from code with side-effects, but it could also be about, say, having a function that appears pure from the outside but is implemented using mutable state internally. If that state is stored in internal memory that is only acquired and released within that function and no reference to it is accessible outside the function, then the scope of the effects is limited and the usual advantages of pure functions still apply viewed from outside. If the state is stored externally, say in a file or database, then potentially it could interact with other parts of the system because they could also acquire a reference to it via another channel, so we might want to know about that. My current thinking is that in terms of modelling the program behaviour we want to see, the most important thing is probably whether any given resource is transient/unique vs persistent/shared, not whether it's internal (memory) or external. For example, we might also want to model a temporary file with a unique OS-provided name or a temporary database table as being completely private state for programming logic purposes. We still need mechanisms to recover from errors by, say, asking the OS to delete that temporary file if we abort with an exception, but then for true robustness we need to deal with things like failing to allocate dynamic memory as well, and in any case these are ultimately still effects that can be confined to the area of the program where those resources are used without interfering elsewhere. I’d like to have a language where interfaces could indicate how any persistent/shared resources might be affected and pass the references identifying those resources around explicitly so we can’t do things like accidentally duplicating a file handle and having two threads concurrently writing to it without proper synchronisation. I’d also like to have a language where implementations could work with private resources internally without needing to do anything special to protect other parts of the code, but with errors being generated if anything could leak a reference to a private resource that might result in unwanted interactions. We can also be interested in the relative timing of when effects happen, i.e., the possible orders in which multiple effects happen. I reckon this usually only matters to the extent that the same resource(s) are affected; two sequences of effects that relate to completely independent resources can probably be interspersed arbitrarily without any real harm being done. There are a lot of practical error classes that fall into this category, and this is where I’m with you in not being entirely convinced about the monadic model. If I’m working with two files, I probably care about opening, then reading/writing, and then closing each of them in the correct order, but not about whether I closed the first before I opened the second. If we start needing some sort of IO monad to capture each sequence and then stacking them up, this could easily become a chore. It’s a little like the problem of using too much hierarchy in OO design: you wind up codifying some arbitrary nesting of the things you care about, but the hierarchy is illusory and navigating it becomes a burden when the nesting isn’t convenient (cf. 27,534 variants of lifting in Haskell). I suspect more effective tools will eventually come from incorporating effects and the resources they affect as true first class citizens in programming languages. I’m intrigued by the kind of ideas we see in substructural type systems, and wonder whether a system built on similar principles might let us constrain the things that actually matter safely but without imposing all kinds of arbitrary prioritisation and/or boilerplate code that is more trouble than it’s worth. Some of the discussions during the development of Rust in recent years have been interesting in this regard[1] as have some of the discussions about the monadic model in Haskell and its limitations. As a final comment, I think the other downside of the pure functions and monads model is that it tends to put all the emphasis on relative ordering. We might also care about absolute timing of effects if we’re interacting with a real time clock or other physical system. If the only way to get that information into your model is to shoehorn in another level of monad, again with the complications of stacking transformers possibly in arbitrary orders, this isn’t ideal. If a clock were just one more stateful resource we have access to in certain parts of our program, much as it would be in typical imperative languages today but with the improved effect/resource usage safeguards, that would be helpful. [1] For example, see http://pcwalton.github.io/blog/2012/12/26/typestate-is-dead/ http://pcwalton.github.io/blog/2012/12/26/typestate-is-dead/
- tome 11y ago> Pretending that modern type systems provide anything near an adequate control of effects is misleading at best Adequate for what exactly? Programmers all around the world are using Haskell every day and finding it saves them headaches because it offers them some benefits with regard to control over effects. It's perfectly adequate for us. I've been writing Haskell every working day for over two years and one of the things I most appreciate about it is its effect system. You keep repeating these kinds of claims but you don't actually offer any alternative, nor have you offered any compelling explanation for why debugging and profiling should be intrinsically harder in a pure functional language. Maybe you can't write an air traffic control system in Haskell. So what? Who cares? Haskell is a phenomenally useful tool for a growing number of developers working on a wide range of different tasks (those tasks don't include air traffic control systems or missile defense systems, but we'll live with that). And pray tell, what would an effect system look like that's not monadic? I'm genuinely interested as I benefit every working day from Haskell's effect typing and if there's something even better I'd like to know about it! I'm keenly waiting for your talk to appear here so I can actually try to understand what you are on about: https://www.youtube.com/channel/UC-WICcSW1k3HsScuXxDrp0w https://www.youtube.com/channel/UC-WICcSW1k3HsScuXxDrp0w Hope it appears at some point!
- pron 11y ago> I've been writing Haskell every working day for over two years and one of the things I most appreciate about it is its effect system. Haskell's type system is not an effect system. > And pray tell, what would an effect system look like that's not monadic? Something like this -- http://research.microsoft.com/en-us/projects/koka/ http://research.microsoft.com/en-us/projects/koka/ -- but with notions of ordering and better composition. > Hope it appears at some point! It's up there now.
- tome 11y ago> Haskell's type system is not an effect system. Haskell's type system models effects through things like monads, applicatives, categories. If you don't agree with this then you're going to have to tell me what your definition of "effect system" and "type system" are. > Something like this -- http://research.microsoft.com/en-us/projects/koka/ http://research.microsoft.com/en-us/projects/koka/ -- but with notions of ordering and better composition. I'll give it a look, thanks, > It's up there now. Wow, that was good timing!