| author | blanchet |
| Tue, 26 Oct 2010 11:21:08 +0200 | |
| changeset 40159 | bfc716a96e47 |
| parent 39472 | 1cf49add5b40 |
| child 40234 | 39af96cc57cb |
| 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 |
|
| 39472 | 14 |
val map_result: ('a -> 'a) -> 'a result -> 'a 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 |
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
20 |
exception EXCEPTIONS of exn list |
|
32100
8ac6b1102f16
added flatten/flatten_list -- supercedes internal plain_exns;
wenzelm
parents:
31427
diff
changeset
|
21 |
val flatten: exn -> exn list |
|
8ac6b1102f16
added flatten/flatten_list -- supercedes internal plain_exns;
wenzelm
parents:
31427
diff
changeset
|
22 |
val flatten_list: exn list -> exn list |
| 28444 | 23 |
val release_all: 'a result list -> 'a list |
24 |
val release_first: 'a result list -> 'a list |
|
25 |
end; |
|
26 |
||
27 |
structure Exn: EXN = |
|
| 23962 | 28 |
struct |
29 |
||
| 28444 | 30 |
(* runtime exceptions as values *) |
31 |
||
| 23962 | 32 |
datatype 'a result = |
33 |
Result of 'a | |
|
34 |
Exn of exn; |
|
35 |
||
36 |
fun get_result (Result x) = SOME x |
|
37 |
| get_result _ = NONE; |
|
38 |
||
39 |
fun get_exn (Exn exn) = SOME exn |
|
40 |
| get_exn _ = NONE; |
|
41 |
||
42 |
fun capture f x = Result (f x) handle e => Exn e; |
|
43 |
||
44 |
fun release (Result y) = y |
|
|
31427
5a07cc86675d
reraise exceptions to preserve original position (ML system specific);
wenzelm
parents:
29564
diff
changeset
|
45 |
| release (Exn e) = reraise e; |
| 23962 | 46 |
|
| 39472 | 47 |
fun map_result f (Result x) = Result (f x) |
48 |
| map_result f e = e; |
|
49 |
||
| 28444 | 50 |
|
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
51 |
(* interrupts *) |
| 28444 | 52 |
|
53 |
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
|
54 |
|
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
55 |
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
|
56 |
|
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
57 |
fun is_interrupt Interrupt = true |
|
39233
9a0c67d4517a
Exn.is_interrupt: include interrupts that have passed through the IO layer;
wenzelm
parents:
39232
diff
changeset
|
58 |
| is_interrupt (IO.Io {cause = Interrupt, ...}) = true
|
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
59 |
| is_interrupt _ = false; |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
60 |
|
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
61 |
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
|
62 |
|
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
63 |
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
|
64 |
| 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
|
65 |
|
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
66 |
|
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
67 |
(* nested exceptions *) |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
68 |
|
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
69 |
exception EXCEPTIONS of exn list; |
| 28444 | 70 |
|
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
71 |
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
|
72 |
| flatten exn = if is_interrupt exn then [] else [exn] |
|
32100
8ac6b1102f16
added flatten/flatten_list -- supercedes internal plain_exns;
wenzelm
parents:
31427
diff
changeset
|
73 |
and flatten_list exns = List.concat (map flatten exns); |
| 23962 | 74 |
|
| 28444 | 75 |
fun release_all results = |
76 |
if List.all (fn Result _ => true | _ => false) results |
|
77 |
then map (fn Result x => x) results |
|
|
32100
8ac6b1102f16
added flatten/flatten_list -- supercedes internal plain_exns;
wenzelm
parents:
31427
diff
changeset
|
78 |
else raise EXCEPTIONS (flatten_list (List.mapPartial get_exn results)); |
| 28444 | 79 |
|
80 |
fun release_first results = release_all results |
|
|
31427
5a07cc86675d
reraise exceptions to preserve original position (ML system specific);
wenzelm
parents:
29564
diff
changeset
|
81 |
handle EXCEPTIONS (exn :: _) => reraise exn; |
| 28444 | 82 |
|
| 23962 | 83 |
end; |