Example of nested signatures in OCaml?

ml, module, ocaml

Solution

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

Problem

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.

Original source