On Mon, May 17, 2010 at 10:21:48PM +0100, King, Simon wrote:
> This is supposed to be a comment on William's question on David's
> remark that my example shows that it is not always a good idea to
> use a forgetful functor (if it exists) for coercion.
>
> David wrote:
> > It is a tempting approach to apply a forgetful functor as coercion
> > when such exists, but your bug report illustrates that this is not
> > a good idea (here specifically, and maybe in general).
>
> He does not say that a forgetful coercion is *always* a bad idea. But there *are* situations in which it is bad, and therefore it is not good to allow forgetful coercion *whenever it exists*.
>
> E.g., it is easy to imagine applications where it is essential that one has a unit vector (norm one). Using a forgetful coercion in the setting of my example, a unit vector would be coerced into something completely different, without Sage complaining.
>
> Another example: For many people, the term order of a polynomial ring doesn't matter. But I often work with Gr�bner bases, and so I don't like when there is a coercion between polynomial rings of different term orders. Perhaps it would be too much to call this a bug (and I *don't* suggest it should change: The majority of users really couldn't care less), but at least it encourages the user to produce incorrect code.
>
> Or, perhaps a killer example: Forgetting the multiplicative structure of MatrixSpace(QQ,2,2) would in fact yield QQ^4. But would you really like to call this forgetful functor a coercion? Would you really like that MatrixSpace(QQ,2,2)([1,2,3,4])+(QQ^4)([1,2,3,4])==(QQ^4)([2,4,6,8]) ??
>
> I understand the comments in this thread like this and will try to take care of it in my patch:
> 1. We agree that coercion must preserve all "important" algebraic structures.
> 2. Which structures are important depends on the context and on the aim of the user.
> 3. If the user explicitly defines a structure, then it is reasonable to assume that this structure is important (here, the inner product) -- hence, in this case, a forgetful functor is not so good.
My 2 cents on this debate: I agree that applying forgetful functors
automatically can come in handy in interactive use. However, from my
experience:
- In code, one rarely cares about such coercions; on the other hand,
having them can prevent catching errors early-on, and letting them
propagate far far away where they get hard to debug.
- Coercion ought to be predictable, consistent, robust, and fast. Oh,
and easy to refactor as well. To this end, the coercion model has
to be as simple and centralized as possible. Handling special cases
all over the places in Sage is just looking for trouble.
- It is hard to disable an existing coercion if at some point in time
it is deemed as too dangerous.
- In short, explicit is better than implicit here.
I hence vote for being very very conservative when adding new
coercions. And when this is done, this should be implemented as much
as possible as a general rule.
Best,
Nicolas
--
Nicolas M. Thi�ry "Isil" <
nth...@users.sf.net>
http://Nicolas.Thiery.name/