Polymorphic lists...

MR K P SCHUPKE k.schupke at imperial.ac.uk
Tue Mar 9 14:09:34 EST 2004

I have written a first attempt at a fold function for the heterogenious list: 

class RFold i r where
   rFold :: (forall a . a -> i -> i) -> i -> r -> i
instance RFold i RNil where
   rFold f i RNil = i 
instance RFold i r => RFold i (a `RCons` r) where
   rFold f i (x `RCons` xs) = f x (rFold f i xs)

This works providing the folded 'op' has the type: forall a . a -> i -> i
which means it does not work for functions like show :: forall a . Show a => a -> i -> i
as the types are different. I have not figured out a way to make it accept a constraint 
like Show for example. Here is an example:

length = rFold (\_ -> (+1)) 0 relation

The use of such a function seems limited, if constraints like Show cannot be used, as
most useful applications of fold would require some kind of class membership for example:

string = rFold shows "" relation

This fails to compile because shows has type:

shows :: forall a . Show a => a -> i -> i

but fold expects

op :: forall a . a -> i -> i

	Keean Schupke.

More information about the Glasgow-haskell-users mailing list