sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.41k stars 474 forks source link

`Permutations()` and other combinatorial classes error out on mutable elements #38814

Open maxale opened 1 week ago

maxale commented 1 week ago

Steps To Reproduce

Calls like Permutations([[1],[2],[3]]), Subsets([[1],[2],[3]]), SetPartitions([[1],[2],[3]]) etc. all result in the error:

TypeError: unhashable type: 'list'

Expected Behavior

Should work.

Actual Behavior

TypeError

Additional Information

No response

Environment

Checklist

maxale commented 1 week ago

Btw, it's ironic, when Set() is happy with mutable elements, while Subsets() is not:

sage: Set([[1],[2],[3]])
Set of elements of [[1], [2], [3]]
sage: Subsets(_)
[...]
TypeError: unhashable type: 'list'

Related #23324.

DaveWitteMorris commented 1 week ago

This seems to be an enhancement request, not a bug. (However, the documentation should probably specify that the elements of the set must be hashable.) As a workaround, you can use tuples, instead of lists:

sage: Permutations( [(1,), (2,), (3,)] )
Permutations of the set [(1,), (2,), (3,)]
mantepse commented 1 week ago

As mentioned at #23324, I don't think that this would be a good idea. I still think that we should require immutable objects also for Set.

maxale commented 1 week ago

As mentioned at #23324, I don't think that this would be a good idea. I still think that we should require immutable objects also for Set.

I support the idea of requiring immutable objects for all these classes (including Set), since that will save them from various side effects. But it's still a bug since documentation does not mention such an important requirement.