title: Why am I getting 'Overlapping instances' error when one doesn't match?
body: <p>I'm attempting to allow embedding a state monad in my Free monad; here's my simple attempt:</p>
  
  <pre><code>{-# language FlexibleInstances, MultiParamTypeClasses #-}
  module Main where
  
  import Control.Monad.Free
  import Control.Monad.State
  import Data.Bifunctor
  
  data Toy state next =
        Output String next
      | LiftState (state -&gt; (next, state))
      | Done
  
  instance Functor (Toy s) where
    fmap f (Output str next) = Output str $ f next
    fmap f (LiftState stateF) = LiftState (first f . stateF)
    fmap f Done = Done
  
  instance MonadState s (Free (Toy s)) where
    state = overState
  
  overState :: (s -&gt; (a, s)) -&gt; Free (Toy s) a
  overState = liftF . LiftState
  
  output :: Show a =&gt; a -&gt; Free (Toy s) ()
  output x = liftF $ Output (show x) ()
  
  done :: Free (Toy s) r
  done = liftF Done
  
  program :: Free (Toy Int) ()
  program = do
    start &lt;- get
    output start
    modify ((+10) :: (Int -&gt; Int))
    end &lt;- get
    output end
    done
  
  interpret :: (Show r) =&gt; Free (Toy s) r -&gt; s -&gt; IO ()
  interpret (Free (LiftState stateF)) s = let (next, newS) = stateF s
                                           in interpret next newS
  interpret (Free (Output str next)) s = print str &gt;&gt; interpret next s
  interpret (Free Done) s = return ()
  interpret (Pure x) s = print x
  
  main :: IO ()
  main = interpret program (5 :: Int)
  </code></pre>
  
  <p>I get the error:</p>
  
  <pre><code>• Overlapping instances for MonadState Int (Free (Toy Int))
      arising from a use of ‘get’
    Matching instances:
      instance [safe] (Functor m, MonadState s m) =&gt;
                      MonadState s (Free m)
        -- Defined in ‘Control.Monad.Free’
      instance MonadState s (Free (Toy s))
        -- Defined at app/Main.hs:18:10
  • In a stmt of a 'do' block: start &lt;- get
    In the expression:
      do { start &lt;- get;
           output start;
           modify ((+ 10) :: Int -&gt; Int);
           end &lt;- get;
           .... }
    In an equation for ‘program’:
        program
          = do { start &lt;- get;
                 output start;
                 modify ((+ 10) :: Int -&gt; Int);
                 .... }
  </code></pre>
  
  <p>As far as I can gather; it's trying to apply this instance:</p>
  
  <pre><code>(Functor m, MonadState s m) =&gt; MonadState s (Free m)
  </code></pre>
  
  <p>from the
  <a href="https://hackage.haskell.org/package/free-3.4.2/docs/Control-Monad-Free.html" rel="nofollow noreferrer">free package here</a>; however in this case it would have to match <code>Free (Toy s)</code> and there's no <code>MonadState s (Toy s)</code> as required so I don't understand why it thinks that it applies.</p>
  
  <p>If I remove my instance definition I get:</p>
  
  <pre><code>• No instance for (MonadState Int (Toy Int))
      arising from a use of ‘modify’
  </code></pre>
  
  <p>Which supports my thought that the other instance doesn't actually apply; How can I get this to compile using my specified instance? And can you explain why this is occurring? Is it due to <code>FlexibleInstances</code> being used?</p>
  
  <p>Thanks!</p>