caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Newbie: Separate compilation of interfaces, modules
@ 2004-02-28 12:30 andrew cooke
  2004-02-28 13:04 ` To beginners list " andrew cooke
  2004-03-02  9:30 ` Jean-Christophe Filliatre
  0 siblings, 2 replies; 3+ messages in thread
From: andrew cooke @ 2004-02-28 12:30 UTC (permalink / raw)
  To: ocaml list


Hi,

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?

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).

Thanks,
Andrew

-- 
  __ _ __ ___  ___| |_____   work web site: http://www.ctio.noao.edu/~andrew
 / _` / _/ _ \/ _ \ / / -_)  personal web site: http://www.acooke.org/andrew
 \__,_\__\___/\___/_\_\___|  personal gallery: http://www.acooke.org/pancito

-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* To beginners list Re: [Caml-list] Newbie: Separate compilation of  interfaces, modules
  2004-02-28 12:30 [Caml-list] Newbie: Separate compilation of interfaces, modules andrew cooke
@ 2004-02-28 13:04 ` andrew cooke
  2004-03-02  9:30 ` Jean-Christophe Filliatre
  1 sibling, 0 replies; 3+ messages in thread
From: andrew cooke @ 2004-02-28 13:04 UTC (permalink / raw)
  To: ocaml list, ocaml_beginners


Ah, sorry - I've just seen the pointer to the beginners list.  Will repost
this question there (but not the one on polymorphic variants).

Cheers,
Andrew

andrew cooke said:
>
> Hi,
>
> 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?
>
> 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).
>
> Thanks,
> Andrew
>
> --
>   __ _ __ ___  ___| |_____   work web site:
> http://www.ctio.noao.edu/~andrew
>  / _` / _/ _ \/ _ \ / / -_)  personal web site:
> http://www.acooke.org/andrew
>  \__,_\__\___/\___/_\_\___|  personal gallery:
> http://www.acooke.org/pancito
>
> -------------------
> 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
>
>


-- 
  __ _ __ ___  ___| |_____   work web site: http://www.ctio.noao.edu/~andrew
 / _` / _/ _ \/ _ \ / / -_)  personal web site: http://www.acooke.org/andrew
 \__,_\__\___/\___/_\_\___|  personal gallery: http://www.acooke.org/pancito

-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Newbie: Separate compilation of interfaces, modules
  2004-02-28 12:30 [Caml-list] Newbie: Separate compilation of interfaces, modules andrew cooke
  2004-02-28 13:04 ` To beginners list " andrew cooke
@ 2004-03-02  9:30 ` Jean-Christophe Filliatre
  1 sibling, 0 replies; 3+ messages in thread
From: Jean-Christophe Filliatre @ 2004-03-02  9:30 UTC (permalink / raw)
  To: andrew; +Cc: ocaml list


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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-03-02  9:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-28 12:30 [Caml-list] Newbie: Separate compilation of interfaces, modules andrew cooke
2004-02-28 13:04 ` To beginners list " andrew cooke
2004-03-02  9:30 ` Jean-Christophe Filliatre

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).