| 1 |
= GenerativeException |
| 2 |
|
| 3 |
In <<StandardML#,Standard ML>>, exception declarations are said to be |
| 4 |
_generative_, because each time an exception declaration is evaluated, |
| 5 |
it yields a new exception. |
| 6 |
|
| 7 |
The following program demonstrates the generativity of exceptions. |
| 8 |
|
| 9 |
[source,sml] |
| 10 |
---- |
| 11 |
exception E |
| 12 |
val e1 = E |
| 13 |
fun isE1 (e: exn): bool = |
| 14 |
case e of |
| 15 |
E => true |
| 16 |
| _ => false |
| 17 |
exception E |
| 18 |
val e2 = E |
| 19 |
fun isE2 (e: exn): bool = |
| 20 |
case e of |
| 21 |
E => true |
| 22 |
| _ => false |
| 23 |
fun pb (b: bool): unit = |
| 24 |
print (concat [Bool.toString b, "\n"]) |
| 25 |
val () = (pb (isE1 e1) |
| 26 |
;pb (isE1 e2) |
| 27 |
; pb (isE2 e1) |
| 28 |
; pb (isE2 e2)) |
| 29 |
---- |
| 30 |
|
| 31 |
In the above program, two different exception declarations declare an |
| 32 |
exception `E` and a corresponding function that returns `true` only on |
| 33 |
that exception. Although declared by syntactically identical |
| 34 |
exception declarations, `e1` and `e2` are different exceptions. The |
| 35 |
program, when run, prints `true`, `false`, `false`, `true`. |
| 36 |
|
| 37 |
A slight modification of the above program shows that even a single |
| 38 |
exception declaration yields a new exception each time it is |
| 39 |
evaluated. |
| 40 |
|
| 41 |
[source,sml] |
| 42 |
---- |
| 43 |
fun f (): exn * (exn -> bool) = |
| 44 |
let |
| 45 |
exception E |
| 46 |
in |
| 47 |
(E, fn E => true | _ => false) |
| 48 |
end |
| 49 |
val (e1, isE1) = f () |
| 50 |
val (e2, isE2) = f () |
| 51 |
fun pb (b: bool): unit = |
| 52 |
print (concat [Bool.toString b, "\n"]) |
| 53 |
val () = (pb (isE1 e1) |
| 54 |
; pb (isE1 e2) |
| 55 |
; pb (isE2 e1) |
| 56 |
; pb (isE2 e2)) |
| 57 |
---- |
| 58 |
|
| 59 |
Each call to `f` yields a new exception and a function that returns |
| 60 |
`true` only on that exception. The program, when run, prints `true`, |
| 61 |
`false`, `false`, `true`. |
| 62 |
|
| 63 |
|
| 64 |
== Type Safety |
| 65 |
|
| 66 |
Exception generativity is required for type safety. Consider the |
| 67 |
following valid SML program. |
| 68 |
|
| 69 |
[source,sml] |
| 70 |
---- |
| 71 |
fun f (): ('a -> exn) * (exn -> 'a) = |
| 72 |
let |
| 73 |
exception E of 'a |
| 74 |
in |
| 75 |
(E, fn E x => x | _ => raise Fail "f") |
| 76 |
end |
| 77 |
fun cast (a: 'a): 'b = |
| 78 |
let |
| 79 |
val (make: 'a -> exn, _) = f () |
| 80 |
val (_, get: exn -> 'b) = f () |
| 81 |
in |
| 82 |
get (make a) |
| 83 |
end |
| 84 |
val _ = ((cast 13): int -> int) 14 |
| 85 |
---- |
| 86 |
|
| 87 |
If exceptions weren't generative, then each call `f ()` would yield |
| 88 |
the same exception constructor `E`. Then, our `cast` function could |
| 89 |
use `make: 'a \-> exn` to convert any value into an exception and then |
| 90 |
`get: exn \-> 'b` to convert that exception to a value of arbitrary |
| 91 |
type. If `cast` worked, then we could cast an integer as a function |
| 92 |
and apply. Of course, because of generative exceptions, this program |
| 93 |
raises `Fail "f"`. |
| 94 |
|
| 95 |
|
| 96 |
== Applications |
| 97 |
|
| 98 |
The `exn` type is effectively a <<UniversalType#,universal type>>. |
| 99 |
|
| 100 |
|
| 101 |
== Also see |
| 102 |
|
| 103 |
* <<GenerativeDatatype#>> |