On Sat, Jul 19, 2014 at 8:22 AM, Nils Bruin <nbr...@sfu.ca> wrote: > Consider A+B where A is a polynomial in ZZ[x,y] and B is a power series in > F_q[[x]] (finite field with q elements). > > Do you expect your CAS to make sense of that addition? Sage does. It > returns an answer in F_q[[x]][y] (i.e., a polynomial in y over power series > in x over F_q) . You can argue whether it's desirable for a system to try > to be that smart, but all computer algebra systems I know are a "step back" > relative to this. Programming languages do not tend to have type models > that would even allow you to try and make sense of this kind of question. >
This can be pretty straightforwardly handled with multiple dispatch promotion in a parametric type system. Similar things are done in real code regularly in Julia. Although I'm not going to try to define these particular types, the polynomial, power series, and finite field example is quite possible given appropriate type definitions – people have done similar things <http://acooke.org/cute/FiniteFiel1.html>. An example that already works and is similar in spirit, is figuring out that a Complex{BigInt} plus a Vector{Rational{Int}} should be a Vector{Complex{Rational{BigInt}}}. This falls out of a surprisingly small set of generic function methods and promotion rules (which are just methods): julia> big(3)^100*im + [1, 1//2, 2] 3-element Array{Complex{Rational{BigInt}},1}: 1//1+515377520732011331036461129765621272702107522001//1*im 1//2+515377520732011331036461129765621272702107522001//1*im 2//1+515377520732011331036461129765621272702107522001//1*im It doesn't quite get you to a CAS (nor is it meant to), but it's getting close. I suspect this sort of thing can also be done in Haskell (although the lack of dependent types may make defining the desired types tough) and can almost certainly be done in a system like Idris <http://www.idris-lang.org/>. Julia kind of cheats here by being dynamic – having runtime values as type parameters is easy if you don't try to do type checking in advance. In any case, I'm mainly just advocating that you don't dismiss multiple dispatch out of hand as a technique for expressing complex, composable, user-extensible promotion rules. You can do this sort of thing quite nicely with multiple dispatch and it might be helpful in Sage. I'm quite ignorant on this point, however, since I don't understand how Sage currently does things and what the pain points are. -- 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+unsubscr...@googlegroups.com. To post to this group, send email to sage-devel@googlegroups.com. Visit this group at http://groups.google.com/group/sage-devel. For more options, visit https://groups.google.com/d/optout.