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