From: Martin Jambon <martin_jambon@em...>  20050606 21:05:20

On Mon, 6 Jun 2005, Nicolas Cannasse wrote: > > > > Hello, > > > > > > > > Some useful operations over the keys of a map are not provided by the > > > > standard Map module, neither by ExtLib's PMap, but are provided only > by > > > > the Set module. > > > > > > Yes I would also love to set diff / union / intersection for PMaps. > > > But I'm not sure which semantic is appropriate. > > > Maybe only have diff / union / inter operations on *keys* would be > enough ? > > > Please fell free to post implementation, maybe based on Set module. > > > > What do you think of this: > > > > (* default merge = fun x y > x *) > > val union : ?merge:('b > 'b > 'b) > ('a, 'b) t > ('a, 'b) t > ('a, > 'b) t > > > > (* a merge function must be given to inter *) > > val inter : merge:('b > 'c > 'd) > ('a, 'b) t > ('a, 'c) t > ('a, 'd) > t > > > > val diff : ('a, 'b) t > ('a, 'c) t > ('a, 'b) t > > Why not generalize a little more ? > > val union : merge:('b > 'c > 'd) > ('a,'b) t > ('a,'c) t > ('a,'d) t > > The only problem is that we can't use "identity" as default merger. It cannot be so simple with the union function because you have to provide 2 additional functions of type ('b > 'd) and ('c > 'd) to convert the bindings which are not in both maps (cannot default to identity either). This could also be achieved by only one merge function of type ('b option > 'c option > 'd), but that's really heavy. In that case, I would first convert my maps of different types to the same type and then use the union function which expects 2 maps of the same type. Since the cost would be linear with respect to the size of the maps, I guess it's OK. Martin  Martin Jambon, PhD http://martin.jambon.free.fr 