caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre Weis <pierre.weis@inria.fr>
To: "Gérard Huet" <Gerard.Huet@inria.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] about Obj.magic
Date: Tue, 4 Jun 2002 12:22:11 +0200	[thread overview]
Message-ID: <20020604122211.A18447@pauillac.inria.fr> (raw)
In-Reply-To: <D37FA3E3-763B-11D6-BE8F-0003938819CE@inria.fr>; from Gerard.Huet@inria.fr on Sun, Jun 02, 2002 at 05:17:17PM +0200

Hi Gerard,

> Hum. For me, the most frequent use of Obj.magic is when I do input_value 
> of some
> persistent data, and the corresponding meta theorem seems tough to me, 
> since it
> involves a complicated state reasoning (status of the file system, 
> knowledge that the
> contents of the file was not tampered with since it was created, 
> hopefully with output_value, knowledge that the current type environment 
> is consistent with the one
> that prevailed when I did output_value, etc).

You are right input_value is a dangerous primitive that is roughly
speaking equivalent to Obj.magic, and this is reflected in its type
scheme:

# input_value;;
- : in_channel -> 'a = <fun>

Hence, you should not ``use of Obj.magic is when (you) do
input_value'': you should instead try to tame a bit this (over)
polymorphic type, for instance by defining specialized versions of
input_value and output_value that are statically type related.

To input/output values of type ty, I generally define two functions
input_ty and output_ty defined as a pair of explicitely typed
functions, as in:

let (output_ty : out_channel -> ty -> unit), (input_ty : in_channel -> ty) =
    (fun oc x -> output_value oc x),
    (fun ic -> input_value ic);;

This way, ``the current type environment is consistent with the one
that prevailed when I did output_value'' is automatically verified (at
least, in the most common case where value IOs are performed from
within the same executable).

On the other hand, file tampering seems to me out of reach of a type
system (and a bit similar to moving around .cmo and .cmi files).

> Since I do not have this meta-theorem, it happens to me occasionally
> to get typically Segmentation fault. Without explicit Obj.magic, but
> with its implicit use with input_value, which is to my view its most
> frequent usage, hard to avoid if you want to manage persistent data.
> Gérard

You are perfectly right: value IOs is mandatory to easily manage
persistent data. That's why we need a safe way to do it. I think this
is the last ennoying glitch that remains in the Objective Caml system.

Best regards,
-- 
Pierre Weis

INRIA, Projet Cristal, http://pauillac.inria.fr/~weis
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2002-06-04 10:22 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-30 21:09 dengping zhu
2002-05-30 22:38 ` Jacques Garrigue
2002-05-31  4:00 ` Daniel de Rauglaudre
2002-05-31  5:37   ` Oleg
2002-05-31  6:17     ` Daniel de Rauglaudre
     [not found]       ` <p05100300b91cea02dbf5@[192.168.1.22]>
2002-05-31  9:50         ` [Caml-list] SML->OCaml (was: about Obj.magic) Daniel de Rauglaudre
2002-05-31 15:39   ` [Caml-list] about Obj.magic dengping zhu
2002-05-31 16:28     ` John D. Barnett
2002-06-01  8:37     ` Jacques Garrigue
2002-06-01 16:00       ` dengping zhu
2002-06-01 17:20         ` Pierre Weis
2002-05-31  9:17 ` Warp
2002-05-31 13:58   ` Jacques Garrigue
2002-05-31 14:06     ` Warp
2002-05-31 18:21       ` John Max Skaller
2002-06-01 17:42         ` Pierre Weis
2002-06-02 15:15           ` John Max Skaller
     [not found]           ` <D37FA3E3-763B-11D6-BE8F-0003938819CE@inria.fr>
2002-06-04 10:22             ` Pierre Weis [this message]
2002-05-31 14:10     ` Remi VANICAT
2002-05-31 16:19     ` Christophe TROESTLER
2002-05-31 18:06       ` Olivier Andrieu
2002-05-31 22:03         ` Christophe TROESTLER

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020604122211.A18447@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=Gerard.Huet@inria.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).