author | wenzelm |
Fri, 03 Oct 2008 21:06:38 +0200 | |
changeset 28490 | 40c3f900c457 |
parent 28459 | f6a4d913cfb1 |
child 29564 | f8b933a62151 |
permissions | -rw-r--r-- |
23962 | 1 |
(* Title: Pure/ML-Systems/exn.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius |
|
4 |
||
28444 | 5 |
Extra support for exceptions. |
23962 | 6 |
*) |
7 |
||
28444 | 8 |
signature EXN = |
9 |
sig |
|
10 |
datatype 'a result = Exn of exn | Result of 'a |
|
11 |
val get_result: 'a result -> 'a option |
|
12 |
val get_exn: 'a result -> exn option |
|
13 |
val capture: ('a -> 'b) -> 'a -> 'b result |
|
14 |
val release: 'a result -> 'a |
|
15 |
exception Interrupt |
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
16 |
exception EXCEPTIONS of exn list |
28444 | 17 |
val release_all: 'a result list -> 'a list |
18 |
val release_first: 'a result list -> 'a list |
|
19 |
end; |
|
20 |
||
21 |
structure Exn: EXN = |
|
23962 | 22 |
struct |
23 |
||
28444 | 24 |
(* runtime exceptions as values *) |
25 |
||
23962 | 26 |
datatype 'a result = |
27 |
Result of 'a | |
|
28 |
Exn of exn; |
|
29 |
||
30 |
fun get_result (Result x) = SOME x |
|
31 |
| get_result _ = NONE; |
|
32 |
||
33 |
fun get_exn (Exn exn) = SOME exn |
|
34 |
| get_exn _ = NONE; |
|
35 |
||
36 |
fun capture f x = Result (f x) handle e => Exn e; |
|
37 |
||
38 |
fun release (Result y) = y |
|
39 |
| release (Exn e) = raise e; |
|
40 |
||
28444 | 41 |
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
42 |
(* interrupt and nested exceptions *) |
28444 | 43 |
|
44 |
exception Interrupt = Interrupt; |
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
45 |
exception EXCEPTIONS of exn list; |
28444 | 46 |
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
47 |
fun plain_exns (Result _) = [] |
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
48 |
| plain_exns (Exn Interrupt) = [] |
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
49 |
| plain_exns (Exn (EXCEPTIONS exns)) = List.concat (map (plain_exns o Exn) exns) |
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
50 |
| plain_exns (Exn exn) = [exn]; |
28444 | 51 |
|
23962 | 52 |
|
28444 | 53 |
fun release_all results = |
54 |
if List.all (fn Result _ => true | _ => false) results |
|
55 |
then map (fn Result x => x) results |
|
56 |
else |
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
57 |
(case List.concat (map plain_exns results) of |
28444 | 58 |
[] => raise Interrupt |
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
59 |
| exns => raise EXCEPTIONS exns); |
28444 | 60 |
|
61 |
fun release_first results = release_all results |
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
62 |
handle EXCEPTIONS (exn :: _) => raise exn; |
28444 | 63 |
|
23962 | 64 |
end; |