English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    
Browse thread
Compiler feature - useful or not?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: weis@y...
Subject: Re: [Caml-list] Compiler feature - useful or not?
> On Tue, 13 Nov 2007, Yaron Minsky wrote:
> 
> >There's a simple trick that Steven Weeks introduced to us and that we now
> >use at Jane Street for this kind of thing.
> >
> >You write down a signature:
> >
> >module type Abs_int : sig
> >type t
> >val to_int : t -> int
> >val of_int : int <- t
> >end
> >
> >And then you write concrete module Int that implements this signature.  You
> >can then write:
> >
> >module Row : Abs_int = Int
> >module Col : Abs_int = Int
> >
> >You can now use Row.t and Col.t as abstract types.  The boilerplate is
> >written once, but can be used over and over.  I've personally seen more
> >use-cases for this with strings than with ints (to separate out different
> >kinds of identifiers)
> 
> That's the best solution I've seen so far.
> 
> Otherwise there's still the following:
>   http://martin.jambon.free.fr/ocaml.html#opaque
> 
> which in theory doesn't require new module or type declarations, but it 
> adds type parameters, which can be confusing.
> 
> 
> Martin

In the next version of the compiler, you will have an extension to the
definition of types with private construction functions (aka ``private''
types) that just fulfills your programming concern: in addition to usual
private sum and record private type definitions, you can now define a type
abbreviation which is private to the implementation part of a module (see
note 1).

Since the values of a private type are concrete, it is much easier for the
programmer to use the values of the type abbreviation.

Using the new private type abbreviation feature, you can write for instance:

row.ml
------
type row = int;;

let make i =
  if i < 0 then failwith "Row: cannot create a negative row" else
  i;;

let from i = i;;

row.mli
-------
type row = private int;;

val make : int -> row;;
val from : row -> int;;

This solution does not require any additional tagging or boxing, only the
usage of injection/projection function for the type. As for usual private
types, the injection function is handy to provide useful invariants (in the
row type example case, we get ``a row value is always a positive integer'').

In addition, the private abbreviation is publicly exposed in the interface
file. Hence, the compiler knows about it: it can (and effectively does)
optimize the programs that use values of type row in the same way as if the
type row were defined as a regular public abbreviation.

Last but not least, being an instance of the identity function, the from
projection function is somewhat generic: we can dream to suppress the burden
of having to write it for each private type definition, if we find a way to
have it automatically associated to the new private type by the compiler.

Best regards,

Note 1: this is a generalization for regular type abbreviations of the
polymorphic variants private type definitions that Jacques Garrigue already
introduced to provide polymorphic variants (and object) types with private
row variables.

-- 
Pierre Weis

INRIA Rocquencourt, http://bat8.inria.fr/~weis/