[Haskell-cafe] Type classes and hFoldr from HList

Greg Buchholz haskell at sleepingsquirrel.org
Sun Nov 6 22:01:28 EST 2005


  I was playing around with the HList library from the paper...

    Strongly typed heterogeneous collections
    http://homepages.cwi.nl/~ralf/HList/

...and I thought I'd try to fold the composition function (.) through a
heterogeneous list of functions, using hFoldr...

>{-# OPTIONS -fglasgow-exts #-}
>{-# OPTIONS -fallow-undecidable-instances #-}
>
>import CommonMain
>
>main = print $ comp "abc"
>
>test = HCons ((+1)::(Int->Int)) (HCons ((*2)::(Int->Int)) (HCons length HNil))
>
>comp = hFoldr (.) id test
>
>instance Apply (a -> b -> c -> d) (a, b) (c -> d)  
>    where
>        apply f (a,b) = f a b
            
...but it fails with the following type error...

]Compiling Main             ( compose.hs, interpreted )
]
]compose.hs:10:7:
]    No instances for (Apply ((b -> c) -> (a -> b) -> a -> c)
]                            (Int -> Int, r)
]                            ([Char] -> a3),
]                      Apply ((b -> c) -> (a -> b) -> a -> c) (Int -> Int, r1) r,
]                      Apply ((b -> c) -> (a -> b) -> a -> c) ([a2] -> Int, a1 ->a1) r1)
]      arising from use of `hFoldr' at compose.hs:10:7-12
]    Probable fix:
]      add an instance declaration for (Apply ((b -> c) -> (a -> b) -> a -> c)
]                                             (Int -> Int, r)
]                                             ([Char] -> a3),
]                                       Apply ((b -> c) -> (a -> b) -> a -> c)
](Int -> Int, r1) r,
]                                       Apply ((b -> c) -> (a -> b) -> a -> c)
]([a2] -> Int, a1 -> a1) r1)
]    In the definition of `comp': comp = hFoldr (.) id test

...Anyway, I couldn't quite tell whether I was using hFoldr incorrectly,
or if I needed to have more constraints placed on the construction of
"test", or if needed some sort of type-level function that resolves...

Apply ((b -> c) -> (a -> b) -> a -> c)

...into (a -> c), or something else altogether.  I figured someone might
be able to help point me in the right direction.


Thanks,

Greg Buchholz



More information about the Haskell-Cafe mailing list