caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Marcin 'Qrczak' Kowalczyk" <qrczak@knm.org.pl>
To: caml-list@inria.fr
Subject: Re: [Caml-list] extensible records again
Date: Wed, 31 Mar 2004 12:05:12 +0200	[thread overview]
Message-ID: <1080727512.13334.16.camel@qrnik> (raw)
In-Reply-To: <20040321062143.BE7D29BBA2@orchestra.cs.caltech.edu>

W liście z sob, 20-03-2004, godz. 22:21 -0800, Michael Vanier napisał:

> I've written a dynamically-typed language in ocaml, and I want to be able to
> add new types to the language without having to add new primitive types to
> the system.

When I had this problem, I found no satisfactory type-safe way, and had
resorted to Obj.magic.

Each object type has a common first field which points to a descriptor.
The descriptor contains type id (unique integer) and implementation of
operations common to all objects (in my case it's only application to a
sequence of arguments; for performance there are separate entries for
known small numbers of arguments and one entry taking a list).

Type ids for new types are allocated dynamically, using a global
counter. I ignore the possibility of wraparound, one would have to make
2**(word_size-1) new types (possible, as types can be defined locally
and then forgotten, but improbable).

The OCaml type which can represent an arbitrary object is a record type
consisting of the descriptor only. Other types are longer records, which
are Object.magic'ed to and from the generic type. Having the descriptor
alone you can apply common operations or check the type.

Unfortunately I found no other representation which is as flexible and
as efficient. It must support performing common operations without
knowing the type, taking some type id suitable for indexing a
dictionary. New types must be definable without having to change the
interpreter modules, and they can be defined both in OCaml code and from
my language. Objects should not require much indirection or duplication.

(The interpreter is now dead, I used it to bootstrap a compiler.)

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/


-------------------
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:[~2004-03-31 10:05 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-21  6:21 Michael Vanier
2004-03-21  8:08 ` Oleg Trott
2004-03-21  8:40   ` Michael Vanier
2004-03-21 16:10     ` Oleg Trott
2004-03-21 17:06       ` skaller
2004-03-21 17:36         ` Oleg Trott
2004-03-22  3:19           ` skaller
2004-03-22  7:49           ` Ville-Pertti Keinonen
2004-03-22  9:32             ` Oleg Trott
2004-03-22 10:25               ` Ville-Pertti Keinonen
2004-03-21 22:35         ` Michael Vanier
2004-03-22  3:39           ` skaller
2004-03-21 22:34       ` Michael Vanier
2004-03-22  3:31         ` skaller
2004-03-22  5:54           ` Michael Vanier
2004-03-23 19:14             ` skaller
2004-03-24  1:41               ` Jacques Garrigue
2004-03-24  8:44                 ` Julien Signoles
2004-03-24 10:04                   ` Jacques Garrigue
2004-03-21  8:53 ` Martin Jambon
2004-03-21  9:22   ` Michael Vanier
2004-03-21 17:00 ` skaller
2004-03-22  8:13 ` Achim Blumensath
2004-03-23  2:14   ` Michael Vanier
2004-03-23  7:25     ` Achim Blumensath
2004-03-31 10:05 ` Marcin 'Qrczak' Kowalczyk [this message]

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=1080727512.13334.16.camel@qrnik \
    --to=qrczak@knm.org.pl \
    --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).