
On 2009 Apr 9, at 11:47, Mark Spezzano wrote:
How exactly do monads “solve” the problem of referential transparency? I understand RT to be such that a function can be replaced with a actual value.
Since a monad could potentially encapsulate any other value—say, data read from a keyboard—doesn’t that violate the assumption of RT on monads?
Monads provide a way to carry extra data or operations around with their values. IO passes an opaque "world state" around in the background, conceptually I/O operations modify the "world state" and it is in fact always valid to replace the monadified version with the unwrapped version --- ignoring IORefs, IO is just a simple state monad. The "world state" insures that operations in the IO monad are constrained to an ordered list. This gives us a referentially transparent *model* that behaves the way we need it to; the fact that actual I/O is not referentially transparent doesn't matter because the model insures that computations in IO behave properly. (Unless you use unsafePerformIO or unsafeInterleaveIO, which is why they're unsafe; you've violated the model, unexpected things can happen as a result.) -- 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