Re: Objective Caml 2.00 released

From: Thierry Bravier (thierry.bravier@dassault-aviation.fr)
Date: Thu Aug 27 1998 - 14:22:29 MET DST


Message-Id: <35E54F85.6774@dassault-aviation.fr>
Date: Thu, 27 Aug 1998 14:22:29 +0200
From: Thierry Bravier <thierry.bravier@dassault-aviation.fr>
To: caml-list@inria.fr
Subject: Re: Objective Caml 2.00 released

Xavier Leroy wrote:
>
> It is my pleasure to announce the release of Objective Caml 2.00.
>
> Objective Caml 2.00:
> --------------------
>
> * Language:
> - Local module definitions "let module X = <module-expr> in <expr>".

Thank you and congratulations for this release.

The local module feature is, I think, really fine and useful
it will allow (in my case) a much bigger use of the module system.

Here are a few suggestions for related issues.

Of course, I might have neglected theoretically bad consequences
of my suggestions. I would like to have opinions about the
implied coding style and its usefulness in ocaml.

Cheers.

TYPE PARAMETERS SCOPE:
(* ================================================================== *)
let remove_duplicates_string comparison_fun =
  let module StringSet =
    Set.Make
      (struct
         type t = string
         let compare = comparison_fun end) in
    fun string_list ->
      StringSet.elements
        (List.fold_right StringSet.add string_list StringSet.empty)
(*
  val remove_duplicates_string :
  (string -> string -> int) -> string list -> string list
*)
 
let remove_duplicates_string_std = remove_duplicates_string compare
let _ = remove_duplicates_string_std ["a"; "b"; "a"; "c"]
let _ = remove_duplicates_string_std ["a"; "c"]
 
(* ================================================================== *)
(*
(*
  Next part will not compile with ocaml-2.00
  but, in my opinion would be nicer/better code, if allowed.
*)
 
let remove_duplicates_any (comparison_fun : 'any -> 'any -> int) =
  let module AnySet =
    Set.Make
      (struct
         type t = 'any
 (*
   ocaml-2.00 error message is: "Unbound type parameter any"
 *)
         let compare = comparison_fun end) in
    fun any_list ->
      AnySet.elements (List.fold_right AnySet.add any_list AnySet.empty)
(*
  val remove_duplicates_any : ('a -> 'a -> int) -> 'a list -> 'a list
*)
 
let remove_duplicates_any_std = remove_duplicates compare
let _ = remove_duplicates_any_std ['a'; 'b'; 'a']
let _ = remove_duplicates_any_std ["Hello"; "World"]
*)
(* ================================================================== *)

ANONYMOUS MODULES:
(* ================================================================== *)
module type CMP = sig
  type t
  val compare : t -> t -> int
end
 
(* ================================================================== *)
module Lexico (C1 : CMP) (C2 : CMP with type t = C1.t)
  : (CMP with type t = C1.t) = struct
    type t = C1.t
    let compare x y =
      let compare1 = C1.compare x y in
        if compare1 != 0 then compare1 else C2.compare x y
  end
 
(* ================================================================== *)
let combine compare1 compare2 =
  let module L =
    Lexico
      (struct type t = int let compare = compare1 end)
      (struct type t = int let compare = compare2 end) in
    L.compare
 
(* ================================================================== *)
(*
(*
  Next part will not compile with ocaml-2.00
  but, in my opinion would be nicer/better code, if allowed.
*)
 
let combine_nicer compare1 compare2 =
  Lexico
    (struct type t = int let compare = compare1 end)
    (struct type t = int let compare = compare2 end).compare
 (*
   ocaml-2.00 error message is: "Syntax error"
 *)
*)
(* ================================================================== *)

MODULES SCOPE:
(* ================================================================== *)
type nat = Z | S of nat
 
let make_nat () =
  let module N = struct
    type t = nat
    let z = Z
    let s n = S n
    let rec int = function
      | Z -> 0
      | S n -> succ (int n)
  end in
    (N.z, N.s, N.int)
 
(*
  As it is make_nat is not very useful because construction and direct
access
  to nat values through constructors Z and S is possible.
*)
 
(* ================================================================== *)
(*
(*
  Next part will not compile with ocaml-2.00
  but, in my opinion would be nicer/better code, if allowed.
*)
 
let make_nat () =
  let module N = struct
    type t = Z | S of t
    let z = Z
    let s n = S n
    let rec int = function
      | Z -> 0
      | S n -> succ (int n)
  end in
    (N.z, N.s, N.int)
 (*
   ocaml-2.00 error message is:
   This `let module' expression has type N.t * (N.t -> N.t) * (N.t ->
int)
   In this type, the locally bound module name N escapes its scope
 *)
 
  (*
    What I would like here is getting an abstract type "make_nat.t" that
    could only be created and accessed through the offered interface
    functions, that is z, s and int
  *)
*)
(* ================================================================== *)

-- 
Thierry Bravier                     Dassault Aviation - DGT / DPR / DESA
78, Quai Marcel Dassault              F-92214 Saint-Cloud Cedex - France
Telephone : (33) 01 47 11 53 07          Telecopie : (33) 01 47 11 52 83
E-Mail :                     mailto:thierry.bravier@dassault-aviation.fr



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:15 MET