I've made some progress in my learning of SML, trying to solidify my understanding of some of the most basic concepts. The following exercise proves that there is still much to be learned.

I'm asked to implement a function named add that receives two numbers and returns their sum. Problem is, add is supposed to receive a disjoint union of integers and real numbers, and return a disjoint union in the following manner: if both numbers received are integers, it should return the sum as an integer. Otherwise, if one of both of the arguments is real, the sum should be returned as a real number.

The most reasonable solution is to use datatype, yet I'm specifically instructed not to use it in any way, shape or form. To put it in other words, I should implement some sort of mechanism for determining the type of the parameters, and, as it follows, determining the return type.

All attempts were futile. Either SML infers that my parameters are of one specific type (which is not the case), or it demands the return value to be of a certain type, which is quite understandable but still frustrating. I've no idea how to overcome SML's typing restrictions. If it is of any use, I'm hinted to use tuples.