<div><font color="#3333ff"><font size="4">Hi everyone, this is my first post: I am a ph.d. student from Italy who is learning the wonderful world of Haskell :)</font></font></div>
<div><font color="#3333ff" size="4"></font> </div>
<div><font color="#3333ff" size="4">I have encountered a problem and I cannot find a way to get past it (or even to begin to understand it). I wish to define a simple type class that defines how to convert a type function into its argument (like going from a dumb constructor like data T a = T a into a itself):</font></div>
<div><font color="#3333ff" size="4"></font> </div>
<div><font size="2" face="Consolas"><font size="2" face="Consolas">
<p>class Convert rec where convert :: rec a -> a</p>
<p><font color="#3333ff" size="4">now when I try to use the conversion operator</font></p>
<p>class (CNum n, HasField n (a -> (b,rec a)) l, Convert rec) => HasMethod n l a b rec where</p>
<p> (..!) :: l -> n -> (a -> (b,a))</p>
<p> </p>
<p>instance (CNum n, HasField n (a -> (b,rec a)) l, Convert rec) => HasMethod n l a b rec where</p>
<p> l ..! n =</p>
<p> let m = l .! n</p>
<p> in (\x -> </p>
<p> let (y,v) = m x</p>
<p> in (y,convert v))</p>
<p> </p>
<p><font color="#3333ff" size="4">in what looks to me as a straightforward use, the compiler complains that :</font></p>
<p><font color="#000000" size="4">*References> :load Main.hs<br>[1 of 5] Compiling Records ( Records.hs, interpreted )<br>[2 of 5] Compiling References ( References.hs, interpreted )<br>[3 of 5] Compiling Methods ( Methods.hs, interpreted )</font></p>
<p><font color="#000000" size="4">Methods.hs:25:14:<br> Could not deduce (HasField n (a -> (b, rec a)) l)<br> from the context (HasMethod n l a b rec1,<br> CNum n,<br> HasField n (a -> (b, rec1 a)) l,<br>
Convert rec1)<br> arising from a use of `.!' at Methods.hs:25:14-19<br> Possible fix:<br> add (HasField n (a -> (b, rec a)) l) to the context of<br> the instance declaration<br>
or add an instance declaration for (HasField n (a -> (b, rec a)) l)<br> In the expression: l .! n<br> In the definition of `m': m = l .! n<br> In the expression:<br> let m = l .! n in (\ x -> let (y, v) = ... in (y, convert v))<br>
Failed, modules loaded: References, Records.<br>*References></font></p>
<p><font color="#3333ff" size="4">I apologize for what might look like a huge dumping of code, but I have no idea what might have caused this to further refine the code or to write a more focused example...</font></p></font></font></div>