A issue with the Set function in sage

47 views
Skip to first unread message

Ars-Magna

unread,
Jun 25, 2020, 7:49:00 AM6/25/20
to sage-devel
Is the following a bug ?

sage: Set([[(Integer(0), Integer(0)), (Integer(1), Integer(1))],[(Integer(0), Integer(0)), (Integer(1), Integer(1))]]).cardinality()

2

Frédéric Chapoton

unread,
Jun 25, 2020, 7:58:21 AM6/25/20
to sage-devel
Try with "set" and you will see an informative error : lists are not hashable

Dima Pasechnik

unread,
Jun 25, 2020, 8:00:23 AM6/25/20
to sage-devel
hard to say. Note that

sage: Set([[(0,0), (1,1)],[(0,0), (1,1)]]).cardinality()
2
sage: Set([((0,0), (1,1)),((0,0), (1,1))]).cardinality()
1

and even
sage: Set([[0],[0]]).cardinality()
2

- the problem is that lists are not hashable, and considered unequal.
The problem is that in general testing objects for equality is far
from obvious, might be algorithmically unsolvable, etc.

>
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/4c7163cb-84c5-4bf0-9ef3-585e709883b7o%40googlegroups.com.

Nils Bruin

unread,
Jun 25, 2020, 1:19:26 PM6/25/20
to sage-devel


On Thursday, June 25, 2020 at 5:00:23 AM UTC-7, Dima Pasechnik wrote:

- the problem is that lists are not hashable, and  considered unequal.
The problem is that in general testing objects for equality is far
from obvious, might be algorithmically unsolvable, etc.

Why is Set trying then? I can see in the documentation that "Sets with unhashable objects work, but with less functionality", but as this example shows, it's more "but with misleading functionality". How is this ever useful?

sage: a,b=list(Set([[0],[0]]))
sage: a==b
True

I don't know if this is ever useful internally (I doubt it; I expect it mainly makes bugs harder to find), but we should definitely not be exposing this as user interface, Python's own "frozenset" and "set" are better behaved.

Martin R

unread,
Jun 25, 2020, 3:43:45 PM6/25/20
to sage-devel
See https://trac.sagemath.org/ticket/23324 for some discussion.

Travis Scrimshaw

unread,
Jun 25, 2020, 8:00:52 PM6/25/20
to sage-devel


- the problem is that lists are not hashable, and  considered unequal.
The problem is that in general testing objects for equality is far
from obvious, might be algorithmically unsolvable, etc.

Why is Set trying then? I can see in the documentation that "Sets with unhashable objects work, but with less functionality", but as this example shows, it's more "but with misleading functionality". How is this ever useful?

Because sometimes you need to have something that is a Sage object in a category. Also sometimes you want a set-like object with unhashable things. However, I agree that it should try a bit harder to remove redundant elements by a brute force check.
 
sage: a,b=list(Set([[0],[0]]))
sage: a==b
True

I don't know if this is ever useful internally (I doubt it; I expect it mainly makes bugs harder to find), but we should definitely not be exposing this as user interface, Python's own "frozenset" and "set" are better behaved.

+1 for general internal use. However, set and frozenset can behave quite differently than over objects in Sage, and something dealing with that nonuniformity leads to more cases and code.

Best,
Travis

Reply all
Reply to author
Forward
0 new messages