From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.1 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE, SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 24E50BBAF for ; Wed, 17 Dec 2008 23:42:16 +0100 (CET) X-IronPort-AV: E=Sophos;i="4.36,239,1228086000"; d="scan'208";a="21432834" Received: from concorde.inria.fr ([192.93.2.39]) by mail1-smtp-roc.national.inria.fr with ESMTP; 17 Dec 2008 23:42:16 +0100 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id mBHMgFFx028566 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Wed, 17 Dec 2008 23:42:15 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ag0IAJAOSUnVBJXyWmdsb2JhbACBbJFnARYICwgQBapGWJESgwY X-IronPort-AV: E=Sophos;i="4.36,239,1228086000"; d="scan'208";a="20524649" Received: from outmailhost.telefonica.net (HELO ctsmtpout4.frontal.correo) ([213.4.149.242]) by mail3-smtp-sop.national.inria.fr with ESMTP; 17 Dec 2008 23:42:14 +0100 Received: from NANA.localdomain (88.6.2.29) by ctsmtpout4.frontal.correo (7.2.056.6) (authenticated as ferferse$telefonica.net) id 494646530011FC16 for caml-list@inria.fr; Wed, 17 Dec 2008 23:42:14 +0100 Received: from mfp by NANA.localdomain with local (Exim 4.69) (envelope-from ) id 1LD55o-0008Rp-EM for caml-list@inria.fr; Wed, 17 Dec 2008 23:42:12 +0100 Date: Wed, 17 Dec 2008 23:42:12 +0100 From: Mauricio Fernandez To: caml-list@inria.fr Subject: Re: [Caml-list] Typing Dynamic Typing in ocaml? Message-ID: <20081217224212.GF20626@NANA.localdomain> Mail-Followup-To: caml-list@inria.fr References: <494956BD.9030000@mcmaster.ca> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <494956BD.9030000@mcmaster.ca> User-Agent: Mutt/1.5.17+20080114 (2008-01-14) X-Miltered: at concorde with ID 49498047.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 ocaml:01 computations:01 integers:01 val:01 val:01 hashtbl:01 hashtbl:01 wrote:01 typing:01 typing:01 caml-list:01 int:01 int:01 arbitrary:02 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