Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of nested signatures in OCaml?

Tags:

module

ocaml

ml

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.

like image 433
Andy Morris Avatar asked Dec 07 '10 01:12

Andy Morris


2 Answers

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
like image 109
nlucaroni Avatar answered Oct 05 '22 13:10

nlucaroni


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)

like image 35
rks Avatar answered Oct 05 '22 13:10

rks