4 <:Elaborate:> is a translation pass from the <:AST:>
5 <:IntermediateLanguage:> to the <:CoreML:> <:IntermediateLanguage:>.
9 This pass performs type inference and type checking according to the
10 <:DefinitionOfStandardML:Definition>. It also defunctorizes the
11 program, eliminating all module-level constructs.
15 * <!ViewGitFile(mlton,master,mlton/elaborate/elaborate.sig)>
16 * <!ViewGitFile(mlton,master,mlton/elaborate/elaborate.fun)>
17 * <!ViewGitFile(mlton,master,mlton/elaborate/elaborate-env.sig)>
18 * <!ViewGitFile(mlton,master,mlton/elaborate/elaborate-env.fun)>
19 * <!ViewGitFile(mlton,master,mlton/elaborate/elaborate-modules.sig)>
20 * <!ViewGitFile(mlton,master,mlton/elaborate/elaborate-modules.fun)>
21 * <!ViewGitFile(mlton,master,mlton/elaborate/elaborate-core.sig)>
22 * <!ViewGitFile(mlton,master,mlton/elaborate/elaborate-core.fun)>
23 * <!ViewGitDir(mlton,master,mlton/elaborate)>
25 == Details and Notes ==
27 At the modules level, the <:Elaborate:> pass:
29 * elaborates signatures with interfaces (see
30 <!ViewGitFile(mlton,master,mlton/elaborate/interface.sig)> and
31 <!ViewGitFile(mlton,master,mlton/elaborate/interface.fun)>)
33 The main trick is to use disjoint sets to efficiently handle sharing
34 of tycons and of structures and then to copy signatures as dags rather
37 * checks functors at the point of definition, using functor summaries
38 to speed up checking of functor applications.
40 When a functor is first type checked, we keep track of the dummy
41 argument structure and the dummy result structure, as well as all the
42 tycons that were created while elaborating the body. Then, if we
43 later need to type check an application of the functor (as opposed to
44 defunctorize an application), we pair up tycons in the dummy argument
45 structure with the actual argument structure and then replace the
46 dummy tycons with the actual tycons in the dummy result structure,
47 yielding the actual result structure. We also generate new tycons for
48 all the tycons that we created while originally elaborating the body.
50 * handles opaque signature constraints.
52 This is implemented by building a dummy structure realized from the
53 signature, just as we would for a functor argument when type checking
54 a functor. The dummy structure contains exactly the type information
55 that is in the signature, which is what opacity requires. We then
56 replace the variables (and constructors) in the dummy structure with
57 the corresponding variables (and constructors) from the actual
58 structure so that the translation to <:CoreML:> uses the right stuff.
59 For each tycon in the dummy structure, we keep track of the
60 corresponding type structure in the actual structure. This is used
61 when producing the <:CoreML:> types (see `expandOpaque` in
62 <!ViewGitFile(mlton,master,mlton/elaborate/type-env.sig)> and
63 <!ViewGitFile(mlton,master,mlton/elaborate/type-env.fun)>).
65 Then, within each `structure` or `functor` body, for each declaration
66 (`<dec>` in the <:StandardML:Standard ML> grammar), the <:Elaborate:>
67 pass does three steps:
72 ** <:PrecedenceParse:>
73 ** `_{ex,im}port` expansion
74 ** profiling insertion
76 3. Overloaded {constant, function, record pattern} resolution
79 === Defunctorization ===
81 The <:Elaborate:> pass performs a number of duties historically
82 assigned to the <:Defunctorize:> pass.
84 As part of the <:Elaborate:> pass, all module level constructs
85 (`open`, `signature`, `structure`, `functor`, long identifiers) are
86 removed. This works because the <:Elaborate:> pass assigns a unique
87 name to every type and variable in the program. This also allows the
88 <:Elaborate:> pass to eliminate `local` declarations, which are purely
89 for namespace management.
94 Here are a number of examples of elaboration.
96 * All variables bound in `val` declarations are renamed.
109 * All variables in `fun` declarations are renamed.
118 fun f_0 x_0 = g_0 x_0
119 and g_0 y_0 = f_0 y_0
122 * Type abbreviations are removed, and the abbreviation is expanded
128 type 'b t = 'b u * real
129 fun f (x : bool t) = x
133 fun f_0 (x_0 : (int * bool) * real) = x_0
136 * Exception declarations create a new constructor and rename the type.
141 exception E of t * real
145 exception E_0 of int * real
148 * The type and value constructors in datatype declarations are renamed.
152 datatype t = A of int | B of real * t
156 datatype t_0 = A_0 of int | B_0 of real * t_0
159 * Local declarations are moved to the top-level. The environment
160 keeps track of the variables in scope.
178 * Structure declarations are eliminated, with all declarations moved
179 to the top level. Long identifiers are renamed.
196 * Open declarations are eliminated.
217 * Functor declarations are eliminated, and the body of a functor is
218 duplicated wherever the functor is applied.
222 functor F(val x : int) =
226 structure F1 = F(val x = 13)
227 structure F2 = F(val x = 14)
239 * Signature constraints are eliminated. Note that signatures do
240 affect how subsequent variables are renamed.