(Introduced in Objective Caml 3.07)
|
Recursive module definitions, introduced by themodule rec …and … construction, generalize regular module definitionsmodulemodule-name=module-expr and module specificationsmodulemodule-name:module-type by allowing the definingmodule-expr and themodule-type to refer recursively to the moduleidentifiers being defined. A typical example of a recursive moduledefinition is:
It can be given the following specification:
This is an experimental extension of OCaml: the class ofrecursive definitions accepted, as well as its dynamic semantics arenot final and subject to change in future releases.
Currently, the compiler requires that all dependency cycles betweenthe recursively-defined module identifiers go through at least one“safe” module. A module is “safe” if all value definitions thatit contains have function typestypexpr1->typexpr2. Evaluation of arecursive module definition proceeds by building initial values forthe safe modules involved, binding all (functional) values tofun_->raiseUndefined_recursive_module. The definingmodule expressions are then evaluated, and the initial valuesfor the safe modules are replaced by the values thus computed. If afunction component of a safe module is applied during this computation(which corresponds to an ill-founded recursive definition), theUndefined_recursive_module exception is raised at runtime:
If there are no safe modules along a dependency cycle, an error is raised
Note that, in thespecification case, themodule-types must beparenthesized if they use thewithmod-constraint construct.