title: How do I specify a type variable used in an inline type, is the same as a type variable used in a function definition?
body: <p>(Apologies if my terminology is wrong).</p>
  
  <p>I'm trying to write a wrapper function that handles exceptions: if the given <code>IO</code> action throws, it returns <code>Nothing</code> (inside an <code>IO</code> context of course), but if the given <code>IO</code> action succeeds, it returns <code>Just v</code>.</p>
  
  <pre><code>tryMaybe :: IO a -&gt; IO (Maybe a)
  tryMaybe action = do
      result &lt;- (try action) :: IO (Either SomeException a)
      return $ case result of
          Left _  -&gt; Nothing
          Right v -&gt; Just v
  </code></pre>
  
  <p>This results in the compiler error message:</p>
  
  <pre><code>  Couldn't match type `a' with `a1'
        `a' is a rigid type variable bound by
            the type signature for tryMaybe :: IO a -&gt; IO (Maybe a)
            at src/Database.hs:33:13
        `a1' is a rigid type variable bound by
             an expression type signature: IO (Either SomeException a1)
             at src/Database.hs:35:15
      Expected type: IO a1
        Actual type: IO a
      In the first argument of `try', namely `action'
  </code></pre>
  
  <p>I'm guessing that the type variable <code>a</code> in the first line isn't the same as the <code>a</code> in the third line - they just happen to have the same name in the source code, and the compiler has renamed it <code>a1</code> in the error message.</p>
  
  <p>So how do I tell Haskell that these are the same types?</p>