caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] extensible records again
@ 2004-03-21  6:21 Michael Vanier
  2004-03-21  8:08 ` Oleg Trott
                   ` (4 more replies)
  0 siblings, 5 replies; 26+ messages in thread
From: Michael Vanier @ 2004-03-21  6:21 UTC (permalink / raw)
  To: caml-list


Hi everyone,

I sent some email to this list a while back asking for advice on how to
handle a problem which called for extensible records.  I got some good
suggestions involving polymorphic variants, but it turns out that the
best proposed solution won't work because of problems with polymorphic
references.  I'll recap the problem here and (once again) ask for advice from
the experts.
 
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.  So, for instance, my basic type definition looks something like
this:
 
    type data =
        Int of int
      | Float of float
      | ...
 
Now I want to add a type for (say) 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.
I can do this as follows:
 
    (* 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 is OK, but it abuses the exception system.  I asked for ways to get the
same result with polymorphic variants.  A suggested approach (thanks to
Aleksey Nogin) was this:
                                                                                        
# type 'a data = Int of int | Ext of 'a;;
type 'a data = Int of int | Ext of 'a
# let open_file f =
      match f with
         Ext `File f -> print_string ("Opening file" ^ f)
       | _ -> raise (Invalid_argument "open_file")
   ;;
val open_file : [> `File of string] data -> unit = <fun>
# let run ( f :  [> ] data -> unit) = f (Ext (`Foo 1));;
val run : ([> `Foo of int] data -> unit) -> unit = <fun>
# run open_file;;
Exception: Invalid_argument "open_file".
 
 
So I tried this.  Aside from the pain of having to change data to 'a data in
lots of places (which I can live with), I got bitten by the polymorphic
reference limitation.  Specifically, I have a mutable stack of data values
which became a mutable stack of 'a data values, but the function which
creates the mutable stack is of type '_a data, so it doesn't type check.  I
can't figure out any way around this.  Basically, my main data type _cannot_
be parameterized.  I would like to be able to say something like
 
# type data = Int of int | Ext of [> ];;
Unbound type parameter [..]
 
but this doesn't work either, as you can see.  Is there any way to get the
effect that I want?
 
Thanks in advance,
 
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


^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2004-03-31 10:05 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-21  6:21 [Caml-list] extensible records again 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 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).