From: Markus Mottl <mottl@miss.wu-wien.ac.at>
To: debourse@pascal.enst.fr (Benoit Deboursetty)
Cc: caml-list@inria.fr (OCAML)
Subject: Re: cyclic value construction ("let rec")
Date: Mon, 3 Apr 2000 14:57:19 +0200 (MET DST) [thread overview]
Message-ID: <200004031257.OAA29741@miss.wu-wien.ac.at> (raw)
In-Reply-To: <Pine.GSO.4.02.10003282041400.6520-100000@pascal.enst.fr> from "Benoit Deboursetty" at Mar 30, 2000 10:12:33 PM
> It would be interesting to know whether people have other workarounds.
> Also, a generic graph module which would implement "map", "iter" etc. in a
> cycle-safe way (like Marshal) would perhaps be interesting in the
> 'user-contributed stdlib extension'...
You will probably always have to rely on "unsafe" things for such
purposes. My quick hack for problems like this looks as follows:
type 'a node = { data : 'a; next : 'a node }
let make_circle n : 'a node =
let module Dummy = struct
type 'a t = { data : 'a; mutable next : 'a t } end in
let res = { Dummy.data = n; Dummy.next = Obj.magic 0 } in
res.Dummy.next <- res;
Obj.magic res
let rec iter f node = f node.data; iter f node.next
let _ = iter (fun x -> print_int x; print_newline ()) (make_circle 42)
Type "'a node" is immutable from the outside. However, exploiting the
undocumented (good reason for this ;-) type casting features of OCaml,
one can cheat and define an identical type within a local module -
which is mutable!
Initialisation, too, relies on "unsafe" features - we simply put a
reference to a dummy value into the slot until it can be filled with the
"right" value. The return value is cast back again to the "global type"
as indicated in the type restriction in the definition of "make_circle"
- this is important, because otherwise you can crash your program (it
would have "any" type).
As far as I know, this should be safe, because the GC doesn't care
about the static types of the values - it only looks at the value tags
at runtime. Of course, the above will only work as long as the memory
layout of the "local" type agrees with the one of the "global type".
Thus, you can confine the "unsafe" part to specific functions (e.g.
"make_circle"). The iteration function "iter" does not change anything
and can therefore work on the "global" type.
Best regards,
Markus Mottl
--
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl
next prev parent reply other threads:[~2000-04-06 13:14 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-03-30 20:12 Benoit Deboursetty
2000-04-03 12:57 ` Markus Mottl [this message]
2000-04-06 13:18 ` Pierre Weis
2000-04-06 13:34 ` Markus Mottl
2000-04-06 14:25 ` Xavier Leroy
2000-04-06 15:12 ` Markus Mottl
2000-04-10 0:03 ` Pierre Weis
2000-04-10 1:41 ` Markus Mottl
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=200004031257.OAA29741@miss.wu-wien.ac.at \
--to=mottl@miss.wu-wien.ac.at \
--cc=caml-list@inria.fr \
--cc=debourse@pascal.enst.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