Skip to content

Refined protocol for _element_constructor_ in element classes with mutability #29101

@sagetrac-nailuj

Description

@sagetrac-nailuj

Problem description:

Given a vector v with base ring R, the constructions

  • w = vector(R, v)
  • w = vector(v, R)
  • w = vector(v)
    all return the original vector v. As a consequence, all changes applied to w are also applied to v and vice versa.
sage: v = vector([1,2,3])
sage: w = vector(v, ZZ)
sage: w is v
True
sage: w[1] = 7
sage: v
(1, 7, 3)

Proposal:

The __call__ method of a parent object serves several purposes: In one-argument calls, (1) it is the identity (in the strong sense of Python's id) on elements of its parent and (2) in general, a convert map, with (3) input 0 as a permissive special case, which is also the default argument of Parent.__call__. (4) In multiple-argument calls, it is the element constructor.

Proposal: Define a general protocol for element classes with mutability which does NOT change the above but only refines it for mutable classes as follows:

  • In element classes with mutability, _element_constructor_(x, ...) MUST support optional arguments copy and mutable.

  • These arguments are allowed to have various defaulting behavior that is the most appropriate for the specific class, but there are some restrictions:

  • copy MUST NOT default to True for mutable inputs x because (as discussed) this is not compatible with (1).

  • mutable=False and copy=False MUST be an error for mutable input x.

Moreover, arithmetic operations need to be consistent: Either always return a new element (even for trivial operations), or always return an immutable element.

We add _test_... methods that check this protocol.

Related:

CC: @kliem @mjungmath @mkoeppe @tscrim @nbruin @dcoudert @mwageringel @pjbruin @kwankyu @williamstein

Component: misc

Keywords: vector, constructor, copy

Branch/Commit: u/mkoeppe/refined_protocol_for__element_constructor__in_element_classes_with_mutability @ 501e7ef

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions