Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove naive __hash__ from SageObject #18246

Closed
videlec opened this issue Apr 18, 2015 · 68 comments
Closed

remove naive __hash__ from SageObject #18246

videlec opened this issue Apr 18, 2015 · 68 comments

Comments

@videlec
Copy link
Contributor

videlec commented Apr 18, 2015

Hashing is a critical function in Python. In several places, objects do use the generic __hash__ from SageObject or a badly implemented one that makes everything slow. For example:

See also #19016 for a ticket with the same purpose (with Element instead of SageObject).

CC: @nathanncohen

Component: coercion

Author: Vincent Delecroix

Branch/Commit: 5627a1d

Reviewer: Volker Braun, Nils Bruin

Issue created by migration from https://trac.sagemath.org/ticket/18246

@videlec videlec added this to the sage-6.7 milestone Apr 18, 2015
@videlec
Copy link
Contributor Author

videlec commented Apr 18, 2015

Commit: a7aa08f

@videlec
Copy link
Contributor Author

videlec commented Apr 18, 2015

New commits:

a7aa08fTrac 18246: remove `__hash__` from Sage object

@videlec
Copy link
Contributor Author

videlec commented Apr 18, 2015

Branch: public/18246

@videlec

This comment has been minimized.

@nathanncohen
Copy link
Mannequin

nathanncohen mannequin commented Apr 18, 2015

comment:3
sage: hash(SageObject())
8751828132417

I am fixing it.

@nathanncohen
Copy link
Mannequin

nathanncohen mannequin commented Apr 18, 2015

comment:4

As expected everything breaks. What do we do?

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 18, 2015

Changed commit from a7aa08f to 6b9f883

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 18, 2015

Branch pushed to git repo; I updated commit sha1. New commits:

6b9f883trac #18246: raise NotImplementedError

@videlec
Copy link
Contributor Author

videlec commented Apr 18, 2015

comment:6

You should raise a TypeError if you want to raise something. Otherwise it breaks too much stuff (in particular cached_method, cached_function)

@nathanncohen
Copy link
Mannequin

nathanncohen mannequin commented Apr 18, 2015

comment:7

TypeError ? Well, why not...

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 18, 2015

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

2869bc2trac #18246: raise TypeError

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 18, 2015

Changed commit from 6b9f883 to 2869bc2

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 18, 2015

Branch pushed to git repo; I updated commit sha1. New commits:

631e5c1Trac 18236: more explicit TypeError message
61b0fceTrac 18246: make all test pass in sage/rings

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 18, 2015

Changed commit from 2869bc2 to 61b0fce

@videlec
Copy link
Contributor Author

videlec commented Apr 18, 2015

comment:10

Most of the time, it is very easy to fix. But I had to implement a lot

def __hash__(self):
    return id(self)

which I guess is close from the default Python implementation.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 12, 2015

Changed commit from 61b0fce to 5a1e636

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 12, 2015

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

46a1f74Trac 18246: remove `__hash__` from Sage object
4e53eeeTrac 18246: fix polynomial term order
2282b78Trac 18246: fix `__hash__` in rings
9f4ea07Trac 18246: fix `__hash__` in plot
a4e58ebTrac 18246: fix `__hash__` in geometry
5a1e636Trac 18246: fix `__hash__` in combinat

@videlec
Copy link
Contributor Author

videlec commented Aug 12, 2015

comment:12

Let us see how much patchbot is happy...

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 12, 2015

Branch pushed to git repo; I updated commit sha1. New commits:

d53c30eTrac 18246: fix `__hash__` in algebras
518c3c2Trac 18246: fix `__hash__` in homology
72c50beFix 18246: fix `__hash__` in quadratic_forms

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 12, 2015

Changed commit from 5a1e636 to 72c50be

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 12, 2015

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

8641677Trac 18246: fix `__hash__` in quadratic_forms

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 12, 2015

Changed commit from 72c50be to 8641677

@videlec
Copy link
Contributor Author

videlec commented Aug 12, 2015

Author: Vincent Delecroix

@videlec videlec modified the milestones: sage-6.7, sage-6.9 Aug 12, 2015
@vbraun vbraun closed this as completed Aug 14, 2015
@vbraun
Copy link
Member

vbraun commented Aug 20, 2015

Changed commit from 4d8ec2a to none

@vbraun
Copy link
Member

vbraun commented Aug 20, 2015

comment:37

Fails on ARM

sage -t --long src/sage/combinat/rigged_configurations/rigged_configurations.py
**********************************************************************
File "src/sage/combinat/rigged_configurations/rigged_configurations.py", line 252, in sage.combinat.rigged_configurations.rigged_configurations.RiggedConfigurations
Failed example:
    RC.list()
Expected:
    [
    <BLANKLINE>
                                             0[ ]0
    (/)  (/)      (/)      -1[ ]-1  -1[ ]-1
                                             -1[ ]-1
    (/)  -1[ ]-1  0[ ]0    0[ ]0    1[ ]1    -1[ ]-1
    <BLANKLINE>
    (/)  (/)      -1[ ]-1  (/)      -1[ ]-1  0[ ]0
       ,        ,        ,        ,        ,
    ]
Got:
    [
    <BLANKLINE>
                                             0[ ]0  
    (/)  (/)      -1[ ]-1  -1[ ]-1  (/)             
                                             -1[ ]-1
    (/)  -1[ ]-1  0[ ]0    1[ ]1    0[ ]0    -1[ ]-1
    <BLANKLINE>
    (/)  (/)      (/)      -1[ ]-1  -1[ ]-1  0[ ]0  
       ,        ,        ,        ,        ,        
    ]
**********************************************************************
1 item had failures:
   1 of  38 in sage.combinat.rigged_configurations.rigged_configurations.RiggedConfigurations
    [239 tests, 1 failure, 250.36 s]

@vbraun vbraun reopened this Aug 20, 2015
@vbraun
Copy link
Member

vbraun commented Aug 20, 2015

Commit: 4d8ec2a

@vbraun
Copy link
Member

vbraun commented Aug 20, 2015

Last 10 new commits:

ab52a2dTrac 18246: fix `__hash__` in algebras
3dfe73eTrac 18246: fix `__hash__` in homology
148ad98Trac 18246: fix `__hash__` in quadratic_forms
124871cTrac 18246: fix doctests for QuadraticForm
1e59d3aTrac 18246: fix some hash values on 32-bit system
e9a8971Trac 18246: use WithEqualityById
072a017Trac 18246: more cleanup
0bf7d7dTrac 18246: typo
a722920Trac 18246: fix a doctest in rigged_configurations.py
4d8ec2aTrac 18246: refix rigged_configurations

@vbraun
Copy link
Member

vbraun commented Aug 20, 2015

Changed branch from 4d8ec2a to public/18246

@tscrim
Copy link
Collaborator

tscrim commented Aug 20, 2015

comment:39

I actually don't like these changes:

-class VectorPartitions(Parent, UniqueRepresentation):
+class VectorPartitions(UniqueRepresentation, Parent):

Why does UniqueRepresentation have to come before Parent? If this how things must be, then we should create a class URParent (or a better name), use that all over Sage to avoid this issue. (FYI: Before this ticket, there are 107 classes which use UR, Parent and 107 which use Parent, UR.)

Also can someone explain to me why are RiggedConfigurations now showing machine dependent iteration order?

@nbruin
Copy link
Contributor

nbruin commented Aug 20, 2015

comment:40

Replying to @tscrim:

I actually don't like these changes:

-class VectorPartitions(Parent, UniqueRepresentation):
+class VectorPartitions(UniqueRepresentation, Parent):

Why does UniqueRepresentation have to come before Parent?

Because UniqueRepresentation provides a hash that is fast and appropriate and Parent doesn't:

sage: Parent.__hash__
<slot wrapper '__hash__' of 'sage.structure.category_object.CategoryObject' objects>
sage: UniqueRepresentation.__hash__
<slot wrapper '__hash__' of 'sage.misc.fast_methods.WithEqualityById' objects>

In order to get the MRO right, UniqueRepresentation needs to appear before Parent:

sage: class A(UniqueRepresentation, Parent): pass
sage: A.__hash__
<slot wrapper '__hash__' of 'sage.misc.fast_methods.WithEqualityById' objects>
sage: class B(Parent, UniqueRepresentation): pass
sage: B.__hash__
<slot wrapper '__hash__' of 'sage.structure.category_object.CategoryObject' objects>

Once all default hashes have been deleted and no classes in the MRO of Parent offer a hash function, the order doesn't matter. Until then, UniqueRepresentation has to come first.

Also can someone explain to me why are RiggedConfigurations now showing machine dependent iteration order?

The code is hard to follow, but in general, if enumeration somewhere involves a set or a dictionary, then results depend heavily on the hash used and the order in which the dictionary was constructed. I'd expect that a changed hash causes things to be enumerated in a different order (because a set or dict is storing its entries in a different order somewhere)

@nbruin
Copy link
Contributor

nbruin commented Sep 4, 2015

comment:42

Looks good, tests pass, very desirable change. Thanks!

@nbruin
Copy link
Contributor

nbruin commented Sep 4, 2015

Changed reviewer from Volker Braun to Volker Braun, Nils Bruin

@vbraun
Copy link
Member

vbraun commented Sep 4, 2015

Changed branch from public/18246 to 4d8ec2a

@vbraun
Copy link
Member

vbraun commented Sep 6, 2015

Changed branch from 4d8ec2a to public/18246

@vbraun
Copy link
Member

vbraun commented Sep 6, 2015

comment:45

More random test failures:

sage -t --long src/sage/combinat/rigged_configurations/rigged_configurations.py
**********************************************************************
File "src/sage/combinat/rigged_configurations/rigged_configurations.py", line 252, in sage.combinat.rigged_configurations.rigged_configurations.RiggedConfigurations
Failed example:
    RC.list()
Expected:
    [
    <BLANKLINE>
                                             0[ ]0
    (/)  (/)      (/)      -1[ ]-1  -1[ ]-1
                                             -1[ ]-1
    (/)  -1[ ]-1  0[ ]0    0[ ]0    1[ ]1    -1[ ]-1
    <BLANKLINE>
    (/)  (/)      -1[ ]-1  (/)      -1[ ]-1  0[ ]0
       ,        ,        ,        ,        ,
    ]
Got:
    [
    <BLANKLINE>
                                             0[ ]0  
    (/)  (/)      -1[ ]-1  -1[ ]-1  (/)             
                                             -1[ ]-1
    (/)  -1[ ]-1  0[ ]0    1[ ]1    0[ ]0    -1[ ]-1
    <BLANKLINE>
    (/)  (/)      (/)      -1[ ]-1  -1[ ]-1  0[ ]0  
       ,        ,        ,        ,        ,        
    ]
**********************************************************************
1 item had failures:
   1 of  38 in sage.combinat.rigged_configurations.rigged_configurations.RiggedConfigurations
    [239 tests, 1 failure, 99.45 s]

@vbraun vbraun reopened this Sep 6, 2015
@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 6, 2015

Branch pushed to git repo; I updated commit sha1. New commits:

1ae9393merge 18246 in Sage-6.9.beta5
5627a1dTrac 18246: fixed random output

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 6, 2015

Changed commit from 4d8ec2a to 5627a1d

@videlec
Copy link
Contributor Author

videlec commented Sep 6, 2015

comment:49

Hoping this is the only failure...

@vbraun
Copy link
Member

vbraun commented Sep 7, 2015

Changed branch from public/18246 to 5627a1d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants