From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA30054; Sun, 21 Mar 2004 09:08:53 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id JAA30197 for ; Sun, 21 Mar 2004 09:08:52 +0100 (MET) Received: from jalapeno.cc.columbia.edu (jalapeno.cc.columbia.edu [128.59.59.238]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i2L88oHd022877 for ; Sun, 21 Mar 2004 09:08:51 +0100 Received: from columbia.edu (tw304h3.cpmc.columbia.edu [156.111.84.180]) (user=ot14 mech=PLAIN bits=0) by jalapeno.cc.columbia.edu (8.12.11/8.12.11) with ESMTP id i2L88mu9007716 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NOT); Sun, 21 Mar 2004 03:08:48 -0500 (EST) Message-ID: <405D4D77.2030403@columbia.edu> Date: Sun, 21 Mar 2004 03:08:23 -0500 From: Oleg Trott User-Agent: Mozilla Thunderbird 0.5 (X11/20040309) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Michael Vanier CC: caml-list@inria.fr Subject: Re: [Caml-list] extensible records again References: <20040321062143.BE7D29BBA2@orchestra.cs.caltech.edu> In-Reply-To: <20040321062143.BE7D29BBA2@orchestra.cs.caltech.edu> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-No-Spam-Score: Local X-Scanned-By: MIMEDefang 2.40 X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; oleg:01 oleg:01 caml-list:01 extensible:01 vanier:01 failwith:01 runtime:01 mli:01 val:01 foo:01 val:01 foo:01 failwith:01 runtime:01 variants:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 277 Michael Vanier wrote: >[...] I want users to be able to add >their own types to the language without hacking the core type definition. > > Note that in a language like Scheme, users can not add new types other than by combining the primitives. So, I suppose you aren't really talking about language users, but those who extend the language. The obvious thing to do is to use polymorphic variants. E.g. (* "core language" *) let plus a b = match (a, b) with | (`Int x), (`Int y) -> `Int (x + y) (* ... *) | _ -> failwith "runtime type error: argument is not a number" >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 = ># let run ( f : [> ] data -> unit) = f (Ext (`Foo 1));; >val run : ([> `Foo of int] data -> unit) -> unit = ># run open_file;; >Exception: Invalid_argument "open_file" > > (* language exension example, adding "files" *) let open = function `File f -> open_file f | _ -> failwith "runtime type error: argument is not a file" HTH Oleg ------------------- 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