Skip to content

Commit 52454d3

Browse files
committed
Improve wording in several places in the OCaml manual, mostly in chapter
1. PR#7698
1 parent 6a4cf6d commit 52454d3

File tree

4 files changed

+59
-44
lines changed

4 files changed

+59
-44
lines changed

Changes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ Working version
137137
- GPR#1540: manual, decouple verbatim and toplevel style in code examples
138138
(Florian Angeletti, review by Gabriel Scherer)
139139

140+
- PR#7698, GPR#1545: improve wording in OCaml manual in several places,
141+
mostly in Chapter 1. This addresses the easier changes suggested in the PR.
142+
(Jim Fehrle, review by Florian Angeletti and David Allsopp)
143+
140144
- GPR#1556: manual, add a consistency test for manual references inside
141145
the compiler source code.
142146
(Florian Angeletti, review by Gabriel Scherer)

manual/manual/tutorials/coreexamples.etex

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
This part of the manual is a tutorial introduction to the
66
OCaml language. A good familiarity with programming in a conventional
7-
languages (say, Pascal or C) is assumed, but no prior exposure to
7+
languages (say, C or Java) is assumed, but no prior exposure to
88
functional languages is required. The present chapter introduces the
99
core language. Chapter~\ref{c:moduleexamples} deals with the
1010
module system, chapter~\ref{c:objectexamples} with the
@@ -61,10 +61,10 @@ usual basic data types: booleans, characters, and immutable character strings.
6161
"Hello world";;
6262
\end{caml_example}
6363

64-
Predefined data structures include tuples, arrays, and lists. General
65-
mechanisms for defining your own data structures are also provided.
66-
They will be covered in more details later; for now, we concentrate on lists.
67-
Lists are either given in extension as a bracketed list of
64+
Predefined data structures include tuples, arrays, and lists. There are also
65+
general mechanisms for defining your own data structures, such as records and
66+
variants, which will be covered in more detail later; for now, we concentrate
67+
on lists. Lists are either given in extension as a bracketed list of
6868
semicolon-separated elements, or built from the empty list "[]"
6969
(pronounce ``nil'') by adding elements in front using the "::"
7070
(``cons'') operator.
@@ -79,8 +79,8 @@ explicit handling of pointers: the OCaml compiler silently introduces
7979
pointers where necessary.
8080

8181
As with most OCaml data structures, inspecting and destructuring lists
82-
is performed by pattern-matching. List patterns have the exact same
83-
shape as list expressions, with identifier representing unspecified
82+
is performed by pattern-matching. List patterns have exactly the same
83+
form as list expressions, with identifiers representing unspecified
8484
parts of the list. As an example, here is insertion sort on a list:
8585
\begin{caml_example}{toplevel}
8686
let rec sort lst =
@@ -110,11 +110,16 @@ sort [3.14; 2.718];;
110110
The "sort" function above does not modify its input list: it builds
111111
and returns a new list containing the same elements as the input list,
112112
in ascending order. There is actually no way in OCaml to modify
113-
in-place a list once it is built: we say that lists are {\em immutable}
113+
a list in-place once it is built: we say that lists are {\em immutable}
114114
data structures. Most OCaml data structures are immutable, but a few
115115
(most notably arrays) are {\em mutable}, meaning that they can be
116116
modified in-place at any time.
117117

118+
The OCaml notation for the type of a function with multiple arguments is \\
119+
"arg1_type -> arg2_type -> ... -> return_type". For example,
120+
the type inferred for "insert", "'a -> 'a list -> 'a list", means that "insert"
121+
takes two arguments, an element of any type "'a" and a list with elements of
122+
the same type "'a" and returns a list of the same type.
118123
\section{Functions as values}
119124
\pdfsection{Functions as values}
120125

@@ -204,7 +209,7 @@ With this functional update notation, the record on the left-hand side
204209
of "with" is copied except for the fields on the right-hand side which
205210
are updated.
206211

