On Thu, Mar 01, 2012 at 02:58:58PM +0000, Vincent Delecroix wrote:
> 2012/3/1, Anne Schilling <a...@math.ucdavis.edu>:
> > Now Mike pointed out further that whether an element needs to be typed or
> > not to be in the set is rather arbitrary:
> >
> > sage: [3,1,2] in Compositions()
> > True
> > sage: [1,2,3,4] in Words()
> > False
> > sage: [1,0,1,0] in DyckWords()
> > True
> > sage: [[1,2],[3]] in Tableaux()
> > True
> > sage: [1,2,3,4] in LyndonWords()
> > True
> > sage: [[1,2,3],[4,5]] in SetPartitions(5)
> > True
> > sage: [1,2,3,4] in Permutations(4)
> > True

Yes, that's a big can of worms. As Vincent says, the first step would
be to fix clear and final specifications for the meaning of "in" and
of the related "==". It is a difficult task, because there are so many
different use cases for such ubiquitous things in maths as equality
testing and membership. There actually are already some specifications
in Sage, but several of us intuitively find them way too permissive to
be viable in the long run. We are preparing a discussion on the topic
on:

        http://wiki.sagemath.org/EqualityCoercion

> On that question, the default implementation in
> sage.parent.Parent.__contains___ is a bit curious: to test whether x
> is in P, it first does y = P(x). If it leads to a TypeError or
> ValueError then False is returned, otherwise, it returns the value of
> x == y.

That's in case the input data 'x' for the constructor would be too
unrelated to the constructed object (think a group element y
constructed from a reduced word x; you might not want to say that x ==
y in that case). Some sort of heuristic to distinguish between
coercion / conversion / construction.

> We definitely needs two methods for testing whether x is in P:
>  1) strong element checking: x should be an instance of Element and
> has parent P or has a Parent with a natural coercion to P (not valid
> for facade)

>  2) permissive type checking: returns True if P(x) is allowed

Also:

    3) membership test for type checking

       should be fast
       should return True if x is an element of P
       may return True or False if x is not an element of P (False preferable 
of course)

For that one, you can use P.is_parent_of(x). It's specifically crafted
for precondition checks at the beginning of a method:

    def f(self, x):
        assert self.is_parent_of(x)

In other words: we should strive to make the code as easy to refactor
when good and final conventions will be fixed (I hope it's not too
late). Some recommendations to this end:

 - Do not depend, in your code, on the exact meaning of ``in``
   whenever coercions/conversions/... are involved
 - Keep the conversion/equality/membership code as simple as possible
 - Don't change the current behavior if there is no compelling reason
   to do so (beside that it's ugly)
 - Gather all the weirdnesses you encounter in
   http://wiki.sagemath.org/combinat/Weirdness or in tickets

Cheers,
                                Nicolas
--
Nicolas M. ThiƩry "Isil" <nthi...@users.sf.net>
http://Nicolas.Thiery.name/

-- 
You received this message because you are subscribed to the Google Groups 
"sage-combinat-devel" group.
To post to this group, send email to sage-combinat-devel@googlegroups.com.
To unsubscribe from this group, send email to 
sage-combinat-devel+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sage-combinat-devel?hl=en.

Reply via email to