7

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.

2 Answers 2

5

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)

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Nested modules make perfect sense. The question, however, is asking about nested module signatures.
Michael, You seem to be begging the question. The conclusive benefits of nested modules begets necessity of nested module signatures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.