[Haskell-cafe] Trying to understand HList / hMapOut

Matthias Fischmann fis at wiwi.hu-berlin.de
Fri Oct 6 10:28:33 EDT 2006


I am using a heterogenous list as in [1] all elements of which are of
a given class C.  I am awed by the beauty of the code (HList, not
mine :-).  Here is what I am trying doing:

import HListPrelude

data T = T Int

class     C a  where foo :: a -> Int
instance  C T  where foo (T i) = i

class HList l => CList l

instance CList HNil
instance (C c, CList cs) => CList (HCons c cs)

Since foo maps all class members to Int, hMapOut should be a
straight-forward way to produce homogenous Int lists from heterogenous

test :: (CList l) => l -> [Int]
test = hMapOut foo

That would be too easy, though...  ghci (6.4) sez:

    Could not deduce (HMapOut (a -> Int) l Int) from the context (CList l)
      arising from use of `hMapOut' at /home/fis/tmp/Main.hs:16:7-13
    Probable fix:
      add (HMapOut (a -> Int) l Int) to the type signature(s) for `test'
      or add an instance declaration for (HMapOut (a -> Int) l Int)
    In the definition of `test': test = hMapOut foo

hugs (version 20050308) sez:

ERROR "/home/fis/esim/HList/FakePrelude.hs":113 - Overlapping instances for class "Show"
*** This instance   : Show (HSucc a)
*** Overlaps with   : Show (HSucc HZero)
*** Common instance : Show (HSucc HZero)

(oops?  should i upgrade, or is there a switch that i missed?)

I am trying to understand the ghci error message, which looks like I
might have missed a point.  It seems like I should make it more
obvious to ghc that CLists actually consist of values of type C.  Is
that a good guess?  But how do I actually do it?  I have
unsuccessfully to extend the context to

 (HMapOut (a -> Int) l Int, CList l)

or even to

 (C a, HMapOut (a -> Int) l Int, CList l)

and I can't see any reason why the instance declarations of HMap in
HListPrelude shouldn't cover my code.

I am feeling a little stuck again...


[1] http://homepages.cwi.nl/~ralf/HList/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20061006/4dfcd0e1/attachment.bin

More information about the Haskell-Cafe mailing list