caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Mauricio Fernandez <mfp@acm.org>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Typing Dynamic Typing in ocaml?
Date: Wed, 17 Dec 2008 23:42:12 +0100	[thread overview]
Message-ID: <20081217224212.GF20626@NANA.localdomain> (raw)
In-Reply-To: <494956BD.9030000@mcmaster.ca>

On Wed, Dec 17, 2008 at 02:45:01PM -0500, Jacques Carette wrote:
> I have two (related) questions:
> 1) Has anyone transcribed the TypeRep library into ocaml?
> http://people.cs.uu.nl/arthurb/dynamic.html
>
> 2) How do I embed 'dynamically known' data into a single ocaml  
> data-structure?
>
> More specifically, I am experimenting with a (new) language which allows  
> deduction and computations to be performed with equal ease on its terms.  
> This language uses ocaml has the host meta-language (ie the interpreter 
> is written in ocaml).  I would like to be able to use arbitrary ocaml 
> data-structures to represent some of my terms, when these terms are known 
> to come from specific theories.  For example, I would like to use Bigint 
> to represent integers, but without exposing that per se.  Perhaps a 
> better way to phrase this would be to say that I want to have a "generic 
> external data container" type in my language terms, which I can 
> instantiate in multiple different ways (in the same program), with data 
> handled in different modules, without having to change the 'generic' 
> data-structure everytime I add a new module.

Does this signature correspond to what you need? (This is a "property list",
typically used to decorate an AST in multiple passes.)

type 'a t
type ('a, 'b) property
val create : unit -> 'a t
val new_property : unit -> ('a, 'b) property
val get : 'a t -> ('b, 'a) property -> 'b option
val set : 'a t -> ('b, 'a) property -> 'b -> unit

Your data structure holds a  'a t  value, and the different modules
declare their own properties (of different types, not reflected in the
property list).

Here's a fairly efficient implementation of mine:

type 'a t = (int, unit -> unit) Hashtbl.t
type ('a, 'b) property = { 
  set : 'b t -> 'a -> unit;
  get : 'b t -> 'a option;
}

let create () = Hashtbl.create 13

let new_id : unit -> int =
  let id = ref 0 in
    fun () -> incr id; !id

let new_property () =
  let id = new_id () in
  let v = ref None in

  let set t x =
    Hashtbl.replace t id (fun () -> v := Some x) in

  let get t =
    try
      (Hashtbl.find t id) ();
      match !v with
          Some x as s -> v := None; s
        | None -> None
    with Not_found -> None
  in { set = set; get = get }

let set t p x = p.set t x
let get t p = p.get t

-- 
Mauricio Fernandez  -   http://eigenclass.org


  parent reply	other threads:[~2008-12-17 22:42 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-17 19:45 Jacques Carette
2008-12-17 19:58 ` [Caml-list] " Stéphane Glondu
2008-12-17 21:08   ` Jacques Carette
2008-12-17 21:15     ` Stéphane Glondu
2008-12-17 21:15     ` Daniel Bünzli
2008-12-17 20:29 ` Mikkel Fahnøe Jørgensen
2008-12-17 21:28 ` David Teller
2008-12-17 22:42 ` Mauricio Fernandez [this message]
2008-12-18  1:16 ` Yaron Minsky

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=20081217224212.GF20626@NANA.localdomain \
    --to=mfp@acm.org \
    --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).