207-
The declaration of a variant type lists all possible shapes for values
212+
The declaration of a variant type lists all possible forms for values
208213
of that type. Each case is identified by a name, called a constructor,
209214
which serves both for constructing values of the variant type and
210215
inspecting them by pattern-matching. Constructor names are capitalized
@@ -262,7 +267,7 @@ type 'a btree = Empty | Node of 'a * 'a btree * 'a btree;;
262267
\end{caml_example}
263268
This definition reads as follows: a binary tree containing values of
264269
type "'a" (an arbitrary type) is either empty, or is a node containing
265-
one value of type "'a" and two subtrees containing also values of type
270+
one value of type "'a" and two subtrees also containing values of type
266271
"'a", that is, two "'a btree".
267272

268273
Operations on binary trees are naturally expressed as recursive functions
@@ -290,9 +295,9 @@ let rec insert x btree =
290295
Though all examples so far were written in purely applicative style,
291296
OCaml is also equipped with full imperative features. This includes the
292297
usual "while" and "for" loops, as well as mutable data structures such
293-
as arrays. Arrays are either given in extension between "[|" and "|]"
294-
brackets, or allocated and initialized with the "Array.make"
295-
function, then filled up later by assignments. For instance, the
298+
as arrays. Arrays are either created by listing semicolon-separated element
299+
values between "[|" and "|]" brackets, or allocated and initialized with the
300+
"Array.make" function, then filled up later by assignments. For instance, the
296301
function below sums two vectors (represented as float arrays) componentwise.
297302
\begin{caml_example}{toplevel}
298303
let add_vect v1 v2 =
@@ -320,7 +325,7 @@ OCaml has no built-in notion of variable -- identifiers whose current
320325
value can be changed by assignment. (The "let" binding is not an
321326
assignment, it introduces a new identifier with a new scope.)
322327
However, the standard library provides references, which are mutable
323-
indirection cells (or one-element arrays), with operators "!" to fetch
328+
indirection cells, with operators "!" to fetch
324329
the current contents of the reference and ":=" to assign the contents.
325330
Variables can then be emulated by "let"-binding a reference. For
326331
instance, here is an in-place insertion sort over arrays:
@@ -357,9 +362,9 @@ let ( := ) r newval = r.contents <- newval;;
357362
\end{caml_example}
358363

