Generalizing unionWithKey, unionWith, ...

Hello everyone, unionWithKey and unionWith have the following types unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a Since they are implemented by means of mergeWithKey, wouldn't it be possible to generalize these functions to the following types ? unionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c unionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c Cheers, Jose -- José António Branquinho de Oliveira Lopes Instituto Superior Técnico Technical University of Lisbon

Jose A. Lopes
unionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c
what should be the result of unionWith undefined (M.singleton False 42) (M.singleton True "bar") ? perhaps you mean intersectionWith, which already has the type you want. - J.W.

Yes! intersectionWith is just what I needed. In any case, coming back to your example, why would you want to use undefined in that particular case? What makes it an interesting example ? Best, Jose On 28-05-2013 12:32, Johannes Waldmann wrote:
Jose A. Lopes
writes: unionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c
what should be the result of
unionWith undefined (M.singleton False 42) (M.singleton True "bar") ?
perhaps you mean intersectionWith, which already has the type you want.
- J.W.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- José António Branquinho de Oliveira Lopes Instituto Superior Técnico Technical University of Lisbon

Jose A. Lopes
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#L4... - J.W.

Dne 28.5.2013 12:32, Johannes Waldmann napsal(a):
Jose A. Lopes
writes: unionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c
what should be the result of
unionWith undefined (M.singleton False 42) (M.singleton True "bar") ? Perhaps the generalized signature should be instead:
```haskell unionWith :: Ord k => (Maybe a -> Maybe b -> c) -> Map k a -> Map k b -> Map k c ``` (The function would always get at least one `Just`.) But this functionality can be achieved using `map`s and the current `unionWith`. P.P.
participants (3)
-
Johannes Waldmann
-
Jose A. Lopes
-
Petr Pudlák