| 1 |
= FirstClassPolymorphism |
| 2 |
|
| 3 |
First-class polymorphism is the ability to treat polymorphic functions |
| 4 |
just like other values: pass them as arguments, store them in data |
| 5 |
structures, etc. Although <<StandardML#,Standard ML>> does have |
| 6 |
polymorphic functions, it does not support first-class polymorphism. |
| 7 |
|
| 8 |
For example, the following declares and uses the polymorphic function |
| 9 |
`id`. |
| 10 |
[source,sml] |
| 11 |
---- |
| 12 |
val id = fn x => x |
| 13 |
val _ = id 13 |
| 14 |
val _ = id "foo" |
| 15 |
---- |
| 16 |
|
| 17 |
If SML supported first-class polymorphism, we could write the |
| 18 |
following. |
| 19 |
[source,sml] |
| 20 |
---- |
| 21 |
fun useId id = (id 13; id "foo") |
| 22 |
---- |
| 23 |
|
| 24 |
However, this does not type check. MLton reports the following error. |
| 25 |
---- |
| 26 |
Error: z.sml 1.24-1.31. |
| 27 |
Function applied to incorrect argument. |
| 28 |
expects: [int] |
| 29 |
but got: [string] |
| 30 |
in: id "foo" |
| 31 |
---- |
| 32 |
The error message arises because MLton infers from `id 13` that `id` |
| 33 |
accepts an integer argument, but that `id "foo"` is passing a string. |
| 34 |
|
| 35 |
Using explicit types sheds some light on the problem. |
| 36 |
[source,sml] |
| 37 |
---- |
| 38 |
fun useId (id: 'a -> 'a) = (id 13; id "foo") |
| 39 |
---- |
| 40 |
|
| 41 |
On this, MLton reports the following errors. |
| 42 |
---- |
| 43 |
Error: z.sml 1.29-1.33. |
| 44 |
Function applied to incorrect argument. |
| 45 |
expects: ['a] |
| 46 |
but got: [int] |
| 47 |
in: id 13 |
| 48 |
Error: z.sml 1.36-1.43. |
| 49 |
Function applied to incorrect argument. |
| 50 |
expects: ['a] |
| 51 |
but got: [string] |
| 52 |
in: id "foo" |
| 53 |
---- |
| 54 |
|
| 55 |
The errors arise because the argument `id` is _not_ polymorphic; |
| 56 |
rather, it is monomorphic, with type `'a \-> 'a`. It is perfectly |
| 57 |
valid to apply `id` to a value of type `'a`, as in the following |
| 58 |
[source,sml] |
| 59 |
---- |
| 60 |
fun useId (id: 'a -> 'a, x: 'a) = id x (* type correct *) |
| 61 |
---- |
| 62 |
|
| 63 |
So, what is the difference between the type specification on `id` in |
| 64 |
the following two declarations? |
| 65 |
[source,sml] |
| 66 |
---- |
| 67 |
val id: 'a -> 'a = fn x => x |
| 68 |
fun useId (id: 'a -> 'a) = (id 13; id "foo") |
| 69 |
---- |
| 70 |
|
| 71 |
While the type specifications on `id` look identical, they mean |
| 72 |
different things. The difference can be made clearer by explicitly |
| 73 |
<<TypeVariableScope#,scoping the type variables>>. |
| 74 |
[source,sml] |
| 75 |
---- |
| 76 |
val 'a id: 'a -> 'a = fn x => x |
| 77 |
fun 'a useId (id: 'a -> 'a) = (id 13; id "foo") (* type error *) |
| 78 |
---- |
| 79 |
|
| 80 |
In `val 'a id`, the type variable scoping means that for any `'a`, |
| 81 |
`id` has type `'a \-> 'a`. Hence, `id` can be applied to arguments of |
| 82 |
type `int`, `real`, etc. Similarly, in `fun 'a useId`, the scoping |
| 83 |
means that `useId` is a polymorphic function that for any `'a` takes a |
| 84 |
function of type `'a \-> 'a` and does something. Thus, `useId` could |
| 85 |
be applied to a function of type `int \-> int`, `real \-> real`, etc. |
| 86 |
|
| 87 |
One could imagine an extension of SML that allowed scoping of type |
| 88 |
variables at places other than `fun` or `val` declarations, as in the |
| 89 |
following. |
| 90 |
---- |
| 91 |
fun useId (id: ('a).'a -> 'a) = (id 13; id "foo") (* not SML *) |
| 92 |
---- |
| 93 |
|
| 94 |
Such an extension would need to be thought through very carefully, as |
| 95 |
it could cause significant complications with <<TypeInference#>>, |
| 96 |
possible even undecidability. |