Using existential types

Tim Docker timd at
Thu Oct 9 05:20:49 EDT 2003

Whilst on existential types, perhaps someone can tell me
if this is a valid use:

I wish to define a flexible report, consisting of a set of columns.
The contents of each column can be defined by mapping a function to a
row descriptor, generating a value, and then converting each value
to a string. I also wish to present an aggregate value at the bottom
of the column defined by another function.

It seems that exisistential types are required to get this sort of
thing to work. Is there another way?



data ColDesc rowv = forall a. ColDesc (rowv -> a) ([a] -> a) (a ->

calculate :: [rowv] -> ColDesc rowv ->  ([String],String)
calculate rs (ColDesc valf sumf fmtf) = let vals = map valf rs in (map
fmtf vals, (fmtf.sumf)  vals)

rows = ["I","wish","to","define","a","report"]

cols = [
    ColDesc id                          (\_ -> "") id,
    ColDesc length                      sum        show,
    ColDesc (\s -> length s * length s) maximum    show

values = [ calculate rows col | col <- cols ]

More information about the Haskell-Cafe mailing list