aptos-core icon indicating copy to clipboard operation
aptos-core copied to clipboard

[simple_set] simple version of set

Open lightmark opened this issue 2 years ago • 13 comments

Description

I simplified insert and remove api by enforcing copy + drop

Test Plan

ut

lightmark avatar Dec 18 '22 06:12 lightmark

please add a method to get all the set elements, like:

public fun into_keys<K: copy + drop>(self: SimpleSet<K>): vector<K>

geekflyer avatar Dec 18 '22 07:12 geekflyer

please add a method to get all the set elements, like:

public fun into_keys<K: copy + drop>(self: SimpleSet<K>): vector<K>

Can you explain the necessity as SimpleMap doesn't provide this.

lightmark avatar Dec 18 '22 07:12 lightmark

please add a method to get all the set elements, like:

public fun into_keys<K: copy + drop>(self: SimpleSet<K>): vector<K>

Let’s first talk about this as this causes problems by exposing implementation details via the exposed order. There is a reason we do not have this for SimpleMap

wrwg avatar Dec 18 '22 08:12 wrwg

please add a method to get all the set elements, like:

public fun into_keys<K: copy + drop>(self: SimpleSet<K>): vector<K>

Can you explain the necessity as SimpleMap doesn't provide this.

A common use-case for Sets is to use them as an intermediary to dedupe values in a vector and return the deduped items as a simple vector. Order doesn't matter.

geekflyer avatar Dec 19 '22 19:12 geekflyer

@movekevin Think of undroppable(uncopyable) objects, how do you want to use it with set? Do you remember why Table has key type copyable required?.. For example, contains() need a &Key so in order to know it contains a key you have to pass in a reference to the key. If this key is not copyable, how do you get it's reference when itself is in the set. It makes little sense, right?

lightmark avatar Dec 23 '22 03:12 lightmark

do

Your point about copyable makes sense. But what about droppable?

movekevin avatar Jan 03 '23 08:01 movekevin

I'm fine with this, but I wonder why we didn't just make a dummy value and use SimpleMap.

SimpleMap<Key, DummyValue> where struct DummyValue has store { }

davidiw avatar Jan 04 '23 19:01 davidiw

I don't think we can have this part of our framework and call it a "set" because it doesn't has a very simple property important for sets: equality. Equality of two sets with different order of insertion is not given.

Being able to compare sets for equality independent of insertion order is a very common use case for sets (I used it often in my daily programming). This cannot go like this into the framework.

Really torn about this. It feels this will be still a utility of use, but set equality is really a common use case (whereas map equality isn't, so SimpleMap not having this is ok).

We are really doctoring around that we do not have an adequate native comparison operator in Move. We should add this one ASAP.

In the meantime, maybe we should call this SimpleIntensionalSet. An intensional data structure, in some mathematic worlds, is one which does not have equality (in difference to extensionality, which is characterized by the law of extensionality, i.e. x == y <==> ALL e. (e in x) == (e in y).

wrwg avatar Jan 05 '23 04:01 wrwg

I don't think we can have this part of our framework and call it a "set" because it doesn't has a very simple property important for sets: equality. Equality of two sets with different order of insertion is not given.

May be this can be provided with adding a native function for serializing the set into bytes that ensures order of elements?

movekevin avatar Jan 05 '23 04:01 movekevin

I don't think we can have this part of our framework and call it a "set" because it doesn't has a very simple property important for sets: equality. Equality of two sets with different order of insertion is not given.

May be this can be provided with adding a native function for serializing the set into bytes that ensures order of elements?

movekevin avatar Jan 05 '23 04:01 movekevin

I'm fine with this, but I wonder why we didn't just make a dummy value and use SimpleMap.

SimpleMap<Key, DummyValue> where struct DummyValue has store { }

I plan to reuse simple map with SimpleMap<Key, ()> once we loose all the V: store constraints.

lightmark avatar Jan 05 '23 07:01 lightmark

extensionality

I don't think we can have this part of our framework and call it a "set" because it doesn't has a very simple property important for sets: equality. Equality of two sets with different order of insertion is not given.

Being able to compare sets for equality independent of insertion order is a very common use case for sets (I used it often in my daily programming). This cannot go like this into the framework.

I took a look at cpp unordered_set and set. The time complexity of their == operator is n^2 and n, respectively. So it can be implemented, just with high cost in MOVE. Simple_set is by nature unordered and the in-memory version that we discussed in slack can be ordered and serialized as a sequence of tuples so that equality can be implemented by comparing their byte format.

btw, in programming world, I think equality just means extensionality... So why would we bother to over-complicate the concept by giving such a name...

lightmark avatar Jan 05 '23 08:01 lightmark

do you want to rename this unordered_set?

davidiw avatar Jan 25 '23 20:01 davidiw

do you want to rename this unordered_set?

Why? Do you rename simple_map to unordered_map?

btw, do we have any plan to implement native implementation for simple_map that it reads a blob from storage and instantiate an in-memory hashmap/btreemap and serialize it into a deterministic binary format into write_set? I believe this may benefit a lot implementation including smart_table.

lightmark avatar Jan 28 '23 17:01 lightmark

do you want to rename this unordered_set?

Why? Do you rename simple_map to unordered_map?

btw, do we have any plan to implement native implementation for simple_map that it reads a blob from storage and instantiate an in-memory hashmap/btreemap and serialize it into a deterministic binary format into write_set? I believe this may benefit a lot implementation including smart_table.

We need to discuss perhaps in person. I definitely are of the opinion a set type without correct equality cannot be part of the framework. The current simple_map is already bad enough. General and fast comparison is coming to Move. Then this can be done right.

wrwg avatar Jan 28 '23 23:01 wrwg

cc @movekevin .Please take a look at @wrwg 's comment. Then let's hold off putting this inside framework and you can use simple_map for now for multi-sig acct.

lightmark avatar Jan 31 '23 10:01 lightmark