[Haskell-cafe] Re: Bi-directional Maps

Hugh Perkins hughperkins at gmail.com
Tue Aug 21 09:08:53 EDT 2007


> Exactly. For this to work there needs to be the constraint that there's a
> one-to-one mapping in each direction. The Bimap should have the uniqueness
> promise that "Set (k, v)" gives. Yet you should be able to search on either
> tuple value.

Or... have the possibility of returning a list of values.

Arguably there are two possible implementations, one that enforces
one-to-one mapping, and one which allows multiple values, in either
direction.

"But how can you change a value if there are non-unique keys?".  Well,
you dont change a value, you change a list of values ;-)

So, let's say our bimap is:

1,1
1,2
5,2
5,3

then:

bimap_getvalue ourbimap 1 gives  [1,2]
bimap_getkey ourbimap 2 gives [1,5]

Executing bimap_setkey ourbimap 2 [1,4] changes the bimap to:

1,1
1,2
4,2
5,3


More information about the Haskell-Cafe mailing list