title: What is the type of return 5 in Haskell when no context is given?
body: <p>In <a href="https://stackoverflow.com/questions/24071729/what-monadic-type-return-return-function">this question</a> the OP asks what the type of the expression <code>return 5</code> is and the answer has already been given in that question: it is a generic type, as can be verified by typing</p>
  
  <pre><code>:t return 5
  </code></pre>
  
  <p>in the Haskell interpreter:</p>
  
  <pre><code>return 5 :: (Num a, Monad m) =&gt; m a
  </code></pre>
  
  <p>The specific implementation of <code>return</code> is determined by the context in which it appears: type inference will restrict <code>m</code> to a specific monad such as <code>Maybe</code>, <code>[]</code>, <code>IO</code>, and so on.</p>
  
  <p>I can also force the interpreter to pick a specific monad by specifying the type, e.g.</p>
  
  <pre><code>Prelude&gt; return 5 :: Maybe Int
  Just 5
  Prelude&gt; return 5 :: [Int]
  [5]
  </code></pre>
  
  <p>and so on.</p>
  
  <p>Now if I type the expression <code>return 5</code> without specifying a type, I get:</p>
  
  <pre><code>Prelude&gt; return 5
  5
  </code></pre>
  
  <p>which is quite surprising to me: I would rather have expected the interpreter to tell me that it cannot pick an appropriate implementation of <code>return</code> because it cannot infer the monadic type to be used.</p>
  
  <p>So my question is: what specific monad has Haskell used here? And based on what criteria was this monad chosen?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Thanks for the answer! In fact, if I try to compile this program:</p>
  
  <pre><code>module Main
  where
  
  a = return 5
  
  main :: IO ()
  main = putStrLn "This program should not compile"
  </code></pre>
  
  <p>I get an error:</p>
  
  <pre><code>No instance for (Monad m0) arising from a use of `return'
  The type variable `m0' is ambiguous
  Relevant bindings include
    a :: m0 Integer (bound at invalid_return.hs:4:1)
  Note: there are several potential instances:
    instance Monad ((-&gt;) r) -- Defined in `GHC.Base'
    instance Monad IO -- Defined in `GHC.Base'
    instance Monad [] -- Defined in `GHC.Base'
    ...plus one other
  In the expression: return 5
  In an equation for `a': a = return 5
  </code></pre>
  
  <p>So it only works in GHCi for the reasons explained by Jon.</p>