title: Handling events in Haskell
body: <p>I would like to implement the following scenario in Haskell. I have an
  enumerable set of 'events' defined like this:</p>
  
  <pre><code>data MyEvent = Event1
               | Event2
               | Event3
  </code></pre>
  
  <p>I want to define handlers for these events to be used in the following way:</p>
  
  <pre><code>eventLoop :: Handler h =&gt; h -&gt; IO ()
  eventLoop currentHandler = do
      event &lt;- getNextEvent
      nextHandler &lt;- currentHandler event
      eventLoop nextHandler
  </code></pre>
  
  <p>Basically I want handlers to be able to return themselves or another handler to
  handle future events. It's the type of handlers I'm not sure about.</p>
  
  <p>My first idea was to define handlers as simple functions, but their type would
  get infinitely long:</p>
  
  <pre><code>myHandler :: Event -&gt; IO (Event -&gt; IO (Event -&gt; ... ))
  </code></pre>
  
  <p>I suspect this can be solved with a type class, where each handler would need
  to implement a function to handle events (which in turn returns another type of
  the same class), but the recursive definition would still apply. Can someone
  more well versed in the type system point me in the right direction? I also
  welcome any different takes on this.</p>
  
  <p>Thanks!</p>