title: Haskell: data definition and Functor class
body: <p>i was trying to define the data type Set but i have problems when i try to instantiate the functor class.</p>
  
  <p>This is my code:</p>
  
  <pre><code>data Set a where
      Empty :: Eq a =&gt; Set a
      Add   :: Eq a =&gt; a -&gt; Set a -&gt; Set a
  
  instance Functor Set where
      fmap = setMap
  
  setMap :: (Eq a, Eq b) =&gt; (a-&gt;b) -&gt; Set a -&gt; Set b 
  setMap f Empty = Empty
  setMap f (Add x set) = Add (f x) $ setMap f set
  </code></pre>
  
  <p>And this is what the compiler says:</p>
  
  <pre><code>    No instance for (Eq a) arising from a use of ‘setMap’
        Possible fix:
          add (Eq a) to the context of
            the type signature for:
              fmap :: (a -&gt; b) -&gt; Set a -&gt; Set b
  </code></pre>
  
  <p>Why isn't the constraint in the setMap definition enough? Or, if it is, why is it wrong?</p>