Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h3>Explanation of the error message</h3> <p>We have</p> <pre><code>foldr (:) :: [a] -&gt; ([a] -&gt; [a]) (.) :: (c -&gt; d ) -&gt; (b -&gt; c) -&gt; (b -&gt; d) </code></pre> <p>From this, we conclude that the second argument <code>y</code> in the composition</p> <pre><code>foldr (:) . y </code></pre> <p>must result in a list, i.e., be of type </p> <pre><code>e -&gt; [a] </code></pre> <p>In our case, <code>y = (* 2) [] [1..10]</code>. So we have:</p> <pre><code>(* 2) [] [1..10] :: e -&gt; [a] -- for some e and a </code></pre> <p>Thus:</p> <pre><code>(* 2) [] :: [f] -&gt; e -&gt; [a] -- for some e, a and f where (Num f, Enum f) </code></pre> <p>(The constraints on <code>f</code> are because we use numeric literals and range notation in the list <code>[1..10]</code>.) Now, consider that <code>2</code> and <code>(*)</code> are overloaded, and</p> <pre><code>(* 2) :: Num g =&gt; g -&gt; g </code></pre> <p>From this we get that <code>[]</code> must have the same type as <code>(* 2) []</code> (and that type must be an instance of <code>Num</code>):</p> <pre><code>[] :: [f] -&gt; e -&gt; [a] -- for some e, a and f where -- (Num f, Enum f, Num ([f] -&gt; e -&gt; [a])) </code></pre> <p>But this is not true, because <code>[]</code> has type <code>[g]</code> for some <code>g</code>. Now let's look at the error message again:</p> <pre><code>Couldn't match expected type `[t0] -&gt; a0 -&gt; [a1]' with actual type `[a2]' In the first argument of `* 2', namely `[]' </code></pre> <p>So modulo renaming of type variables, this is exactly what the error message says.</p> <h3>Addendum</h3> <p>The error points to <code>`[]'</code>, so it's perhaps enlightening to see what the typechecker thinks should go in this position. In GHC 7.8, we'll be able to use the <code>TypeHoles</code> extension for this, but for now, an easy way to do it is to abstract from <code>[]</code>, i.e., replace it by a function argument:</p> <pre><code>Prelude&gt; :t \ x -&gt; foldr (:) . (* 2) x [1..10] \ x -&gt; foldr (:) . (* 2) x [1..10] :: (Enum t, Num ([t] -&gt; a -&gt; [a1]), Num t) =&gt; ([t] -&gt; a -&gt; [a1]) -&gt; a -&gt; [a1] -&gt; [a1] </code></pre> <p>From this, you can easily see that this term is actually still type-correct, it only requires <code>x</code> to be of the type</p> <pre><code>[t] -&gt; a -&gt; [a1] </code></pre> <p>with the additional class constraints (which are admittedly unlikely to hold).</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