1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
\section{s:modtypes}{Module types (module specifications)}
%HEVEA\cutname{modtypes.html}
Module types are the module-level equivalent of type expressions: they
specify the general shape and type properties of modules.
\ikwd{sig\@\texttt{sig}}
\ikwd{end\@\texttt{end}}
\ikwd{functor\@\texttt{functor}}
\ikwd{with\@\texttt{with}}
\ikwd{and\@\texttt{and}}
\ikwd{val\@\texttt{val}}
\ikwd{external\@\texttt{external}}
\ikwd{type\@\texttt{type}}
\ikwd{exception\@\texttt{exception}}
\ikwd{class\@\texttt{class}}
\ikwd{module\@\texttt{module}}
\ikwd{open\@\texttt{open}}
\ikwd{include\@\texttt{include}}
\begin{syntax}
module-type:
modtype-path
| 'sig' { specification [';;'] } 'end'
| 'functor' '(' module-name ':' module-type ')' '->' module-type
| module-type '->' module-type
| module-type 'with' mod-constraint { 'and' mod-constraint }
| '(' module-type ')'
;
mod-constraint:
'type' [type-params] typeconstr type-equation { type-constraint }
| 'module' module-path '=' extended-module-path
;
%BEGIN LATEX
\end{syntax}
\begin{syntax}
%END LATEX
specification:
'val' value-name ':' typexpr
| 'external' value-name ':' typexpr '=' external-declaration
| type-definition
| 'exception' constr-decl
| class-specification
| classtype-definition
| 'module' module-name ':' module-type
| 'module' module-name { '(' module-name ':' module-type ')' }
':' module-type
| 'module' 'type' modtype-name
| 'module' 'type' modtype-name '=' module-type
| 'open' module-path
| 'include' module-type
\end{syntax}
See also the following language extensions:
\hyperref[s:module-type-of]{recovering the type of a module},
\hyperref[s:signature-substitution]{substitution inside a signature},
\hyperref[s:module-alias]{type-level module aliases},
\hyperref[s:attributes]{attributes},
\hyperref[s:extension-nodes]{extension nodes},
\hyperref[s:generative-functors]{generative functors},
and \hyperref[ss:module-type-substitution]{module type substitutions}.
\subsection{ss:mty-simple}{Simple module types}
The expression @modtype-path@ is equivalent to the module type bound
to the name @modtype-path@.
The expression @'(' module-type ')'@ denotes the same type as
@module-type@.
\subsection{ss:mty-signatures}{Signatures}
\ikwd{sig\@\texttt{sig}}
\ikwd{end\@\texttt{end}}
Signatures are type specifications for structures. Signatures
@'sig' \ldots 'end'@ are collections of type specifications for value
names, type names, exceptions, module names and module type names. A
structure will match a signature if the structure provides definitions
(implementations) for all the names specified in the signature (and
possibly more), and these definitions meet the type requirements given
in the signature.
An optional @";;"@ is allowed after each specification in a
signature. It serves as a syntactic separator with no semantic
meaning.
\subsubsection*{sss:mty-values}{Value specifications}
\ikwd{val\@\texttt{val}}
A specification of a value component in a signature is written
@'val' value-name ':' typexpr@, where @value-name@ is the name of the
value and @typexpr@ its expected type.
\ikwd{external\@\texttt{external}}
The form @'external' value-name ':' typexpr '=' external-declaration@
is similar, except that it requires in addition the name to be
implemented as the external function specified in @external-declaration@
(see chapter~\ref{c:intf-c}).
\subsubsection*{sss:mty-type}{Type specifications}
\ikwd{type\@\texttt{type}}
A specification of one or several type components in a signature is
written @'type' typedef { 'and' typedef }@ and consists of a sequence
of mutually recursive definitions of type names.
Each type definition in the signature specifies an optional type
equation @'=' typexpr@ and an optional type representation
@'=' constr-decl \ldots@ or @'=' '{' field-decl \ldots '}'@.
The implementation of the type name in a matching structure must
be compatible with the type expression specified in the equation (if
given), and have the specified representation (if given). Conversely,
users of that signature will be able to rely on the type equation
or type representation, if given. More precisely, we have the
following four situations:
\begin{description}
\item[Abstract type: no equation, no representation.] ~ \\
Names that are defined as abstract types in a signature can be
implemented in a matching structure by any kind of type definition
(provided it has the same number of type parameters). The exact
implementation of the type will be hidden to the users of the
structure. In particular, if the type is implemented as a variant type
or record type, the associated constructors and fields will not be
accessible to the users; if the type is implemented as an
abbreviation, the type equality between the type name and the
right-hand side of the abbreviation will be hidden from the users of the
structure. Users of the structure consider that type as incompatible
with any other type: a fresh type has been generated.
\item[Type abbreviation: an equation @'=' typexpr@, no representation.] ~ \\
The type name must be implemented by a type compatible with @typexpr@.
All users of the structure know that the type name is
compatible with @typexpr@.
\item[New variant type or record type: no equation, a representation.] ~ \\
The type name must be implemented by a variant type or record type
with exactly the constructors or fields specified. All users of the
structure have access to the constructors or fields, and can use them
to create or inspect values of that type. However, users of the
structure consider that type as incompatible with any other type: a
fresh type has been generated.
\item[Re-exported variant type or record type: an equation,
a representation.] ~ \\
This case combines the previous two: the representation of the type is
made visible to all users, and no fresh type is generated.
\end{description}
\subsubsection*{sss:mty-exn}{Exception specification}
\ikwd{exception\@\texttt{exception}}
The specification @'exception' constr-decl@ in a signature requires the
matching structure to provide an exception with the name and arguments
specified in the definition, and makes the exception available to all
users of the structure.
\subsubsection*{sss:mty-class}{Class specifications}
\ikwd{class\@\texttt{class}}
A specification of one or several classes in a signature is written
@'class' class-spec { 'and' class-spec }@ and consists of a sequence
of mutually recursive definitions of class names.
Class specifications are described more precisely in
section~\ref{ss:class-spec}.
\subsubsection*{sss:mty-classtype}{Class type specifications}
\ikwd{class\@\texttt{class}}
\ikwd{type\@\texttt{type}}
A specification of one or several class types in a signature is
written @'class' 'type' classtype-def@ @{ 'and' classtype-def }@ and
consists of a sequence of mutually recursive definitions of class type
names. Class type specifications are described more precisely in
section~\ref{ss:classtype}.
\subsubsection*{sss:mty-module}{Module specifications}
\ikwd{module\@\texttt{module}}
A specification of a module component in a signature is written
@'module' module-name ':' module-type@, where @module-name@ is the
name of the module component and @module-type@ its expected type.
Modules can be nested arbitrarily; in particular, functors can appear
as components of structures and functor types as components of
signatures.
For specifying a module component that is a functor, one may write
\begin{center}
@'module' module-name '(' name_1 ':' module-type_1 ')'
\ldots '(' name_n ':' module-type_n ')'
':' module-type@
\end{center}
instead of
\begin{center}
@'module' module-name ':'
'functor' '(' name_1 ':' module-type_1 ')' '->' \ldots
'->' module-type@
\end{center}
\subsubsection*{sss:mty-mty}{Module type specifications}
\ikwd{type\@\texttt{type}}
\ikwd{module\@\texttt{module}}
A module type component of a signature can be specified either as a
manifest module type or as an abstract module type.
An abstract module type specification
@'module' 'type' modtype-name@ allows the name @modtype-name@ to be
implemented by any module type in a matching signature, but hides the
implementation of the module type to all users of the signature.
A manifest module type specification
@'module' 'type' modtype-name '=' module-type@
requires the name @modtype-name@ to be implemented by the module type
@module-type@ in a matching signature, but makes the equality between
@modtype-name@ and @module-type@ apparent to all users of the signature.
\subsubsection{sss:mty-open}{Opening a module path}
\ikwd{open\@\texttt{open}}
The expression @'open' module-path@ in a signature does not specify
any components. It simply affects the parsing of the following items
of the signature, allowing components of the module denoted by
@module-path@ to be referred to by their simple names @name@ instead of
path accesses @module-path '.' name@. The scope of the @"open"@
stops at the end of the signature expression.
\subsubsection{sss:mty-include}{Including a signature}
\ikwd{include\@\texttt{include}}
The expression @'include' module-type@ in a signature performs textual
inclusion of the components of the signature denoted by @module-type@.
It behaves as if the components of the included signature were copied
at the location of the @'include'@. The @module-type@ argument must
refer to a module type that is a signature, not a functor type.
\subsection{ss:mty-functors}{Functor types}
\ikwd{functor\@\texttt{functor}}
The module type expression
@'functor' '(' module-name ':' module-type_1 ')' '->' module-type_2@
is the type of functors (functions from modules to modules) that take
as argument a module of type @module-type_1@ and return as result a
module of type @module-type_2@. The module type @module-type_2@ can
use the name @module-name@ to refer to type components of the actual
argument of the functor. If the type @module-type_2@ does not
depend on type components of @module-name@, the module type expression
can be simplified with the alternative short syntax
@ module-type_1 '->' module-type_2 @.
No restrictions are placed on the type of the functor argument; in
particular, a functor may take another functor as argument
(``higher-order'' functor).
When the result module type is itself a functor,
\begin{center}
@'functor' '(' name_1 ':' module-type_1 ')' '->' \ldots '->'
'functor' '(' name_n ':' module-type_n ')' '->' module-type@
\end{center}
one may use the abbreviated form
\begin{center}
@'functor' '(' name_1 ':' module-type_1 ')' \ldots
'(' name_n ':' module-type_n ')' '->' module-type@
\end{center}
\subsection{ss:mty-with}{The "with" operator}
\ikwd{with\@\texttt{with}}
Assuming @module-type@ denotes a signature, the expression
@module-type 'with' mod-constraint@ @{ 'and' mod-constraint }@ denotes
the same signature where type equations have been added to some of the
type specifications, as described by the constraints following the
"with" keyword. The constraint @'type' [type-parameters] typeconstr
'=' typexpr@ adds the type equation @'=' typexpr@ to the specification
of the type component named @typeconstr@ of the constrained signature.
The constraint @'module' module-path '=' extended-module-path@ adds
type equations to all type components of the sub-structure denoted by
@module-path@, making them equivalent to the corresponding type
components of the structure denoted by @extended-module-path@.
For instance, if the module type name "S" is bound to the signature
\begin{verbatim}
sig type t module M: (sig type u end) end
\end{verbatim}
then "S with type t=int" denotes the signature
\begin{verbatim}
sig type t=int module M: (sig type u end) end
\end{verbatim}
and "S with module M = N" denotes the signature
\begin{verbatim}
sig type t module M: (sig type u=N.u end) end
\end{verbatim}
A functor taking two arguments of type "S" that share their "t" component
is written
\begin{verbatim}
functor (A: S) (B: S with type t = A.t) ...
\end{verbatim}
Constraints are added left to right. After each constraint has been
applied, the resulting signature must be a subtype of the signature
before the constraint was applied. Thus, the @'with'@ operator can
only add information on the type components of a signature, but never
remove information.
|