caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Looking up exceptions in a dictionary
  2001-11-05 10:00 [Caml-list] Looking up exceptions in a dictionary Marcin 'Qrczak' Kowalczyk
@ 2001-11-05  9:29 ` Fabrice Le Fessant
  2001-11-05 10:18 ` Xavier Leroy
  1 sibling, 0 replies; 3+ messages in thread
From: Fabrice Le Fessant @ 2001-11-05  9:29 UTC (permalink / raw)
  To: Marcin 'Qrczak' Kowalczyk; +Cc: caml-list


In the CDK, there is a Printexc2 module  which allows users to specify
printers for exceptions, that can be used by Printexc.to_string. The
way this is implemented is:

1- when a printer is registered, it has type exn -> string. Such a
printer is:

let not_found_handler exn = match exn with
  Not_found -> "Not Found"
| _ -> raise exn

Thus, it reraises the excepion  if it can not print it.

2- In Printexc.to_string, each printer is tried until a string is
returned. This doesn't require to know about the internal structure of
exceptions. 

- Fabrice

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* [Caml-list] Looking up exceptions in a dictionary
@ 2001-11-05 10:00 Marcin 'Qrczak' Kowalczyk
  2001-11-05  9:29 ` Fabrice Le Fessant
  2001-11-05 10:18 ` Xavier Leroy
  0 siblings, 2 replies; 3+ messages in thread
From: Marcin 'Qrczak' Kowalczyk @ 2001-11-05 10:00 UTC (permalink / raw)
  To: caml-list

I want to set up a framework for registering exception constructors and
specific exception values, and later translate exceptions arrived using
registered translation functions. The problem boils down to having
a dictionary indexed by exception constructors and exception values.

I haven't found the exception representation described. Is the
following description accurate?

An exception is a block whose first field is a pointer used to
distinguish among exception constructors, and the rest are arguments.
The constructor pointer points to a one-field block which contains
a string pointer containing the constructor name.

I think the dictionary can be constructed as I say below. Can it be
done in a better way?

Since the real thing to be looked up is a pointer, and pointers don't
have a stable hash, the first step is a Hashtbl.t indexed by string
values - names of constructors, to quickly narrow the search. Names
are insufficient to reliably distinguish exceptions, so under each
name there is an association list holding actual constructor pointers
for all exceptions having a given name, looked up using List.assq. Now
we have found the constructor. A regular Hashtbl.t here can be used
to find specific exception values if needed, which can be done by
exn value assuming that the constructors match.

-- 
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^
QRCZAK

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Looking up exceptions in a dictionary
  2001-11-05 10:00 [Caml-list] Looking up exceptions in a dictionary Marcin 'Qrczak' Kowalczyk
  2001-11-05  9:29 ` Fabrice Le Fessant
@ 2001-11-05 10:18 ` Xavier Leroy
  1 sibling, 0 replies; 3+ messages in thread
From: Xavier Leroy @ 2001-11-05 10:18 UTC (permalink / raw)
  To: Marcin 'Qrczak' Kowalczyk; +Cc: caml-list

> I haven't found the exception representation described. Is the
> following description accurate?
> 
> An exception is a block whose first field is a pointer used to
> distinguish among exception constructors, and the rest are arguments.
> The constructor pointer points to a one-field block which contains
> a string pointer containing the constructor name.

That's 100% accurate.

> I think the dictionary can be constructed as I say below. Can it be
> done in a better way?
> 
> Since the real thing to be looked up is a pointer, and pointers don't
> have a stable hash, the first step is a Hashtbl.t indexed by string
> values - names of constructors, to quickly narrow the search. Names
> are insufficient to reliably distinguish exceptions, so under each
> name there is an association list holding actual constructor pointers
> for all exceptions having a given name, looked up using List.assq.

You can use the "functorial" interface to Hashtbl to indicate that you
want keys to be compared by address and not by structure:

module ExnHashtbl = 
  Hashtbl.Make(struct type t = string
                      let equal = (==)
                      let hash = Hashtbl.hash
               end)

- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2001-11-05 10:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-05 10:00 [Caml-list] Looking up exceptions in a dictionary Marcin 'Qrczak' Kowalczyk
2001-11-05  9:29 ` Fabrice Le Fessant
2001-11-05 10:18 ` Xavier Leroy

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