caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* The Unix module & records
@ 2000-05-12 16:55 Amit Dubey
  2000-05-12 17:39 ` Jean-Christophe Filliatre
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Amit Dubey @ 2000-05-12 16:55 UTC (permalink / raw)
  To: caml-list


	Hi,

	I'm still in the process of learning caml, but I'm considering
using it for a project that I'm working on.  One of the first things
I need to do is to figure out how to load a file!  I've tried the following
code:

1: let main () =
2:        let file_info : Unix.stats = Unix.stat Sys.argv.(1) in
3:        let fd = Unix.openfile Sys.argv.(1) [ Unix.O_RDONLY ] perm in
4:        let buffer = ref "" in
5:        let bytes_read = Unix.read (fd) (buffer) (file_info.st_size) in
	<snipped>

But it fails with the error "Unbound record field label st_size" on line 5.
I can't explain why this is happening; looking at the Unix.ml file, I see
the st_size field in the Unix.stats record.  Any suggestions would be
appreciated.

Thanks,
Amit Dubey




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: The Unix module & records
  2000-05-12 16:55 The Unix module & records Amit Dubey
@ 2000-05-12 17:39 ` Jean-Christophe Filliatre
  2000-05-12 23:34 ` Markus Mottl
  2000-05-13 11:30 ` Gerd Stolpmann
  2 siblings, 0 replies; 4+ messages in thread
From: Jean-Christophe Filliatre @ 2000-05-12 17:39 UTC (permalink / raw)
  To: Amit Dubey; +Cc: caml-list


> 1: let main () =
> 2:        let file_info : Unix.stats = Unix.stat Sys.argv.(1) in
> 3:        let fd = Unix.openfile Sys.argv.(1) [ Unix.O_RDONLY ] perm in
> 4:        let buffer = ref "" in
> 5:        let bytes_read = Unix.read (fd) (buffer) (file_info.st_size) in
> 	<snipped>
> 
> But it fails with the error "Unbound record field label st_size" on line 5.
> I can't explain why this is happening; looking at the Unix.ml file, I see
> the st_size field in the Unix.stats record.  Any suggestions would be
> appreciated.

Since you  didn't open the  Unix module with  "open Unix" you  have to
qualify any identifier  from that module with "Unix."  (as you did for
stat, read, ... by the way), including the field name st_size.

So you should have written "file_info.Unix.st_size".

As you are probably going to  say, it becomes quite heavy. Thus, since
you use many identifiers from  the Unix module, you should rather open
it, like this :

======================================================================
open Unix

let main () =
      let file_info = stat Sys.argv.(1) in
      let fd = openfile Sys.argv.(1) [ O_RDONLY ] perm in
      let buffer = ref "" in
      let bytes_read = read fd buffer file_info.st_size in
      <snipped>
======================================================================


-- 
Jean-Christophe Filliatre    
  Computer Science Laboratory   Phone (650) 859-5173
  SRI International             FAX   (650) 859-2844
  333 Ravenswood Ave.           email  filliatr@csl.sri.com
  Menlo Park, CA 94025, USA     web    http://www.csl.sri.com/~filliatr

  



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: The Unix module & records
  2000-05-12 16:55 The Unix module & records Amit Dubey
  2000-05-12 17:39 ` Jean-Christophe Filliatre
@ 2000-05-12 23:34 ` Markus Mottl
  2000-05-13 11:30 ` Gerd Stolpmann
  2 siblings, 0 replies; 4+ messages in thread
From: Markus Mottl @ 2000-05-12 23:34 UTC (permalink / raw)
  To: Amit Dubey; +Cc: OCAML

> 1: let main () =
> 2:        let file_info : Unix.stats = Unix.stat Sys.argv.(1) in
> 3:        let fd = Unix.openfile Sys.argv.(1) [ Unix.O_RDONLY ] perm in
> 4:        let buffer = ref "" in
> 5:        let bytes_read = Unix.read (fd) (buffer) (file_info.st_size) in
> 	<snipped>
> 
> But it fails with the error "Unbound record field label st_size" on line 5.
> I can't explain why this is happening; looking at the Unix.ml file, I see
> the st_size field in the Unix.stats record.  Any suggestions would be
> appreciated.

Unless you open the Unix-library with "open Unix", you need to write the
fully qualified name:

  file_info.Unix.st_size

instead of

  file_info.st_size

Furthermore, strings are not resizable: so you cannot read in anything into
an empty one. Btw., you need not write parenthesis around function
arguments if they are just one word (= an evaluated expression).

Try something like the following code:

  let read_file name =
    let size = (Unix.stat name).Unix.st_size in
    let buf = String.create size
    and file = open_in name in
    really_input file buf 0 size;
    close_in file;
    buf

Best regards,
Markus Mottl

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: The Unix module & records
  2000-05-12 16:55 The Unix module & records Amit Dubey
  2000-05-12 17:39 ` Jean-Christophe Filliatre
  2000-05-12 23:34 ` Markus Mottl
@ 2000-05-13 11:30 ` Gerd Stolpmann
  2 siblings, 0 replies; 4+ messages in thread
