This question came from our site for professionals, academics, and students working within the systems development life cycle.

2

$\begingroup$This doesn't appear to meet the criteria for CS Theory Stack Exchange, but it does look like you're looking for a more mathematical or theoretical answer. I think that Computer Science may be appropriate for this. Since you requested a move to get better answers, I'll send it to where it is likely to be well-received.$\endgroup$
– Thomas OwensApr 20 '14 at 21:00

2 Answers
2

As you probably already know, the way the common type inference algorithms work is that they traverse the syntax tree and for every subexpression they generate a type constraint. Then, they take this constraints, assume conjunction between them, and solve them (typically looking for a most general solution).

When you also have overloading, when analyzing an overloaded operator you generate several type constraints, instead of one, and assume disjunction between them, if the overloading is bounded. Because you are essentially saying that the operator can have ``either this, or this, or that type." If it is unbounded, one needs to resort to universal quantification, just as with polymorphic types, but with additional constraints that constrain the actual overloading types. The paper I reference covers these topics in more depth.

You're done! Well, except that you need defaulting. If your language allows defaulting the Times class to Int (and then Double), then your examples will work exactly as stated. The other way to fix it, of course, is to not automatically promote Int to Double, or to only do it when immediately necessary, and to use Int literals as Int only (again, promoting only when immediately necessary); this solution will also work.