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
[Caml-list] About modules again
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-07-15 (08:44)
From: Markus Mottl <markus@o...>
Subject: Re: [Caml-list] About modules again
On Mon, 15 Jul 2002, Nicolas FRANCOIS wrote:
> (it's an Euclidian Ring, but that's not the point). So you see my problem
> : I have to define a concrete method in an abstract module, just not to
> have to fill the definition for every new Ring I define.
> Is there a way to do this ? Or is this specific to objects ?

This is the point where higher-order modules (functors) enter the game.
If you have some common functionality that is shared across all rings,
you can factor it out in a functor body. E.g.:

  module MakeRing (Spec : RING_SPEC) : RING = struct
    include Spec

    (* All common stuff follows here *)

Module "Spec" should adhere to a signature "RING_SPEC", the latter
specifying what is different or at least parameterizable for each
ring. Applying the functor to such a specification yields a module that
implements the signature RING, e.g.:

  module Ring_Z = MakeRing (Ring_Z_Spec)

The specification of rings over Z (Ring_Z_Spec) may itself be the result
of a functor application. This way you can highly modularize the creation
of algebraic structures.

Btw., you'd surely get into troubles using OO here. It is not very
suitable for such purposes, because it lacks expressiveness if you want
to specify interaction of several types (sorts) at an abstract level.

Markus Mottl

Markus Mottl                                   
Austrian Research Institute
for Artificial Intelligence        
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: