caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: lukstafi@gmail.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Existential types
Date: Mon, 05 Sep 2005 08:28:39 +0900 (JST)	[thread overview]
Message-ID: <20050905.082839.104027887.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <4a708d205090412435bca9cb0@mail.gmail.com>

From: Lukasz Stafiniak <lukstafi@gmail.com>

> I use an abstract type and a one-way typecast operator to implement
> existential quantification; e. g.
> 
> type 'a t
> type unknown_t
> let some (v : 'a t) = ((Obj.magic v) : unknown_t t)
> 
> Good?

This is a standard technique that is used in labltk for instance.
There should be no problem as long as your ['a t] type has a uniform
representation (i.e. this use of magic only encodes more refined type
distinctions.)
Note that if your values are implemented in ocaml, there is a
reasonable possibility that you could avoid the magic altogether.
And if this is not the case (i.e. implemented in ocaml but magic
required), there is a serious risk that this is actually unsound.
Remember that the function Obj.repr of type ['a -> Obj.t] is unsound,
contrary to the intuition.
Which leads to the rule of thumb: the only "safe" uses of  magic are
when dealing with "unsafe" values (implemented in C.)

By the way, I'm not sure I would call this existantial quantification,
as it is only one-way. This looks more like a simple form of
subtyping.

Jacques Garrigue


  reply	other threads:[~2005-09-04 23:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-04 19:43 Lukasz Stafiniak
2005-09-04 23:28 ` Jacques Garrigue [this message]
2005-09-05 12:26   ` [Caml-list] " Lukasz Stafiniak

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=20050905.082839.104027887.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=lukstafi@gmail.com \
    /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).