Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you just want the solution, look at the end.</p> <p>You nearly answered your own question already. Literals in Haskell are overloaded:</p> <pre><code>Prelude&gt; :t 3 3 :: Num a =&gt; a </code></pre> <p>Since <code>(*)</code> also has a <code>Num</code> constraint</p> <pre><code>Prelude&gt; :t (*) (*) :: Num a =&gt; a -&gt; a -&gt; a </code></pre> <p>this extends to the product:</p> <pre><code>Prelude&gt; :t 3 * 20 3 * 20 :: Num a =&gt; a </code></pre> <p>So, depending on context, this can be specialized to be of type <code>Int</code>, <code>Integer</code>, <code>Float</code>, <code>Double</code>, <code>Rational</code> and more, as needed. In particular, as <code>Fractional</code> is a subclass of <code>Num</code>, it can be used without problems in a division, but then the constraint will become stronger and be for class <code>Fractional</code>:</p> <pre><code>Prelude&gt; :t 3 * 20 / 4 3 * 20 / 4 :: Fractional a =&gt; a </code></pre> <p>The big difference is the identifier <code>c</code> is an <code>Integer</code>. The reason why a simple let-binding in GHCi prompt isn't assigned an overloaded type is the dreaded <a href="http://www.haskell.org/haskellwiki/Monomorphism_restriction" rel="noreferrer">monomorphism restriction</a>. In short: if you define a value that doesn't have any explicit arguments, then it cannot have overloaded type unless you provide an explicit type signature. Numeric types are then defaulted to <code>Integer</code>.</p> <p>Once <code>c</code> is an <code>Integer</code>, the result of the multiplication is <code>Integer</code>, too:</p> <pre><code>Prelude&gt; :t 3 * c 3 * c :: Integer </code></pre> <p>And <code>Integer</code> is not in the <code>Fractional</code> class.</p> <p>There are two solutions to this problem.</p> <ol> <li><p>Make sure your identifiers have overloaded type, too. In this case, it would be as simple as saying</p> <pre><code> Prelude&gt; let c :: Num a =&gt; a; c = 20 Prelude&gt; :t c c :: Num a =&gt; a </code></pre></li> <li><p>Use <code>fromIntegral</code> to cast an integral value to an arbitrary numeric value:</p> <pre><code> Prelude&gt; :t fromIntegral fromIntegral :: (Integral a, Num b) =&gt; a -&gt; b Prelude&gt; let c = 20 Prelude&gt; :t c c :: Integer Prelude&gt; :t fromIntegral c fromIntegral c :: Num b =&gt; b Prelude&gt; 3 * fromIntegral c / 4 15.0 </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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