Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / doc / guide / src / Defunctorize.adoc
1 Defunctorize
2 ============
3
4 <:Defunctorize:> is a translation pass from the <:CoreML:>
5 <:IntermediateLanguage:> to the <:XML:> <:IntermediateLanguage:>.
6
7 == Description ==
8
9 This pass converts a <:CoreML:> program to an <:XML:> program by
10 performing:
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
24 This pass is grossly misnamed and does not perform defunctorization.
25
26 === Datatype Lifting ===
27
28 This pass moves all `datatype` declarations to the top level.
29
30 <:StandardML:Standard ML> `datatype` declarations can contain type
31 variables that are not bound in the declaration itself. For example,
32 the following program is valid.
33 [source,sml]
34 ----
35 fun '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
44 Unfortunately, the `datatype` declaration can not be immediately moved
45 to the top level, because that would leave `'a` free.
46 [source,sml]
47 ----
48 datatype 'b t = T of 'a * 'b
49 fun 'a f (x: 'a) =
50 let
51 val y: int t = T (x, 1)
52 in
53 13
54 end
55 ----
56
57 In order to safely move `datatype`s, this pass must close them, as
58 well as add any free type variables as extra arguments to the type
59 constructor. For example, the above program would be translated to
60 the following.
61 [source,sml]
62 ----
63 datatype ('a, 'b) t = T of 'a * 'b
64 fun '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
74 The <:Defunctorize:> pass originally eliminated
75 <:StandardML:Standard ML> functors by duplicating their body at each
76 application. These duties have been adopted by the <:Elaborate:>
77 pass.