| 1 |
= Defunctorize |
| 2 |
|
| 3 |
<<Defunctorize#>> is a translation pass from the <<CoreML#>> |
| 4 |
<<IntermediateLanguage#>> to the <<XML#>> <<IntermediateLanguage#>>. |
| 5 |
|
| 6 |
== Description |
| 7 |
|
| 8 |
This pass converts a <<CoreML#>> program to an <<XML#>> program by |
| 9 |
performing: |
| 10 |
|
| 11 |
* linearization |
| 12 |
* <<MatchCompile#>> |
| 13 |
* polymorphic `val` dec expansion |
| 14 |
* `datatype` lifting (to the top-level) |
| 15 |
|
| 16 |
== Implementation |
| 17 |
|
| 18 |
* https://github.com/MLton/mlton/blob/master/mlton/defunctorize/defunctorize.sig[`defunctorize.sig`] |
| 19 |
* https://github.com/MLton/mlton/blob/master/mlton/defunctorize/defunctorize.fun[`defunctorize.fun`] |
| 20 |
|
| 21 |
== Details and Notes |
| 22 |
|
| 23 |
This pass is grossly misnamed and does not perform defunctorization. |
| 24 |
|
| 25 |
=== Datatype Lifting |
| 26 |
|
| 27 |
This pass moves all `datatype` declarations to the top level. |
| 28 |
|
| 29 |
<<StandardML#,Standard ML>> `datatype` declarations can contain type |
| 30 |
variables that are not bound in the declaration itself. For example, |
| 31 |
the following program is valid. |
| 32 |
[source,sml] |
| 33 |
---- |
| 34 |
fun 'a f (x: 'a) = |
| 35 |
let |
| 36 |
datatype 'b t = T of 'a * 'b |
| 37 |
val y: int t = T (x, 1) |
| 38 |
in |
| 39 |
13 |
| 40 |
end |
| 41 |
---- |
| 42 |
|
| 43 |
Unfortunately, the `datatype` declaration can not be immediately moved |
| 44 |
to the top level, because that would leave `'a` free. |
| 45 |
[source,sml] |
| 46 |
---- |
| 47 |
datatype 'b t = T of 'a * 'b |
| 48 |
fun 'a f (x: 'a) = |
| 49 |
let |
| 50 |
val y: int t = T (x, 1) |
| 51 |
in |
| 52 |
13 |
| 53 |
end |
| 54 |
---- |
| 55 |
|
| 56 |
In order to safely move `datatype`s, this pass must close them, as |
| 57 |
well as add any free type variables as extra arguments to the type |
| 58 |
constructor. For example, the above program would be translated to |
| 59 |
the following. |
| 60 |
[source,sml] |
| 61 |
---- |
| 62 |
datatype ('a, 'b) t = T of 'a * 'b |
| 63 |
fun 'a f (x: 'a) = |
| 64 |
let |
| 65 |
val y: ('a * int) t = T (x, 1) |
| 66 |
in |
| 67 |
13 |
| 68 |
end |
| 69 |
---- |
| 70 |
|
| 71 |
== Historical Notes |
| 72 |
|
| 73 |
The <<Defunctorize#>> pass originally eliminated |
| 74 |
<<StandardML#,Standard ML>> functors by duplicating their body at each |
| 75 |
application. These duties have been adopted by the <<Elaborate#>> |
| 76 |
pass. |