The Ocaml language supports recursion between modules, but the compiler does not support recursion between compilation units. Thus, you cannot A.ml need B.ml and B.ml needing A.ml Refactoring to remove recursion is best if you can do it easily, but suppose you cannot.
One solution explained by nlucaroni is to collect both modules into the same file and use module rec . Another solution sometimes is to turn one module into a functor, for example, turn A into a functor F , which takes an argument with signature B and first compiles a file that defines F , then B , then a file that just defines module A = F(B)
Ocamlyacc makes things more complicated, but you can fool it! You can write module A = functor (...) -> struct in the .mly header and the corresponding end in the footer. However, you will have to rewrite the generated .mli to add module A : functor (...) -> sig and end as part of the build process. (I know that I did this earlier in order to solve the same problem as yours, although I do not remember where I cannot give an example of real life.)
Another possibility worth exploring is to switch from Ocamlyacc to Menhir , which is a replacement for Ocamlyacc (with little to do with porting, because the syntax is the same) with some nice features that can help you, for example, support for parameterized parser modules ( i.e. functors).
Gilles
source share