开发者

First class modules in OCaml 3.12: What kinds of things will they make easier (or possible)?

开发者 https://www.devze.com 2022-12-23 19:43 出处:网络
I\'ve heard that \"first class modules\" are coming in OCaml 3.12.What advantages will they offer? What kids of th开发者_运维知识库ings will be easier?What problem are they trying to solve?A simple ex

I've heard that "first class modules" are coming in OCaml 3.12. What advantages will they offer? What kids of th开发者_运维知识库ings will be easier? What problem are they trying to solve? A simple example would suffice.


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号