[Haskell-cafe] Generalizing unionWithKey, unionWith, ...

Johannes Waldmann waldmann at imn.htwk-leipzig.de
Tue May 28 13:04:15 CEST 2013


Jose A. Lopes <jose.lopes <at> ist.utl.pt> writes:

> What makes it an interesting example ?

it shows that your proposed type for unionWith is not reasonable.

> why would you want to use undefined in that particular case?

the two argument maps have disjoint key sets,
so the combining function will never be called,
and writing "undefined" just states this.

of course, all of this refers to the implicit specification 
for unionWith, which should contain something like

M.keysSet (M.unionWith f m1 m2) = S.union (M.keysSet m1) (M.keysSet m2)

(hence the name, union) and this is an implication of

M.lookup k (M.unionWith f m1 m1) = 
    case (M.lookup k m1, M.lookup k m2) of
        (Nothing,Nothing) -> Nothing
        (Just v1, Nothing) -> Just v1
        (Nothing,Just v2) -> Just v2
        (Just v1, Just v2) -> Just (f v1 v2)

I would very much welcome that such specs be added to the 
library documentation - in some suitable way, e.g., haddock
can generate "source" links already, and it would be nice if it 
also could show "spec" links, pointing to small/quick-check properties, 
which I guess are in the source code somewhere already,
cf. 
https://github.com/haskell/containers/blob/master/tests/map-properties.hs#L408 

- J.W.





More information about the Haskell-Cafe mailing list