List Info

Thread: Re: "ocaml_beginners"::[] type constructors again




Re: "ocaml_beginners"::[] type constructors again
country flaguser name
United States
2007-05-27 02:36:08


Thanks for your reply Jon. There's still a point that's not
clear to me : if the A module in your example corresponds to a file
a.ml used in the making of some .cma library, is it necessary to create an auxiliary
submodule as follows :

(*contents of a.ml *)
module TemporarySubmoduleForImplementationHiding : sig
type t
end = struct
type t = int list
end

type t=TemporarySubmoduleForImplementationHiding.t

or is it possible to do it "directly" ?

>; In OCaml, the objective is to write as little code as possible. The above
> example requires you to write out lots of constructors by hand.

You've convinced me that my example is not the right way to do things, but if
I actually did it, I wouldn't write them out by hand. I would ask OCaml to
do it for me !

let rec interval a b=if (a>b) then [] else a::(interval (a+1) b);;
let sec_interval=interval(0)(59) in
let s=String.concat("|")(List.map (function i->"Sec"^(string_of_int i)^" " ) sec_interval) in
print_string ("nnn"^s^"nnn");;

and then a copy&paste would do the job ...

Ewan

--- In ocaml_beginners%40yahoogroups.com">ocaml_beginnersyahoogroups.com, Jon Harrop <jon...> wrote:
&gt;
> On Sunday 27 May 2007 07:04:00 roparzhhemon wrote:
&gt; > Just how do you implement this "private datatype&quot; in OCaml ?
>
> # type t = private A;;
> type t = private A
>
> > E.g, for seconds, one could write
&gt; >
>; > type number_of_seconds=Sec0 |Sec1 |Sec2 |Sec3 (...) |Sec59;;
> >
>; > Is this good or bad practice in OCaml
&gt;
> In OCaml, the objective is to write as little code as possible. The above
> example requires you to write out lots of constructors by hand.
&gt;
> A shorter way is:
>
> type secs = Sec of int
>
> > Another related question : suppose I'm creating an OCaml library,
> > and I have a type int_set that is in fact an int list, but I don't want the
> > end user to know how it's implemented.
> > If I just write type t=int list in file "int_set.ml";, the end user only
>; > has to write "module X=Int_set;;" in the toplevel to learn that
>; > Int_set.t is simply int list. Is there a way to hide the implementation ?
>
> Yes indeed. If you consider your module written with a full interface (you can
> get this by compiling the module structure with -i to get the inferred
> signature):
>
> module A : sig
> type t = int list
>; end = struct
&gt; type t = int list
>; end
>
> All you do is leave off the definition of the type in the signature and it is
> abstracted away:
&gt;
> module A : sig
> type t
> end = struct
&gt; type t = int list
>; end
>
> Everything outside the module A is then unaware that A.t is actually an int
> list.
&gt;
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
>; The F#.NET Journal
> http://www.ffconsultancy.com/products/fsharp_journal/?e
&gt;

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] type constructors again
country flaguser name
United Kingdom
2007-05-27 04:24:59

On Sun, May 27, 2007 at 07:36:08AM -0000, roparzhhemon wrote:
&gt; Thanks for your reply Jon. There's still a point that's not clear
&gt; to me : if the A module in your example corresponds to a file a.ml
>; used in the making of some .cma library, is it necessary to create
&gt; an auxiliary submodule as follows :

Beginners should, as a rule of thumb, never use modules and
submdodules, struct, sig etc. Instead just put your interface in one
file called foo.mli and your implementation in another file called
foo.ml. These two files together define a module called Foo.

Rich.

--
Richard Jones
Red Hat

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )