Debian Codegraph @

doc/guide/src/Monomorphise.adoc

raw
1 = Monomorphise
2
3 <<Monomorphise#>> is a translation pass from the <<XML#>>
4 <<IntermediateLanguage#>> to the <<SXML#>> <<IntermediateLanguage#>>.
5
6 == Description
7
8 Monomorphisation eliminates polymorphic values and datatype
9 declarations by duplicating them for each type at which they are used.
10
11 Consider the following <<XML#>> program.
12 [source,sml]
13 ----
14 datatype 'a t = T of 'a
15 fun 'a f (x: 'a) = T x
16 val a = f 1
17 val b = f 2
18 val z = f (3, 4)
19 ----
20
21 The result of monomorphising this program is the following <<SXML#>> program:
22 [source,sml]
23 ----
24 datatype t1 = T1 of int
25 datatype t2 = T2 of int * int
26 fun f1 (x: int) = T1 x
27 fun f2 (x: int * int) = T2 x
28 val a = f1 1
29 val b = f1 2
30 val z = f2 (3, 4)
31 ----
32
33 == Implementation
34
35 * https://github.com/MLton/mlton/blob/master/mlton/xml/monomorphise.sig[`monomorphise.sig`]
36 * https://github.com/MLton/mlton/blob/master/mlton/xml/monomorphise.fun[`monomorphise.fun`]
37
38 == Details and Notes
39
40 The monomorphiser works by making one pass over the entire program.
41 On the way down, it creates a cache for each variable declared in a
42 polymorphic declaration that maps a lists of type arguments to a new
43 variable name. At a variable reference, it consults the cache (based
44 on the types the variable is applied to). If there is already an
45 entry in the cache, it is used. If not, a new entry is created. On
46 the way up, the monomorphiser duplicates a variable declaration for
47 each entry in the cache.
48
49 As with variables, the monomorphiser records all of the type at which
50 constructors are used. After the entire program is processed, the
51 monomorphiser duplicates each datatype declaration and its associated
52 constructors.
53
54 The monomorphiser duplicates all of the functions declared in a
55 `fun` declaration as a unit. Consider the following program
56 [source,sml]
57 ----
58 fun 'a f (x: 'a) = g x
59 and g (y: 'a) = f y
60 val a = f 13
61 val b = g 14
62 val c = f (1, 2)
63 ----
64
65 and its monomorphisation
66
67 [source,sml]
68 ----
69 fun f1 (x: int) = g1 x
70 and g1 (y: int) = f1 y
71 fun f2 (x : int * int) = g2 x
72 and g2 (y : int * int) = f2 y
73 val a = f1 13
74 val b = g1 14
75 val c = f2 (1, 2)
76 ----
77
78 == Pathological datatype declarations
79
80 SML allows a pathological polymorphic datatype declaration in which
81 recursive uses of the defined type constructor are applied to
82 different type arguments than the definition. This has been
83 disallowed by others on type theoretic grounds. A canonical example
84 is the following.
85 [source,sml]
86 ----
87 datatype 'a t = A of 'a | B of ('a * 'a) t
88 val z : int t = B (B (A ((1, 2), (3, 4))))
89 ----
90
91 The presence of the recursion in the datatype declaration might appear
92 to cause the need for the monomorphiser to create an infinite number
93 of types. However, due to the absence of polymorphic recursion in
94 SML, there are in fact only a finite number of instances of such types
95 in any given program. The monomorphiser translates the above program
96 to the following one.
97 [source,sml]
98 ----
99 datatype t1 = B1 of t2
100 datatype t2 = B2 of t3
101 datatype t3 = A3 of (int * int) * (int * int)
102 val z : int t = B1 (B2 (A3 ((1, 2), (3, 4))))
103 ----
104
105 It is crucial that the monomorphiser be allowed to drop unused
106 constructors from datatype declarations in order for the translation
107 to terminate.