| author | wenzelm |
| Thu, 21 Sep 2023 23:45:03 +0200 | |
| changeset 78681 | 38fe769658be |
| parent 78674 | 88f47c70187a |
| child 78683 | cde40295ffd6 |
| 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 |
| 78674 | 20 |
val is_res: 'a result -> bool |
21 |
val is_exn: 'a result -> bool |
|
|
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
22 |
val get_res: 'a result -> 'a option |
| 28444 | 23 |
val get_exn: 'a result -> exn option |
24 |
val capture: ('a -> 'b) -> 'a -> 'b result
|
|
25 |
val release: 'a result -> 'a |
|
|
61077
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
26 |
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
|
27 |
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
|
28 |
val map_exn: (exn -> exn) -> 'a result -> 'a result |
|
39232
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 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
|
31 |
val interruptible_capture: ('a -> 'b) -> 'a -> 'b result
|
|
78673
90b12b919b5f
clarified signature (again): follow Isabelle/Java/Scala;
wenzelm
parents:
78671
diff
changeset
|
32 |
val failure_rc: exn -> int |
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
33 |
exception EXCEPTIONS of exn list |
| 62505 | 34 |
val trace: (exn -> string) -> (string -> unit) -> (unit -> 'a) -> 'a |
| 28444 | 35 |
end; |
36 |
||
37 |
structure Exn: EXN = |
|
| 23962 | 38 |
struct |
39 |
||
| 62821 | 40 |
(* location *) |
| 62505 | 41 |
|
| 62821 | 42 |
val polyml_location = PolyML.Exception.exceptionLocation; |
43 |
||
44 |
val reraise = PolyML.Exception.reraise; |
|
| 62505 | 45 |
|
46 |
||
| 62492 | 47 |
(* user errors *) |
48 |
||
49 |
exception ERROR of string; |
|
50 |
||
51 |
fun error msg = raise ERROR msg; |
|
52 |
||
53 |
fun cat_error "" msg = error msg |
|
54 |
| cat_error msg "" = error msg |
|
55 |
| cat_error msg1 msg2 = error (msg1 ^ "\n" ^ msg2); |
|
56 |
||
57 |
||
| 44158 | 58 |
(* exceptions as values *) |
| 28444 | 59 |
|
| 23962 | 60 |
datatype 'a result = |
|
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
61 |
Res of 'a | |
| 23962 | 62 |
Exn of exn; |
63 |
||
| 78674 | 64 |
fun is_res (Res _) = true |
65 |
| is_res _ = false; |
|
66 |
||
67 |
fun is_exn (Exn _) = true |
|
68 |
| is_exn _ = false; |
|
69 |
||
|
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
70 |
fun get_res (Res x) = SOME x |
|
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
71 |
| get_res _ = NONE; |
| 23962 | 72 |
|
73 |
fun get_exn (Exn exn) = SOME exn |
|
74 |
| get_exn _ = NONE; |
|
75 |
||
|
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
76 |
fun capture f x = Res (f x) handle e => Exn e; |
| 23962 | 77 |
|
|
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
78 |
fun release (Res y) = y |
|
31427
5a07cc86675d
reraise exceptions to preserve original position (ML system specific);
wenzelm
parents:
29564
diff
changeset
|
79 |
| release (Exn e) = reraise e; |
| 23962 | 80 |
|
|
61077
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
81 |
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
|
82 |
| 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
|
83 |
|
|
61077
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
84 |
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
|
85 |
|
|
06cca32aa519
thread context for exceptions from forks, e.g. relevant when printing errors;
wenzelm
parents:
56667
diff
changeset
|
86 |
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
|
87 |
| map_exn f (Exn e) = Exn (f e); |
| 39472 | 88 |
|
| 28444 | 89 |
|
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
90 |
(* interrupts *) |
| 28444 | 91 |
|
| 78681 | 92 |
fun is_interrupt Thread.Thread.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
|
93 |
| 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
|
94 |
| is_interrupt _ = false; |
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
95 |
|
|
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
96 |
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
|
97 |
| 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
|
98 |
|
|
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
|
99 |
fun interruptible_capture f x = |
|
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
43537
diff
changeset
|
100 |
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
|
101 |
|
|
78673
90b12b919b5f
clarified signature (again): follow Isabelle/Java/Scala;
wenzelm
parents:
78671
diff
changeset
|
102 |
fun failure_rc exn = if is_interrupt exn then 130 else 2; |
|
90b12b919b5f
clarified signature (again): follow Isabelle/Java/Scala;
wenzelm
parents:
78671
diff
changeset
|
103 |
|
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
104 |
|
| 44247 | 105 |
(* concatenated exceptions *) |
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34136
diff
changeset
|
106 |
|
|
28459
f6a4d913cfb1
simplified Exn.EXCEPTIONS, flatten nested occurrences;
wenzelm
parents:
28444
diff
changeset
|
107 |
exception EXCEPTIONS of exn list; |
| 28444 | 108 |
|
| 62505 | 109 |
|
110 |
(* low-level trace *) |
|
111 |
||
112 |
fun trace exn_message output e = |
|
113 |
PolyML.Exception.traceException |
|
114 |
(e, fn (trace, exn) => |
|
115 |
let |
|
116 |
val title = "Exception trace - " ^ exn_message exn; |
|
117 |
val () = output (String.concatWith "\n" (title :: trace)); |
|
118 |
in reraise exn end); |
|
119 |
||
| 23962 | 120 |
end; |