Objective Caml 3.12 Jacques Garrigue ( ) http://www.math.nagoya-u.ac.jp/~garrigue/ with Alain Frisch (Lexifi), OCaml developper team (INRIA)
Jacques Garrigue Modules in Objective Caml 3.12 1 Objective Caml
Jacques Garrigue Modules in Objective Caml 3.12 2 ( val id : a -> a) 3.12 ( )
Jacques Garrigue Modules in Objective Caml 3.12 3 Objective Caml 3.12 open (local open) (local abstract type) (explicit polymorphism annotations) (first-class modules) (signature of a module) (destructive substitution)
Jacques Garrigue Modules in Objective Caml 3.12 4 10 Claudio Russo Moscow ML
Jacques Garrigue Modules in Objective Caml 3.12 5 1 # module type ID = sig val id : a -> a ;; module type ID = sig val id : a -> a # let f id = let module Id = (val id : ID) in (Id.id 1, Id.id true) ;; val f : (module ID) -> int * bool = <fun> # f (module struct let id x = print_line "Id!"; x : ID);; Id! Id! - : int * bool = (1, true)
Jacques Garrigue Modules in Objective Caml 3.12 6 2 module type DEVICE = sig... let devices : (string, (module DEVICE)) Hashtbl.t = Hashtbl.create 17 module PDF = struct... let () = Hashtbl.add devices "PDF" (module PDF: DEVICE)... module Device = (val (try Hashtbl.find devices Sys.argv.(1) with Not_found -> prerr_line "Unknown device"; exit 2) : DEVICE)
Jacques Garrigue Modules in Objective Caml 3.12 7 3 module type PLUGIN = sig type t (* *) val state : t val start : t -> unit val stop : unit -> t ;; let plugins = ref ([] : (string * (module PLUGIN)) list) ;; let new_instance name = let module P = (val List.assoc name!plugins : PLUGIN) in object (* *) val mutable state = P.state method start = P.start state method stop = state <- P.stop () ;; val new_instance : string -> < start : unit; stop : unit > = <fun>
Jacques Garrigue Modules in Objective Caml 3.12 8 4 module type Compute = sig class compute : object method x : int module Default = struct (* *) class compute = object method x = 0 let compute = ref (module Default : Compute) let incr () = (* *) let module M = struct module C = (val!compute : Compute) class compute = object inherit C.compute as super method x = super#x + 1 in compute := (module M : Compute)
Jacques Garrigue Modules in Objective Caml 3.12 9 5 GADTs module TypEq : sig type ( a, b) t (* *) val apply : ( a, b) t -> a -> b val refl : ( a, a) t val sym :... =... module rec Typ : sig module type PAIR = type t and t1 and t2 val eq: (t, t1 * t2) TypEq.t val t1: t1 Typ.typ val t2: t2 Typ.typ type a typ = Int of ( a, int) TypEq.t String of ( a, string) TypEq.t Pair of (module PAIR with type t = a) (* t1 t2... *) = Typ
Jacques Garrigue Modules in Objective Caml 3.12 10 5 GADTs ( )... (* to_string *) let rec to_string : a. a typ -> a -> string = fun (type s) t x -> (* s *) match t with Int eq -> string_of_int (TypEq.apply eq x) String eq -> Printf.sprintf "%S" (TypEq.apply eq x) Pair p -> let module P = (val p : PAIR with type t = s) in let (x1, x2) = TypEq.apply P.eq x in Printf.sprintf "(%s,%s)" (to_string P.t1 x1) (to_string P.t2 x2) Kyseliov ML Workshop 2010
Jacques Garrigue Modules in Objective Caml 3.12 11 OCaml applicative ( ) module type S = sig type t val x : t let r = ref (module struct type t = int let x = 0 : S) module F(X:sig ) = (val!r : S) module A = struct module M = F(A) ;; module M : sig type t = F(A).t val x : t r := (module struct type t = float let x = 0. : S) ;; module N = F(A) ;; module N : sig type t = F(A).t val x : t (* t *) generative ( ) pack unpack
Jacques Garrigue Modules in Objective Caml 3.12 12 (implicit-unpack) module type ID = sig val id : a -> a ;; let f (module Id:ID) = (Id.id 1, Id.id true);; (* unpack *) f (module struct let id x = x );; (* pack *) let rec to_string : a. a typ -> a -> string = fun (type s) (t : s typ) x -> (* *) match t with Int eq -> string_of_int (TypEq.apply eq x) String eq -> Printf.sprintf "%S" (TypEq.apply eq x) Pair (module P) -> (* unpack *) let (x1, x2) = TypEq.apply P.eq x in Printf.sprintf "(%s,%s)" (to_string P.t1 x1) (to_string P.t2 x2) ;;
Jacques Garrigue Modules in Objective Caml 3.12 13 Objective Caml OCaml
Jacques Garrigue Modules in Objective Caml 3.12 14 GADT module rec module rec M : S = M module type of module MyList : sig include module type of List val remove : a -> a list -> a list = struct include List let rec remove a =...
Jacques Garrigue Modules in Objective Caml 3.12 15 2 module type Printable = sig type t val print : t -> unit module type Comparable = sig type t val compare : t -> t -> int module type PrintableComparable = sig (* *) type t val print : t -> unit val compare : t -> t -> int
Jacques Garrigue Modules in Objective Caml 3.12 16 include include module type PrintableComparable = sig include Printable include Comparable with type t = t Error: Multiple definition of the type name t. Names must be unique in a given structure or signature.
Jacques Garrigue Modules in Objective Caml 3.12 17 module type T = sig type t module PrintableF(X:T) = struct module type S = sig val print : t -> unit module ComparableF(X:T) = struct module type S = sig val comparable : t -> t -> int module PrintableComparableF(X:T) = struct module type S = sig include PrintableF(X).S include ComparableF(X).S
Jacques Garrigue Modules in Objective Caml 3.12 18 with module type ComparableInt = Comparable with type t := int ;; module type ComparableInt = sig val compare : int -> int -> int ComparableF(Int).S Comparable ComparableF module ComparableF(X:T) = struct module type S = Comparable with type t := X.t
Jacques Garrigue Modules in Objective Caml 3.12 19 module type PrintableComparable = sig include Printable include Comparable with type t := t module type PrintableInt = (Printable with type t = int) with type t := int module type Printable = sig type printable include Printable with type t := printable
Jacques Garrigue Modules in Objective Caml 3.12 20
Jacques Garrigue Modules in Objective Caml 3.12 21 Objective Caml 3.12 GADTs