| 1 |
= TypeVariableScope |
| 2 |
|
| 3 |
In <<StandardML#,Standard ML>>, every type variable is _scoped_ (or |
| 4 |
bound) at a particular point in the program. A type variable can be |
| 5 |
either implicitly scoped or explicitly scoped. For example, `'a` is |
| 6 |
implicitly scoped in |
| 7 |
|
| 8 |
[source,sml] |
| 9 |
---- |
| 10 |
val id: 'a -> 'a = fn x => x |
| 11 |
---- |
| 12 |
|
| 13 |
and is implicitly scoped in |
| 14 |
|
| 15 |
[source,sml] |
| 16 |
---- |
| 17 |
val id = fn x: 'a => x |
| 18 |
---- |
| 19 |
|
| 20 |
On the other hand, `'a` is explicitly scoped in |
| 21 |
|
| 22 |
[source,sml] |
| 23 |
---- |
| 24 |
val 'a id: 'a -> 'a = fn x => x |
| 25 |
---- |
| 26 |
|
| 27 |
and is explicitly scoped in |
| 28 |
|
| 29 |
[source,sml] |
| 30 |
---- |
| 31 |
val 'a id = fn x: 'a => x |
| 32 |
---- |
| 33 |
|
| 34 |
A type variable can be scoped at a `val` or `fun` declaration. An SML |
| 35 |
type checker performs scope inference on each top-level declaration to |
| 36 |
determine the scope of each implicitly scoped type variable. After |
| 37 |
scope inference, every type variable is scoped at exactly one |
| 38 |
enclosing `val` or `fun` declaration. Scope inference shows that the |
| 39 |
first and second example above are equivalent to the third and fourth |
| 40 |
example, respectively. |
| 41 |
|
| 42 |
Section 4.6 of the <<DefinitionOfStandardML#,Definition>> specifies |
| 43 |
precisely the scope of an implicitly scoped type variable. A free |
| 44 |
occurrence of a type variable `'a` in a declaration `d` is said to be |
| 45 |
_unguarded_ in `d` if `'a` is not part of a smaller declaration. A |
| 46 |
type variable `'a` is implicitly scoped at `d` if `'a` is unguarded in |
| 47 |
`d` and `'a` does not occur unguarded in any declaration containing |
| 48 |
`d`. |
| 49 |
|
| 50 |
|
| 51 |
== Scope inference examples |
| 52 |
|
| 53 |
* In this example, |
| 54 |
+ |
| 55 |
[source,sml] |
| 56 |
---- |
| 57 |
val id: 'a -> 'a = fn x => x |
| 58 |
---- |
| 59 |
+ |
| 60 |
`'a` is unguarded in `val id` and does not occur unguarded in any |
| 61 |
containing declaration. Hence, `'a` is scoped at `val id` and the |
| 62 |
declaration is equivalent to the following. |
| 63 |
+ |
| 64 |
[source,sml] |
| 65 |
---- |
| 66 |
val 'a id: 'a -> 'a = fn x => x |
| 67 |
---- |
| 68 |
|
| 69 |
* In this example, |
| 70 |
+ |
| 71 |
[source,sml] |
| 72 |
---- |
| 73 |
val f = fn x => let exception E of 'a in E x end |
| 74 |
---- |
| 75 |
+ |
| 76 |
`'a` is unguarded in `val f` and does not occur unguarded in any |
| 77 |
containing declaration. Hence, `'a` is scoped at `val f` and the |
| 78 |
declaration is equivalent to the following. |
| 79 |
+ |
| 80 |
[source,sml] |
| 81 |
---- |
| 82 |
val 'a f = fn x => let exception E of 'a in E x end |
| 83 |
---- |
| 84 |
|
| 85 |
* In this example (taken from the <<DefinitionOfStandardML#,Definition>>), |
| 86 |
+ |
| 87 |
[source,sml] |
| 88 |
---- |
| 89 |
val x: int -> int = let val id: 'a -> 'a = fn z => z in id id end |
| 90 |
---- |
| 91 |
+ |
| 92 |
`'a` occurs unguarded in `val id`, but not in `val x`. Hence, `'a` is |
| 93 |
implicitly scoped at `val id`, and the declaration is equivalent to |
| 94 |
the following. |
| 95 |
+ |
| 96 |
[source,sml] |
| 97 |
---- |
| 98 |
val x: int -> int = let val 'a id: 'a -> 'a = fn z => z in id id end |
| 99 |
---- |
| 100 |
|
| 101 |
|
| 102 |
* In this example, |
| 103 |
+ |
| 104 |
[source,sml] |
| 105 |
---- |
| 106 |
val f = (fn x: 'a => x) (fn y => y) |
| 107 |
---- |
| 108 |
+ |
| 109 |
`'a` occurs unguarded in `val f` and does not occur unguarded in any |
| 110 |
containing declaration. Hence, `'a` is implicitly scoped at `val f`, |
| 111 |
and the declaration is equivalent to the following. |
| 112 |
+ |
| 113 |
[source,sml] |
| 114 |
---- |
| 115 |
val 'a f = (fn x: 'a => x) (fn y => y) |
| 116 |
---- |
| 117 |
+ |
| 118 |
This does not type check due to the <<ValueRestriction#>>. |
| 119 |
|
| 120 |
* In this example, |
| 121 |
+ |
| 122 |
[source,sml] |
| 123 |
---- |
| 124 |
fun f x = |
| 125 |
let |
| 126 |
fun g (y: 'a) = if true then x else y |
| 127 |
in |
| 128 |
g x |
| 129 |
end |
| 130 |
---- |
| 131 |
+ |
| 132 |
`'a` occurs unguarded in `fun g`, not in `fun f`. Hence, `'a` is |
| 133 |
implicitly scoped at `fun g`, and the declaration is equivalent to |
| 134 |
+ |
| 135 |
[source,sml] |
| 136 |
---- |
| 137 |
fun f x = |
| 138 |
let |
| 139 |
fun 'a g (y: 'a) = if true then x else y |
| 140 |
in |
| 141 |
g x |
| 142 |
end |
| 143 |
---- |
| 144 |
+ |
| 145 |
This fails to type check because `x` and `y` must have the same type, |
| 146 |
but the `x` occurs outside the scope of the type variable `'a`. MLton |
| 147 |
reports the following error. |
| 148 |
+ |
| 149 |
---- |
| 150 |
Error: z.sml 3.21-3.41. |
| 151 |
Then and else branches disagree. |
| 152 |
then: [???] |
| 153 |
else: ['a] |
| 154 |
in: if true then x else y |
| 155 |
note: type would escape its scope: 'a |
| 156 |
escape to: z.sml 1.1-6.5 |
| 157 |
---- |
| 158 |
+ |
| 159 |
This problem could be fixed either by adding an explicit type |
| 160 |
constraint, as in `fun f (x: 'a)`, or by explicitly scoping `'a`, as |
| 161 |
in `fun 'a f x = ...`. |
| 162 |
|
| 163 |
|
| 164 |
== Restrictions on type variable scope |
| 165 |
|
| 166 |
It is not allowed to scope a type variable within a declaration in |
| 167 |
which it is already in scope (see the last restriction listed on page |
| 168 |
9 of the <<DefinitionOfStandardML#,Definition>>). For example, the |
| 169 |
following program is invalid. |
| 170 |
|
| 171 |
[source,sml] |
| 172 |
---- |
| 173 |
fun 'a f (x: 'a) = |
| 174 |
let |
| 175 |
fun 'a g (y: 'a) = y |
| 176 |
in |
| 177 |
() |
| 178 |
end |
| 179 |
---- |
| 180 |
|
| 181 |
MLton reports the following error. |
| 182 |
|
| 183 |
---- |
| 184 |
Error: z.sml 3.11-3.12. |
| 185 |
Type variable scoped at an outer declaration: 'a. |
| 186 |
scoped at: z.sml 1.1-6.6 |
| 187 |
---- |
| 188 |
|
| 189 |
This is an error even if the scoping is implicit. That is, the |
| 190 |
following program is invalid as well. |
| 191 |
|
| 192 |
[source,sml] |
| 193 |
---- |
| 194 |
fun f (x: 'a) = |
| 195 |
let |
| 196 |
fun 'a g (y: 'a) = y |
| 197 |
in |
| 198 |
() |
| 199 |
end |
| 200 |
---- |