<span>> {-# OPTIONS -fglasgow-exts #-}<br>><br>> module VarArg where<br>> import Data.FiniteMap -- for an example below<br>><br>> class BuildList a r | r-> a where<br>> build' :: [a] -> a -> r<br>
><br>> instance BuildList a [a] where<br>> build' l x = reverse$ x:l<br>><br>> instance BuildList a r => BuildList a (a->r) where<br>> build' l x y = build'(x:l) y<br>><br>> --build :: forall r a. (BuildList a r) => a -> r<br>
> build x = build' [] x<br> <br><br>I'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' :: [Build r] -> Build r -> r <br><br>follow by the instance for [a] resulting in <br><br>instance BuildList [a] where<br> type Build [a] = a<br> build' l x = reverse $ x:l<br><br>Until here, everything is working, and I'm able to do <br>
<br>> build' [2,3,4] 1 :: [Integer]<br>> [4,3,2,1]<br><br>then I move on to the next instance (a -> r) with <br><br>instance BuildList r => BuildList (a-> r) where<br> type Build (a -> r) = a<br>
build' l x = \ y -> build'(x:l) y<br><br><br>But I get the following error <br><br> Couldn't match expected type `Build r' against inferred type `a'<br> `a' is a rigid type variable bound by<br>
the instance declaration at /home/adolfo/foo.hs:347:35<br> In the first argument of `(:)', namely `x'<br> In the first argument of `build'', namely `(x : l)'<br> In the expression: build' (x : l) y<br>
<br>then I try with :<br><br>instance BuildList r => BuildList (a-> r) where<br> type Build (a -> r) = Build r<br> build' l x = \ y -> build'(x:l) y<br><br><br>And I get <br><br> Couldn't match expected type `a' against inferred type `Build r'<br>
`a' 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 -> r)]<br> In the second argument of `(:)', namely `l'<br>
In the first argument of `build'', namely `(x : l)'<br><br>I have been trying to figure out, which type should it be, but I haven't found the correct one,<br><br>any ideas ?<br><br>Thanks<br><br>-<br>
Adolfo Builes<br>
</span>