Opened 6 years ago

Last modified 5 years ago

#22707 needs_work defect

Remove CombinatorialFreeModule.__contains__

Reported by: Simon King Owned by:
Priority: major Milestone: sage-8.2
Component: algebra Keywords:
Cc: Sage Combinat CC user, Nicolas M. Thiéry Merged in:
Authors: Simon King Reviewers:
Report Upstream: N/A Work issues:
Branch: u/SimonKing/Remove_custom_contains (Commits, GitHub, GitLab) Commit: ba5f963354c69109a6c5abaaaf5dfd1ecbcbafa8
Dependencies: Stopgaps:

Status badges

Description

Parent.__contains__ is implemented so that x in R is basically the same as R(x)==x.

CombinatorialFreeModule overrides it, so that it becomes x.parent()==R. That notion of containment is totally against Sage's coercion framework and should finally (after 9 years) be removed.

Change History (5)

comment:1 Changed 6 years ago by Simon King

Branch: u/SimonKing/Remove_custom_contains

comment:2 Changed 6 years ago by Simon King

Authors: Simon King
Commit: ba5f963354c69109a6c5abaaaf5dfd1ecbcbafa8
Status: newneeds_review

I didn't run the tests yet...


New commits:

ba5f963Remove custom containment test in CombinatorialFreeModule

comment:3 Changed 6 years ago by Travis Scrimshaw

Cc: Sage Combinat CC user Nicolas M. Thiéry added

This broadens the result of __contains__ by allowing things that convert into the CFM, so it is a behavior change. I'm not against this change, and I doubt anyone is actually using this distinction, but it is something to be aware of.

sage: F.<x,y> = FreeAlgebra(QQ)
sage: R.<x,y> = FreeAlgebra(ZZ)
sage: elt = 2*x + y
sage: elt in F
False
sage: elt in R
True
sage: F(elt) == elt
True
sage: F(elt) in R
False

Also, in the cases of returning False, it is likely a slower check.

comment:4 Changed 6 years ago by Nicolas M. Thiéry

Hi Simon, Travis,

Hmm, the behaviour implemented in Parent is tricky and questionable. Generally speaking, anything involving mathematical equality is intrinsically a potential can of worm; see e.g. https://wiki.sagemath.org/EqualityCoercion.

Let's take an example:

sage: S = SymmetricFunctions(QQ)
sage: S.inject_shorthands()
sage: f = s[3]; f
s[3]
sage: f.parent()
Symmetric Functions over Rational Field in the Schur basis
sage: e
Symmetric Functions over Rational Field in the elementary basis
sage: f in e
False
sage: e(s[3])
e[1, 1, 1] - 2*e[2, 1] + e[3]

As a user, I might not expect s to be in e: it's certainly a symmetric function, but not one expressed in the e basis. There is a canonical coercion between the two, but it is nowhere trivial.

Well, maybe I just have to accept that there is no perfect answer and consistency should just be the priority. I don't remember: did we specify explicitly somewhere what the semantic of contains should be, and how it should behave w.r.t. coercion?

In summary, like for equality, I am not comfortable with having coercion involved, but maybe that's ok. On the other hand conversion certainly should not be involved, as there is nothing canonical with them. Maybe that's more or less taken care of by the equality test of R(x)=x.

comment:5 Changed 5 years ago by Frédéric Chapoton

Milestone: sage-8.0sage-8.2
Status: needs_reviewneeds_work

does not apply

Note: See TracTickets for help on using tickets.