17

I've heard that "first class modules" are coming in OCaml 3.12. What advantages will they offer? What kids of things will be easier? What problem are they trying to solve? A simple example would suffice.

aneccodeal
  • 8,531
  • 7
  • 45
  • 74

2 Answers2

13

It's only one possible applications, but first class modules make it easy to encode existential types, with basically a module packing an existential type and a value using this type). For example, See Alain Frisch work on Dynamic types (code taken from Alain Frisch work on dyntypes : http://caml.inria.fr/cgi-bin/viewvc.cgi/ocaml/branches/dyntypes/stdlib/dyntypes.ml?view=markup )

module type DYN = sig
  type t
  val x: t
  val t: t ttype
end

type dyn = (module DYN)

let dyn (type s) t x =
  let module M = struct
    type t = s
    let x = x
    let t = t
  end
  in
  (module M : DYN)

The idea here is that "ttype" is a concrete representation of that type, an algebraic datatype with Int, Float constructors and so on, and you have here a value, whose type is concealed, but that carries a concrete representation of that type, that you can use for example to get a safer serialization/deserialization.

Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
gasche
  • 31,259
  • 3
  • 78
  • 100
  • Nice. A further application of the same concept that I see: combining an existential type with the ability to "tie" one object to another and have that checked by the type system. E.g. create a cursor for a database connection and then type-check that it cannot be used with a different connection. Sweet! – Michael Ekstrand Mar 22 '10 at 02:49
13

Maybe a bit late, but the new paper First-class modules: hidden power and tantalizing promises is exactly on topic. It's a set of recipes/pearls around first-class modules, by Oleg Kiselyov (oleg) and Jeremy Yallop (author, for example, of the Deriving project).

gasche
  • 31,259
  • 3
  • 78
  • 100