Movatterモバイル変換


[0]ホーム

URL:


Chapter 12 Language extensions

2 Recursive modules

(Introduced in Objective Caml 3.07)

definition::=...
 modulerecmodule-name:module-type=module-expr { andmodule-name:module-type=module-expr }
 
specification::=...
 modulerecmodule-name:module-type{ andmodule-name:module-type }

Recursive module definitions, introduced by themodule recand … 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:

modulerec A :sigtype t = Leafof string | Nodeof ASet.tval compare: t -> t -> intend =structtype t = Leafof string | Nodeof ASet.tlet compare t1 t2 =match (t1, t2)with | (Leaf s1, Leaf s2) -> Stdlib.compare s1 s2 | (Leaf _, Node _) -> 1 | (Node _, Leaf _) -> -1 | (Node n1, Node n2) -> ASet.compare n1 n2endand ASet : Set.Swithtype elt = A.t = Set.Make(A)

It can be given the following specification:

modulerec A :sigtype t = Leafof string | Nodeof ASet.tval compare: t -> t -> intendand ASet : Set.Swithtype elt = A.t

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:

modulerec M:sigval f: unit -> intend =structlet f () = N.xendand N:sigval x: intend =structlet x = M.f ()end
Exception:Undefined_recursive_module ("extensions/recursivemodules.etex", 1, 43).

If there are no safe modules along a dependency cycle, an error is raised

modulerec M:sigval x: intend =struct let x = N.y endand N:sigval x: intval y:intend =structlet x = M.xlet y = 0end
Error: Cannot safely evaluate the definition of the following cycle of recursively-defined modules: M -> N -> M. There are no safe modules in this cycle (see manual section 12.2). Module M defines an unsafe value, x . Module N defines an unsafe value, x .

Note that, in thespecification case, themodule-types must beparenthesized if they use thewithmod-constraint construct.

« Recursive definitions of valuesPrivate types »
Copyright © 2025 Institut National deRecherche en Informatique et en Automatique

[8]ページ先頭

©2009-2026 Movatter.jp