title: How does IncoherentInstances work?
body: <p>Playing around with <a href="https://stackoverflow.com/a/8369267/208257">some code</a>:</p>
  
  <pre><code>{-# LANGUAGE FlexibleInstances, OverlappingInstances #-}
  
  class Arity f where
    arity :: f -&gt; Int
  
  instance Arity x where
    arity _ = 0
  
  instance Arity f =&gt; Arity ((-&gt;) a f) where
    arity f = 1 + arity (f undefined)
  </code></pre>
  
  <p>Without <code>IncoherentInstances</code>:</p>
  
  <pre><code>ghci&gt; arity foldr
  blah blah ambiguous blah blah possible fix blah
  ghci&gt; arity (foldr :: (a -&gt; Int -&gt; Int) -&gt; Int -&gt; [a] -&gt; Int)
  3
  ghci&gt; let f x y = 3 in arity f
  2
  ghci&gt; arity $ \x y -&gt; 3
  2
  </code></pre>
  
  <p>If we add <code>IncoherentInstances</code> to the list of pragmas, then it can handle <code>foldr</code> without needing a monomorphic type signature, but it gets the wrong answer on lambdas:</p>
  
  <pre><code>ghci&gt; arity foldr
  3
  ghci&gt; let f x y = 3 in arity f
  2
  ghci&gt; arity $ \x y -&gt; 3 -- should be 2
  0
  </code></pre>
  
  <p>What is the black magic behind Incoherent Instances? Why does it do what it does here?</p>