| 1 |
= TypeChecking |
| 2 |
|
| 3 |
MLton's type checker follows the <<DefinitionOfStandardML#,Definition>> |
| 4 |
closely, so you may find differences between MLton and other SML |
| 5 |
compilers that do not follow the Definition so closely. In |
| 6 |
particular, SML/NJ has many deviations from the Definition -- please |
| 7 |
see <<SMLNJDeviations#>> for those that we are aware of. |
| 8 |
|
| 9 |
In some respects MLton's type checker is more powerful than other SML |
| 10 |
compilers, so there are programs that MLton accepts that are rejected |
| 11 |
by some other SML compilers. These kinds of programs fall into a few |
| 12 |
simple categories. |
| 13 |
|
| 14 |
* MLton resolves flexible record patterns using a larger context than |
| 15 |
many other SML compilers. For example, MLton accepts the |
| 16 |
following. |
| 17 |
+ |
| 18 |
[source,sml] |
| 19 |
---- |
| 20 |
fun f {x, ...} = x |
| 21 |
val _ = f {x = 13, y = "foo"} |
| 22 |
---- |
| 23 |
|
| 24 |
* MLton uses as large a context as possible to resolve the type of |
| 25 |
variables constrained by the value restriction to be monotypes. For |
| 26 |
example, MLton accepts the following. |
| 27 |
+ |
| 28 |
[source,sml] |
| 29 |
---- |
| 30 |
structure S: |
| 31 |
sig |
| 32 |
val f: int -> int |
| 33 |
end = |
| 34 |
struct |
| 35 |
val f = (fn x => x) (fn y => y) |
| 36 |
end |
| 37 |
---- |
| 38 |
|
| 39 |
|
| 40 |
== Type error messages |
| 41 |
|
| 42 |
To aid in the understanding of type errors, MLton's type checker |
| 43 |
displays type errors differently than other SML compilers. In |
| 44 |
particular, when two types are different, it is important for the |
| 45 |
programmer to easily understand why they are different. So, MLton |
| 46 |
displays only the differences between two types that don't match, |
| 47 |
using underscores for the parts that match. For example, if a |
| 48 |
function expects `real * int` but gets `real * real`, the type error |
| 49 |
message would look like |
| 50 |
|
| 51 |
---- |
| 52 |
expects: _ * [int] |
| 53 |
but got: _ * [real] |
| 54 |
---- |
| 55 |
|
| 56 |
As another aid to spotting differences, MLton places brackets `[]` |
| 57 |
around the parts of the types that don't match. A common situation is |
| 58 |
when a function receives a different number of arguments than it |
| 59 |
expects, in which case you might see an error like |
| 60 |
|
| 61 |
---- |
| 62 |
expects: [int * real] |
| 63 |
but got: [int * real * string] |
| 64 |
---- |
| 65 |
|
| 66 |
The brackets make it easy to see that the problem is that the tuples |
| 67 |
have different numbers of components -- not that the components don't |
| 68 |
match. Contrast that with a case where a function receives the right |
| 69 |
number of arguments, but in the wrong order, in which case you might |
| 70 |
see an error like |
| 71 |
|
| 72 |
---- |
| 73 |
expects: [int] * [real] |
| 74 |
but got: [real] * [int] |
| 75 |
---- |
| 76 |
|
| 77 |
Here the brackets make it easy to see that the components do not match. |
| 78 |
|
| 79 |
We appreciate feedback on any type error messages that you find |
| 80 |
confusing, or suggestions you may have for improvements to error |
| 81 |
messages. |
| 82 |
|
| 83 |
|
| 84 |
== The shortest/most-recent rule for type names |
| 85 |
|
| 86 |
In a type error message, MLton often has a number of choices in |
| 87 |
deciding what name to use for a type. For example, in the following |
| 88 |
type-incorrect program |
| 89 |
|
| 90 |
[source,sml] |
| 91 |
---- |
| 92 |
type t = int |
| 93 |
fun f (x: t) = x |
| 94 |
val _ = f "foo" |
| 95 |
---- |
| 96 |
|
| 97 |
MLton reports the error message |
| 98 |
|
| 99 |
---- |
| 100 |
Error: z.sml 3.9-3.15. |
| 101 |
Function applied to incorrect argument. |
| 102 |
expects: [t] |
| 103 |
but got: [string] |
| 104 |
in: f "foo" |
| 105 |
---- |
| 106 |
|
| 107 |
MLton could have reported `expects: [int]` instead of `expects: [t]`. |
| 108 |
However, MLton uses the shortest/most-recent rule in order to decide |
| 109 |
what type name to display. This rule means that, at the point of the |
| 110 |
error, MLton first looks for the shortest name for a type in terms of |
| 111 |
number of structure identifiers (e.g. `foobar` is shorter than `A.t`). |
| 112 |
Next, if there are multiple names of the same length, then MLton uses |
| 113 |
the most recently defined name. It is this tiebreaker that causes |
| 114 |
MLton to prefer `t` to `int` in the above example. |
| 115 |
|
| 116 |
In signature matching, most recently defined is not taken to include |
| 117 |
all of the definitions introduced by the structure (since the matching |
| 118 |
takes place outside the structure and before it is defined). For |
| 119 |
example, in the following type-incorrect program |
| 120 |
|
| 121 |
[source,sml] |
| 122 |
---- |
| 123 |
structure S: |
| 124 |
sig |
| 125 |
val x: int |
| 126 |
end = |
| 127 |
struct |
| 128 |
type t = int |
| 129 |
val x = "foo" |
| 130 |
end |
| 131 |
---- |
| 132 |
|
| 133 |
MLton reports the error message |
| 134 |
|
| 135 |
---- |
| 136 |
Error: z.sml 2.4-4.6. |
| 137 |
Variable in structure disagrees with signature (type): x. |
| 138 |
structure: val x: [string] |
| 139 |
defn at: z.sml 7.11-7.11 |
| 140 |
signature: val x: [int] |
| 141 |
spec at: z.sml 3.11-3.11 |
| 142 |
---- |
| 143 |
|
| 144 |
If there is a type that only exists inside the structure being |
| 145 |
matched, then the prefix `_str.` is used. For example, in the |
| 146 |
following type-incorrect program |
| 147 |
|
| 148 |
[source,sml] |
| 149 |
---- |
| 150 |
structure S: |
| 151 |
sig |
| 152 |
val x: int |
| 153 |
end = |
| 154 |
struct |
| 155 |
datatype t = T |
| 156 |
val x = T |
| 157 |
end |
| 158 |
---- |
| 159 |
|
| 160 |
MLton reports the error message |
| 161 |
|
| 162 |
---- |
| 163 |
Error: z.sml 2.4-4.6. |
| 164 |
Variable in structure disagrees with signature (type): x. |
| 165 |
structure: val x: [_str.t] |
| 166 |
defn at: z.sml 7.11-7.11 |
| 167 |
signature: val x: [int] |
| 168 |
spec at: z.sml 3.11-3.11 |
| 169 |
---- |
| 170 |
|
| 171 |
in which the `[_str.t]` refers to the type defined in the structure. |