I think whatever code path users construct objects through should be validated, and Sage library developers should be able to exclude validation from internal library code for the sake of speed/simplicity if it makes sense for them (ideally any assumptions are still mentioned in the docstrings though). With the Parent/Element framework this means validation is done by _element_constructor_ and not __init__. I think it can make sense for __init__ to perform normalization (or not) depending on the situation. To be bit more concrete, consider an implementation of the group of integer multiplication mod p (in this example normalization means "reduce an integer mod p").
Input should be validated when an element is created explicitly by a user (input must be coerceable to an integer, and then be converted to an Integer). If a user does a + b with a and b elements of the group, then _mul_ can call __init__ either with the unreduced product (so valid but not normalized) and __init__ can normalize it mod p, or _mul_ can perform the reduction mod p before calling __init__. Either of those I think would be reasonable, and which makes sense will depend on the specifics of the implementation. To keep the _mul_ method short, I would probably normalize in __init__ in this example, but if I were implementing the ring of integers mod p instead of just the multiplicative group I would normalize in the _add_ and _mul_ methods so that I could use a different reduction algorithm in each for speed (and also normalize in _element_constructor_).
I don't know if we need a strict policy for this, but some guidance in our developer guide could be helpful. I think the spirit of this guidance would be "users should not be able to break things using the public API (meaning they should get a clear error message if they try to do something that is not allowed), but no promises are made about what happens when internal functions/methods are called directly".
Vincent Macri (he/him)
[△EXTERNAL]
--
You received this message because you are subscribed to the Google Groups "sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sage-devel/a6de5e8f-3b05-4e2f-8189-8164f873e1dbn%40googlegroups.com.