caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Patrick M Doane <patrick@watson.org>
To: caml-list@inria.fr
Subject: [Caml-list] Exceptions as datatypes
Date: Tue, 21 May 2002 08:52:10 -0400 (EDT)	[thread overview]
Message-ID: <20020521081525.R80985-100000@fledge.watson.org> (raw)

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
  | _ -> "<unknown>"
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


             reply	other threads:[~2002-05-21 12:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-21 12:52 Patrick M Doane [this message]
2002-05-22  2:59 ` Jacques Garrigue
2002-05-22 11:57   ` Patrick M Doane

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020521081525.R80985-100000@fledge.watson.org \
    --to=patrick@watson.org \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).