[MLton] improved overloading for SML

Matthew Fluet fluet@cs.cornell.edu
Mon, 17 Oct 2005 09:32:01 -0400 (EDT)


>> Apart from the use here, what was the original purpose of useless
>> variant elimination? I see an advantage in reducing a sum to one
>> variant (eliminates the tag entirely). Any other?
>
> That's the main one.  There are also benefits to eliminating variants
> even when they're is more than one remaining, since more compact
> representations can sometimes be chosen for the remaining ones.
> Dead-code elimination is another reason -- by eliminating useless
> variants one eliminates dead code in unreachable cases.  This can then
> allow other optimizations to do more.

The dead-code elimination can be significant.  Probably one of the biggest 
wins of the unused variant elimination is in the IO stack.  Although the 
Basis Library suggests building the Imperative IO on top of the Stream IO, 
both MLton and some other implementations find that it is benefitial to 
also have a Buffer I (that is, an imperative buffered input structure, 
where the buffer is updated in-place as more input is requested).  Some 
care needs to be taken to allow the user to transparently fall back to the 
Stream IO should they choose to do so.  On the other hand, for many 
programs, the Imperative IO interface is all that is ever used.  In that 
instance, the unused variant elimination can determine that the sum type 
used in the Imperative IO interface (which distinguishes between the 
Buffer I and the Stream IO implementations) is only ever used at the 
Buffer I variant; hence, the entire Stream IO implementation may be 
eliminated as dead code.