On Tue 18 Jan, Stefan Karrmann wrote:
> What are the pros and cons of unique typing with multiple environments
> vs. the IO monad?

I don't think anybody else has answered this, so I'll have a go.

In the absence of concurrency, I think there is little to chose between
Cleans unique world as value IO and Haskell monadic IO. They are essentially
equivalant as far as I can see. But despite their similarity, there seems to
be a significant difference between the Clean and Haskell camps in the
philosophical interpretation of IO.

 Clean position (as I understand it)
 -----------------------------------
 The state of the world is a value, and the effect of all IO operations can
 be modelled as pure functions which take a world state as argument and
 return a new world state. Clean is therefore purely functional and IO
 functions preserve referential transparency.

 Haskell position (as I understand it)
 -------------------------------------
 Haskell is incapable of desribing IO, it can only evaluate an expression
 of type IO (). This expression is used by an 'IO monad machine' (which
 is not part of Haskell) to actually execute IO operations. Because the
 IO monad machine is not part of Haskell, it can freely make use of all
 the horrors one would expect in a typical imperative language without
 damaging pure functionality in Haskell itself.
 
 Monadic Pro 1
 -------------
 Haskells monadic operators and do expressions are a little more convienient
 to use than Cleans explicit world (environment) passing IMHO.

 Multiple Environment Pro 1
 --------------------------
 Clean doesn't actually use a single monolithic world. Instead the world
 is composed of a number of sub-worlds which may be separated out (opened),
 manipulated separately, and then brought together again (closed). Some
 people like this because it enables you to see at a glance which
 environments an IO function modifies. (Personally, I find that this rather
 awkward, so maybe it's a con).

 Multiple Environment Pro 2
 --------------------------
 In the presence of concurrency, the multiple environments approach has
 an advantage in that it allows deterministic concurrency in IO. The
 argument is that if the world is composed of independent sub_worlds then
 it shouldn't matter what order IO operations on those sub_worlds actually
 occur (because they are independent). This is good, because it makes
 concurrency can be exploited safely. Unfortunately, I think that by
 preserving determinism programs are more likely to block, whereas
 'non-determistic' concurrency (see below) doesn't have this problem
 in principle (but bad non-deterministic programs may still block
 unnecessarily).  

 Monadic Pro 2
 -------------
 Haskell can't use this approach to deterministic concurrency because
 it doesn't make use of multiple environments. Concurrent Haskell provides
 non-deterministic concurrency using forkIO. But that's not so bad.
 Non-deterministic concurrency is more usefull IMHO. It also means we
 don't have to pretend IO environments are independent when in reality
 they rarely are. In Concurrent Haskell it is the programmers responsibility
 to co-ordinate different threads so that the program works as intended.

Regards
-- 
Adrian Hey

Reply via email to