From: Gerd Stolpmann @ 2000-05-13 11:30 UTC (permalink / raw)
  To: Amit Dubey, caml-list

On Fri, 12 May 2000, Amit Dubey wrote:
>Hi,
>
>	I'm still in the process of learning caml, but I'm considering
>using it for a project that I'm working on.  One of the first things
>I need to do is to figure out how to load a file!  I've tried the following
>code:
>
>1: let main () =
>2:        let file_info : Unix.stats = Unix.stat Sys.argv.(1) in
>3:        let fd = Unix.openfile Sys.argv.(1) [ Unix.O_RDONLY ] perm in
>4:        let buffer = ref "" in
>5:        let bytes_read = Unix.read (fd) (buffer) (file_info.st_size) in
>	<snipped>
>
>But it fails with the error "Unbound record field label st_size" on line 5.
>I can't explain why this is happening; looking at the Unix.ml file, I see
>the st_size field in the Unix.stats record.  Any suggestions would be
>appreciated.

You must write file_info.Unix.st_size. This is a point where OCaml differs
from other languages where labels are always local to the record; in OCaml
record labels are contained in module namespaces. The reason is that it must
be possible to infer the type of the expression from the label; i.e. if the
compiler sees "st_size" it looks up which (declared) type contains this
label. In the module you wrote it does not find such a type, and so the
compiler stops with an error.

In other languages, variables must be declared, and so it is possible that
the compiler can find out the type of the variable (or expression) before
it looks up the type containing the label. You tried to solve the problem
by giving the type annotation file_info : Unix.stats, but it does work
in OCaml because this "strategy" is not compatible (enough) with type inference
in  general; the variable file_info may also be an expression, and it would be
a pain if you had to type-annotate every expression to which you apply a record
label.

The OCaml solution is to look up the label first, and find out the type (at
least another typing constraint) of the variable or expression after that.
Because of this, labels are also members of the modules containing the record
type; otherwise the label lookup would be ambiguous.

Of course, you do not need the annotation file_info : Unix.stats.

Sometimes it would be nice if the compiler could open a module only partially;
for example if I could write

	open Unix only record labels

to import only record labels into the current module. Ambigous record labels
have only low impact on the overall type safety, because such labels are often
only used by the functions of the same module, and incorrectly associated
labels will almost always cause type errors.

BTW, there seems to be another error. Unix.read wants a string as second
argument, and the string must already be allocated:

	let buffer = String.create n 

where n is the maximum number of bytes to read at once.

Hope this helps,

Gerd
-- 
----------------------------------------------------------------------------
Gerd Stolpmann      Telefon: +49 6151 997705 (privat)
Viktoriastr. 100             
64293 Darmstadt     EMail:   gerd@gerd-stolpmann.de
Germany                     
----------------------------------------------------------------------------



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2000-05-14 21:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-05-12 16:55 The Unix module & records Amit Dubey
2000-05-12 17:39 ` Jean-Christophe Filliatre
2000-05-12 23:34 ` Markus Mottl
2000-05-13 11:30 ` Gerd Stolpmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).