
Hi everyone, this is my first post: I am a ph.d. student from Italy who is learning the wonderful world of Haskell :) 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): class Convert rec where convert :: rec a -> a now when I try to use the conversion operator class (CNum n, HasField n (a -> (b,rec a)) l, Convert rec) => HasMethod n l a b rec where (..!) :: l -> n -> (a -> (b,a)) instance (CNum n, HasField n (a -> (b,rec a)) l, Convert rec) => HasMethod n l a b rec where l ..! n = let m = l .! n in (\x -> let (y,v) = m x in (y,convert v)) in what looks to me as a straightforward use, the compiler complains that : *References> :load Main.hs [1 of 5] Compiling Records ( Records.hs, interpreted ) [2 of 5] Compiling References ( References.hs, interpreted ) [3 of 5] Compiling Methods ( Methods.hs, interpreted ) Methods.hs:25:14: Could not deduce (HasField n (a -> (b, rec a)) l) from the context (HasMethod n l a b rec1, CNum n, HasField n (a -> (b, rec1 a)) l, Convert rec1) arising from a use of `.!' at Methods.hs:25:14-19 Possible fix: add (HasField n (a -> (b, rec a)) l) to the context of the instance declaration or add an instance declaration for (HasField n (a -> (b, rec a)) l) In the expression: l .! n In the definition of `m': m = l .! n In the expression: let m = l .! n in (\ x -> let (y, v) = ... in (y, convert v)) Failed, modules loaded: References, Records. *References> 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...

On Tue, 2010-03-09 at 15:50 +0100, Giuseppe Maggiore wrote:
class (CNum n, HasField n (a -> (b,rec a)) l, Convert rec) => HasMethod n l a b rec where
(..!) :: l -> n -> (a -> (b,a))
instance (CNum n, HasField n (a -> (b,rec a)) l, Convert rec) => HasMethod n l a b rec where
l ..! n =
let m = l .! n
in (\x ->
let (y,v) = m x
in (y,convert v))
i think you have in mind something like: import Control.Arrow (..!) :: (CNum n, HasField n (a -> (b,rec a)) l, Convert rec) => l -> n -> (a -> (b,a)) l ..! n = second convert . (l .! n) 1. I don't see a point of creating class. I mean you provide a wildcard implementation - why not provide just a method? 2. I'm afraid that it might be monomorphism restriction. But I'm not sure. 3. Without code I can hardly test the problems ;) I can write what I _think_ code would look like. Regards PS. I would be grateful for ASCII-only posts: http://www.asciiribbon.org/ PPS. convert looks strangly similar to copointed: http://hackage.haskell.org/packages/archive/category-extras/0.53.5/doc/html/...

Thanks, that helped (monomorphism restriction and copointed)...
I'll see if I can come up with a more specific example!
On Tue, Mar 9, 2010 at 6:12 PM, Maciej Piechotka
On Tue, 2010-03-09 at 15:50 +0100, Giuseppe Maggiore wrote:
class (CNum n, HasField n (a -> (b,rec a)) l, Convert rec) => HasMethod n l a b rec where
(..!) :: l -> n -> (a -> (b,a))
instance (CNum n, HasField n (a -> (b,rec a)) l, Convert rec) => HasMethod n l a b rec where
l ..! n =
let m = l .! n
in (\x ->
let (y,v) = m x
in (y,convert v))
i think you have in mind something like:
import Control.Arrow
(..!) :: (CNum n, HasField n (a -> (b,rec a)) l, Convert rec) => l -> n -> (a -> (b,a)) l ..! n = second convert . (l .! n)
1. I don't see a point of creating class. I mean you provide a wildcard implementation - why not provide just a method? 2. I'm afraid that it might be monomorphism restriction. But I'm not sure. 3. Without code I can hardly test the problems ;) I can write what I _think_ code would look like.
Regards PS. I would be grateful for ASCII-only posts: http://www.asciiribbon.org/ PPS. convert looks strangly similar to copointed:
http://hackage.haskell.org/packages/archive/category-extras/0.53.5/doc/html/...
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Giuseppe Maggiore Ph.D. Student (Languages and Games) Microsoft Student Partner Mobile: +393319040031 Office: +390412348444
participants (2)
-
Giuseppe Maggiore
-
Maciej Piechotka