| 1 |
= UniversalType |
| 2 |
|
| 3 |
A universal type is a type into which all other types can be embedded. |
| 4 |
Here's a <<StandardML#,Standard ML>> signature for a universal type. |
| 5 |
|
| 6 |
[source,sml] |
| 7 |
---- |
| 8 |
signature UNIVERSAL_TYPE = |
| 9 |
sig |
| 10 |
type t |
| 11 |
|
| 12 |
val embed: unit -> ('a -> t) * (t -> 'a option) |
| 13 |
end |
| 14 |
---- |
| 15 |
|
| 16 |
The idea is that `type t` is the universal type and that each call to |
| 17 |
`embed` returns a new pair of functions `(inject, project)`, where |
| 18 |
`inject` embeds a value into the universal type and `project` extracts |
| 19 |
the value from the universal type. A pair `(inject, project)` |
| 20 |
returned by `embed` works together in that `project u` will return |
| 21 |
`SOME v` if and only if `u` was created by `inject v`. If `u` was |
| 22 |
created by a different function `inject'`, then `project` returns |
| 23 |
`NONE`. |
| 24 |
|
| 25 |
Here's an example embedding integers and reals into a universal type. |
| 26 |
|
| 27 |
[source,sml] |
| 28 |
---- |
| 29 |
functor Test (U: UNIVERSAL_TYPE): sig end = |
| 30 |
struct |
| 31 |
val (intIn: int -> U.t, intOut) = U.embed () |
| 32 |
val r: U.t ref = ref (intIn 13) |
| 33 |
val s1 = |
| 34 |
case intOut (!r) of |
| 35 |
NONE => "NONE" |
| 36 |
| SOME i => Int.toString i |
| 37 |
val (realIn: real -> U.t, realOut) = U.embed () |
| 38 |
val () = r := realIn 13.0 |
| 39 |
val s2 = |
| 40 |
case intOut (!r) of |
| 41 |
NONE => "NONE" |
| 42 |
| SOME i => Int.toString i |
| 43 |
val s3 = |
| 44 |
case realOut (!r) of |
| 45 |
NONE => "NONE" |
| 46 |
| SOME x => Real.toString x |
| 47 |
val () = print (concat [s1, " ", s2, " ", s3, "\n"]) |
| 48 |
end |
| 49 |
---- |
| 50 |
|
| 51 |
Applying `Test` to an appropriate implementation will print |
| 52 |
|
| 53 |
---- |
| 54 |
13 NONE 13.0 |
| 55 |
---- |
| 56 |
|
| 57 |
Note that two different calls to embed on the same type return |
| 58 |
different embeddings. |
| 59 |
|
| 60 |
Standard ML does not have explicit support for universal types; |
| 61 |
however, there are at least two ways to implement them. |
| 62 |
|
| 63 |
|
| 64 |
== Implementation Using Exceptions |
| 65 |
|
| 66 |
While the intended use of SML exceptions is for exception handling, an |
| 67 |
accidental feature of their design is that the `exn` type is a |
| 68 |
universal type. The implementation relies on being able to declare |
| 69 |
exceptions locally to a function and on the fact that exceptions are |
| 70 |
<<GenerativeException#,generative>>. |
| 71 |
|
| 72 |
[source,sml] |
| 73 |
---- |
| 74 |
structure U:> UNIVERSAL_TYPE = |
| 75 |
struct |
| 76 |
type t = exn |
| 77 |
|
| 78 |
fun 'a embed () = |
| 79 |
let |
| 80 |
exception E of 'a |
| 81 |
fun project (e: t): 'a option = |
| 82 |
case e of |
| 83 |
E a => SOME a |
| 84 |
| _ => NONE |
| 85 |
in |
| 86 |
(E, project) |
| 87 |
end |
| 88 |
end |
| 89 |
---- |
| 90 |
|
| 91 |
|
| 92 |
== Implementation Using Functions and References |
| 93 |
|
| 94 |
[source,sml] |
| 95 |
---- |
| 96 |
structure U:> UNIVERSAL_TYPE = |
| 97 |
struct |
| 98 |
datatype t = T of {clear: unit -> unit, |
| 99 |
store: unit -> unit} |
| 100 |
|
| 101 |
fun 'a embed () = |
| 102 |
let |
| 103 |
val r: 'a option ref = ref NONE |
| 104 |
fun inject (a: 'a): t = |
| 105 |
T {clear = fn () => r := NONE, |
| 106 |
store = fn () => r := SOME a} |
| 107 |
fun project (T {clear, store}): 'a option = |
| 108 |
let |
| 109 |
val () = store () |
| 110 |
val res = !r |
| 111 |
val () = clear () |
| 112 |
in |
| 113 |
res |
| 114 |
end |
| 115 |
in |
| 116 |
(inject, project) |
| 117 |
end |
| 118 |
end |
| 119 |
---- |
| 120 |
|
| 121 |
Note that due to the use of a shared ref cell, the above |
| 122 |
implementation is not thread safe. |
| 123 |
|
| 124 |
One could try to simplify the above implementation by eliminating the |
| 125 |
`clear` function, making `type t = unit \-> unit`. |
| 126 |
|
| 127 |
[source,sml] |
| 128 |
---- |
| 129 |
structure U:> UNIVERSAL_TYPE = |
| 130 |
struct |
| 131 |
type t = unit -> unit |
| 132 |
|
| 133 |
fun 'a embed () = |
| 134 |
let |
| 135 |
val r: 'a option ref = ref NONE |
| 136 |
fun inject (a: 'a): t = fn () => r := SOME a |
| 137 |
fun project (f: t): 'a option = (r := NONE; f (); !r) |
| 138 |
in |
| 139 |
(inject, project) |
| 140 |
end |
| 141 |
end |
| 142 |
---- |
| 143 |
|
| 144 |
While correct, this approach keeps the contents of the ref cell alive |
| 145 |
longer than necessary, which could cause a space leak. The problem is |
| 146 |
in `project`, where the call to `f` stores some value in some ref cell |
| 147 |
`r'`. Perhaps `r'` is the same ref cell as `r`, but perhaps not. If |
| 148 |
we do not clear `r'` before returning from `project`, then `r'` will |
| 149 |
keep the value alive, even though it is useless. |
| 150 |
|
| 151 |
|
| 152 |
== Also see |
| 153 |
|
| 154 |
* <<PropertyList#>>: Lisp-style property lists implemented with a universal type |