From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id KAA02112; Tue, 2 Mar 2004 10:43:34 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id KAA02349 for ; Tue, 2 Mar 2004 10:43:32 +0100 (MET) Received: from lri.lri.fr (lri.lri.fr [129.175.15.1]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i229heIq004248 for ; Tue, 2 Mar 2004 10:43:41 +0100 Received: from pc8-123 (pc8-123 [129.175.8.123]) by lri.lri.fr (8.12.10/jtpda-5.4) with ESMTP id i229UjO7022965 ; Tue, 2 Mar 2004 10:30:50 +0100 (MET) Received: from filliatr by pc8-123 with local (Exim 3.35 #1 (Debian)) id 1Ay6Ef-0004Ro-00; Tue, 02 Mar 2004 10:30:45 +0100 From: Jean-Christophe Filliatre MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <16452.21573.57928.3506@gargle.gargle.HOWL> Date: Tue, 2 Mar 2004 10:30:45 +0100 To: andrew@acooke.org Cc: "ocaml list" Subject: Re: [Caml-list] Newbie: Separate compilation of interfaces, modules In-Reply-To: <56125.139.229.3.218.1077971452.squirrel@localhost> References: <56125.139.229.3.218.1077971452.squirrel@localhost> X-Mailer: VM 7.03 under Emacs 20.7.2 Reply-To: Jean-Christophe.Filliatre@lri.fr (Jean-Christophe Filliatre) X-MailScanner: Found to be clean X-Miltered: at nez-perce by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; filliatre:01 filliatre:01 lri:01 caml-list:01 newbie:01 conceptual:01 mli:01 mli:01 reuse:01 schema:01 struct:01 struct:01 functor:01 functor:01 compiler:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Keywords: X-UID: 17 andrew cooke writes: > > I can't work out how to structure the following in files (there are > examples in the documentation, but all seem to be for interactive work). > I don't know if the problem is conceptual (not understanding modules, > signatures etc) or practical (don't know what to type). > > I have an interface, PersistentArray, that I define in PersistentArray.mli > > Then I have an implementation, BinaryTreeArray, that I code in BinaryTree.ml > > But where do I put the information that BinaryTreeArray implements > PersistentArray? What should go in BinaryTreeArray.mli so that I can use > it as a particular implementation of PersistentArray? In ocaml, files define modules and signatures with the following convention: code in file a.ml defines a module A and code in file a.mli defines the signature for this module (The ocaml compiler checks that a.ml has indeed the signature declared in a.mli). In a simple situation with only one implementation, the interface for your module BinaryTreeArray should go in file BinaryTreeArray.mli (or, symmetrically, the code in BinaryTreeArray.ml should go instead in PersistentArray.ml to have signature PersistentArray.mli). Since you are willing to reuse a single interface for several implementations, this simple schema one-file-one-module does not suit. You need to *name* your PersistentArray signature; for this purpose, you can for instance create a file sig.mli containing its definition: sig.mli: module type PersistentArray = sig ... end Then you can define several implementations with this signature: module BinaryTreeArray : Sig.PersistentArray = struct ... end module TrieArray : Sig.PersistentArray = struct ... end > The idea is that later I can define some other implementation, say > TrieArray, that can be used as the PersistentArray implementation without > changing any more code than is necessary (preferably just one line > somewhere). There are several ways to do this: 1. give a name to the implementation of your choice module PA = BinaryTreeArray and use PA in the following. Then switching to another implementation only requires to change the first line. 2. write your code as a functor module F(PA : Sig.PersistentArray) = struct ... end and then only the functor instanciation will change: module M = F(BinaryTreeArray) Hope this helps, -- Jean-Christophe ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners