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's bare essentials, so the following code is self-contained, and does not require an understanding of syb-with-class.<br>
<br>> {-# LANGUAGE RankNTypes, KindSignatures, MultiParamTypeClasses, FlexibleInstances, ScopedTypeVariables, FlexibleContexts #-}<br>> module Main where<br><br>A Proxy data type used to pass some extra type info around in gmapQ.<br>
<br>> data Proxy (a :: * -> *) = Proxy<br><br>A simple multiple parameter type class.<br><br>> class Data (ctx :: * -> *) a <br><br>gmapQ takes a function, forall a. (Data ctx a) => a -> r, applies the<br>
function to all the immediate sub-types of 'a' and collects the<br>results as a list. This is possible because the 'a' is forall'd, and<br>(in the real implementation) the Data class provides the mechanisms<br>
for accessing all the immediate sub-types of 'a' and doing useful<br>things with them. <br><br>> gmapQ :: Proxy ctx -> (forall a. (Data ctx a) => a -> r) -> (forall a. (Data ctx a) => a -> [r])<br>
> gmapQ = undefined<br><br>Now we create a context. <br><br>> data Foo m a = Foo { unFoo :: m a }<br><br>And create a Data instance for it.<br><br>> 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's not really important to the issue at hand).<br><br>> bar1 :: forall a m r. (Data (Foo m) a, Num r) => a -> r<br>> 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 'm', such as (Monad m) =><br>
<br>> -- bar2 :: forall a m r. (Monad m, Data (Foo m) a, Num r) => a -> r<br>> -- bar2 x = sum $ gmapQ (undefined :: Proxy (Foo m)) (bar2 :: forall b. (Monad m, Data (Foo m) b, Num r) => b -> r) x<br><br>
I get the error:<br><br>/tmp/a.lhs:48:54:<br> Ambiguous type variable `m' in the constraint:<br> `Monad m' arising from a use of `bar2' 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>> -- instance (Monad m) => Data (Foo m) a<br><br>Which, by itself is fine. But that results in my needing to add (Monad<br>
m) to the 'bar' function. And that is what I can't figure out how to<br>do..<br><br>Thanks!<br><br>- jeremy<br>