Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To answer your second question, you can use type equivalence constraint (from TypeFamilies extension) to relax your <code>RList</code> instance definition:</p> <pre><code>class HList l =&gt; RList r l instance RList r HNil instance (RList r1 l, r1 ~ r2) =&gt; RList r1 (HCons (R r2 a) l) </code></pre> <p>Now your <code>rc</code> will be inferred to the desired type.</p> <p>I don't think I can 'clearly explain' the phenomenon though (somebody surely will), but it's obvious that the difference between <code>rPair</code> and <code>rCons</code> is that while the former binds <code>r</code> type of both arguments to the same type variable, the later doesn't: the second argument is just <code>l</code> constrained with that there should be some instance of <code>RList</code> for that <code>l</code>). Since there is no type signature for <code>rc</code> (note that if you provide one your original example typechecks) and r1 and r2 have polymorphic, <em>not equivalent</em>, <code>r</code>'s , compiler is trying to find an instance definition for <code>RList r (HCons (R r1 Double) HNil)</code> (<code>r</code> comes from the 1st argument and <code>r1</code> - from the 2nd) and fails to do so. With type equivalence constraint we define an instance of RList with two distinct <code>r1</code> and <code>r2</code> with the only condition that these needs to be equivalent, so it looks like GHC binds them to the same polymorphic type variable when resolving instance of <code>RList</code> for <code>l</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