Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / ShowBasis.adoc
1 ShowBasis
2 =========
3
4 MLton has a flag, `-show-basis <file>`, that causes MLton to pretty
5 print to _file_ the basis defined by the input program. For example,
6 if `foo.sml` contains
7 [source,sml]
8 ----
9 fun f x = x + 1
10 ----
11 then `mlton -show-basis foo.basis foo.sml` will create `foo.basis`
12 with the following contents.
13 ----
14 val f: int -> int
15 ----
16
17 If you only want to see the basis and do not wish to compile the
18 program, you can call MLton with `-stop tc`.
19
20 == Displaying signatures ==
21
22 When displaying signatures, MLton prefixes types defined in the
23 signature them with `_sig.` to distinguish them from types defined in the
24 environment. For example,
25 [source,sml]
26 ----
27 signature SIG =
28 sig
29 type t
30 val x: t * int -> unit
31 end
32 ----
33 is displayed as
34 ----
35 signature SIG =
36 sig
37 type t
38 val x: _sig.t * int -> unit
39 end
40 ----
41
42 Notice that `int` occurs without the `_sig.` prefix.
43
44 MLton also uses a canonical name for each type in the signature, and
45 that name is used everywhere for that type, no matter what the input
46 signature looked like. For example:
47 [source,sml]
48 ----
49 signature SIG =
50 sig
51 type t
52 type u = t
53 val x: t
54 val y: u
55 end
56 ----
57 is displayed as
58 ----
59 signature SIG =
60 sig
61 type t
62 type u = _sig.t
63 val x: _sig.t
64 val y: _sig.t
65 end
66 ----
67
68 Canonical names are always relative to the "top" of the signature,
69 even when used in nested substructures. For example:
70 [source,sml]
71 ----
72 signature S =
73 sig
74 type t
75 val w: t
76 structure U:
77 sig
78 type u
79 val x: t
80 val y: u
81 end
82 val z: U.u
83 end
84 ----
85 is displayed as
86 ----
87 signature S =
88 sig
89 type t
90 val w: _sig.t
91 val z: _sig.U.u
92 structure U:
93 sig
94 type u
95 val x: _sig.t
96 val y: _sig.U.u
97 end
98 end
99 ----
100
101 == Displaying structures ==
102
103 When displaying structures, MLton uses signature constraints wherever
104 possible, combined with `where type` clauses to specify the meanings
105 of the types defined within the signature. For example:
106 [source,sml]
107 ----
108 signature SIG =
109 sig
110 type t
111 val x: t
112 end
113 structure S: SIG =
114 struct
115 type t = int
116 val x = 13
117 end
118 structure S2:> SIG = S
119 ----
120 is displayed as
121 ----
122 signature SIG =
123 sig
124 type t
125 val x: _sig.t
126 end
127 structure S: SIG
128 where type t = int
129 structure S2: SIG
130 where type t = S2.t
131 ----