Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] extensible records?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michael Vanier <mvanier@c...>
Subject: [Caml-list] extensible records?

Hi everyone!

I'm having a problem figuring out how to implement something in ocaml, and
I'd like to solicit advice.  I'm writing a little 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.  So, for instance, my basic
type definition looks something like this:

    type data =
        Int of int
      | Float of float
      | ...

Now let's say I want to add a type for files.  I don't want this to be a type
at the same level as ints or floats, because I want users to be able to add
their own types to the language without hacking the core type definition
(which is possible in, for instance, python).  So my ideal solution would be
something like this (not real ocaml code):

    (* types.mli *)
    type data =
        Int of int
      | Float of float
      | ...
      | Extension of extension

    type extension = []  (* nothing yet, not valid ocaml *)

    (* files.mli *)
    extension File of out_channel  (* not valid ocaml *)

    (* files.ml *)
    let close (d : data) =
      match data with
        Extension ext ->
          (match ext with
               File f -> (* close the file *)
             | _ -> raise (Failure "invalid type"))
        | _ -> raise (Failure "invalid type")

I can see two ways I can do this already, but both involve abusing the
exception system.  Way 1:

    (* types.mli *)
    type data =
        Int of int
      | Float of float
      | ...
      | Extension of unit -> unit


    (* files.ml *)
    exception File of out_channel

    let open_file filename =
      (* open the file as new_file *)
      raise (File new_file)

    let close (d : data) =
      match data with
        Extension ext ->
          try ext ()
          with ext' ->
            (match ext' with 
                File f -> (* close the file *)
              | _ -> raise (Failure "invalid type"))
        | _ -> raise (Failure "invalid type")

Aside from abusing the exception system, I've been told that this would be
slow due to the exception handling overhead.  A more direct way would be:

    (* types.mli *)
    type data =
        Int of int
      | Float of float
      | ...
      | Extension of exn


    (* files.ml *)
    exception File of out_channel

    let close (d : data) =
      match data with
        Extension ext ->
          (match ext with 
              File f -> (* close the file *)
            | _ -> raise (Failure "invalid type")))
        | _ -> raise (Failure "invalid type")

This would (I think) work, and wouldn't involve excessive overhead.  But
really, all I'm doing is abusing the fact that exceptions are the one kind of
extensible record type built in to ocaml.  What I want to know is: is there a
better way to do this?

I thought of using polymorphic variant types, but my (limited) understanding
is that you either declare all possible variants straight up in the .mli file
(which totally defeats my purpose) or you don't declare anything and let the
compiler infer the type (which I don't know how to write into a .mli file).
If this can be done with polymorphic variants, how would I do it?
Alternatively, if this can be done with objects, how would I do it?  Or
should I just stick with abusing exceptions?

Cheers,

Mike

-------------------
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