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