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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset= ISO-8859-1">
<TITLE>
Module expressions (module implementations)
</TITLE>
</HEAD>
<BODY >
<A HREF="manual016.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual018.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual006.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<HR>
<H2>5.11 Module expressions (module implementations)</H2> <A NAME="s:module-expr"></A>
Module expressions are the module-level equivalent of value
expressions: they evaluate to modules, thus providing implementations
for the specifications expressed in module types.<A NAME="@manual.kwd137"></A><BR>
<BR>
<A NAME="@manual.kwd138"></A>
<A NAME="@manual.kwd139"></A>
<A NAME="@manual.kwd140"></A>
<A NAME="@manual.kwd141"></A>
<A NAME="@manual.kwd142"></A>
<A NAME="@manual.kwd143"></A>
<A NAME="@manual.kwd144"></A>
<A NAME="@manual.kwd145"></A><BR>
<BR>
<DIV ALIGN=center>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD ALIGN=right NOWRAP>
<TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<TT><I><FONT COLOR=maroon>module-path</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>struct</FONT></TT> { <TT><I><FONT COLOR=maroon>definition</FONT></I></TT> [<TT><FONT COLOR=blue>;;</FONT></TT>] } <TT><FONT COLOR=blue>end</FONT></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>functor</FONT></TT> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT> <TT><FONT COLOR=blue>-></FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP>
<TT><I><FONT COLOR=maroon>definition</FONT></I></TT></TD>
<TD ALIGN=right NOWRAP>::=</TD>
<TD ALIGN=left NOWRAP>
<TT><FONT COLOR=blue>let</FONT></TT> [<TT><FONT COLOR=blue>rec</FONT></TT>] <TT><I><FONT COLOR=maroon>let-binding</FONT></I></TT> { <TT><FONT COLOR=blue>and</FONT></TT> <TT><I><FONT COLOR=maroon>let-binding</FONT></I></TT> }</TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>external</FONT></TT> <TT><I><FONT COLOR=maroon>value-name</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>external-declaration</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>type-definition</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>exception-definition</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>class-definition</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><I><FONT COLOR=maroon>classtype-definition</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>module</FONT></TT> <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> { <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT> }
[ <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT> ] <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>module</FONT></TT> <TT><FONT COLOR=blue>type</FONT></TT> <TT><I><FONT COLOR=maroon>modtype-name</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT></TD>
</TR>
<TR><TD ALIGN=right NOWRAP> </TD>
<TD ALIGN=right NOWRAP>|</TD>
<TD ALIGN=left NOWRAP> <TT><FONT COLOR=blue>open</FONT></TT> <TT><I><FONT COLOR=maroon>module-path</FONT></I></TT></TD>
</TR></TABLE></DIV><BR>
<H3>5.11.1 Simple module expressions</H3>The expression <TT><I><FONT COLOR=maroon>module-path</FONT></I></TT> evaluates to the module bound to the name
<TT><I><FONT COLOR=maroon>module-path</FONT></I></TT>.<BR>
<BR>
The expression <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT> evaluates to the same module as
<TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT>.<BR>
<BR>
The expression <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT> checks that the
type of <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> is a subtype of <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT>, that is, that all
components specified in <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT> are implemented in
<TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT>, and their implementation meets the requirements given
in <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT>. In other terms, it checks that the implementation
<TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> meets the type specification <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT>. The whole
expression evaluates to the same module as <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT>, except that
all components not specified in <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT> are hidden and can no
longer be accessed.<BR>
<BR>
<H3>5.11.2 Structures</H3><A NAME="@manual.kwd146"></A>
<A NAME="@manual.kwd147"></A>Structures <TT><FONT COLOR=blue>struct</FONT></TT> ... <TT><FONT COLOR=blue>end</FONT></TT> are collections of definitions for
value names, type names, exceptions, module names and module type
names. The definitions are evaluated in the order in which they appear
in the structure. The scope of the bindings performed by the
definitions extend to the end of the structure. As a consequence, a
definition may refer to names bound by earlier definitions in the same
structure.<BR>
<BR>
For compatibility with toplevel phrases (chapter <A HREF="manual021.html#c:camllight">8</A>)
and with Caml Light, an optional <TT>;;</TT> is allowed after each
definition in a structure. The <TT>;;</TT> has no semantic meaning. Also for
compatibility, <TT><FONT COLOR=blue>;;</FONT></TT> <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> is allowed as a component of a structure,
meaning <TT><FONT COLOR=blue>let</FONT></TT> <TT><FONT COLOR=blue>_</FONT></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>expr</FONT></I></TT>, i.e. evaluate <TT><I><FONT COLOR=maroon>expr</FONT></I></TT> for its side-effects.<BR>
<BR>
<H4>Value definitions</H4><A NAME="@manual.kwd148"></A>A value definition <TT><FONT COLOR=blue>let</FONT></TT> [<TT><FONT COLOR=blue>rec</FONT></TT>] <TT><I><FONT COLOR=maroon>let-binding</FONT></I></TT> { <TT><FONT COLOR=blue>and</FONT></TT> <TT><I><FONT COLOR=maroon>let-binding</FONT></I></TT> }
bind value names in the same way as a <TT><FONT COLOR=blue>let</FONT></TT> ... <TT><FONT COLOR=blue>in</FONT></TT> ... expression
(see section <A HREF="manual013.html#s:localdef">5.7.1</A>). The value names appearing in the
left-hand sides of the bindings are bound to the corresponding values
in the right-hand sides.<A NAME="@manual.kwd149"></A><BR>
<BR>
A value definition <TT><FONT COLOR=blue>external</FONT></TT> <TT><I><FONT COLOR=maroon>value-name</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>typexpr</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>external-declaration</FONT></I></TT>
implements <TT><I><FONT COLOR=maroon>value-name</FONT></I></TT> as the external function specified in
<TT><I><FONT COLOR=maroon>external-declaration</FONT></I></TT> (see chapter <A HREF="manual028.html#c:intf-c">15</A>).<BR>
<BR>
<H4>Type definitions</H4><A NAME="@manual.kwd150"></A>A definition of one or several type components is written
<TT><FONT COLOR=blue>type</FONT></TT> <TT><I><FONT COLOR=maroon>typedef</FONT></I></TT> { <TT><FONT COLOR=blue>and</FONT></TT> <TT><I><FONT COLOR=maroon>typedef</FONT></I></TT> } and consists of a sequence
of mutually recursive definitions of type names.<BR>
<BR>
<H4>Exception definitions</H4><A NAME="@manual.kwd151"></A>Exceptions are defined with the syntax <TT><FONT COLOR=blue>exception</FONT></TT> <TT><I><FONT COLOR=maroon>constr-decl</FONT></I></TT>.<BR>
<BR>
<H4>Class definitions</H4>A definition of one or several classes is written <TT><FONT COLOR=blue>class</FONT></TT>
<TT><I><FONT COLOR=maroon>class-binding</FONT></I></TT> { <TT><FONT COLOR=blue>and</FONT></TT> <TT><I><FONT COLOR=maroon>class-binding</FONT></I></TT> } and consists of a sequence of
mutually recursive definitions of class names. Class definitions are
described more precisely in section <A HREF="manual015.html#s:classdef">5.9.3</A>.<BR>
<BR>
<H4>Class type definitions</H4>A definition of one or several classes is written
<TT><FONT COLOR=blue>class</FONT></TT> <TT><FONT COLOR=blue>type</FONT></TT> <TT><I><FONT COLOR=maroon>classtype-def</FONT></I></TT> { <TT><FONT COLOR=blue>and</FONT></TT> <TT><I><FONT COLOR=maroon>classtype-def</FONT></I></TT> } and consists of
a sequence of mutually recursive definitions of class type names.
Class type definitions are described more precisely in
section <A HREF="manual015.html#s:classtype">5.9.5</A>.<BR>
<BR>
<H4>Module definitions</H4><A NAME="@manual.kwd152"></A>The basic form for defining a module component is
<TT><FONT COLOR=blue>module</FONT></TT> <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT>, which evaluates <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> and binds
the result to the name <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT>.<BR>
<BR>
One can write
<DIV ALIGN=center>
<TT><FONT COLOR=blue>module</FONT></TT> <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT>
</DIV>
instead of
<DIV ALIGN=center>
<TT><FONT COLOR=blue>module</FONT></TT> <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT>.
</DIV>
Another derived form is
<DIV ALIGN=center>
<TT><FONT COLOR=blue>module</FONT></TT> <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>name</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>)</FONT></TT> ...
<TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>name</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT><SUB><FONT SIZE=2><I>n</I></FONT></SUB> <TT><FONT COLOR=blue>)</FONT></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT>
</DIV>
which is equivalent to
<DIV ALIGN=center>
<TT><FONT COLOR=blue>module</FONT></TT> <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT>
<TT><FONT COLOR=blue>functor</FONT></TT> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>name</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>)</FONT></TT> <TT><FONT COLOR=blue>-></FONT></TT> ...
<TT><FONT COLOR=blue>-></FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT>
</DIV><BR>
<H4>Module type definitions</H4><A NAME="@manual.kwd153"></A>
<A NAME="@manual.kwd154"></A>A definition for a module type is written
<TT><FONT COLOR=blue>module</FONT></TT> <TT><FONT COLOR=blue>type</FONT></TT> <TT><I><FONT COLOR=maroon>modtype-name</FONT></I></TT> <TT><FONT COLOR=blue>=</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT>.
It binds the name <TT><I><FONT COLOR=maroon>modtype-name</FONT></I></TT> to the module type denoted by the
expression <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT>.<BR>
<BR>
<H4>Opening a module path</H4><A NAME="@manual.kwd155"></A>The expression <TT><FONT COLOR=blue>open</FONT></TT> <TT><I><FONT COLOR=maroon>module-path</FONT></I></TT> in a structure does not define any
components nor perform any bindings. It simply affects the parsing of
the following items of the structure, allowing components of the
module denoted by <TT><I><FONT COLOR=maroon>module-path</FONT></I></TT> to be referred to by their simple names
<TT><I><FONT COLOR=maroon>name</FONT></I></TT> instead of path accesses <TT><I><FONT COLOR=maroon>module-path</FONT></I></TT> <TT><FONT COLOR=blue>.</FONT></TT> <TT><I><FONT COLOR=maroon>name</FONT></I></TT>. The scope of
the <TT>open</TT> stops at the end of the structure expression.<BR>
<BR>
<H3>5.11.3 Functors</H3>
<H4>Functor definition</H4><A NAME="@manual.kwd156"></A>The expression <TT><FONT COLOR=blue>functor</FONT></TT> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> <TT><FONT COLOR=blue>:</FONT></TT> <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT> <TT><FONT COLOR=blue>)</FONT></TT> <TT><FONT COLOR=blue>-></FONT></TT>
<TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> evaluates to a functor that takes as argument modules of
the type <TT><I><FONT COLOR=maroon>module-type</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>, binds <TT><I><FONT COLOR=maroon>module-name</FONT></I></TT> to these modules,
evaluates <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT> in the extended environment, and returns the
resulting modules as results. No restrictions are placed on the type of the
functor argument; in particular, a functor may take another functor as
argument (``higher-order'' functor).<BR>
<BR>
<H4>Functor application</H4>The expression <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> <TT><FONT COLOR=blue>(</FONT></TT> <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB> <TT><FONT COLOR=blue>)</FONT></TT> evaluates
<TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB> to a functor and <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB> to a module, and
applies the former to the latter. The type of <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT><SUB><FONT SIZE=2>2</FONT></SUB> must
match the type expected for the arguments of the functor <TT><I><FONT COLOR=maroon>module-expr</FONT></I></TT><SUB><FONT SIZE=2>1</FONT></SUB>.<BR>
<BR>
<BR>
<BR>
<HR>
<A HREF="manual016.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual018.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual006.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
</BODY>
</HTML>
|