Details and Notes
This pass is grossly misnamed and does not perform defunctorization.
This pass moves all datatype declarations to the top level.
Standard ML datatype declarations can contain type variables that are not bound in the declaration itself. For example, the following program is valid.
fun 'a f (x: 'a) = let datatype 'b t = T of 'a * 'b val y: int t = T (x, 1) in 13 end
Unfortunately, the datatype declaration can not be immediately moved to the top level, because that would leave 'a free.
datatype 'b t = T of 'a * 'b fun 'a f (x: 'a) = let val y: int t = T (x, 1) in 13 end
In order to safely move `datatype`s, this pass must close them, as well as add any free type variables as extra arguments to the type constructor. For example, the above program would be translated to the following.
datatype ('a, 'b) t = T of 'a * 'b fun 'a f (x: 'a) = let val y: ('a * int) t = T (x, 1) in 13 end