359364
In some special cases, you may need to store a polymorphic function in
360-
a data structure, keeping its polymorphism. Without user-provided
361-
type annotations, this is not allowed, as polymorphism is only
362-
introduced on a global level. However, you can give explicitly
365+
a data structure, keeping its polymorphism. Doing this requires
366+
user-provided type annotations, since polymorphism is only introduced
367+
automatically for global definitions. However, you can explicitly give
363368
polymorphic types to record fields.
364369
\begin{caml_example}{toplevel}
365370
type idref = { mutable id: 'a. 'a -> 'a };;
@@ -374,9 +379,10 @@ g r;;
374379

375380
OCaml provides exceptions for signalling and handling exceptional
376381
conditions. Exceptions can also be used as a general-purpose non-local
377-
control structure. Exceptions are declared with the "exception"
378-
construct, and signalled with the "raise" operator. For instance, the
379-
function below for taking the head of a list uses an exception to
382+
control structure, although this should not be overused since it can
383+
make the code harder to understand. Exceptions are declared with the
384+
"exception" construct, and signalled with the "raise" operator. For instance,
385+
the function below for taking the head of a list uses an exception to
380386
signal the case where an empty list is given.
381387
\begin{caml_example}{toplevel}
382388
exception Empty_list;;
@@ -409,11 +415,12 @@ name_of_binary_digit 0;;
409415
name_of_binary_digit (-1);;
410416
\end{caml_example}
411417

412-
The "with" part is actually a regular pattern-matching on the
413-
exception value. Thus, several exceptions can be caught by one
418+
The "with" part does pattern matching on the
419+
exception value with the same syntax and behavior as "match". Thus,
420+
several exceptions can be caught by one
414421
"try"\ldots"with" construct. Also, finalization can be performed by
415-
trapping all exceptions, performing the finalization, then raising
416-
again the exception:
422+
trapping all exceptions, performing the finalization, then re-raising
423+
the exception:
417424
\begin{caml_example}{toplevel}
418425
let temporarily_set_reference ref newval funct =
419426
let oldval = !ref in
@@ -626,7 +633,12 @@ The source code must be put in a file with extension ".ml". It
626633
consists of a sequence of phrases, which will be evaluated at runtime
627634
in their order of appearance in the source file. Unlike in interactive
628635
mode, types and values are not printed automatically; the program must
629-
call printing functions explicitly to produce some output. Here is a
636+
call printing functions explicitly to produce some output. The ";;" used
637+
in the interactive examples is not required in
638+
source files created for use with OCaml compilers, but can be helpful
639+
to mark the end of a top-level expression unambiguously even when
640+
there are syntax errors.
641+
Here is a
630642
sample standalone program to print Fibonacci numbers:
631643
\begin{verbatim}
632644
(* File fib.ml *)

manual/manual/tutorials/moduleexamples.etex

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,12 @@ module type PRIOQUEUE_WITH_OPT =
178178
\section{Functors}
179179
\pdfsection{Functors}
180180

181-
Functors are ``functions'' from structures to structures. They are used to
182-
express parameterized structures: a structure \var{A} parameterized by a
183-
structure \var{B} is simply a functor \var{F} with a formal parameter
184-
\var{B} (along with the expected signature for \var{B}) which returns
185-
the actual structure \var{A} itself. The functor \var{F} can then be
186-
applied to one or several implementations \nth{B}{1} \ldots \nth{B}{n}
187-
of \var{B}, yielding the corresponding structures
188-
\nth{A}{1} \ldots \nth{A}{n}.
181+
Functors are ``functions'' from modules to modules. Functors let you create
182+
parameterized modules and then provide other modules as parameter(s) to get
183+
a specific implementation. For instance, a "Set" module implementing sets
184+
as sorted lists could be parameterized to work with any module that provides
185+
an element type and a comparison function "compare" (such as "OrderedString"):
189186

190-
For instance, here is a structure implementing sets as sorted lists,
191-
parameterized by a structure providing the type of the set elements
192-
and an ordering function over this type (used to keep the sets
193-
sorted):
194187
\begin{caml_example}{toplevel}
195188
type comparison = Less | Equal | Greater;;
196189
module type ORDERED_TYPE =

manual/manual/tutorials/objectexamples.etex

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22
\label{c:objectexamples}
33
\pdfchapterfold{-15}{Tutorial: Objects in OCaml}
44
%HEVEA\cutname{objectexamples.html}
5-
{\it (Chapter written by J�r�me Vouillon, Didier R�my and Jacques Garrigue)}
5+
{\it (Chapter written by J\'er\^ome Vouillon, Didier R\'emy and Jacques Garrigue)}
66

77
\bigskip
88

99
\noindent This chapter gives an overview of the object-oriented features of
10-
OCaml. Note that the relation between object, class and type
11-
in OCaml is very different from that in mainstream
12-
object-oriented languages like Java or C++, so that you should not
13-
assume that similar keywords mean the same thing.
10+
OCaml.
11+
12+
Note that the relationship between object, class and type in OCaml is
13+
different than in mainstream object-oriented languages such as Java and
14+
C++, so you shouldn't assume that similar keywords mean the same thing.
15+
Object-oriented features are used much less frequently in OCaml than
16+
in those languages. OCaml has alternatives that are often more appropriate,
17+
such as modules and functors. Indeed, many OCaml programs do not use objects
18+
at all.
19+
1420

1521
\begin{htmlonly}
1622

@@ -70,7 +76,7 @@ automatically defined by the class definition above. It stands for the
7076
object type "<get_x : int; move : int -> unit>", listing the methods
7177
of class "point" along with their types.
7278

73-
We now invoke some methods to "p":
79+
We now invoke some methods of "p":
7480
\begin{caml_example}{toplevel}
7581
p#get_x;;
7682
p#move 3;;
@@ -219,7 +225,7 @@ in sections \ref{ss:reference-to-self} and \ref{ss:parameterized-classes}.
219225
\pdfsection{Reference to self}
220226
\label{ss:reference-to-self}
221227

222-
A method or an initializer can send messages to self (that is,
228+
A method or an initializer can invoke methods on self (that is,
223229
the current object). For that, self must be explicitly bound, here to
224230
the variable "s" ("s" could be any identifier, even though we will
225231
often choose the name "self".)

0 commit comments

Comments
 (0)