title: Understanding how Either is an instance of Functor
body: <p>In my free time I'm learning Haskell, so this is a beginner question.</p>
  
  <p>In my readings I came across an example illustrating how <code>Either a</code> is made an instance of <code>Functor</code>:</p>
  
  <pre><code>instance Functor (Either a) where
      fmap f (Right x) = Right (f x)
      fmap f (Left x) = Left x
  </code></pre>
  
  <p>Now, I'm trying to understand why the implementation maps in the case of a <code>Right</code> value constructor, but doesn't in the case of a <code>Left</code>?</p>
  
  <p>Here is my understanding:</p>
  
  <p>First let me rewrite the above instance as</p>
  
  <pre><code>instance Functor (Either a) where
      fmap g (Right x) = Right (g x)
      fmap g (Left x) = Left x
  </code></pre>
  
  <p>Now:</p>
  
  <ol>
  <li><p>I know that <code>fmap :: (c -&gt; d) -&gt; f c -&gt; f d</code></p></li>
  <li><p>if we substitute <code>f</code> with <code>Either a</code> we get <code>fmap :: (c -&gt; d) -&gt; Either a c -&gt; Either a d</code></p></li>
  <li><p>the type of <code>Right (g x)</code> is <code>Either a (g x)</code>, and the type of <code>g x</code> is <code>d</code>, so we have that the type of <code>Right (g x)</code> is <code>Either a d</code>, which is what we expect from <code>fmap</code> (see 2. above)</p></li>
  <li><p>now, if we look at <code>Left (g x)</code> we can use the same reasoning to say that its type is <code>Either (g x) b</code>, that is <code>Either d b</code>, which is not what we expect from <code>fmap</code> (see 2. above): the <code>d</code> should be the second parameter, not the first! So we can't map over <code>Left</code>.</p></li>
  </ol>
  
  <p>Is my reasoning correct?</p>