Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Nesting Modules
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-11-02 (14:02)
From: Tom Hawkins <tom@c...>
Subject: Re: [Caml-list] Nesting Modules
I found a convenient way to compose a set modules into a hierarchical 
library.  First define the set of modules (ml/mli file pairs) -- these 
become the leaves in the module tree.  Next, define the branch modules 
in the hierarchy using "module Leaf = Leaf" (ml only, no mli).  Then 
compile and link.  The only restriction is that all functional code must 
reside in leaf modules.  Here's an example...

# Define the leaf modules.
echo 'let name = "leaf1"' >
echo 'val name : string'  > leaf1.mli
echo 'let name = "leaf2"' >
echo 'val name : string'  > leaf2.mli
echo 'let name = "leaf3"' >
echo 'val name : string'  > leaf3.mli

# Define the branch modules.
echo 'module Leaf2 = Leaf2;; module Leaf3 = Leaf3'     >
echo 'module Leaf1 = Leaf1;; module Branch2 = Branch2' >

# Compile all leaf modules.
ocamlc -c leaf1.mli
ocamlc -c
ocamlc -c leaf2.mli
ocamlc -c
ocamlc -c leaf3.mli
ocamlc -c

# Compile all branch modules.
ocamlc -c
ocamlc -c

# Link the library.
ocamlc -a -o branch1.cma leaf1.cmo leaf2.cmo leaf3.cmo branch2.cmo 

# Delete all *.cmi files except top.
rm leaf*.cmi
rm branch2.cmi

# Run.
ocaml branch1.cma

         Objective Caml version 3.09.0

- : string = "leaf3"

Thanks for everyone's help!