Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This has nothing to do with ghci. This is the monomorphism restriction being irritating. If you try to compile the following file:</p> <pre><code>add = uncurry (+) main = do print $ add (1,2 :: Int) print $ add (1,2 :: Double) </code></pre> <p>You will get an error. If you expand:</p> <pre><code>main = do print $ uncurry (+) (1,2 :: Int) print $ uncurry (+) (1,2 :: Double) </code></pre> <p>Everything is fine, as expected. The monomorphism restriction refuses to make something that "looks like a value" (i.e. defined with no arguments on the left-hand side of the equals) typeclass polymorphic, because that would defeat the caching that would normally occur. Eg.</p> <pre><code>foo :: Integer foo = expensive computation bar :: (Num a) =&gt; a bar = expensive computation </code></pre> <p><code>foo</code> is guaranteed only to be computed once (well, in GHC at least), whereas <code>bar</code> will be computed every time it is mentioned. The monomorphism restriction seeks to save you from the latter case by defaulting to the former when it looks like that's what you wanted.</p> <p>If you only use the function once (or always at the same type), type inference will take care of inferring the right type for you. In that case ghci is doing something slightly different by guessing sooner. But using it at two different types shows what is going on.</p> <p>When in doubt, use a type signature (or turn off the wretched thing with <code>{-# LANGUAGE NoMonomorphismRestriction #-}</code>).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload