How to write a Semigroup Instance?
<h1>Problem</h1>

<p>Given a data type, implement the Semigroup instance. Heres the data type I was given to implement: 
<code>data Or a b = Fst a | Snd b deriving  (Eq, Show, Num)</code>. And it should function like so: 
<br></p>

<pre><code>Prelude&gt; Fst 1 &lt;&gt; Snd 2
Snd 2
Prelude&gt; Fst 1 &lt;&gt; Fst 2
Fst 2
Prelude&gt; Snd 1 &lt;&gt; Fst 2
Snd 1
Prelude&gt; Snd 1 &lt;&gt; Snd 2
Snd 1
</code></pre>

<p>When I test values like <code>&gt; Fst "help" &lt;&gt; Fst "me"</code> it works correctly, but when I test other values  I get errors. When I try to fix these errors by deriving the classes from the errors, I get more errors. What am I doing wrong here?</p>

<h1>My Code</h1>

<pre><code>data Or a b =
    Fst a
  | Snd b
  deriving (Eq, Show)

instance (Semigroup a, Semigroup b, Num a, Num b) =&gt; Semigroup (Or a b) where
 (Snd a) &lt;&gt; _ = Snd a
  _ &lt;&gt; (Snd a) = Snd a
  (Fst a) &lt;&gt; (Fst b) = Fst b  
</code></pre>

<h1>Error</h1>

<p>When I try to test with integers <code>&gt; Fst 1 &lt;&gt; Fst 2</code> I get:</p>

<pre><code>No instance for (Num a0) arising from a use of ‘it’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
  instance RealFloat a =&gt; Num (Data.Complex.Complex a)
    -- Defined in ‘Data.Complex’
  instance Data.Fixed.HasResolution a =&gt; Num (Data.Fixed.Fixed a)
    -- Defined in ‘Data.Fixed’
  instance forall (k :: BOX) (f :: k -&gt; *) (a :: k).
           Num (f a) =&gt;
           Num (Data.Monoid.Alt f a)
    -- Defined in ‘Data.Monoid’
  ...plus 21 others
In the first argument of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: print it
</code></pre>

<p>When I try to derive the Num class <code>data Or a b = Fst a | Snd b deriving  (Eq, Show, Num)</code> I get:</p>

<pre><code>    Can't make a derived instance of ‘Num (Or a b)’:
      ‘Num’ is not a derivable class

    In the data declaration for ‘Or’
Failed, modules loaded: none.
</code></pre>