title: How can I write a purescript effect handler that subracts an effect from the set
body: <p>PureScript by Example, in the section <a href="https://leanpub.com/purescript/read#leanpub-auto-handlers-and-actions" rel="nofollow noreferrer">The Eff Monad -> Handlers and Actions</a> states that "[an effect] handler usually subtracts effects from the set". However, the examples are rather opaque and I can't work out how to write my own handler to achieve this.</p>
  
  <p>Specifically, I'm working with purescript-aff and <a href="https://github.com/slamdata/purescript-affjax" rel="nofollow noreferrer">purescript-affjax</a>. I'd like to use runAff (from purescript-aff) in combination with get (from purescript-affjax).</p>
  
  <p>The problem is that <code>get</code> uses the Affjax monad, whereas <code>runAff</code> expects something using the Aff monad.</p>
  
  <p>Affjax is defined as:</p>
  
  <pre><code>type Affjax e a = Aff (ajax :: AJAX | e) (AffjaxResponse a)
  </code></pre>
  
  <p>Aff is defined as:</p>
  
  <pre><code>foreign import data Aff :: # ! -&gt; * -&gt; *
  </code></pre>
  
  <p>I, therefore, want to write a function that has the following type:</p>
  
  <pre><code>Affjax e a -&gt; Aff e a
  </code></pre>
  
  <p>This seems to me to require a handler that subtracts the <code>ajax</code> part of the effect set. How would one code such a handler?</p>
  
  <p>Attempting to pattern-match, as below, of course results in the error <code>unexpected |</code>.</p>
  
  <pre><code>handleAffjax :: Affjax e a -&gt; Aff e a
  handleAffjax (Aff ( | eff1 ) resp1) = Aff eff1 resp1
  </code></pre>
  
  <p>Thanks all.</p>
  
  <h2>Update</h2>
  
  <p>Inspired by <a href="https://stackoverflow.com/a/30697564/4445916%20@christoph-hegemann&#39;s%20answer%20below">@christoph-hegemann</a>'s answer below, I was able to track down most of the issues with my code.</p>
  
  <p>I think the answer to the question in the title is that one does not subtract an effect from the set, and that description is a little confusing. The effect remains after you've handled it.</p>
  
  <p>The intuition I was missing is that I have to add the expected effects to the calling function type. The lack of a type declaration on <code>main</code> hid that to some extent. I worked it out when I turned Christoph's example into the following compiling example:</p>
  
  <pre><code>module Main where
  
  import Debug.Trace
  import Network.HTTP.Affjax
  import Control.Monad.Aff
  import Control.Monad.Eff
  import Control.Monad.Eff.Exception
  
  initialUrl :: URL
  initialUrl = "http://127.0.0.1:8000/api/v1/navitem/2/"
  
  runGet :: forall e. Eff (ajax :: AJAX, trace :: Trace | e) Unit
  runGet = runAff errorHandler successHandler (get initialUrl)
  
  errorHandler :: forall e. Error -&gt; Eff (trace :: Trace | e) Unit
  errorHandler err = print err
  
  successHandler :: forall e. AffjaxResponse String -&gt; Eff (trace :: Trace | e) Unit
  successHandler res = print res.response
  
  main = runGet
  </code></pre>