Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To expand on sepp2k's answer a bit: if you try to compile the following (or load it into GHCi), you get an error:</p> <pre><code>import Data.List (sort) f = head . sort </code></pre> <p>This is a violation of the monomorphism restriction because we have a class constraint (introduced by <code>sort</code>) but no explicit arguments: we're (somewhat mysteriously) told that we have an <code>Ambiguous type variable</code> in the constraint <code>Ord a</code>.</p> <p>Your example (<code>let x = 3</code>) has a similarly ambiguous type variable, but it doesn't give the same error, because it's saved by <a href="http://www.haskell.org/onlinereport/decls.html" rel="nofollow noreferrer">Haskell's "defaulting" rules</a>:</p> <blockquote> <p>Any monomorphic type variables that remain when type inference for an entire module is complete, are considered <em>ambiguous</em>, and are resolved to particular types using the defaulting rules (Section 4.3.4).</p> </blockquote> <p>See <a href="https://stackoverflow.com/questions/2861988/haskell-defaulting-constraints-to-type/2862060#2862060">this answer</a> for more information about the defaulting rules—the important point is that they only work for certain numeric classes, so <code>x = 3</code> is fine while <code>f = sort</code> isn't.</p> <p>As a side note: if you'd prefer that <code>x = 3</code> end up being an <code>Int</code> instead of an <code>Integer</code>, and <code>y = 3.0</code> be a <code>Rational</code> instead of a <code>Double</code>, you can use a "default declaration" to override the default defaulting rules:</p> <pre><code>default (Int, Rational) </code></pre>
 

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