<span>&gt; {-# OPTIONS -fglasgow-exts #-}<br>&gt;<br>&gt; module VarArg where<br>&gt; import Data.FiniteMap -- for an example below<br>&gt;<br>&gt; class BuildList a r | r-&gt; a where<br>&gt; build&#39; :: [a] -&gt; a -&gt; r<br>

&gt;<br>&gt; instance BuildList a [a] where<br>&gt; build&#39; l x = reverse$ x:l<br>&gt;<br>&gt; instance BuildList a r =&gt; BuildList a (a-&gt;r) where<br>&gt; build&#39; l x y = build&#39;(x:l) y<br>&gt;<br>&gt; --build :: forall r a. (BuildList a r) =&gt; a -&gt; r<br>

&gt; build x = build&#39; [] x<br> <br><br>I&#39;m trying to replace the code below to work with type families, I started out replacing the definition of class with :<br><br>class BuildList r where<br>    type Build r <br>

    build&#39; :: [Build r] -&gt; Build r -&gt; r <br><br>follow by the instance for [a] resulting in <br><br>instance BuildList [a] where<br>    type Build [a] = a<br>    build&#39; l x = reverse $ x:l<br><br>Until here, everything is working, and I&#39;m able to do <br>

<br>&gt; build&#39; [2,3,4] 1 :: [Integer]<br>&gt; [4,3,2,1]<br><br>then I move on to the next instance (a -&gt; r) with <br><br>instance BuildList r =&gt; BuildList (a-&gt; r) where<br>     type Build (a -&gt; r) =  a<br>

     build&#39; l x =  \ y -&gt; build&#39;(x:l) y<br><br><br>But I get the following error <br><br>    Couldn&#39;t match expected type `Build r&#39; against inferred type `a&#39;<br>      `a&#39; is a rigid type variable bound by<br>

          the instance declaration at /home/adolfo/foo.hs:347:35<br>    In the first argument of `(:)&#39;, namely `x&#39;<br>    In the first argument of `build&#39;&#39;, namely `(x : l)&#39;<br>    In the expression: build&#39; (x : l) y<br>

<br>then I try with :<br><br>instance BuildList r =&gt; BuildList (a-&gt; r) where<br>     type Build (a -&gt; r) =  Build r<br>     build&#39; l x =  \ y -&gt; build&#39;(x:l) y<br><br><br>And I get <br><br>      Couldn&#39;t match expected type `a&#39; against inferred type `Build r&#39;<br>

      `a&#39; is a rigid type variable bound by<br>          the instance declaration at /home/adolfo/foo.hs:347:35<br>      Expected type: [Build r]<br>      Inferred type: [Build (a -&gt; r)]<br>    In the second argument of `(:)&#39;, namely `l&#39;<br>

    In the first argument of `build&#39;&#39;, namely `(x : l)&#39;<br><br>I have been trying to figure out, which type should it be, but I haven&#39;t found the correct one,<br><br>any ideas ?<br><br>Thanks<br><br>-<br>
Adolfo Builes<br>
</span>