tags:

views:

440

answers:

2

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.

+4  A: 

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/viewcvs.cgi/ocaml/branches/dyntypes/stdlib/dyntypes.ml?rev=9460&view=auto )

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.

gasche
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 E
+2  A: 

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