Browse thread
Typing Dynamic Typing in ocaml?
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Mauricio Fernandez <mfp@a...> |
| Subject: | Re: [Caml-list] Typing Dynamic Typing in ocaml? |
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