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] Yelling
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Neel Krishnaswami <neelk@a...>
Subject: Re: [Caml-list] Yelling
Eray Ozkural writes:
> BTW, does somebody have a good idea of how to make one module
> inherit from another? Say, for implementing Graph using
> Digraph. It's too bad there isn't an Ada like package extension
> mechanism.

You can pretty easily simulate the effect of this kind of inheritance
using the module system, but it's a moderate pain.

First, I'll talk about module signatures. Signatures have a structural
subtyping relationship -- signature BAR is a subtype of FOO if it
supports all of FOO's interface. It is permitted to offer more
operations, but must include all of FOO's types and values. Then you
can use a module of signature BAR wherever a FOO is expected. Here's
an example:

  module type FOO =
      type t
      val v : t
  module type BAR = (* This is a subtype of FOO *)
      type t
      val v : t
      val v' : t

Notice that there's no syntax to abbreviate the definition of BAR; you
need to manually include all of the types and operations in it. This
will be your main annoyance, but for a closed library it's tolerable.

Happily, there is syntax to abbreviate the *implementation* of module
extensions. You can implement one module, and then use the include
keyword to add its types and bindings to some other module. For
  module Foo : FOO =
      type t = Foo of unit
      let v = Foo()
  module Bar : BAR with type t = Foo.t =
      include Foo (* Add the type t and value v to the Foo module *)
      let v' = v

The "with type t = Foo.t" thing at the top of Bar is a sharing
declaration. It informs the compiler that the type Bar.t is the same
as the type Foo.t, so you can freely intermix Foo.t and Bar.t values.
(Otherwise the compiler would forbid it, on the grounds that these are
two different abstract types.)

I hope this helps! 

Neel Krishnaswami

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: