author | wenzelm |
Mon, 22 Feb 2021 14:48:03 +0100 | |
changeset 73275 | f0db1e4c89bc |
parent 63925 | 500646ef617a |
child 73278 | 7dbae202ff84 |
permissions | -rw-r--r-- |
62508
d0b68218ea55
discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
wenzelm
parents:
62505
diff
changeset
|
1 |
(* Title: Pure/General/exn.ML |
23962 | 2 |
Author: Makarius |
3 |
||
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
4 |
Support for exceptions. |
23962 | 5 |
*) |
6 |
||
62491 | 7 |
signature BASIC_EXN = |
8 |
sig |
|
9 |
exception ERROR of string |
|
10 |
val error: string -> 'a |
|
11 |
val cat_error: string -> string -> 'a |
|
12 |
end; |
|
13 |
||
28444 | 14 |
signature EXN = |
15 |
sig |
|
62491 | 16 |
include BASIC_EXN |
62821 | 17 |
val polyml_location: exn -> PolyML.location option |
62505 | 18 |
val reraise: exn -> 'a |
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
19 |
datatype 'a result = Res of 'a | Exn of exn |
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
20 |
val get_res: 'a result -> 'a option |
28444 | 21 |
val get_exn: 'a result -> exn option |
22 |
val capture: ('a -> 'b) -> 'a -> 'b result |
|
23 |
val release: 'a result -> 'a |
|
61077
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
24 |
val map_res: ('a -> 'b) -> 'a result -> 'b result |
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
25 |
val maps_res: ('a -> 'b result) -> 'a result -> 'b result |
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
26 |
val map_exn: (exn -> exn) -> 'a result -> 'a result |
28444 | 27 |
exception Interrupt |
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
28 |
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
|
29 |
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
|
30 |
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
|
31 |
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
|
32 |
val interruptible_capture: ('a -> 'b) -> 'a -> 'b result |
73275 | 33 |
val interrupt_return_code: int |
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
56628
diff
changeset
|
34 |
val return_code: exn -> int -> int |
56628 | 35 |
val capture_exit: int -> ('a -> 'b) -> 'a -> 'b |
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
36 |
exception EXCEPTIONS of exn list |
62505 | 37 |
val trace: (exn -> string) -> (string -> unit) -> (unit -> 'a) -> 'a |
28444 | 38 |
end; |
39 |
||
40 |
structure Exn: EXN = |
|
23962 | 41 |
struct |
42 |
||
62821 | 43 |
(* location *) |
62505 | 44 |
|
62821 | 45 |
val polyml_location = PolyML.Exception.exceptionLocation; |
46 |
||
47 |
val reraise = PolyML.Exception.reraise; |
|
62505 | 48 |
|
49 |
||
62492 | 50 |
(* user errors *) |
51 |
||
52 |
exception ERROR of string; |
|
53 |
||
54 |
fun error msg = raise ERROR msg; |
|
55 |
||
56 |
fun cat_error "" msg = error msg |
|
57 |
| cat_error msg "" = error msg |
|
58 |
| cat_error msg1 msg2 = error (msg1 ^ "\n" ^ msg2); |
|
59 |
||
60 |
||
44158 | 61 |
(* exceptions as values *) |
28444 | 62 |
|
23962 | 63 |
datatype 'a result = |
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
64 |
Res of 'a | |
23962 | 65 |
Exn of exn; |
66 |
||
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
67 |
fun get_res (Res x) = SOME x |
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
68 |
| get_res _ = NONE; |
23962 | 69 |
|
70 |
fun get_exn (Exn exn) = SOME exn |
|
71 |
| get_exn _ = NONE; |
|
72 |
||
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
73 |
fun capture f x = Res (f x) handle e => Exn e; |
23962 | 74 |
|
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
75 |
fun release (Res y) = y |
31427
5a07cc86675d
reraise exceptions to preserve original position (ML system specific);
wenzelm
parents:
29564
diff
changeset
|
76 |
| release (Exn e) = reraise e; |
23962 | 77 |
|
61077
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
78 |
fun map_res f (Res x) = Res (f x) |
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
79 |
| map_res f (Exn e) = Exn e; |
42223
098c86e53153
more precise propagation of reports/results through some inner syntax layers;
wenzelm
parents:
40483
diff
changeset
|
80 |
|
61077
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
81 |
fun maps_res f = (fn Res x => x | Exn e => Exn e) o map_res f; |
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
82 |
|
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
83 |
fun map_exn f (Res x) = Res x |
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
84 |
| map_exn f (Exn e) = Exn (f e); |
39472 | 85 |
|
28444 | 86 |
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
87 |
(* interrupts *) |
28444 | 88 |
|
63925 | 89 |
exception Interrupt = Thread.Thread.Interrupt; |
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
90 |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
91 |
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
|
92 |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
93 |
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
|
94 |
| 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
|
95 |
| is_interrupt _ = false; |
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
96 |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
97 |
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
|
98 |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
99 |
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
|
100 |
| 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
|
101 |
|
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
|
102 |
fun interruptible_capture f x = |
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
103 |
Res (f x) handle e => if is_interrupt e then reraise e else Exn e; |
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
|
104 |
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
105 |
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
56628
diff
changeset
|
106 |
(* POSIX return code *) |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
56628
diff
changeset
|
107 |
|
73275 | 108 |
val interrupt_return_code : int = 130; |
109 |
||
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
56628
diff
changeset
|
110 |
fun return_code exn rc = |
73275 | 111 |
if is_interrupt exn then interrupt_return_code else rc; |
56628 | 112 |
|
113 |
fun capture_exit rc f x = |
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
56628
diff
changeset
|
114 |
f x handle exn => exit (return_code exn rc); |
56628 | 115 |
|
116 |
||
44247 | 117 |
(* concatenated exceptions *) |
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
118 |
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
119 |
exception EXCEPTIONS of exn list; |
28444 | 120 |
|
62505 | 121 |
|
122 |
(* low-level trace *) |
|
123 |
||
124 |
fun trace exn_message output e = |
|
125 |
PolyML.Exception.traceException |
|
126 |
(e, fn (trace, exn) => |
|
127 |
let |
|
128 |
val title = "Exception trace - " ^ exn_message exn; |
|
129 |
val () = output (String.concatWith "\n" (title :: trace)); |
|
130 |
in reraise exn end); |
|
131 |
||
23962 | 132 |
end; |