8
6
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
9
<META name="GENERATOR" content="hevea 1.08">
7
<META name="GENERATOR" content="hevea 1.09">
10
8
<LINK rel="stylesheet" type="text/css" href="manual.css">
12
The toplevel system (ocaml)
9
<TITLE>The toplevel system (ocaml)</TITLE>
16
<A HREF="manual022.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
17
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
18
<A HREF="manual024.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
12
<A HREF="manual022.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
13
<A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
14
<A HREF="manual024.html"><IMG SRC="next_motif.gif" ALT="Next"></A>
21
<H1 CLASS="chapter"><A NAME="htoc108">Chapter 9</A> The toplevel system (ocaml)</H1> <A NAME="c:camllight"></A>
23
This chapter describes the toplevel system for Objective Caml, that permits interactive use of the Objective Caml system
16
<H1 CLASS="chapter"><A NAME="htoc108">Chapter�9</A>��The toplevel system (ocaml)</H1><P> <A NAME="c:camllight"></A>
17
</P><P>This chapter describes the toplevel system for Objective Caml, that permits interactive use of the Objective Caml system
24
18
through a read-eval-print loop. In this mode, the system repeatedly
25
19
reads Caml phrases from the input, then typechecks, compile and
26
20
evaluate them, then prints the inferred type and result value, if
27
21
any. The system prints a <TT>#</TT> (sharp) prompt before reading each
30
Input to the toplevel can span several lines. It is terminated by <TT>;;</TT> (a
22
phrase.</P><P>Input to the toplevel can span several lines. It is terminated by <TT>;;</TT> (a
31
23
double-semicolon). The toplevel input consists in one or several
32
toplevel phrases, with the following syntax:<BR>
34
<DIV CLASS="center"><TABLE CELLSPACING=2 CELLPADDING=0>
35
<TR><TD ALIGN=right NOWRAP>
36
<A NAME="toplevel-input"></A>
37
<FONT COLOR=maroon><I><TT>toplevel-input</TT></I></FONT></TD>
38
<TD ALIGN=right NOWRAP>::=</TD>
39
<TD ALIGN=left NOWRAP>
40
{ <FONT COLOR=maroon><I><a href="#toplevel-phrase"><font color=maroon><TT>toplevel-phrase</TT></font></a></I></FONT> } <FONT COLOR=blue><TT>;;</TT></FONT></TD>
42
<TR><TD ALIGN=right NOWRAP>
43
<A NAME="toplevel-phrase"></A>
44
<FONT COLOR=maroon><I><TT>toplevel-phrase</TT></I></FONT></TD>
45
<TD ALIGN=right NOWRAP>::=</TD>
46
<TD ALIGN=left NOWRAP>
47
<FONT COLOR=maroon><I><a href="#toplevel-definition"><font color=maroon><TT>toplevel-definition</TT></font></a></I></FONT></TD>
49
<TR><TD ALIGN=right NOWRAP> </TD>
50
<TD ALIGN=right NOWRAP>∣</TD>
51
<TD ALIGN=left NOWRAP> <FONT COLOR=maroon><I><a href="manual015.html#expr"><font color=maroon><TT>expr</TT></font></a></I></FONT></TD>
53
<TR><TD ALIGN=right NOWRAP> </TD>
54
<TD ALIGN=right NOWRAP>∣</TD>
55
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>#</TT></FONT> <FONT COLOR=maroon><I><a href="manual009.html#ident"><font color=maroon><TT>ident</TT></font></a> <a href="#directive-argument"><font color=maroon><TT>directive-argument</TT></font></a></I></FONT></TD>
57
<TR><TD ALIGN=right NOWRAP>
58
<A NAME="toplevel-definition"></A>
59
<FONT COLOR=maroon><I><TT>toplevel-definition</TT></I></FONT></TD>
60
<TD ALIGN=right NOWRAP>::=</TD>
61
<TD ALIGN=left NOWRAP>
62
<FONT COLOR=blue><TT>let</TT></FONT> [<FONT COLOR=blue><TT>rec</TT></FONT>] <FONT COLOR=maroon><I><a href="manual015.html#let-binding"><font color=maroon><TT>let-binding</TT></font></a></I></FONT> { <FONT COLOR=blue><TT>and</TT></FONT> <FONT COLOR=maroon><I><a href="manual015.html#let-binding"><font color=maroon><TT>let-binding</TT></font></a></I></FONT> }</TD>
64
<TR><TD ALIGN=right NOWRAP> </TD>
65
<TD ALIGN=right NOWRAP>∣</TD>
66
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>external</TT></FONT> <FONT COLOR=maroon><TT><a href="manual011.html#value-name"><font color=maroon><I>value-name</I></font></a></TT> <FONT COLOR=blue><TT>:</TT></FONT> <TT><a href="manual012.html#typexpr"><font color=maroon><I>typexpr</I></font></a></TT> <FONT COLOR=blue><TT>=</TT></FONT> <TT><I>external-declaration</I></TT></FONT></TD>
68
<TR><TD ALIGN=right NOWRAP> </TD>
69
<TD ALIGN=right NOWRAP>∣</TD>
70
<TD ALIGN=left NOWRAP> <FONT COLOR=maroon><I><a href="manual016.html#type-definition"><font color=maroon><TT>type-definition</TT></font></a></I></FONT></TD>
72
<TR><TD ALIGN=right NOWRAP> </TD>
73
<TD ALIGN=right NOWRAP>∣</TD>
74
<TD ALIGN=left NOWRAP> <FONT COLOR=maroon><I><a href="manual016.html#exception-definition"><font color=maroon><TT>exception-definition</TT></font></a></I></FONT></TD>
76
<TR><TD ALIGN=right NOWRAP> </TD>
77
<TD ALIGN=right NOWRAP>∣</TD>
78
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>module</TT></FONT> <FONT COLOR=maroon><I><a href="manual011.html#module-name"><font color=maroon><TT>module-name</TT></font></a></I></FONT> [ <FONT COLOR=blue><TT>:</TT></FONT> <FONT COLOR=maroon><I><a href="manual018.html#module-type"><font color=maroon><TT>module-type</TT></font></a></I></FONT> ] <FONT COLOR=blue><TT>=</TT></FONT> <FONT COLOR=maroon><I><a href="manual019.html#module-expr"><font color=maroon><TT>module-expr</TT></font></a></I></FONT></TD>
80
<TR><TD ALIGN=right NOWRAP> </TD>
81
<TD ALIGN=right NOWRAP>∣</TD>
82
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>module</TT></FONT> <FONT COLOR=blue><TT>type</TT></FONT> <FONT COLOR=maroon><TT><a href="manual011.html#modtype-name"><font color=maroon><I>modtype-name</I></font></a></TT> <FONT COLOR=blue><TT>=</TT></FONT> <TT><a href="manual018.html#module-type"><font color=maroon><I>module-type</I></font></a></TT></FONT></TD>
84
<TR><TD ALIGN=right NOWRAP> </TD>
85
<TD ALIGN=right NOWRAP>∣</TD>
86
<TD ALIGN=left NOWRAP> <FONT COLOR=blue><TT>open</TT></FONT> <FONT COLOR=maroon><I><a href="manual011.html#module-path"><font color=maroon><TT>module-path</TT></font></a></I></FONT></TD>
88
<TR><TD ALIGN=right NOWRAP>
89
<A NAME="directive-argument"></A>
90
<FONT COLOR=maroon><I><TT>directive-argument</TT></I></FONT></TD>
91
<TD ALIGN=right NOWRAP>::=</TD>
92
<TD ALIGN=left NOWRAP>
93
<FONT COLOR=maroon><I><TT>nothing</TT></I></FONT></TD>
95
<TR><TD ALIGN=right NOWRAP> </TD>
96
<TD ALIGN=right NOWRAP>∣</TD>
97
<TD ALIGN=left NOWRAP> <FONT COLOR=maroon><I><a href="manual009.html#string-literal"><font color=maroon><TT>string-literal</TT></font></a></I></FONT></TD>
99
<TR><TD ALIGN=right NOWRAP> </TD>
100
<TD ALIGN=right NOWRAP>∣</TD>
101
<TD ALIGN=left NOWRAP> <FONT COLOR=maroon><I><a href="manual009.html#integer-literal"><font color=maroon><TT>integer-literal</TT></font></a></I></FONT></TD>
103
<TR><TD ALIGN=right NOWRAP> </TD>
104
<TD ALIGN=right NOWRAP>∣</TD>
105
<TD ALIGN=left NOWRAP> <FONT COLOR=maroon><I><a href="manual011.html#value-path"><font color=maroon><TT>value-path</TT></font></a></I></FONT></TD>
106
</TR></TABLE></DIV><BR>
108
A phrase can consist of a definition, similar to those found in
109
implementations of compilation units or in <FONT COLOR=blue><TT>struct</TT></FONT> … <FONT COLOR=blue><TT>end</TT></FONT>
24
toplevel phrases, with the following syntax:</P><TABLE CLASS="display dcenter"><TR VALIGN="middle"><TD CLASS="dcell"><TABLE CELLSPACING=6 CELLPADDING=0><TR><TD ALIGN=right NOWRAP>
25
<I><A NAME="toplevel-input"><FONT COLOR=maroon>toplevel-input</FONT></A></I></TD><TD ALIGN=center NOWRAP>::=</TD><TD ALIGN=left NOWRAP>
26
{�<I><A HREF="#toplevel-phrase"><FONT COLOR=maroon>toplevel-phrase</FONT></A></I>�}�<FONT COLOR=blue><TT>;;</TT></FONT>
28
<TR><TD ALIGN=right NOWRAP> </TD></TR>
29
<TR><TD ALIGN=right NOWRAP>
30
<I><A NAME="toplevel-phrase"><FONT COLOR=maroon>toplevel-phrase</FONT></A></I></TD><TD ALIGN=center NOWRAP>::=</TD><TD ALIGN=left NOWRAP>
31
<I><A HREF="manual019.html#definition"><FONT COLOR=maroon>definition</FONT></A></I>
33
<TR><TD ALIGN=right NOWRAP> </TD><TD ALIGN=center NOWRAP>∣</TD><TD ALIGN=left NOWRAP>�<I><A HREF="expr.html#expr"><FONT COLOR=maroon>expr</FONT></A></I>
35
<TR><TD ALIGN=right NOWRAP> </TD><TD ALIGN=center NOWRAP>∣</TD><TD ALIGN=left NOWRAP>�<FONT COLOR=blue><TT>#</TT></FONT>�<I><A HREF="lex.html#ident"><FONT COLOR=maroon>ident</FONT></A></I>��<I><A HREF="#directive-argument"><FONT COLOR=maroon>directive-argument</FONT></A></I>
37
<TR><TD ALIGN=right NOWRAP> </TD></TR>
38
<TR><TD ALIGN=right NOWRAP>
39
<I><A NAME="directive-argument"><FONT COLOR=maroon>directive-argument</FONT></A></I></TD><TD ALIGN=center NOWRAP>::=</TD><TD ALIGN=left NOWRAP>
42
<TR><TD ALIGN=right NOWRAP> </TD><TD ALIGN=center NOWRAP>∣</TD><TD ALIGN=left NOWRAP>�<I><A HREF="lex.html#string-literal"><FONT COLOR=maroon>string-literal</FONT></A></I>
44
<TR><TD ALIGN=right NOWRAP> </TD><TD ALIGN=center NOWRAP>∣</TD><TD ALIGN=left NOWRAP>�<I><A HREF="lex.html#integer-literal"><FONT COLOR=maroon>integer-literal</FONT></A></I>
46
<TR><TD ALIGN=right NOWRAP> </TD><TD ALIGN=center NOWRAP>∣</TD><TD ALIGN=left NOWRAP>�<I><A HREF="manual011.html#value-path"><FONT COLOR=maroon>value-path</FONT></A></I>
48
<TR><TD ALIGN=right NOWRAP> </TD></TR>
50
</TABLE><P>A phrase can consist of a definition, similar to those found in
51
implementations of compilation units or in <FONT COLOR=blue><TT>struct</TT></FONT> … <FONT COLOR=blue><TT>end</TT></FONT>
110
52
module expressions. The definition can bind value names, type names,
111
53
an exception, a module name, or a module type name. The toplevel
112
54
system performs the bindings, then prints the types and values (if
113
any) for the names thus defined.<BR>
115
A phrase may also consist in a <TT>open</TT> directive (see
116
section <A HREF="manual019.html#s:module-expr">6.11</A>), or a value expression
117
(section <A HREF="manual015.html#s:value-expr">6.7</A>). Expressions are simply evaluated,
55
any) for the names thus defined.</P><P>A phrase may also consist in a <TT>open</TT> directive (see
56
section�<A HREF="manual019.html#s:module-expr">6.11</A>), or a value expression
57
(section�<A HREF="expr.html#s:value-expr">6.7</A>). Expressions are simply evaluated,
118
58
without performing any bindings, and the value of the expression is
121
Finally, a phrase can also consist in a toplevel directive,
59
printed.</P><P>Finally, a phrase can also consist in a toplevel directive,
122
60
starting with <TT>#</TT> (the sharp sign). These directives control the
123
61
behavior of the toplevel; they are listed below in
124
section <A HREF="#s:toplevel-directives">9.2</A>.<BR>
127
<FONT COLOR=purple>Unix:</FONT>
128
<BLOCKQUOTE CLASS="quote">
62
section�<A HREF="#s:toplevel-directives">9.2</A>.</P><BLOCKQUOTE CLASS="quote"><FONT COLOR=purple>Unix:</FONT>��
129
63
The toplevel system is started by the command <TT>ocaml</TT>, as follows:
131
65
ocaml <I>options objects</I> # interactive mode
185
107
of Objective Caml, and Unix kernels usually do not handle nested <TT>#!</TT>
186
108
scripts. A better solution is to put the following as the first line
188
<PRE CLASS="verbatim">
189
#!/usr/local/bin/ocamlrun /usr/local/bin/ocaml
192
<FONT COLOR=purple>Windows:</FONT>
193
<BLOCKQUOTE CLASS="quote">
110
</P><PRE CLASS="verbatim"> #!/usr/local/bin/ocamlrun /usr/local/bin/ocaml
111
</PRE></BLOCKQUOTE><BLOCKQUOTE CLASS="quote"><FONT COLOR=purple>Windows:</FONT>��
194
112
In addition to the text-only command <TT>ocaml.exe</TT>, which works exactly
195
113
as under Unix (see above), a graphical user interface for the
196
114
toplevel is available under the name <TT>ocamlwin.exe</TT>. It should be
197
115
launched from the Windows file manager or program manager.
198
116
This interface provides a text window in which commands can be entered
199
117
and edited, and the toplevel responses are printed.
202
<H2 CLASS="section"><A NAME="htoc109">9.1</A> Options</H2> <A NAME="s:toplevel-options"></A>
203
The following command-line options are recognized by the <TT>ocaml</TT> command.
204
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description"><B><TT>-I</TT> <I>directory</I></B><DD CLASS="dd-description">
118
</BLOCKQUOTE><H2 CLASS="section"><A NAME="htoc109">9.1</A>��Options</H2><P> <A NAME="s:toplevel-options"></A></P><P>The following command-line options are recognized by the <TT>ocaml</TT> command.</P><DL CLASS="description"><DT CLASS="dt-description"><B><TT>-I</TT> <I>directory</I></B></DT><DD CLASS="dd-description">
205
119
Add the given directory to the list of directories searched for
206
120
source and compiled files. By default, the current directory is
207
121
searched first, then the standard library directory. Directories added
208
122
with <TT>-I</TT> are searched after the current directory, in the order in
209
123
which they were given on the command line, but before the standard
210
library directory.<BR>
212
If the given directory starts with <TT>+</TT>, it is taken relative to the
124
library directory.<P>If the given directory starts with <TT>+</TT>, it is taken relative to the
213
125
standard library directory. For instance, <TT>-I +labltk</TT> adds the
214
subdirectory <TT>labltk</TT> of the standard library to the search path.<BR>
216
Directories can also be added to the search path once
126
subdirectory <TT>labltk</TT> of the standard library to the search path.</P><P>Directories can also be added to the search path once
217
127
the toplevel is running with the <TT>#directory</TT> directive
218
(section <A HREF="#s:toplevel-directives">9.2</A>).<BR>
220
<DT CLASS="dt-description"><TT><B>-nolabels</B></TT><DD CLASS="dd-description">
128
(section�<A HREF="#s:toplevel-directives">9.2</A>).</P></DD><DT CLASS="dt-description"><TT><B>-nolabels</B></TT></DT><DD CLASS="dd-description">
221
129
Ignore non-optional labels in types. Labels cannot be used in
222
applications, and parameter order becomes strict.<BR>
224
<DT CLASS="dt-description"><TT><B>-principal</B></TT><DD CLASS="dd-description">
130
applications, and parameter order becomes strict.</DD><DT CLASS="dt-description"><TT><B>-principal</B></TT></DT><DD CLASS="dd-description">
225
131
Check information path during type-checking, to make sure that all
226
132
types are derived in a principal way. All programs accepted in
227
133
<TT>-principal</TT> mode are also accepted in default mode with equivalent
230
<DT CLASS="dt-description"><TT><B>-rectypes</B></TT><DD CLASS="dd-description">
134
types.</DD><DT CLASS="dt-description"><TT><B>-rectypes</B></TT></DT><DD CLASS="dd-description">
231
135
Allow arbitrary recursive types during type-checking. By default,
232
136
only recursive types where the recursion goes through an object type
235
<DT CLASS="dt-description"><TT><B>-unsafe</B></TT><DD CLASS="dd-description">
236
See the corresponding option for <TT>ocamlc</TT>, chapter <A HREF="manual022.html#c:camlc">8</A>.
137
are supported.</DD><DT CLASS="dt-description"><TT><B>-unsafe</B></TT></DT><DD CLASS="dd-description">
138
See the corresponding option for <TT>ocamlc</TT>, chapter�<A HREF="manual022.html#c:camlc">8</A>.
237
139
Turn bound checking off on array and string accesses (the <TT>v.(i)</TT> and
238
140
<TT>s.[i]</TT> constructs). Programs compiled with <TT>-unsafe</TT> are therefore
239
141
slightly faster, but unsafe: anything can happen if the program
240
accesses an array or string outside of its bounds.<BR>
242
<DT CLASS="dt-description" style="background-color:yellow; color:red">
243
<TT><B>-version</B></TT><DD CLASS="dd-description" style="background-color:yellow; color:red">
244
Print version and exit.<BR>
246
<DT CLASS="dt-description"><B><TT>-w</TT> <I>warning-list</I></B><DD CLASS="dd-description">
247
Enable or disable warnings according to the argument <I>warning-list</I>.</DL>
249
<FONT COLOR=purple>Unix:</FONT>
250
<BLOCKQUOTE CLASS="quote">
142
accesses an array or string outside of its bounds.</DD><DT CLASS="dt-description"><TT><B>-version</B></TT></DT><DD CLASS="dd-description">
143
Print version and exit.</DD><DT CLASS="dt-description"><B><TT>-w</TT> <I>warning-list</I></B></DT><DD CLASS="dd-description">
144
Enable or disable warnings according to the argument <I>warning-list</I>.</DD></DL><BLOCKQUOTE CLASS="quote"><FONT COLOR=purple>Unix:</FONT>��
251
145
The following environment variables are also consulted:
252
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
253
<TT><B>LC_CTYPE</B></TT><DD CLASS="dd-description"> If set to <TT>iso_8859_1</TT>, accented characters (from the
146
<DL CLASS="description"><DT CLASS="dt-description">
147
<B><TT>LC_CTYPE</TT></B></DT><DD CLASS="dd-description"> If set to <TT>iso_8859_1</TT>, accented characters (from the
254
148
ISO Latin-1 character set) in string and character literals are
255
149
printed as is; otherwise, they are printed as decimal escape sequences
256
(<TT>\</TT><I>ddd</I>).<BR>
258
<DT CLASS="dt-description"><TT><B>TERM</B></TT><DD CLASS="dd-description"> When printing error messages, the toplevel system
150
(<TT>\</TT><I>ddd</I>).</DD><DT CLASS="dt-description"><TT><B>TERM</B></TT></DT><DD CLASS="dd-description"> When printing error messages, the toplevel system
259
151
attempts to underline visually the location of the error. It
260
152
consults the <TT>TERM</TT> variable to determines the type of output terminal
261
and look up its capabilities in the terminal database.<BR>
263
<DT CLASS="dt-description"><TT><B>HOME</B></TT><DD CLASS="dd-description"> Directory where the <TT>.ocamlinit</TT> file is searched.
267
<H2 CLASS="section"><A NAME="htoc110">9.2</A> Toplevel directives</H2>
268
<A NAME="s:toplevel-directives"></A>
269
The following directives control the toplevel behavior, load files in
270
memory, and trace program execution.<BR>
272
<B>Note:</B> all directives start with a <TT>#</TT> (sharp) symbol. This <TT>#</TT>
153
and look up its capabilities in the terminal database.</DD><DT CLASS="dt-description"><TT><B>HOME</B></TT></DT><DD CLASS="dd-description"> Directory where the <TT>.ocamlinit</TT> file is searched.
155
</BLOCKQUOTE><H2 CLASS="section"><A NAME="htoc110">9.2</A>��Toplevel directives</H2><P>
156
<A NAME="s:toplevel-directives"></A></P><P>The following directives control the toplevel behavior, load files in
157
memory, and trace program execution.</P><P><B>Note:</B> all directives start with a <TT>#</TT> (sharp) symbol. This <TT>#</TT>
273
158
must be typed before the directive, and must not be confused with the
274
159
<TT>#</TT> prompt displayed by the interactive loop. For instance,
275
160
typing <TT>#quit;;</TT> will exit the toplevel loop, but typing <TT>quit;;</TT>
276
will result in an “unbound value <TT>quit</TT>” error.
277
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description"><TT><B>#quit;;</B></TT><DD CLASS="dd-description">
278
Exit the toplevel loop and terminate the <TT>ocaml</TT> command.<BR>
280
<DT CLASS="dt-description"><B><TT>#labels </TT><I>bool</I><TT>;;</TT></B><DD CLASS="dd-description">
161
will result in an “unbound value <TT>quit</TT>” error.</P><DL CLASS="description"><DT CLASS="dt-description"><TT><B>#quit;;</B></TT></DT><DD CLASS="dd-description">
162
Exit the toplevel loop and terminate the <TT>ocaml</TT> command.</DD><DT CLASS="dt-description"><B><TT>#labels </TT><I>bool</I><TT>;;</TT></B></DT><DD CLASS="dd-description">
281
163
Ignore labels in function types if argument is <TT>false</TT>, or switch back
282
to default behaviour (commuting style) if argument is <TT>true</TT>.<BR>
284
<DT CLASS="dt-description"><B><TT>#warnings "</TT><I>warning-list</I><TT>";;</TT></B><DD CLASS="dd-description">
285
Enable or disable warnings according to the argument.<BR>
287
<DT CLASS="dt-description"><B><TT>#directory "</TT><I>dir-name</I><TT>";;</TT></B><DD CLASS="dd-description">
164
to default behaviour (commuting style) if argument is <TT>true</TT>.</DD><DT CLASS="dt-description"><B><TT>#warnings "</TT><I>warning-list</I><TT>";;</TT></B></DT><DD CLASS="dd-description">
165
Enable or disable warnings according to the argument.</DD><DT CLASS="dt-description"><B><TT>#directory "</TT><I>dir-name</I><TT>";;</TT></B></DT><DD CLASS="dd-description">
288
166
Add the given directory to the list of directories searched for
289
source and compiled files.<BR>
291
<DT CLASS="dt-description"><B><TT>#cd "</TT><I>dir-name</I><TT>";;</TT></B><DD CLASS="dd-description">
292
Change the current working directory.<BR>
294
<DT CLASS="dt-description"><B><TT>#load "</TT><I>file-name</I><TT>";;</TT></B><DD CLASS="dd-description">
167
source and compiled files.</DD><DT CLASS="dt-description"><B><TT>#cd "</TT><I>dir-name</I><TT>";;</TT></B></DT><DD CLASS="dd-description">
168
Change the current working directory.</DD><DT CLASS="dt-description"><B><TT>#load "</TT><I>file-name</I><TT>";;</TT></B></DT><DD CLASS="dd-description">
295
169
Load in memory a bytecode object file (<TT>.cmo</TT> file) produced by
296
the batch compiler <TT>ocamlc</TT>.<BR>
298
<DT CLASS="dt-description"><B><TT>#use "</TT><I>file-name</I><TT>";;</TT></B><DD CLASS="dd-description">
170
the batch compiler <TT>ocamlc</TT>.</DD><DT CLASS="dt-description"><B><TT>#use "</TT><I>file-name</I><TT>";;</TT></B></DT><DD CLASS="dd-description">
299
171
Read, compile and execute source phrases from the given file.
300
172
This is textual inclusion: phrases are processed just as if
301
173
they were typed on standard input. The reading of the file stops at
302
the first error encountered.<BR>
304
<DT CLASS="dt-description"><B><TT>#install_printer </TT><I>printer-name</I><TT>;;</TT></B><DD CLASS="dd-description">
174
the first error encountered.</DD><DT CLASS="dt-description"><B><TT>#install_printer </TT><I>printer-name</I><TT>;;</TT></B></DT><DD CLASS="dd-description">
305
175
This directive registers the function named <I>printer-name</I> (a
306
176
value path) as a printer for values whose types match the argument
307
177
type of the function. That is, the toplevel loop will call
308
<I>printer-name</I> when it has such a value to print.<BR>
310
The printing function <I>printer-name</I> should have type
311
<FONT COLOR=blue><TT>Format.formatter</TT> <TT>-></TT> <FONT COLOR=maroon><TT><I>t</I></TT></FONT> <TT>-></TT> <TT>unit</TT></FONT>, where <I>t</I> is the
178
<I>printer-name</I> when it has such a value to print.<P>The printing function <I>printer-name</I> should have type
179
<FONT COLOR=blue><TT>Format.formatter</TT> <TT>-></TT> <FONT COLOR=maroon><I>t</I></FONT> <TT>-></TT> <TT>unit</TT></FONT>, where <FONT COLOR=maroon><I>t</I></FONT> is the
312
180
type for the values to be printed, and should output its textual
313
representation for the value of type <I>t</I> on the given formatter,
181
representation for the value of type <FONT COLOR=maroon><I>t</I></FONT> on the given formatter,
314
182
using the functions provided by the <TT>Format</TT> library. For backward
315
183
compatibility, <I>printer-name</I> can also have type
316
<FONT COLOR=maroon><I><TT>t</TT></I></FONT> <FONT COLOR=blue><TT>-></TT> <TT>unit</TT></FONT> and should then output on the standard
317
formatter, but this usage is deprecated.<BR>
319
<DT CLASS="dt-description"><B><TT>#remove_printer </TT><I>printer-name</I><TT>;;</TT></B><DD CLASS="dd-description">
320
Remove the named function from the table of toplevel printers.<BR>
322
<DT CLASS="dt-description"><B><TT>#trace </TT><I>function-name</I><TT>;;</TT></B><DD CLASS="dd-description">
184
<FONT COLOR=maroon><I>t</I></FONT> <FONT COLOR=blue><TT>-></TT> <TT>unit</TT></FONT> and should then output on the standard
185
formatter, but this usage is deprecated.</P></DD><DT CLASS="dt-description"><B><TT>#remove_printer </TT><I>printer-name</I><TT>;;</TT></B></DT><DD CLASS="dd-description">
186
Remove the named function from the table of toplevel printers.</DD><DT CLASS="dt-description"><B><TT>#trace </TT><I>function-name</I><TT>;;</TT></B></DT><DD CLASS="dd-description">
323
187
After executing this directive, all calls to the function named
324
<I>function-name</I> will be “traced”. That is, the argument and the
188
<I>function-name</I> will be “traced”. That is, the argument and the
325
189
result are displayed for each call, as well as the exceptions escaping
326
190
out of the function, raised either by the function itself or by
327
191
another function it calls. If the function is curried, each argument
328
is printed as it is passed to the function.<BR>
330
<DT CLASS="dt-description"><B><TT>#untrace </TT><I>function-name</I><TT>;;</TT></B><DD CLASS="dd-description">
331
Stop tracing the given function.<BR>
333
<DT CLASS="dt-description"><TT><B>#untrace_all;;</B></TT><DD CLASS="dd-description">
334
Stop tracing all functions traced so far.<BR>
336
<DT CLASS="dt-description"><B><TT>#print_depth </TT><I>n</I><TT>;;</TT></B><DD CLASS="dd-description">
192
is printed as it is passed to the function.</DD><DT CLASS="dt-description"><B><TT>#untrace </TT><I>function-name</I><TT>;;</TT></B></DT><DD CLASS="dd-description">
193
Stop tracing the given function.</DD><DT CLASS="dt-description"><B><TT>#untrace_all;;</TT></B></DT><DD CLASS="dd-description">
194
Stop tracing all functions traced so far.</DD><DT CLASS="dt-description"><B><TT>#print_depth </TT><I>n</I><TT>;;</TT></B></DT><DD CLASS="dd-description">
337
195
Limit the printing of values to a maximal depth of <I>n</I>.
338
196
The parts of values whose depth exceeds <I>n</I> are printed as <TT>...</TT>
341
<DT CLASS="dt-description"><B><TT>#print_length </TT><I>n</I><TT>;;</TT></B><DD CLASS="dd-description">
197
(ellipsis).</DD><DT CLASS="dt-description"><B><TT>#print_length </TT><I>n</I><TT>;;</TT></B></DT><DD CLASS="dd-description">
342
198
Limit the number of value nodes printed to at most <I>n</I>.
343
Remaining parts of values are printed as <TT>...</TT> (ellipsis).</DL>
345
<H2 CLASS="section"><A NAME="htoc111">9.3</A> The toplevel and the module system</H2> <A NAME="s:toplevel-modules"></A>
346
Toplevel phrases can refer to identifiers defined in compilation units
199
Remaining parts of values are printed as <TT>...</TT> (ellipsis).</DD></DL><H2 CLASS="section"><A NAME="htoc111">9.3</A>��The toplevel and the module system</H2><P> <A NAME="s:toplevel-modules"></A></P><P>Toplevel phrases can refer to identifiers defined in compilation units
347
200
with the same mechanisms as for separately compiled units: either by
348
201
using qualified names (<TT>Modulename.localname</TT>), or by using
349
the <TT>open</TT> construct and unqualified names (see section <A HREF="manual011.html#s:names">6.3</A>).<BR>
351
However, before referencing another compilation unit, an
202
the <TT>open</TT> construct and unqualified names (see section�<A HREF="manual011.html#s:names">6.3</A>).</P><P>However, before referencing another compilation unit, an
352
203
implementation of that unit must be present in memory.
353
204
At start-up, the toplevel system contains implementations for all the
354
205
modules in the the standard library. Implementations for user modules
355
206
can be entered with the <TT>#load</TT> directive described above. Referencing
356
207
a unit for which no implementation has been provided
357
results in the error “Reference to undefined global `...' ”.<BR>
359
Note that entering <TT>open </TT><I>Mod</I> merely accesses the compiled
208
results in the error “Reference to undefined global `…'�”.</P><P>Note that entering <TT>open </TT><I>Mod</I> merely accesses the compiled
360
209
interface (<TT>.cmi</TT> file) for <I>Mod</I>, but does not load the
361
210
implementation of <I>Mod</I>, and does not cause any error if no
362
211
implementation of <I>Mod</I> has been loaded. The error
363
“reference to undefined global <I>Mod</I>” will occur only when
364
executing a value or module definition that refers to <I>Mod</I>.<BR>
367
<H2 CLASS="section"><A NAME="htoc112">9.4</A> Common errors</H2>
368
This section describes and explains the most frequently encountered
370
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description"><B>Cannot find file <I>filename</I></B><DD CLASS="dd-description">
212
“reference to undefined global <I>Mod</I>” will occur only when
213
executing a value or module definition that refers to <I>Mod</I>.</P><H2 CLASS="section"><A NAME="htoc112">9.4</A>��Common errors</H2><P>This section describes and explains the most frequently encountered
214
error messages.</P><DL CLASS="description"><DT CLASS="dt-description"><B>Cannot find file <I>filename</I></B></DT><DD CLASS="dd-description">
371
215
The named file could not be found in the current directory, nor in the
372
directories of the search path. <BR>
374
If <I>filename</I> has the format <I>mod</I><TT>.cmi</TT>, this
216
directories of the search path. <P>If <I>filename</I> has the format <I>mod</I><TT>.cmi</TT>, this
375
217
means you have referenced the compilation unit <I>mod</I>, but its
376
218
compiled interface could not be found. Fix: compile <I>mod</I><TT>.mli</TT> or
377
<I>mod</I><TT>.ml</TT> first, to create the compiled interface <I>mod</I><TT>.cmi</TT>.<BR>
379
If <I>filename</I> has the format <I>mod</I><TT>.cmo</TT>, this
219
<I>mod</I><TT>.ml</TT> first, to create the compiled interface <I>mod</I><TT>.cmi</TT>.</P><P>If <I>filename</I> has the format <I>mod</I><TT>.cmo</TT>, this
380
220
means you are trying to load with <TT>#load</TT> a bytecode object file that
381
does not exist yet. Fix: compile <I>mod</I><TT>.ml</TT> first.<BR>
383
If your program spans several directories, this error can also appear
221
does not exist yet. Fix: compile <I>mod</I><TT>.ml</TT> first.</P><P>If your program spans several directories, this error can also appear
384
222
because you haven't specified the directories to look into. Fix: use
385
223
the <TT>#directory</TT> directive to add the correct directories to the
388
<DT CLASS="dt-description"><B>This expression has type <I>t</I><SUB>1</SUB>, but is used with type <I>t</I><SUB>2</SUB></B><DD CLASS="dd-description">
389
See section <A HREF="manual022.html#s:comp-errors">8.4</A>.<BR>
391
<DT CLASS="dt-description"><B>Reference to undefined global <I>mod</I></B><DD CLASS="dd-description">
224
search path.</P></DD><DT CLASS="dt-description"><B>This expression has type <I>t</I><SUB>1</SUB>, but is used with type <I>t</I><SUB>2</SUB></B></DT><DD CLASS="dd-description">
225
See section�<A HREF="manual022.html#s:comp-errors">8.4</A>.</DD><DT CLASS="dt-description"><B>Reference to undefined global <I>mod</I></B></DT><DD CLASS="dd-description">
392
226
You have neglected to load in memory an implementation for a module
393
with <TT>#load</TT>. See section <A HREF="#s:toplevel-modules">9.3</A> above.</DL>
395
<H2 CLASS="section"><A NAME="htoc113">9.5</A> Building custom toplevel systems: <TT>ocamlmktop</TT></H2>
396
The <TT>ocamlmktop</TT> command builds Objective Caml toplevels that
397
contain user code preloaded at start-up. <BR>
399
The <TT>ocamlmktop</TT> command takes as argument a set of <TT>.cmo</TT> and <TT>.cma</TT>
227
with <TT>#load</TT>. See section�<A HREF="#s:toplevel-modules">9.3</A> above.</DD></DL><H2 CLASS="section"><A NAME="htoc113">9.5</A>��Building custom toplevel systems: <TT>ocamlmktop</TT></H2><P>The <TT>ocamlmktop</TT> command builds Objective Caml toplevels that
228
contain user code preloaded at start-up. </P><P>The <TT>ocamlmktop</TT> command takes as argument a set of <TT>.cmo</TT> and <TT>.cma</TT>
400
229
files, and links them with the object files that implement the Objective Caml toplevel. The typical use is:
401
<PRE CLASS="verbatim">
402
ocamlmktop -o mytoplevel foo.cmo bar.cmo gee.cmo
403
</PRE>This creates the bytecode file <TT>mytoplevel</TT>, containing the Objective Caml toplevel system, plus the code from the three <TT>.cmo</TT>
230
</P><PRE CLASS="verbatim"> ocamlmktop -o mytoplevel foo.cmo bar.cmo gee.cmo
231
</PRE><P>This creates the bytecode file <TT>mytoplevel</TT>, containing the Objective Caml toplevel system, plus the code from the three <TT>.cmo</TT>
404
232
files. This toplevel is directly executable and is started by:
405
<PRE CLASS="verbatim">
407
</PRE>This enters a regular toplevel loop, except that the code from
233
</P><PRE CLASS="verbatim"> ./mytoplevel
234
</PRE><P>This enters a regular toplevel loop, except that the code from
408
235
<TT>foo.cmo</TT>, <TT>bar.cmo</TT> and <TT>gee.cmo</TT> is already loaded in memory, just as
409
236
if you had typed:
410
<PRE CLASS="verbatim">
237
</P><PRE CLASS="verbatim"> #load "foo.cmo";;
412
238
#load "bar.cmo";;
413
239
#load "gee.cmo";;
414
</PRE>on entrance to the toplevel. The modules <TT>Foo</TT>, <TT>Bar</TT> and <TT>Gee</TT> are
240
</PRE><P>on entrance to the toplevel. The modules <TT>Foo</TT>, <TT>Bar</TT> and <TT>Gee</TT> are
415
241
not opened, though; you still have to do
416
<PRE CLASS="verbatim">
418
</PRE>yourself, if this is what you wish.<BR>
421
<H2 CLASS="section"><A NAME="htoc114">9.6</A> Options</H2>
422
The following command-line options are recognized by <TT>ocamlmktop</TT>.
423
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description"><B><TT>-cclib</TT> <I>libname</I></B><DD CLASS="dd-description">
242
</P><PRE CLASS="verbatim"> open Foo;;
243
</PRE><P>yourself, if this is what you wish.</P><H2 CLASS="section"><A NAME="htoc114">9.6</A>��Options</H2><P>The following command-line options are recognized by <TT>ocamlmktop</TT>.</P><DL CLASS="description"><DT CLASS="dt-description"><B><TT>-cclib</TT> <I>libname</I></B></DT><DD CLASS="dd-description">
424
244
Pass the <TT>-l</TT><I>libname</I> option to the C linker when linking in
425
“custom runtime” mode. See the corresponding option for
426
<TT>ocamlc</TT>, in chapter <A HREF="manual022.html#c:camlc">8</A>.<BR>
428
<DT CLASS="dt-description"><B><TT>-ccopt</TT> <I>option</I></B><DD CLASS="dd-description">
245
“custom runtime” mode. See the corresponding option for
246
<TT>ocamlc</TT>, in chapter�<A HREF="manual022.html#c:camlc">8</A>.</DD><DT CLASS="dt-description"><B><TT>-ccopt</TT> <I>option</I></B></DT><DD CLASS="dd-description">
429
247
Pass the given option to the C compiler and linker, when linking in
430
“custom runtime” mode. See the corresponding option for
431
<TT>ocamlc</TT>, in chapter <A HREF="manual022.html#c:camlc">8</A>.<BR>
433
<DT CLASS="dt-description"><TT><B>-custom</B></TT><DD CLASS="dd-description">
434
Link in “custom runtime” mode. See the corresponding option for
435
<TT>ocamlc</TT>, in chapter <A HREF="manual022.html#c:camlc">8</A>.<BR>
437
<DT CLASS="dt-description"><B><TT>-I</TT> <I>directory</I></B><DD CLASS="dd-description">
248
“custom runtime” mode. See the corresponding option for
249
<TT>ocamlc</TT>, in chapter�<A HREF="manual022.html#c:camlc">8</A>.</DD><DT CLASS="dt-description"><TT><B>-custom</B></TT></DT><DD CLASS="dd-description">
250
Link in “custom runtime” mode. See the corresponding option for
251
<TT>ocamlc</TT>, in chapter�<A HREF="manual022.html#c:camlc">8</A>.</DD><DT CLASS="dt-description"><B><TT>-I</TT> <I>directory</I></B></DT><DD CLASS="dd-description">
438
252
Add the given directory to the list of directories searched for
439
compiled object code files (<TT>.cmo</TT> and <TT>.cma</TT>).<BR>
441
<DT CLASS="dt-description"><B><TT>-o</TT> <I>exec-file</I></B><DD CLASS="dd-description">
253
compiled object code files (<TT>.cmo</TT> and <TT>.cma</TT>).</DD><DT CLASS="dt-description"><B><TT>-o</TT> <I>exec-file</I></B></DT><DD CLASS="dd-description">
442
254
Specify the name of the toplevel file produced by the linker.
443
The default is <TT>a.out</TT>.</DL>
448
<A HREF="manual022.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
449
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
450
<A HREF="manual024.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
255
The default is <TT>a.out</TT>.</DD></DL><HR>
256
<A HREF="manual022.html"><IMG SRC="previous_motif.gif" ALT="Previous"></A>
257
<A HREF="index.html"><IMG SRC="contents_motif.gif" ALT="Up"></A>
258
<A HREF="manual024.html"><IMG SRC="next_motif.gif" ALT="Next"></A>