## Support of algebraic operations on sets

Bert Freudenberg bert at freudenbergs.de
Fri Jun 15 15:00:03 UTC 2007

```On Jun 15, 2007, at 16:42 , sig wrote:

> On 15/06/07, Bert Freudenberg <bert at freudenbergs.de> wrote:
>>
>> On Jun 15, 2007, at 14:45 , sig wrote:
>>
>> > actually i missing these operations for dictionaries.
>> > in current implementation if you diff/union on two dictionaries,
>> > you'll find out that associations play role as set elements, not
>> keys.
>> >
>> > for dictionaries 'a difference: b' i get not exactly what i would
>> > expect.
>>
>> Dictionaries are like other collections - a collection of
>> "elements" (the values). The keys are only interesting for accessing
>> - like indices on Arrays.  #do: operates on the values. So does
>> #select:, and #difference:. You wouldn't expect #difference: on an
>> Array to work on its indices, would you?
>>
> Consider following example (union of two dictionaries):
>
> | dict1 dict2 result1 result2 |
> dict1 := Dictionary new.
> dict2 := Dictionary new.
> dict1 at: 1 put: 5.
> dict2 at: 1 put: 6.
>
> result1 := dict1 union: dict2.
> result2 := dict2 union: dict1.
>
> union defined as commutative operation fails in this example, because
> result1<>result2.
> it can be considered as commutative only in case if we taking
> resulting keys into account, not values.
> from this point same logics must be followed when doing
> intersection/difference for dictionaries - take only keys into
> account, not their associative values.
>
> So, i'd expect 'dict1 difference: dict2'  must return empty
> dictionary, as well as
> dict1 intersection: dict2  return non-empty dictionary.
>
> Btw, all this is based on wrong semantics of Association '=' mehod,
> which compares not only keys, but values also. This was discussed in
> other topic before.

The actual problem here is that Dictionary is implemented as a
subclass of Set while it is no proper Set. If it wasn't, then the
#union: implementation in Collection would do The Right Thing. Of
course we'd need Dictionary>>asSet defined as "^self values asSet".

So IMHO what *should* happen is that #union: always answers a Set.

- Bert -

```