[Haskell-cafe] I/O without monads, using an event loop

Andrew Butterfield Andrew.Butterfield at cs.tcd.ie
Fri May 30 10:23:46 EDT 2008

Robin Green wrote:
> I have been thinking about to what extent you could cleanly do I/O
> without explicit use of the I/O monad, and without uniqueness types
> (which are the main alternative to monads in pure functional
> programming, and are used in the Concurrent Clean programming language).
> Suppose you have a main event handler function, like this:
> eventMain :: (Event, SystemState AppState) -> (Command, SystemState
> AppState)
> This function could be called over and over in an event loop, until an
> EndProgram command was received, and the event loop would itself do all
> the actual I/O (the SystemStates are only in-memory representations of
> some part of the system state, plus the application's own state). Things
> like disk I/O could be done with commands which generate events when
> complete. Interprocess communication could be done in the same way.
> Then eventMain, and everything called by it, would be
> referentially-transparent, and yet non-monadic. You could of course
> build higher-level stuff on top of that.
Given the above, without uniqueness typing, and because there is clearly 
no monad,
I could write

 :: (Event,Event,SystemState AppState)
  -> ((Command,SystemState AppState),(Command,SystemState AppState))
breakMain (e1,e2,sys) = ( eventMain (e1,sys) , eventMain (e2,sys) )

Now what happens? Do we get two copies of SystemState ?

Simpler still, I can write  (sys,eventMain e sys) -- what happens here?
I have references to both before- and after- state.

It is this probelm with copying the un-copiable (external I/O state),
that requires pure functional languages to outlaw such programs,
either via uniqueness typing, or via a monad interface that completely
hides any direct reference to the underlying state and which
then sequences the application of state transformers.

Andrew Butterfield     Tel: +353-1-896-2517     Fax: +353-1-677-2204
Foundations and Methods Research Group Director.
School of Computer Science and Statistics,
Room F.13, O'Reilly Institute, Trinity College, University of Dublin

More information about the Haskell-Cafe mailing list