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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
<html>
<head>
<title>
Mercury Coding Standard for the Mercury Project
</title>
</head>
<body
bgcolor="#ffffff"
text="#000000"
>
<hr>
<!-------------------------->
This file contains some very old notes about the module system.
<hr>
<!-------------------------->
I've designed my module system. It's based on a Prolog-syntax (but
not otherwise backwards compatible) strongly-typed logic programming
language. I'd be interested in any comments people have about it.
<p>
Note: currently I don't allow nested modules. I want that to change
eventually, but this will suffice for a first implementation.
<p>
<hr>
<!-------------------------->
<p>
The module system has two primary purposes: to prevent accidental name
clashes, and to enforce encapsulation. (Although there are other benefits,
such as encouraging more well-structured software.)
<p>
Accidental name clashes are prevented by using a standard naming
convention where every symbol is prefixed by the name of the module in
which it is defined. This module qualifier may be omitted for symbols
in the current namespace (i.e. symbols in the current module, or
symbols which have been explicitly imported into the current namespace)
so long as no ambiguity results.
<p>
Encapsulation is enforced by only allowing symbols which have been
explicitly exported (eg. those in a module's interface section) to be
used by other modules.
<p>
<hr>
<!-------------------------->
Each module must consist of
<pre>
:- module ModuleName.
:- interface.
...
:- implementation.
...
:- end_module ModuleName. % optional
</pre>
The semantics of these declarations is as follows.
<dl>
<dt> :- module ModuleName.
<dd>
Every module must start with a declaration of this form.
(There is an implementation-dependant mapping from
module names to filenames which must be adhered to.
The mapping may be arbitrary complex - for example, the
implementation may allow the user to list this mapping
in a configuration file - but in general, the name of the
file that a module resides in will be just the name
of the module plus an appropriate extensions.
The mapping is used by the implementation so that it
can determine where to find the required modules.)
<dt> :- use_module Module1, Module2, ..., ModuleN.
<dd>
The exported symbols in the listed modules are made
available for use in the current module. These symbols must
still be qualified with a module name, for example
list:append(A,B,C)
in order to be used.
<dt> :- import_module Module1, Module2, ..., ModuleN.
<dd>
As for `use_module', except that symbols are imported into the
current namespace and do not need to be qualified with a module name
except to avoid ambiguities.
(Use of this declaration in large programs is probably a bad idea,
but it may be useful for small programs. Also, it would probably
not be a good idea to use this declaration together with low-level
export directives [described below] that don't specify an
explicit module qualifier, because it could cause accidental
re-exporting of imported symbols.)
<dt> :- export_module Module1, Module2, ..., ModuleN.
<dd>
All exported symbols in the specified modules are re-exported
from this module. This declaration can only occur in the
interface section.
<dt> :- interface.
<dd>
All exportable declarations (ie. all type, predicate, constructor, and
operator declarations) in the interface section
are automatically exported.
<dt> :- implementation.
<dd>
Declarations and definitions in the implementation section
are by default private.
<dt> :- end_module ModuleName.
<dd>
Syntactic sugar.
This declaration is optional, but if it occurs it must be the
last declaration in the file and the ModuleName must match with
that specified in the `module' declaration.
</dl>
<hr>
<!-------------------------->
<p>
The module system also has the following lower-level declarations,
which allow you to import, use, or export individual symbols rather
than entire modules.
They can be used instead of (or in conjunction with) the higher-level
declarations.
All of the export declarations can only occur in the interface
section.
<pre>
:- export_sym Sym1, Sym2, ..., SumN.
Sym1, Sym2, ..., SymN may be any symbol specifiers.
All symbols matching those symbol specifiers are exported.
A symbol specifier is one of
SymbolNameSpecifier
Matches any symbol matched by the SymbolNameSpecifier.
TypedConstructorSpecifier
Matches any constructors matched by the
TypedConstructorSpecifier.
cons(ConstructorSpecifier)
Matches only constructors.
pred(PredicateSpecifier)
Matches only predicates.
func(FunctionSpecifier)
Matches only functions.
adt(SymbolNameSpecifier)
Matches only type names.
type(SymbolNameSpecifier)
Matches type names matched by the SymbolNameSpecifier,
and also matches any constructors for the matched type
names.
op(OpSpecifier)
Matches any operators matched by OpSpecifier.
module(ModuleSpecifier)
Matches all symbols exported from the specified module.
module_ops(ModuleSpecifier)
Matches all operators exported from the specified
module. (This includes any entity exported from
the module which has the same name as an operator
declared in the module.)
A ConstructorSpecifier is one of
SymbolSpecifier
TypedConstructorSpecifier
A TypedConstructorSpecifier is one of
SymbolSpecifier::Type
Matches only constructors with the specified result
type.
SymbolName(ArgType1, ..., ArgTypeN)
Matches only constructors with the specified argument
types.
SymbolName(ArgType1, ..., ArgTypeN)::Type
Matches only constructors with the specified argument
and result types.
A PredicateSpecifier is one of
SymbolName(ArgType1, ..., ArgTypeN)
Matches only predicates with the specified argument
types.
SymbolSpecifier
A FunctionSpecifier is the same as a ConstructorSpecifier.
An OpSpecifier is one of
SymbolSpecifier
Matches any operators matched by SymbolSpecifier
infix(SymbolSpecifier)
Matches only infix operators
postfix(SymbolSpecifier)
Matches only postfix operators
prefix(SymbolSpecifier)
Matches only prefix operators
A SymbolSpecifier is one of
SymbolName
SymbolName/Arity
Matches only symbols of the specified arity.
A SymbolName is one of
Name
Matches symbols with the specified name in the
current namespace.
ModuleSpecifier:Name
Matches symbols with the specified name exported
by the specified module.
A ModuleSpecifier is just an identifier.
:- export_pred Pred1, ..., PredN.
This is just an abbreviation for
:- export_sym pred(Pred1), ..., pred(PredN).
All predicates matched by the listed PredicateSpecifiers are exported.
If there is any ambiguity, *all* matching predicates are exported.
:- export_func Func1, ..., FuncN.
This is just an abbreviation for
:- export_sym func(Func1), ..., func(FuncN).
All predicates matched by the listed FunctionSpecifiers are exported.
If there is any ambiguity, *all* matching predicates are exported.
:- export_adt Type1, ..., TypeN.
This is just an abbreviation for
:- export_sym adt(Type1), ..., adt(TypeN).
The listed types are exported. Constructors for these types
are not exported, so this can be used to create abstract data types,
hence the name. (However this declaration does not prevent
the constructors from being exported by other declarations.)
Type1, ..., TypeN can be any SymbolSpecifiers.
:- export_cons Cons1, ..., ConsN.
This is just an abbreviation for
:- export_sym cons(Cons1), ..., cons(ConsN).
:- export_op Op1, ..., OpN.
This is just an abbreviation for
:- export_sym op(Op1), ..., op(OpN).
:- export_type Type1, ..., TypeN.
This is just an abbreviation for
:- export_sym type(Type1), ..., type(TypeN).
The listed types and all the constructors for the listed types
are exported.
:- export_module_ops
This is just an abbreviation for
:- export_sym module_ops(Op1), ..., op(OpN).
:- import_module_ops Module1, ..., ModuleN.
This is just an abbreviation for
:- export_sym module_ops(Module1), ..., module_ops(ModuleN).
:- export_module_ops Module1, Module2, ..., ModuleN.
All exported operators in the specified modules are re-exported
from this module. This declaration can only occur in the
interface section.
:- import_sym Sym1, Sym2, ..., SumN.
The listed symbols are imported, ie. made available for use
in the current module.
Sym1, ..., SymN may be any symbol specifiers, but they
should have explicit module qualifications.
:- import_pred Pred1, Pred2, ..., PredN.
:- import_func Func1, Func2, ..., FuncN.
:- import_type Type1, Type2, ..., TypeN.
:- import_adt Type1, Type2, ..., TypeN.
:- import_cons Cons1, Cons2, ..., ConsN.
:- import_op Op1, Op2, ..., OpN.
Abbreviations for special cases of import_sym.
:- use_sym Sym1, Sym2, ..., SumN.
The listed symbols are imported into the current namespace,
i.e. made available for use without explicit module
qualification, exactly as for `use_module'.
Sym1, ..., SymN may be any symbol specifiers, but they should
have explicit module qualifications.
:- use_pred Pred1, Pred2, ..., PredN.
:- use_func Func1, Func1, ..., Func1.
:- use_type Type1, Type2, ..., TypeN.
:- use_adt Type1, Type2, ..., TypeN.
:- use_cons Cons1, Cons2, ..., ConsN.
:- use_op Op1, Op2, ..., OpN.
Abbreviations for special cases of use_sym.
</pre>
<p>
Note that although there are quite a few constructs, most of these
are syntactic sugar. The first phase of processing fully qualifies
all symbols, and translates all the module system declarations into
just `module', `import_sym', and `export_sym'.
<p>
<pre>
%-----------------------------------------------------------------------------%
SOME EXAMPLE MODULES
%-----------------------------------------------------------------------------%
% Version using explicit export declaration.
% Bag ADT.
% Implemented as unsorted lists.
:- module bag.
:- interface.
:- export_pred init, insert, contains.
:- implementation.
:- import_module list.
:- type bag(T) = list(T).
:- pred init(bag(_)).
init([]).
:- pred insert(bag(T), T, bag(T)).
insert(Bag0, Item, [Item|Bag0]).
:- pred contains(bag(T), T).
contains(Bag, Item) :-
list:member(Item, Bag).
:- end_module bag.
%-----------------------------------------------------------------------------%
% Version using pred declarations in the interface section.
% Bag ADT.
% Implemented as unsorted lists.
:- module bag.
:- interface.
:- pred init(bag(_)).
:- pred insert(bag(T), T, bag(T)).
:- pred contains(bag(T), T).
:- implementation.
:- import_module list.
:- type bag(T) = list(T).
init([]).
insert(Bag0, Item, [Item|Bag0]).
contains(Bag, Item) :-
list:member(Item, Bag).
:- end_module bag.
%-----------------------------------------------------------------------------%
% Version using type inference to avoid explicit type
% declarations. (Bad style, IMHO, but we allow for the
% possibility in order to attract the Prolog die-hards.)
% Bag ADT.
% Implemented as unsorted lists.
:- module bag.
:- import_module list.
:- implementation. % ensure that the bag/1 constructor is not exported
:- type bag(T) ---> bag(list(T)). % define type bag(T) and
% constructor bag/1::list(T)->bag(T).
:- interface.
init(bag([])).
insert(bag(Bag0), Item, bag([Item|Bag0])).
contains(bag(Bag), Item) :-
list:member(Item, Bag).
:- end_module bag.
%-----------------------------------------------------------------------------%
% Version using type inference to avoid explicit type
% declarations. (This is speculative; type inference
% is not high on the list of things to implement.)
% Bag ADT.
% Implemented as unsorted lists.
:- module bag.
:- import_module list.
:- export_pred init, insert, contains.
:- type bag(T) ---> bag(list(T)).
init(bag([])).
insert(bag(Bag0), Item, bag([Item|Bag0])).
contains(bag(Bag), Item) :-
list:member(Item, Bag).
:- end_module bag.
%-----------------------------------------------------------------------------%
</pre>
<p>
Here's a rough sketch of an algorithm (probably buggy) for handling these
things.
<ol>
<li> Let S be the set of modules interfaces on which
the current module (directly or indirectly) depends.
For each module in S, read in the module and write out the
full interface of that module (i.e. copy the type and mode
declarations for the predicates into the interface and add
`:- adt' declarations for types which are exported as
opaque abstract data types.
<li> For each module in S, read in the full interface
and resolve all the types, insts, and modes in the bodies
of the symbols defined in the interface (add explicit module
qualifiers). This gives us a "resolved full interface" file.
Write this out.
<li> Read in the resolved full interface files for each module in S.
Resolve all the types, insts, and modes in the bodies
of the symbols defined in the current module.
Resolve all the preds and constructors in the current module.
Generate code for the current module.
</ol>
<pre>
/* file t1.nl */
:- module t1.
:- use_module t2.
:- type t1 ---> g(t2) ; x.
:- export_type t1/0.
:- end_module t1.
/* full interface file for module t1 */
:- module t1.
:- use_module t2.
:- type t1 ---> g(t2:t2) ; x.
:- export_type t1/0.
:- export_adt t2/0.
:- end_module t1.
/* file t2.nl */
:- module t2.
:- use_module t1.
:- type t2 ---> f(t1:t1) ; y.
:- export_type t2/0.
:- end_module t2.
%-----------------------------------------------------------------------------%
</pre>
<hr>
<!-------------------------->
Last update was $Date: 1997/04/03 05:17:40 $ by $Author: aet $@cs.mu.oz.au. <br>
</body>
</html>
|