Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / ScopeInference.adoc
CommitLineData
7f918cf1
CE
1ScopeInference
2==============
3
4Scope inference is an analysis/rewrite pass for the <:AST:>
5<:IntermediateLanguage:>, invoked from <:Elaborate:>.
6
7== Description ==
8
9This pass adds free type variables to the `val` or `fun`
10declaration where they are implicitly scoped.
11
12== Implementation ==
13
14<!ViewGitFile(mlton,master,mlton/elaborate/scope.sig)>
15<!ViewGitFile(mlton,master,mlton/elaborate/scope.fun)>
16
17== Details and Notes ==
18
19Scope inference determines for each type variable, the declaration
20where it is bound. Scope inference is a direct implementation of the
21specification given in section 4.6 of the
22<:DefinitionOfStandardML: Definition>. Recall that a free occurrence
23of a type variable `'a` in a declaration `d` is _unguarded_
24in `d` if `'a` is not part of a smaller declaration. A type
25variable `'a` is implicitly scoped at `d` if `'a` is
26unguarded in `d` and `'a` does not occur unguarded in any
27declaration containing `d`.
28
29The first pass of scope inference walks down the tree and renames all
30explicitly bound type variables in order to avoid name collisions. It
31then walks up the tree and adds to each declaration the set of
32unguarded type variables occurring in that declaration. At this
33point, if declaration `d` contains an unguarded type variable
34`'a` and the immediately containing declaration does not contain
35`'a`, then `'a` is implicitly scoped at `d`. The final
36pass walks down the tree leaving a `'a` at the a declaration where
37it is scoped and removing it from all enclosed declarations.