Hi Jeroen, On 2017-12-18, Jeroen Demeyer <[email protected]> wrote: > Sage has NamedConvertMap to convert via special methods like > _real_mpfi_(). When looking for a conversion map, it first tries to find > a coercion map and then it checks for a default conversion map, ... > Why try to find a coercion map first?
One axiom of Sage's coercion system is: IF there is a coercion from A to B, then it coincides with the default conversion from A to B. Assuming that Sage does satisfy that axiom, I think one could indeed simplify the logic: Do not check whether a coercion from A to B exists (which may be costly), maybe just check whether the coercion is in the cache. If it isn't cached, do the default conversion (since we assume that it coincides with coercion anyway). However, be careful! I suppose the current logic was chosen in order to be better safe than sorry: If you are looking for a default conversion, then it must coincide with the coercion (if it exists), hence, do construct the coercion (if it exists) and use it as conversion, so that there is no danger to accidentally pick an inconsistent conversion. > For coercion, it is slightly more complicated: ideally, one would want > to use the _real_mpfi_() map if it exists, but that is wrong since the > map might not define a coercion (only a conversion). One way to answer the question whether a coercion from A to B exists is by implementing the method B._coerce_map_from_. If that method returns "True" then the coercion system knows that it is right to choose _real_mpfi_() for coercion. > 1. Check for a coercion map. > > 2. If coercion is possible, check for a NamedConvertMap. If so, use that > instead. No. If there is a coercion map, then (by definition) coercion is possible, and the coercion map is cached. So, do not use something else instead. > 3. If there is no NamedConvertMap, use the coercion map from step 1. The coercion system works the other way around, IIRC: 1. It checks whether a coercion from A to B exists. That question already is answered affirmatively if B._coerce_map_from_(A) returns True. It may also return a map. 2. If a coercion exists and B._coerce_map_from_(A) didn't return a map, then try to construct a direct conversion (it doesn't matter if it is implemented by means of B._element_constructor_ or by means of a named method). 3. If the construction of a direct conversion fails, then there is still the possibility of composing coercion maps, which is detected by some backtracking algorithm in the coercion graph. The resulting composed map is then also used for conversion. > I feel like there should a mechanism to prefer a NamedConvertMap over > other maps. Has somebody encountered this problem before for some other > parent? Actually I have hardly encountered NamedConvertMap before. When I implement a conversion, it is, in most cases, by _element_constructor_. Best regards, Simon -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.
