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...