[Haskell-cafe] How to abstract away set representation

martin martin.drautzburg at web.de
Sun Mar 8 20:52:50 UTC 2015


Hello all,

I was trying to construct a data type "Profile" which allows some set-like behavior. I started with functions to count,
add and filter profiles.

type Count = Int


data Prof a = P {
        pCount  :: Count,
        pFilter :: (a -> Bool) -> Prof a,
        pAdd    :: (Prof a) -> (Prof a)
        } |
        NoProfile

When I tried to actually implement a Profile, the first two functions were easy:

exProfile :: [Int] -> Prof Int
exProfile xs = P {
                   pCount    = length xs,
                   pFilter   = \p -> exProfile [x | x<-xs, p x],
}

But I didn't know how to implement pAdd. I was tempted to write something like

	pAdd = exProfile (xs ++ ys)

But I wouldn't know where to take the ys from. I also hesitate to declare a toList function (which would solve the
problem) in Prof. I would only do this *because* it is handy in exProfile, not because it is an intrinsic feature of Prof.

What would be a good way to work with set-like behavior without being tied to one particular implementation?


More information about the Haskell-Cafe mailing list