| 1 |
= Elaborate |
| 2 |
|
| 3 |
<<Elaborate#>> is a translation pass from the <<AST#>> |
| 4 |
<<IntermediateLanguage#>> to the <<CoreML#>> <<IntermediateLanguage#>>. |
| 5 |
|
| 6 |
== Description |
| 7 |
|
| 8 |
This pass performs type inference and type checking according to the |
| 9 |
<<DefinitionOfStandardML#,Definition>>. It also defunctorizes the |
| 10 |
program, eliminating all module-level constructs. |
| 11 |
|
| 12 |
== Implementation |
| 13 |
|
| 14 |
* https://github.com/MLton/mlton/blob/master/mlton/elaborate/elaborate.sig[`elaborate.sig`] |
| 15 |
* https://github.com/MLton/mlton/blob/master/mlton/elaborate/elaborate.fun[`elaborate.fun`] |
| 16 |
* https://github.com/MLton/mlton/blob/master/mlton/elaborate/elaborate-env.sig[`elaborate-env.sig`] |
| 17 |
* https://github.com/MLton/mlton/blob/master/mlton/elaborate/elaborate-env.fun[`elaborate-env.fun`] |
| 18 |
* https://github.com/MLton/mlton/blob/master/mlton/elaborate/elaborate-modules.sig[`elaborate-modules.sig`] |
| 19 |
* https://github.com/MLton/mlton/blob/master/mlton/elaborate/elaborate-modules.fun[`elaborate-modules.fun`] |
| 20 |
* https://github.com/MLton/mlton/blob/master/mlton/elaborate/elaborate-core.sig[`elaborate-core.sig`] |
| 21 |
* https://github.com/MLton/mlton/blob/master/mlton/elaborate/elaborate-core.fun[`elaborate-core.fun`] |
| 22 |
* https://github.com/MLton/mlton/tree/master/mlton/elaborate[`elaborate`] |
| 23 |
|
| 24 |
== Details and Notes |
| 25 |
|
| 26 |
At the modules level, the <<Elaborate#>> pass: |
| 27 |
|
| 28 |
* elaborates signatures with interfaces (see |
| 29 |
https://github.com/MLton/mlton/blob/master/mlton/elaborate/interface.sig[`interface.sig`] and |
| 30 |
https://github.com/MLton/mlton/blob/master/mlton/elaborate/interface.fun[`interface.fun`]) |
| 31 |
+ |
| 32 |
The main trick is to use disjoint sets to efficiently handle sharing |
| 33 |
of tycons and of structures and then to copy signatures as dags rather |
| 34 |
than as trees. |
| 35 |
|
| 36 |
* checks functors at the point of definition, using functor summaries |
| 37 |
to speed up checking of functor applications. |
| 38 |
+ |
| 39 |
When a functor is first type checked, we keep track of the dummy |
| 40 |
argument structure and the dummy result structure, as well as all the |
| 41 |
tycons that were created while elaborating the body. Then, if we |
| 42 |
later need to type check an application of the functor (as opposed to |
| 43 |
defunctorize an application), we pair up tycons in the dummy argument |
| 44 |
structure with the actual argument structure and then replace the |
| 45 |
dummy tycons with the actual tycons in the dummy result structure, |
| 46 |
yielding the actual result structure. We also generate new tycons for |
| 47 |
all the tycons that we created while originally elaborating the body. |
| 48 |
|
| 49 |
* handles opaque signature constraints. |
| 50 |
+ |
| 51 |
This is implemented by building a dummy structure realized from the |
| 52 |
signature, just as we would for a functor argument when type checking |
| 53 |
a functor. The dummy structure contains exactly the type information |
| 54 |
that is in the signature, which is what opacity requires. We then |
| 55 |
replace the variables (and constructors) in the dummy structure with |
| 56 |
the corresponding variables (and constructors) from the actual |
| 57 |
structure so that the translation to <<CoreML#>> uses the right stuff. |
| 58 |
For each tycon in the dummy structure, we keep track of the |
| 59 |
corresponding type structure in the actual structure. This is used |
| 60 |
when producing the <<CoreML#>> types (see `expandOpaque` in |
| 61 |
https://github.com/MLton/mlton/blob/master/mlton/elaborate/type-env.sig[`type-env.sig`] and |
| 62 |
https://github.com/MLton/mlton/blob/master/mlton/elaborate/type-env.fun[`type-env.fun`]). |
| 63 |
+ |
| 64 |
Then, within each `structure` or `functor` body, for each declaration |
| 65 |
(`<dec>` in the <<StandardML#,Standard ML>> grammar), the <<Elaborate#>> |
| 66 |
pass does three steps: |
| 67 |
+ |
| 68 |
-- |
| 69 |
1. <<ScopeInference#>> |
| 70 |
2. {empty} |
| 71 |
** <<PrecedenceParse#>> |
| 72 |
** `_{ex,im}port` expansion |
| 73 |
** profiling insertion |
| 74 |
** unification |
| 75 |
3. Overloaded {constant, function, record pattern} resolution |
| 76 |
-- |
| 77 |
|
| 78 |
=== Defunctorization |
| 79 |
|
| 80 |
The <<Elaborate#>> pass performs a number of duties historically |
| 81 |
assigned to the <<Defunctorize#>> pass. |
| 82 |
|
| 83 |
As part of the <<Elaborate#>> pass, all module level constructs |
| 84 |
(`open`, `signature`, `structure`, `functor`, long identifiers) are |
| 85 |
removed. This works because the <<Elaborate#>> pass assigns a unique |
| 86 |
name to every type and variable in the program. This also allows the |
| 87 |
<<Elaborate#>> pass to eliminate `local` declarations, which are purely |
| 88 |
for namespace management. |
| 89 |
|
| 90 |
|
| 91 |
== Examples |
| 92 |
|
| 93 |
Here are a number of examples of elaboration. |
| 94 |
|
| 95 |
* All variables bound in `val` declarations are renamed. |
| 96 |
+ |
| 97 |
[source,sml] |
| 98 |
---- |
| 99 |
val x = 13 |
| 100 |
val y = x |
| 101 |
---- |
| 102 |
+ |
| 103 |
---- |
| 104 |
val x_0 = 13 |
| 105 |
val y_0 = x_0 |
| 106 |
---- |
| 107 |
|
| 108 |
* All variables in `fun` declarations are renamed. |
| 109 |
+ |
| 110 |
[source,sml] |
| 111 |
---- |
| 112 |
fun f x = g x |
| 113 |
and g y = f y |
| 114 |
---- |
| 115 |
+ |
| 116 |
---- |
| 117 |
fun f_0 x_0 = g_0 x_0 |
| 118 |
and g_0 y_0 = f_0 y_0 |
| 119 |
---- |
| 120 |
|
| 121 |
* Type abbreviations are removed, and the abbreviation is expanded |
| 122 |
wherever it is used. |
| 123 |
+ |
| 124 |
[source,sml] |
| 125 |
---- |
| 126 |
type 'a u = int * 'a |
| 127 |
type 'b t = 'b u * real |
| 128 |
fun f (x : bool t) = x |
| 129 |
---- |
| 130 |
+ |
| 131 |
---- |
| 132 |
fun f_0 (x_0 : (int * bool) * real) = x_0 |
| 133 |
---- |
| 134 |
|
| 135 |
* Exception declarations create a new constructor and rename the type. |
| 136 |
+ |
| 137 |
[source,sml] |
| 138 |
---- |
| 139 |
type t = int |
| 140 |
exception E of t * real |
| 141 |
---- |
| 142 |
+ |
| 143 |
---- |
| 144 |
exception E_0 of int * real |
| 145 |
---- |
| 146 |
|
| 147 |
* The type and value constructors in datatype declarations are renamed. |
| 148 |
+ |
| 149 |
[source,sml] |
| 150 |
---- |
| 151 |
datatype t = A of int | B of real * t |
| 152 |
---- |
| 153 |
+ |
| 154 |
---- |
| 155 |
datatype t_0 = A_0 of int | B_0 of real * t_0 |
| 156 |
---- |
| 157 |
|
| 158 |
* Local declarations are moved to the top-level. The environment |
| 159 |
keeps track of the variables in scope. |
| 160 |
+ |
| 161 |
[source,sml] |
| 162 |
---- |
| 163 |
val x = 13 |
| 164 |
local val x = 14 |
| 165 |
in val y = x |
| 166 |
end |
| 167 |
val z = x |
| 168 |
---- |
| 169 |
+ |
| 170 |
---- |
| 171 |
val x_0 = 13 |
| 172 |
val x_1 = 14 |
| 173 |
val y_0 = x_1 |
| 174 |
val z_0 = x_0 |
| 175 |
---- |
| 176 |
|
| 177 |
* Structure declarations are eliminated, with all declarations moved |
| 178 |
to the top level. Long identifiers are renamed. |
| 179 |
+ |
| 180 |
[source,sml] |
| 181 |
---- |
| 182 |
structure S = |
| 183 |
struct |
| 184 |
type t = int |
| 185 |
val x : t = 13 |
| 186 |
end |
| 187 |
val y : S.t = S.x |
| 188 |
---- |
| 189 |
+ |
| 190 |
---- |
| 191 |
val x_0 : int = 13 |
| 192 |
val y_0 : int = x_0 |
| 193 |
---- |
| 194 |
|
| 195 |
* Open declarations are eliminated. |
| 196 |
+ |
| 197 |
[source,sml] |
| 198 |
---- |
| 199 |
val x = 13 |
| 200 |
val y = 14 |
| 201 |
structure S = |
| 202 |
struct |
| 203 |
val x = 15 |
| 204 |
end |
| 205 |
open S |
| 206 |
val z = x + y |
| 207 |
---- |
| 208 |
+ |
| 209 |
---- |
| 210 |
val x_0 = 13 |
| 211 |
val y_0 = 14 |
| 212 |
val x_1 = 15 |
| 213 |
val z_0 = x_1 + y_0 |
| 214 |
---- |
| 215 |
|
| 216 |
* Functor declarations are eliminated, and the body of a functor is |
| 217 |
duplicated wherever the functor is applied. |
| 218 |
+ |
| 219 |
[source,sml] |
| 220 |
---- |
| 221 |
functor F(val x : int) = |
| 222 |
struct |
| 223 |
val y = x |
| 224 |
end |
| 225 |
structure F1 = F(val x = 13) |
| 226 |
structure F2 = F(val x = 14) |
| 227 |
val z = F1.y + F2.y |
| 228 |
---- |
| 229 |
+ |
| 230 |
---- |
| 231 |
val x_0 = 13 |
| 232 |
val y_0 = x_0 |
| 233 |
val x_1 = 14 |
| 234 |
val y_1 = x_1 |
| 235 |
val z_0 = y_0 + y_1 |
| 236 |
---- |
| 237 |
|
| 238 |
* Signature constraints are eliminated. Note that signatures do |
| 239 |
affect how subsequent variables are renamed. |
| 240 |
+ |
| 241 |
[source,sml] |
| 242 |
---- |
| 243 |
val y = 13 |
| 244 |
structure S : sig |
| 245 |
val x : int |
| 246 |
end = |
| 247 |
struct |
| 248 |
val x = 14 |
| 249 |
val y = x |
| 250 |
end |
| 251 |
open S |
| 252 |
val z = x + y |
| 253 |
---- |
| 254 |
+ |
| 255 |
---- |
| 256 |
val y_0 = 13 |
| 257 |
val x_0 = 14 |
| 258 |
val y_1 = x_0 |
| 259 |
val z_0 = x_0 + y_0 |
| 260 |
---- |