From: Francois Pottier <Francois.Pottier@inria.fr>
To: Mark Seaborn <mrs35@cam.ac.uk>
Cc: caml-list@inria.fr, "François Pottier" <Francois.Pottier@inria.fr>
Subject: Re: [Caml-list] Dynamic types, casts, and rights amplification
Date: Wed, 11 Jul 2001 09:52:36 +0200 [thread overview]
Message-ID: <20010711095236.A3033@pauillac.inria.fr> (raw)
In-Reply-To: <20010703214914J.mrs35@cam.ac.uk>; from mrs35@cam.ac.uk on Tue, Jul 03, 2001 at 09:49:14PM +0100
[-- Attachment #1: Type: text/plain, Size: 1235 bytes --]
Mark,
> Here's a nice use of OCaml's casts to provide what can be viewed as an
> open dynamic type (except that variant tags -- called brands here --
> are created at run time).
There are ways of implementing such a dynamic type without using unsafe
type casts (i.e. Obj.magic). I have attached a file containing two sample
implementations.
The first one generates exceptions dynamically and uses them as tags. The
fact that they are exceptions is irrelevant; what matters is the ability to
generate new tags at runtime. Unfortunately, this piece of code isn't accepted
by O'Caml, because it doesn't recognize the type variable 'a in the exception
declaration as bound by the preceding type annotation.
The second one uses references as temporary buffers for data exchange. The
idea was posted a while ago on this mailing list, but I couldn't find the
exact reference. Instead of being tagged, a value is encoded within a function
whose effect is to write the value into a reference.
The second implementation is interesting, because it makes it easy to see why
one shouldn't create polymorphic brands: brands *are* references in this
approach.
--
François Pottier
Francois.Pottier@inria.fr
http://pauillac.inria.fr/~fpottier/
[-- Attachment #2: dyn.ml --]
[-- Type: text/plain, Size: 796 bytes --]
module type Dyn = sig
type 'a brand
type t
val make_brand : unit -> 'a brand
val pack : 'a brand -> 'a -> t
val unpack : 'a brand -> t -> 'a option
val null : t
end
module Dyn : Dyn = struct
type 'a brand = ('a -> exn) * (exn -> 'a option)
type t = exn
let make_brand : unit -> 'a brand = function () ->
let module X = struct
exception E of 'a
let pack x = E x
let unpack = function
| E x -> Some x
| _ -> None
end in
X.pack, X.unpack
let pack = fst
let unpack = snd
exception Null
let null = Null
end
module Dyn : Dyn = struct
type 'a brand = ('a option) ref
type t = unit -> unit
let make_brand () =
ref None
let pack b x () =
b := Some x
let unpack b f =
b := None;
f();
!b
let null () =
()
end
next prev parent reply other threads:[~2001-07-11 7:52 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-07-03 20:49 Mark Seaborn
2001-07-11 7:52 ` Francois Pottier [this message]
2001-07-11 10:13 ` 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=20010711095236.A3033@pauillac.inria.fr \
--to=francois.pottier@inria.fr \
--cc=caml-list@inria.fr \
--cc=mrs35@cam.ac.uk \
/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