author | wenzelm |
Thu, 28 Oct 2010 21:59:01 +0200 | |
changeset 40234 | 39af96cc57cb |
parent 39472 | 1cf49add5b40 |
child 40483 | 3848283c14bb |
permissions | -rw-r--r-- |
34136 | 1 |
(* Title: Pure/General/exn.ML |
23962 | 2 |
Author: Makarius |
3 |
||
28444 | 4 |
Extra support for exceptions. |
23962 | 5 |
*) |
6 |
||
28444 | 7 |
signature EXN = |
8 |
sig |
|
34136 | 9 |
datatype 'a result = Result of 'a | Exn of exn |
28444 | 10 |
val get_result: 'a result -> 'a option |
11 |
val get_exn: 'a result -> exn option |
|
12 |
val capture: ('a -> 'b) -> 'a -> 'b result |
|
13 |
val release: 'a result -> 'a |
|
40234
39af96cc57cb
added Exn.interruptible_capture, which reraises interrupts as required by user-code (when Exn.capture is not immediately followed by Exn.release);
wenzelm
parents:
39472
diff
changeset
|
14 |
val map_result: ('a -> 'b) -> 'a result -> 'b result |
28444 | 15 |
exception Interrupt |
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
16 |
val interrupt: unit -> 'a |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
17 |
val is_interrupt: exn -> bool |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
18 |
val interrupt_exn: 'a result |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
19 |
val is_interrupt_exn: 'a result -> bool |
40234
39af96cc57cb
added Exn.interruptible_capture, which reraises interrupts as required by user-code (when Exn.capture is not immediately followed by Exn.release);
wenzelm
parents:
39472
diff
changeset
|
20 |
val interruptible_capture: ('a -> 'b) -> 'a -> 'b result |
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
21 |
exception EXCEPTIONS of exn list |
32100
8ac6b1102f16
added flatten/flatten_list -- supercedes internal plain_exns;
wenzelm
parents:
31427
diff
changeset
|
22 |
val flatten: exn -> exn list |
8ac6b1102f16
added flatten/flatten_list -- supercedes internal plain_exns;
wenzelm
parents:
31427
diff
changeset
|
23 |
val flatten_list: exn list -> exn list |
28444 | 24 |
val release_all: 'a result list -> 'a list |
25 |
val release_first: 'a result list -> 'a list |
|
26 |
end; |
|
27 |
||
28 |
structure Exn: EXN = |
|
23962 | 29 |
struct |
30 |
||
28444 | 31 |
(* runtime exceptions as values *) |
32 |
||
23962 | 33 |
datatype 'a result = |
34 |
Result of 'a | |
|
35 |
Exn of exn; |
|
36 |
||
37 |
fun get_result (Result x) = SOME x |
|
38 |
| get_result _ = NONE; |
|
39 |
||
40 |
fun get_exn (Exn exn) = SOME exn |
|
41 |
| get_exn _ = NONE; |
|
42 |
||
43 |
fun capture f x = Result (f x) handle e => Exn e; |
|
44 |
||
45 |
fun release (Result y) = y |
|
31427
5a07cc86675d
reraise exceptions to preserve original position (ML system specific);
wenzelm
parents:
29564
diff
changeset
|
46 |
| release (Exn e) = reraise e; |
23962 | 47 |
|
39472 | 48 |
fun map_result f (Result x) = Result (f x) |
40234
39af96cc57cb
added Exn.interruptible_capture, which reraises interrupts as required by user-code (when Exn.capture is not immediately followed by Exn.release);
wenzelm
parents:
39472
diff
changeset
|
49 |
| map_result f (Exn e) = (Exn e); |
39472 | 50 |
|
28444 | 51 |
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
52 |
(* interrupts *) |
28444 | 53 |
|
54 |
exception Interrupt = Interrupt; |
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
55 |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
56 |
fun interrupt () = raise Interrupt; |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
57 |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
58 |
fun is_interrupt Interrupt = true |
40234
39af96cc57cb
added Exn.interruptible_capture, which reraises interrupts as required by user-code (when Exn.capture is not immediately followed by Exn.release);
wenzelm
parents:
39472
diff
changeset
|
59 |
| is_interrupt (IO.Io {cause, ...}) = is_interrupt cause |
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
60 |
| is_interrupt _ = false; |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
61 |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
62 |
val interrupt_exn = Exn Interrupt; |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
63 |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
64 |
fun is_interrupt_exn (Exn exn) = is_interrupt exn |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
65 |
| is_interrupt_exn _ = false; |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
66 |
|
40234
39af96cc57cb
added Exn.interruptible_capture, which reraises interrupts as required by user-code (when Exn.capture is not immediately followed by Exn.release);
wenzelm
parents:
39472
diff
changeset
|
67 |
fun interruptible_capture f x = |
39af96cc57cb
added Exn.interruptible_capture, which reraises interrupts as required by user-code (when Exn.capture is not immediately followed by Exn.release);
wenzelm
parents:
39472
diff
changeset
|
68 |
Result (f x) handle e => if is_interrupt e then reraise e else Exn e; |
39af96cc57cb
added Exn.interruptible_capture, which reraises interrupts as required by user-code (when Exn.capture is not immediately followed by Exn.release);
wenzelm
parents:
39472
diff
changeset
|
69 |
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
70 |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
71 |
(* nested exceptions *) |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
72 |
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
73 |
exception EXCEPTIONS of exn list; |
28444 | 74 |
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
75 |
fun flatten (EXCEPTIONS exns) = flatten_list exns |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
76 |
| flatten exn = if is_interrupt exn then [] else [exn] |
32100
8ac6b1102f16
added flatten/flatten_list -- supercedes internal plain_exns;
wenzelm
parents:
31427
diff
changeset
|
77 |
and flatten_list exns = List.concat (map flatten exns); |
23962 | 78 |
|
28444 | 79 |
fun release_all results = |
80 |
if List.all (fn Result _ => true | _ => false) results |
|
81 |
then map (fn Result x => x) results |
|
32100
8ac6b1102f16
added flatten/flatten_list -- supercedes internal plain_exns;
wenzelm
parents:
31427
diff
changeset
|
82 |
else raise EXCEPTIONS (flatten_list (List.mapPartial get_exn results)); |
28444 | 83 |
|
84 |
fun release_first results = release_all results |
|
31427
5a07cc86675d
reraise exceptions to preserve original position (ML system specific);
wenzelm
parents:
29564
diff
changeset
|
85 |
handle EXCEPTIONS (exn :: _) => reraise exn; |
28444 | 86 |
|
23962 | 87 |
end; |