Chromium Code Reviews

Side by Side Diff: doc/definition/static-semantics.tex

Issue 1236443002: Core static semantics (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Show elaboration rules Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « doc/definition/macros.tex ('k') | doc/definition/strong-dart.pdf » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 \subsection*{Expression typing: $\yieldsOk{\Phi, \Delta, \Gamma}{e}{\opt{\tau}}{ e'}{\tau'}$}
2 \hrulefill\\
3
4
5 \sstext{
6 Expression typing is a relation between typing contexts, a term ($e$), an
7 optional type ($\opt{\tau}$), and a type ($\tau'$). The term $e$ represents the
8 term being checked. The optional type $\opt{\tau}$ is the type against which
vsm 2015/07/15 18:25:34 Is this also the contextual type? If so, maybe de
Leaf 2015/07/15 22:27:25 Done.
9 the term is being checked (if present). The output type $\tau'$ is the most
10 precise type synthesized for the term. It should always be the case that the
11 synthesized (output) type is a subtype of the checked (input) type if the latter
12 is present. The checking/synthesis pattern allows for the propogation of type
13 information both downwards and upwards. It is often the case that downwards
14 propogation is not useful. Consequently, to simplify the presentation the rules
15 which do not use the checking type require that it be empty ($\_$).
16
17 The first typing rule allows contextual type information to be dropped so that
18 such rules apply in the cast that we have contextual type information, subject
vsm 2015/07/15 18:25:34 'cast '-> 'case' ? This sentence reads a little a
Leaf 2015/07/15 22:27:25 Done.
19 to the contextual type being a supertype of the synthesized type:
20
21 }{
22 For subsumption, the elaboration of the underlying term carries through.
23 }
24
25 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}{e}{\_}{e'}{\sigma} \quad\quad
26 \subtypeOf{\Phi, \Delta}{\sigma}{\tau}
27 }
28 {\yieldsOk{\Phi, \Delta, \Gamma}{e}{\tau}{e'}{\sigma}}
29
30 \sstext{
31 The implicit downcast rule also allows this when the contextual type is a
32 subtype of the synthesized type, corresponding to an implicit downcast.
33 }{
34 In an implicit downcast, the elaboration adds a check so that an error
35 will be thrown if the types do not match at runtime.
36 }
37
38 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}{e}{\_}{e'}{\sigma} \quad\quad
39 \subtypeOf{\Phi, \Delta}{\tau}{\sigma}
40 }
41 {\yieldsOk{\Phi, \Delta, \Gamma}{e}{\tau}{\echeck{e'}{\tau}}{\tau}}
42
43 \sstext{Variables are typed according to their declarations:}{}
44
45 \axiom{\yieldsOk{\Phi, \Delta, \extends{\Gamma}{x}{\tau}}{x}{\_}{x}{\tau}}
46
47 \sstext{Numbers, booleans, and null all have a fixed synthesized type.}{}
48
49 \axiom{\yieldsOk{\Phi, \Delta, \Gamma}{i}{\_}{i}{\Num}}
50
51 \axiom{\yieldsOk{\Phi, \Delta, \Gamma}{\eff}{\_}{\eff}{\Bool}}
52
53 \axiom{\yieldsOk{\Phi, \Delta, \Gamma}{\ett}{\_}{\ett}{\Bool}}
54
55 \axiom{\yieldsOk{\Phi, \Delta, \Gamma}{\enull}{\_}{\enull}{\Bottom}}
56
57 \sstext{A $\ethis$ expression is well-typed if we are inside of a method, and $\ sigma$
58 is the type of the enclosing class.}{}
59
60 \infrule{\Gamma = \Gamma'_{\sigma}
61 }
62 {
63 \yieldsOk{\Phi, \Delta, \Gamma}{\ethis}{\_}{\ethis}{\sigma}
64 }
65
66 \sstext{A fully annotated function is well-typed if its body is well-typed at it s
67 declared return type, under the assumption that the variables have their
68 declared types.
69 }{
70
71 A fully annotated function elaborates to a function with an elaborated body.
72 The rest of the function elaboration rules fill in the reified type using
73 contextual information if present and applicable, or $\Dynamic$ otherwise.
74
75 }
76
77 \infrule{\Gamma' = \extends{\Gamma}{\many{x}}{\many{\tau}} \quad\quad
78 \yieldsOk{\Phi, \Delta, \Gamma'}{e}{\sigma}{e'}{\sigma'}
79 }
80 {\yieldsOk{\Phi, \Delta, \Gamma}
81 {\elambda{\many{x:\tau}}{\sigma}{e}}
82 {\_}
83 {\elambda{\many{x:\tau}}{\sigma}{e'}}
84 {\Arrow[-]{\many{\tau}}{\sigma}}
85 }
86
87 \sstext{A function with a missing argument type is well-typed if it is well-type d with
88 the argument type replaced with $\Dynamic$.}
89 {}
90
91 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}
92 {\elambda{x_0:\opt{\tau_0}, \ldots, x_i:\Dynamic, \ldots, x_n: \opt{\tau_n}}{\opt{\sigma}}{e}}
93 {\opt{\tau}}
94 {e_f}
95 {\tau_f}
96 }
97 {\yieldsOk{\Phi, \Delta, \Gamma}
98 {\elambda{x_0:\opt{\tau_0}, \ldots, x_i:\_, \ldots, x_n:\opt{\ tau_n}}{\opt{\sigma}}{e}}
99 {\opt{\tau}}
100 {e_f}
101 {\tau_f}
102 }
103
104 \sstext{A function with a missing argument type is well-typed if it is well-type d with
105 the argument type replaced with the corresponding argument type from the context
106 type. Note that this rule overlaps with the previous: the formal presentation
107 leaves this as a non-deterministic choice.}{}
108
109 \infrule{\tau_c = \Arrow[k]{\upsilon_0, \ldots, \upsilon_n}{\upsilon_r} \\
110 \yieldsOk{\Phi, \Delta, \Gamma}
111 {\elambda{x_0:\opt{\tau_0}, \ldots, x_i:\upsilon_i, \ldots, x_ n:\opt{\tau_n}}{\opt{\sigma}}{e}}
112 {\tau_c}
113 {e_f}
114 {\tau_f}
115 }
116 {\yieldsOk{\Phi, \Delta, \Gamma}
117 {\elambda{x_0:\opt{\tau_0}, \ldots, x_i:\_, \ldots, x_n:\opt{\ tau_n}}{\opt{\sigma}}{e}}
118 {\tau_c}
119 {e_f}
120 {\tau_f}
121 }
122
123 \sstext{A function with a missing return type is well-typed if it is well-typed with
124 the return type replaced with $\Dynamic$.}{}
125
126 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}
127 {\elambda{\many{x:\opt{\tau}}}{\Dynamic}{e}}
128 {\opt{\tau_c}}
129 {e_f}
130 {\tau_f}
131 }
132 {\yieldsOk{\Phi, \Delta, \Gamma}
133 {\elambda{\many{x:\opt{\tau}}}{\_}{e}}
134 {\opt{\tau_c}}
135 {e_f}
136 {\tau_f}
137 }
138
139 \sstext{A function with a missing return type is well-typed if it is well-typed with
140 the return type replaced with the corresponding return type from the context
141 type. Note that this rule overlaps with the previous: the formal presentation
142 leaves this as a non-deterministic choice. }{}
143
144 \infrule{\tau_c = \Arrow[k]{\upsilon_0, \ldots, \upsilon_n}{\upsilon_r} \\
145 \yieldsOk{\Phi, \Delta, \Gamma}
146 {\elambda{\many{x:\opt{\tau}}}{\upsilon_r}{e}}
147 {\tau_c}
148 {e_f}
149 {\tau_f}
150 }
151 {\yieldsOk{\Phi, \Delta, \Gamma}
152 {\elambda{\many{x:\opt{\tau}}}{\_}{e}}
153 {\tau_c}
154 {e_f}
155 {\tau_f}
156 }
157
158
159 \sstext{Instance creation creates an instance of the appropriate type.}{}
160
161 % FIXME(leafp): inference
162 % FIXME(leafp): deal with bounds
163 \infrule{(C : \dclass{\TApp{C}{T_0,\ldots,T_n}}{\TApp{C'}{\upsilon_0, \ldots, \u psilon_k}}{\ldots}) \in \Phi \\
164 \mbox{len}(\many{\tau}) = n+1}
165 {\yieldsOk{\Phi, \Delta, \Gamma}
166 {\enew{C}{\many{\tau}}{}}
167 {\_}
168 {\enew{C}{\many{\tau}}{}}
169 {\TApp{C}{\many{\tau}}}
170 }
171
172
173 \sstext{Members of the set of primitive operations (left unspecified) can only b e
174 applied. Applications of primitives are well-typed if the arguments are
175 well-typed at the types given by the signature of the primitive.}{}
176
177 \infrule{\eprim\, :\, \Arrow[]{\many{\tau}}{\sigma} \quad\quad
178 \yieldsOk{\Phi, \Delta, \Gamma}{e}{\tau}{e'}{\tau'}
179 }
180 {\yieldsOk{\Phi, \Delta, \Gamma}
181 {\eprimapp{\eprim}{\many{e}}}
182 {\_}
183 {\eprimapp{\eprim}{\many{e'}}}
184 {\sigma}
185 }
186
187 \sstext{Function applications are well-typed if the applicand is well-typed and has
188 function type, and the arguments are well-typed.}
189 {
190
191 Function application of an expression of function type elaborates to either a
192 call or a dynamic (checked) call, depending on the variance of the applicand.
193 If the applicand is a covariant (fuzzy) type, then a dynamic call is generated.
194
195 }
196
197 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}
198 {e}
199 {\_}
200 {e'}
201 {\Arrow[k]{\many{\tau_a}}{\tau_r}} \\
202 \yieldsOk{\Phi, \Delta, \Gamma}
203 {e_a}
204 {\tau_a}
205 {e_a'}
206 {\tau_a'} \quad \mbox{for}\ e_a, \tau_a \in \many{e_a}, \many{ \tau_a}
207 \iftrans{\\ e_c = \begin{cases}
208 \ecall{e'}{\many{e_a'}} & \text{if $k = -$}\\
209 \edcall{e'}{\many{e_a'}} & \text{if $k = +$}
210 \end{cases}}
211 }
212 {\yieldsOk{\Phi, \Delta, \Gamma}
213 {\ecall{e}{\many{e_a}}}
214 {\_}
215 {e_c}
216 {\tau_r}
217 }
218
219 \sstext{Application of an expression of type $\Dynamic$ is well-typed if the arg uments
220 are well-typed at any type. }
221 {
222
223 Application of an expression of type $\Dynamic$ elaborates to a dynamic call.
224
225 }
226
227 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}
228 {e}
229 {\_}
230 {e'}
231 {\Dynamic} \\
232 \yieldsOk{\Phi, \Delta, \Gamma}
233 {e_a}
234 {\_}
235 {e_a'}
236 {\tau_a'} \quad \mbox{for}\ e_a \in \many{e_a}
237 }
238 {\yieldsOk{\Phi, \Delta, \Gamma}
239 {\ecall{e}{\many{e_a}}}
240 {\_}
241 {\edcall{e'}{\many{e_a'}}}
242 {\Dynamic}
243 }
244
245 \sstext{A dynamic call expression is well-typed so long as the applicand and the
246 arguments are well-typed at any type.}{}
247
248 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}
249 {e}
250 {\_}
251 {e'}
252 {\tau} \\
253 \yieldsOk{\Phi, \Delta, \Gamma}
254 {e_a}
255 {\_}
256 {e_a'}
257 {\tau_a} \quad \mbox{for}\ e_a \in \many{e_a}
258 }
259 {\yieldsOk{\Phi, \Delta, \Gamma}
260 {\edcall{e}{\many{e_a}}}
261 {\_}
262 {\edcall{e'}{\many{e_a'}}}
263 {\Dynamic}
264 }
265
266 \sstext{A method load is well-typed if the term is well-typed, and the method na me is
267 present in the type of the term.}{}
268
269 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}
270 {e}
271 {\_}
272 {e'}
273 {\sigma} \quad\quad
274 \methodLookup{\Phi}{\sigma}{m}{\tau}
275 }
276 {\yieldsOk{\Phi, \Delta, \Gamma}
277 {\eload{e}{m}}
278 {\_}
279 {\eload{e'}{m}}
280 {\tau}
281 }
282
283 \sstext{A method load from a term of type $\Dynamic$ is well-typed if the term i s
284 well-typed.}
285 {
286
287 A method load from a term of type $\Dynamic$ elaborates to a dynamic (checked)
288 load.
289
290 }
291
292 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}
293 {e}
294 {\_}
295 {e'}
296 {\Dynamic}
297 }
298 {\yieldsOk{\Phi, \Delta, \Gamma}
299 {\eload{e}{m}}
300 {\_}
301 {\edload{e'}{m}}
302 {\Dynamic}
303 }
304
305 \sstext{A dynamic method load is well typed so long as the term is well-typed.}{ }
306
307 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}
308 {e}
309 {\_}
310 {e'}
311 {\Dynamic}
312 }
313 {\yieldsOk{\Phi, \Delta, \Gamma}
314 {\edload{e}{m}}
315 {\_}
316 {\edload{e'}{m}}
317 {\Dynamic}
318 }
319
320 \sstext{A field load from $\ethis$ is well-typed if the field name is present in the
321 type of $\ethis$.}{}
322
323 \infrule{\Gamma = \Gamma_\tau & \fieldLookup{\Phi}{\tau}{x}{\sigma}
324 }
325 {\yieldsOk{\Phi, \Delta, \Gamma}
326 {\eload{\ethis}{x}}
327 {\_}
328 {\eload{\ethis}{x}}
329 {\sigma}
330 }
331
332 \sstext{An assignment expression is well-typed so long as the term is well-typed at a
333 type which is compatible with the type of the variable being assigned.}{}
334
335 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}
336 {e}
337 {\opt{\tau}}
338 {e'}
339 {\sigma} \quad\quad
340 \yieldsOk{\Phi, \Delta, \Gamma}
341 {x}
342 {\sigma}
343 {x}
344 {\sigma'}
345 }
346 {\yieldsOk{\Phi, \Delta, \Gamma}
347 {\eassign{x}{e}}
348 {\opt{\tau}}
349 {\eassign{x}{e'}}
350 {\sigma}
351 }
352
353 \sstext{A field assignment is well-typed if the term being assigned is well-type d, the
354 field name is present in the type of $\ethis$, and the declared type of the
355 field is compatible with the type of the expression being assigned.}{}
356
357 \infrule{\Gamma = \Gamma_\tau \quad\quad
358 \yieldsOk{\Phi, \Delta, \Gamma}
359 {e}
360 {\opt{\tau}}
361 {e'}
362 {\sigma} \\
363 \fieldLookup{\Phi}{\tau}{x}{\sigma'} \quad\quad
364 \subtypeOf{\Phi, \Delta}{\sigma}{\sigma'}
365 }
366 {\yieldsOk{\Phi, \Delta, \Gamma}
367 {\eset{\ethis}{x}{e}}
368 {\_}
369 {\eset{\ethis}{x}{e}}
370 {\sigma}
371 }
372
373 \sstext{A throw expression is well-typed at any type.}{}
374
375 \axiom{\yieldsOk{\Phi, \Delta, \Gamma}
376 {\ethrow}
377 {\_}
378 {\ethrow}
379 {\sigma}
380 }
381
382 \sstext{A cast expression is well-typed so long as the term being cast is well-t yped.
383 The synthesized type is the cast-to type. We require that the cast-to type be a
384 ground type.}{}
385
386 \comment{TODO(leafp): specify ground types}
387
388 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}{e}{\_}{e'}{\sigma} \quad\quad \mbox{$\t au$ is ground}
389 }
390 {\yieldsOk{\Phi, \Delta, \Gamma}
391 {\eas{e}{\tau}}
392 {\_}
393 {\eas{e'}{\tau}}
394 {\tau}
395 }
396
397 \sstext{An instance check expression is well-typed if the term being checked is
398 well-typed. We require that the cast to-type be a ground type.}{}
399
400 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}{e}{\_}{e'}{\sigma} \quad\quad \mbox{$\t au$ is ground}
401 }
402 {\yieldsOk{\Phi, \Delta, \Gamma}
403 {\eis{e}{\tau}}
404 {\_}
405 {\eis{e'}{\tau}}
406 {\Bool}
407 }
408
409 \sstext{A check expression is well-typed so long as the term being checked is
410 well-typed. The synthesized type is the target type of the check.}{}
411
412
413 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}{e}{\_}{e'}{\sigma}
414 }
415 {\yieldsOk{\Phi, \Delta, \Gamma}
416 {\echeck{e}{\tau}}
417 {\_}
418 {\echeck{e'}{\tau}}
419 {\tau}
420 }
421
422 \subsection*{Declaration typing: $\declOk[d]{\Phi, \Delta, \Gamma}{\mathit{vd}}{ \mathit{vd'}}{\Gamma'}$}
423 \hrulefill\\
424
425 \sstext{
426 Variable declaration typing checks the well-formedness of the components, and
427 produces an output context $\Gamma'$ which contains the binding introduced by
428 the declaration.
429
430 A simple variable declaration with a declared type is well-typed if the
431 initializer for the declaration is well-typed at the declared type. The output
432 context binds the variable at the declared type.
433 }
434 {
435 Elaboration of declarations elaborates the underlying expressions.
436 }
437
438 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}{e}{\tau}{e'}{\tau'}
439 }
440 {\declOk[d]{\Phi, \Delta, \Gamma}
441 {\dvar{x:\tau}{e}}
442 {\dvar{x:\tau'}{e'}}
443 {\extends{\Gamma}{x}{\tau}}
444 }
445
446 \sstext{A simple variable declaration without a declared type is well-typed if t he
447 initializer for the declaration is well-typed at any type. The output context
448 binds the variable at the synthesized type (a simple form of type inference).}{}
449
450 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}{e}{\_}{e'}{\tau'}
451 }
452 {\declOk[d]{\Phi, \Delta, \Gamma}
453 {\dvar{x:\_}{e}}
454 {\dvar{x:\tau'}{e'}}
455 {\extends{\Gamma}{x}{\tau'}}
456 }
457
458 \sstext{A function declaration is well-typed if the body of the function is well -typed
459 with the given return type, under the assumption that the function and its
460 parameters have their declared types. The function is assumed to have a
461 contravariant (precise) function type. The output context binds the function
462 variable only.}{}
463
464 \infrule{\tau_f = \Arrow[-]{\many{\tau_a}}{\tau_r} \quad\quad
465 \Gamma' = \extends{\Gamma}{f}{\tau_f} \quad\quad
466 \Gamma'' = \extends{\Gamma'}{\many{x}}{\many{\tau_a}} \\
467 \stmtOk{\Phi, \Delta, \Gamma''}{s}{\tau_r}{s'}{\Gamma_0}
468 }
469 {\declOk[d]{\Phi, \Delta, \Gamma}
470 {\dfun{\tau_r}{f}{\many{x:\tau_a}}{s}}
471 {\dfun{\tau_r}{f}{\many{x:\tau_a}}{s'}}
472 {\Gamma'}
473 }
474
475 \subsection*{Statement typing: $\stmtOk{\Phi, \Delta, \Gamma}{\mathit{s}}{\tau}{ \mathit{s'}}{\Gamma'}$}
476 \hrulefill\\
477
478 \sstext{The statement typing relation checks the well-formedness of statements a nd
479 produces an output context which reflects any additional variable bindings
480 introduced into scope by the statements.
481 }{
482
483 Statement elaboration elaborates the underlying expressions.
484
485 }
486
487 \sstext{A variable declaration statement is well-typed if the variable declarati on is
488 well-typed per the previous relation, with the corresponding output context.
489 }{}
490
491 \infrule{\declOk[d]{\Phi, \Delta, \Gamma}
492 {\mathit{vd}}
493 {\mathit{vd'}}
494 {\Gamma'}
495 }
496 {\stmtOk{\Phi, \Delta, \Gamma}
497 {\mathit{vd}}
498 {\tau}
499 {\mathit{vd'}}
500 {\Gamma'}
501 }
502
503 \sstext{An expression statement is well-typed if the expression is well-typed at any
504 type per the expression typing relation.}{}
505
506 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}{e}{\_}{e'}{\tau}
507 }
508 {\stmtOk{\Phi, \Delta, \Gamma}{e}{\tau}{e'}{\Gamma}
509 }
510
511 \sstext{A conditional statement is well-typed if the condition is well-typed as a
512 boolean, and the statements making up the two arms are well-typed. The output
513 context is unchanged.}{}
514
515 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}{e}{\Bool}{e'}{\sigma} \\
516 \stmtOk{\Phi, \Delta, \Gamma}{s_1}{\tau_r}{s_1'}{\Gamma_1} \quad\quad
517 \stmtOk{\Phi, \Delta, \Gamma}{s_2}{\tau_r}{s_2'}{\Gamma_2}
518 }
519 {\stmtOk{\Phi, \Delta, \Gamma}
520 {\sifthenelse{e}{s_1}{s_2}}
521 {\tau_r}
522 {\sifthenelse{e'}{s_1'}{s_2'}}
523 {\Gamma}
524 }
525
526 \sstext{A return statement is well-typed if the expression being returned is wel l-typed
527 at the given return type. }{}
528
529 \infrule{\yieldsOk{\Phi, \Delta, \Gamma}{e}{\tau_r}{e'}{\tau}
530 }
531 {\stmtOk{\Phi, \Delta, \Gamma}{\sreturn{e}}{\tau_r}{\sreturn{e'}}{\Gamma }
532 }
533
534 \sstext{A sequence statement is well-typed if the first component is well-typed, and the
535 second component is well-typed with the output context of the first component as
536 its input context. The final output context is the output context of the second
537 component.}{}
538
539 \infrule{\stmtOk{\Phi, \Delta, \Gamma}{s_1}{\tau_r}{s_1'}{\Gamma'} \quad\quad
540 \stmtOk{\Phi, \Delta, \Gamma'}{s_2}{\tau_r}{s_2'}{\Gamma''}
541 }
542 {\stmtOk{\Phi, \Delta, \Gamma}{s_1;s_2}{\tau_r}{s_1';s_2'}{\Gamma''}
543 }
OLDNEW
« no previous file with comments | « doc/definition/macros.tex ('k') | doc/definition/strong-dart.pdf » ('j') | no next file with comments »

Powered by Google App Engine