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 OAA16202; Tue, 21 May 2002 14:52:34 +0200 (MET DST) 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 OAA16357 for ; Tue, 21 May 2002 14:52:33 +0200 (MET DST) Received: from fledge.watson.org (fledge.watson.org [204.156.12.50]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g4LCqVX01498 for ; Tue, 21 May 2002 14:52:31 +0200 (MET DST) Received: from fledge.watson.org (localhost [127.0.0.1]) by fledge.watson.org (8.12.3/8.12.3) with ESMTP id g4LCqBb4020735 for ; Tue, 21 May 2002 08:52:12 -0400 (EDT) (envelope-from patrick@watson.org) Received: from localhost (patrick@localhost) by fledge.watson.org (8.12.3/8.12.3/Submit) with ESMTP id g4LCqB3H020732 for ; Tue, 21 May 2002 08:52:11 -0400 (EDT) X-Authentication-Warning: fledge.watson.org: patrick owned process doing -bs Date: Tue, 21 May 2002 08:52:10 -0400 (EDT) From: Patrick M Doane To: caml-list@inria.fr Subject: [Caml-list] Exceptions as datatypes Message-ID: <20020521081525.R80985-100000@fledge.watson.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk It seems possible to use exceptions as datatypes with some (potentially useful) changes to the typing discipline. For example: module M1 = struct type t = Int of int | Float of float let to_string = function | Int n -> string_of_int n | Float f -> string_of_float f end module M2 = struct exception Int of int exception Float of float let to_string = function | Int n -> string_of_int n | Float f -> string_of_float f | _ -> "" end let test () = print_endline (M1.to_string (M1.Int 5)); print_endline (M2.to_string (M2.Int 5)); Since the 'exn' type is not polymoprhic, there are no concerns about a top-level structure failing to generalize a type variable. It's also extensible: For example, a syntax tree could include annotations without changing its type: type t = | Int of int | Binary of op * t * t | Annotation of exn | ... For most applications, the traditional or polymorphic variants work fine - but I can think of several examples where these exception datatypes could be extremely useful. Looking at the assembly output, it appears that the the matching is linear with two memory reads per comparison. I think that the code generation for exception matching can be improved though. Is the following strategy sound? Each module reserves a chunk of memory in its data section of the same size as the number of exceptions declared. Whenever an exception is created, it would include a pointer into that chunk (with an offset based on its ordering in the file). Pattern matching on exception values subtracts that pointer from the head of the chunk and then use a logarithmic search or jump table depending on the number of cases. If the pattern matching was performed on exception values from more than one module, then the subtraction (and subsequent comparisons) is done for each module. ------------------- 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