6

In OCaml, you can nest signatures:

module type FOO =
sig
  module type BAR
  (* … *)
end

I was just wondering if anyone had any examples of this in use, since I can’t think of any places where it would be needed. I imagine it’s probably useful in the return signatures of functors, but I can’t think of any specific things.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Andy Morris
  • 720
  • 3
  • 10

2 Answers2

5

I recall seeing a few modules (maybe in batteries), that included an Infix module inside that could be opened separately and only when truly wanted. For example,

module Rational = 
  struct
    let add a b = ...
    let sub a b = ...

    module Infix =
      struct
        let (<+>) = add
        let (<->) = sub
      end
  end

In this way if you were to open the Rational.Infix module, you wouldn't de-scope(?) any functions with the same names as anything in Rational.

I am working on a project where we use modules to demarcate types. Having a module define only one type and manipulate that type helps in organization; especially when the modules are small and having a separate file wouldn't be advantageous, and variant types don't make sense.

module Node = 
  struct 

  end
module Edge = 
  struct

  end

type 'a tree = { nodes : 'a Node.t; edges : 'a Edge.t; }

We also use them, although as separate files (combined with -mlpack), for all the parsers we need for biological data --Nexus, Fasta, Phylip, et cetera.

Lastly, often when prototying a new algorithm we will write it in ocaml first, then work on a C version. We usually keep the ocaml version in a inner module with the same function names.

module Align = 
  struct
    module OCaml = 
      struct

      end
  end
nlucaroni
  • 47,556
  • 6
  • 64
  • 86
  • 1
    Nested modules make perfect sense. The question, however, is asking about nested module *signatures*. – Michael Ekstrand Dec 13 '10 at 14:42
  • Michael, You seem to be begging the question. The conclusive benefits of nested modules begets necessity of nested module signatures. – nlucaroni Dec 13 '10 at 16:14
4

First example which came to my mind : http://caml.inria.fr/pub/docs/manual-ocaml/libref/type_Map.html

(it's indeed a functor signature)

rks
  • 920
  • 5
  • 12