#16316: cached_function and cached_method for unhashable elements
-------------------------------------+-------------------------------------
       Reporter:  saraedum           |        Owner:
           Type:  enhancement        |       Status:  positive_review
       Priority:  minor              |    Milestone:  sage-6.3
      Component:  misc               |   Resolution:
       Keywords:                     |    Merged in:
        Authors:  Julian Rueth       |    Reviewers:  Peter Bruin, Travis
Report Upstream:  N/A                |  Scrimshaw
         Branch:                     |  Work issues:
  u/saraedum/ticket/16316            |       Commit:
   Dependencies:  #16251             |  83944fabe78c5561f0cc5b0845d5e8bdb30098c2
                                     |     Stopgaps:
-------------------------------------+-------------------------------------

Comment (by pbruin):

 Replying to [comment:38 tscrim]:
 > Replying to [comment:34 pbruin]:
 > > Hmm, I see. But if we store the parent itself, then I think we have to
 make sure that all unhashable elements belong to parents satisfying unique
 representation.  Otherwise, calling a cached function on an element whose
 parent is equal but not identical to the parent of an element that was
 used before could return a cached result that lives in this equal-but-not-
 identical parent.
 >
 > No, this isn't a problem because equal parents have equal hashes.
 That is exactly what causes the problem.  Consider the following example
 with a hypothetical parent class `MyParent` that has unhashable elements
 but does not have unique representation:
 {{{
 sage: @cached_function
 sage: def f(x):
 ....:     """
 ....:     Return `x^2 + 1` in the parent of `x`.
 ....:     """
 ....:     return x^2 + 1
 ....:
 sage: P = MyParent()
 sage: Q = MyParent()  # Q is equal but not identical to P
 sage: f(P(0)).parent() is P
 True
 sage: f(Q(0)).parent() is Q
 False  # since the parents are equal, f(q) returns the cached f(p)
 sage: f(P(0)) is f(Q(0))
 True
 }}}
 This already happens before this patch with parent ''P'' and ''Q'' where
 there exists a coercion map compatible with hashing (e.g. '''Z''' and
 '''Q'''):
 {{{
 sage: @cached_function
 ....: def f(x):
 ....:     return x^2 + 1
 ....:
 sage: f(ZZ(0)).parent()
 Integer Ring
 sage: f(QQ(0)).parent()
 Integer Ring
 sage: f(ZZ(0)) is f(QQ(0))
 True
 }}}
 However, I think we should avoid this situation whenever possible.

--
Ticket URL: <http://trac.sagemath.org/ticket/16316#comment:40>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.

Reply via email to