Hello,<br><br>I am trying to understand why I am getting an ambigious type variable error, and what I can do to work around it. The problem is occurring while trying to use syb-with-class, but I have stripped it down to it&#39;s bare essentials, so the following code is self-contained, and does not require an understanding of syb-with-class.<br>
<br>&gt; {-# LANGUAGE RankNTypes, KindSignatures, MultiParamTypeClasses, FlexibleInstances, ScopedTypeVariables, FlexibleContexts #-}<br>&gt; module Main where<br><br>A Proxy data type used to pass some extra type info around in gmapQ.<br>
<br>&gt; data Proxy (a :: * -&gt; *) = Proxy<br><br>A simple multiple parameter type class.<br><br>&gt; class Data (ctx :: * -&gt; *) a <br><br>gmapQ takes a function, forall a. (Data ctx a) =&gt; a -&gt; r, applies the<br>
function to all the immediate sub-types of &#39;a&#39; and collects the<br>results as a list. This is possible because the &#39;a&#39; is forall&#39;d, and<br>(in the real implementation) the Data class provides the mechanisms<br>
for accessing all the immediate sub-types of &#39;a&#39; and doing useful<br>things with them. <br><br>&gt; gmapQ :: Proxy ctx -&gt; (forall a. (Data ctx a) =&gt; a -&gt; r) -&gt; (forall a. (Data ctx a) =&gt; a -&gt; [r])<br>
&gt; gmapQ = undefined<br><br>Now we create a context. <br><br>&gt; data Foo m a = Foo { unFoo :: m a }<br><br>And create a Data instance for it.<br><br>&gt; instance Data (Foo m) a<br><br>Here is an example that attempts to recursively apply itself to all<br>
the subtrees. <br><br>(I believe this function will ultimately return 0, since it tries to<br>sum the results of the children. When you reach types with no more<br>sub-types, I think you will get an empty list, and sum [] == 0. Though<br>
it&#39;s not really important to the issue at hand).<br><br>&gt; bar1 :: forall a m r. (Data (Foo m) a, Num r) =&gt; a -&gt; r<br>&gt; bar1 x = sum $ gmapQ (undefined :: Proxy (Foo m)) bar1 x<br><br>The problem I have is when I try to add an additional constraint on &#39;m&#39;, such as (Monad m) =&gt;<br>
<br>&gt; -- bar2 :: forall a m r. (Monad m, Data (Foo m) a, Num r) =&gt; a -&gt; r<br>&gt; -- bar2 x = sum $ gmapQ (undefined :: Proxy (Foo m)) (bar2 :: forall b. (Monad m, Data (Foo m) b, Num r) =&gt; b -&gt; r) x<br><br>
I get the error:<br><br>/tmp/a.lhs:48:54:<br>    Ambiguous type variable `m&#39; in the constraint:<br>      `Monad m&#39; arising from a use of `bar2&#39; at /tmp/a.lhs:48:54-57<br>    Probable fix: add a type signature that fixes these type variable(s)<br>
Failed, modules loaded: none.<br><br>Adding all the scoped type variable stuff does not seem to help. Alas,<br>I can not figure out if this is a limitation of the type-checker, or<br>something that is fundamentally impossible. Nor can I figure out how<br>
to work around the issue.<br><br>In my real code I need to define the data instance like:<br><br>&gt; -- instance (Monad m) =&gt; Data (Foo m) a<br><br>Which, by itself is fine. But that results in my needing to add (Monad<br>
m) to the &#39;bar&#39; function. And that is what I can&#39;t figure out how to<br>do..<br><br>Thanks!<br><br>- jeremy<br>