| 1 |
= StaticSum |
| 2 |
|
| 3 |
While SML makes it impossible to write functions whose types would |
| 4 |
depend on the values of their arguments, or so called dependently |
| 5 |
typed functions, it is possible, and arguably commonplace, to write |
| 6 |
functions whose types depend on the types of their arguments. Indeed, |
| 7 |
the types of parametrically polymorphic functions like `map` and |
| 8 |
`foldl` can be said to depend on the types of their arguments. What |
| 9 |
is less commonplace, however, is to write functions whose behavior |
| 10 |
would depend on the types of their arguments. Nevertheless, there are |
| 11 |
several techniques for writing such functions. |
| 12 |
<<TypeIndexedValues#,Type-indexed values>> and <<Fold#,fold>> are two such |
| 13 |
techniques. This page presents another such technique dubbed static |
| 14 |
sums. |
| 15 |
|
| 16 |
|
| 17 |
== Ordinary Sums |
| 18 |
|
| 19 |
Consider the sum type as defined below: |
| 20 |
[source,sml] |
| 21 |
---- |
| 22 |
structure Sum = struct |
| 23 |
datatype ('a, 'b) t = INL of 'a | INR of 'b |
| 24 |
end |
| 25 |
---- |
| 26 |
|
| 27 |
While a generic sum type such as defined above is very useful, it has |
| 28 |
a number of limitations. As an example, we could write the function |
| 29 |
`out` to extract the value from a sum as follows: |
| 30 |
[source,sml] |
| 31 |
---- |
| 32 |
fun out (s : ('a, 'a) Sum.t) : 'a = |
| 33 |
case s |
| 34 |
of Sum.INL a => a |
| 35 |
| Sum.INR a => a |
| 36 |
---- |
| 37 |
|
| 38 |
As can be seen from the type of `out`, it is limited in the sense that |
| 39 |
it requires both variants of the sum to have the same type. So, `out` |
| 40 |
cannot be used to extract the value of a sum of two different types, |
| 41 |
such as the type `(int, real) Sum.t`. As another example of a |
| 42 |
limitation, consider the following attempt at a `succ` function: |
| 43 |
[source,sml] |
| 44 |
---- |
| 45 |
fun succ (s : (int, real) Sum.t) : ??? = |
| 46 |
case s |
| 47 |
of Sum.INL i => i + 1 |
| 48 |
| Sum.INR r => Real.nextAfter (r, Real.posInf) |
| 49 |
---- |
| 50 |
|
| 51 |
The above definition of `succ` cannot be typed, because there is no |
| 52 |
type for the codomain within SML. |
| 53 |
|
| 54 |
|
| 55 |
== Static Sums |
| 56 |
|
| 57 |
Interestingly, it is possible to define values `inL`, `inR`, and |
| 58 |
`match` that satisfy the laws |
| 59 |
---- |
| 60 |
match (inL x) (f, g) = f x |
| 61 |
match (inR x) (f, g) = g x |
| 62 |
---- |
| 63 |
and do not suffer from the same limitions. The definitions are |
| 64 |
actually quite trivial: |
| 65 |
[source,sml] |
| 66 |
---- |
| 67 |
structure StaticSum = struct |
| 68 |
fun inL x (f, _) = f x |
| 69 |
fun inR x (_, g) = g x |
| 70 |
fun match x = x |
| 71 |
end |
| 72 |
---- |
| 73 |
|
| 74 |
Now, given the `succ` function defined as |
| 75 |
[source,sml] |
| 76 |
---- |
| 77 |
fun succ s = |
| 78 |
StaticSum.match s |
| 79 |
(fn i => i + 1, |
| 80 |
fn r => Real.nextAfter (r, Real.posInf)) |
| 81 |
---- |
| 82 |
we get |
| 83 |
[source,sml] |
| 84 |
---- |
| 85 |
succ (StaticSum.inL 1) = 2 |
| 86 |
succ (StaticSum.inR Real.maxFinite) = Real.posInf |
| 87 |
---- |
| 88 |
|
| 89 |
To better understand how this works, consider the following signature |
| 90 |
for static sums: |
| 91 |
[source,sml] |
| 92 |
---- |
| 93 |
structure StaticSum :> sig |
| 94 |
type ('dL, 'cL, 'dR, 'cR, 'c) t |
| 95 |
val inL : 'dL -> ('dL, 'cL, 'dR, 'cR, 'cL) t |
| 96 |
val inR : 'dR -> ('dL, 'cL, 'dR, 'cR, 'cR) t |
| 97 |
val match : ('dL, 'cL, 'dR, 'cR, 'c) t -> ('dL -> 'cL) * ('dR -> 'cR) -> 'c |
| 98 |
end = struct |
| 99 |
type ('dL, 'cL, 'dR, 'cR, 'c) t = ('dL -> 'cL) * ('dR -> 'cR) -> 'c |
| 100 |
open StaticSum |
| 101 |
end |
| 102 |
---- |
| 103 |
|
| 104 |
Above, `'d` stands for domain and `'c` for codomain. The key |
| 105 |
difference between an ordinary sum type, like `(int, real) Sum.t`, and |
| 106 |
a static sum type, like `(int, real, real, int, real) StaticSum.t`, is |
| 107 |
that the ordinary sum type says nothing about the type of the result |
| 108 |
of deconstructing a sum while the static sum type specifies the type. |
| 109 |
|
| 110 |
With the sealed static sum module, we get the type |
| 111 |
[source,sml] |
| 112 |
---- |
| 113 |
val succ : (int, int, real, real, 'a) StaticSum.t -> 'a |
| 114 |
---- |
| 115 |
for the previously defined `succ` function. The type specifies that |
| 116 |
`succ` maps a left `int` to an `int` and a right `real` to a `real`. |
| 117 |
For example, the type of `StaticSum.inL 1` is |
| 118 |
`(int, 'cL, 'dR, 'cR, 'cL) StaticSum.t`. Unifying this with the |
| 119 |
argument type of `succ` gives the type `(int, int, real, real, int) |
| 120 |
StaticSum.t -> int`. |
| 121 |
|
| 122 |
The `out` function is quite useful on its own. Here is how it can be |
| 123 |
defined: |
| 124 |
[source,sml] |
| 125 |
---- |
| 126 |
structure StaticSum = struct |
| 127 |
open StaticSum |
| 128 |
val out : ('a, 'a, 'b, 'b, 'c) t -> 'c = |
| 129 |
fn s => match s (fn x => x, fn x => x) |
| 130 |
end |
| 131 |
---- |
| 132 |
|
| 133 |
Due to the value restriction, lack of first class polymorphism and |
| 134 |
polymorphic recursion, the usefulness and convenience of static sums |
| 135 |
is somewhat limited in SML. So, don't throw away the ordinary sum |
| 136 |
type just yet. Static sums can nevertheless be quite useful. |
| 137 |
|
| 138 |
|
| 139 |
=== Example: Send and Receive with Argument Type Dependent Result Types |
| 140 |
|
| 141 |
In some situations it would seem useful to define functions whose |
| 142 |
result type would depend on some of the arguments. Traditionally such |
| 143 |
functions have been thought to be impossible in SML and the solution |
| 144 |
has been to define multiple functions. For example, the |
| 145 |
https://smlfamily.github.io/Basis/socket.html[`Socket` structure] of the |
| 146 |
Basis library defines 16 `send` and 16 `recv` functions. In contrast, |
| 147 |
the Net structure |
| 148 |
(https://github.com/MLton/mltonlib/blob/master/com/sweeks/basic/unstable/net.sig[`net.sig`]) of the |
| 149 |
Basic library designed by Stephen Weeks defines only a single `send` |
| 150 |
and a single `receive` and the result types of the functions depend on |
| 151 |
their arguments. The implementation |
| 152 |
(https://github.com/MLton/mltonlib/blob/master/com/sweeks/basic/unstable/net.sml[`net.sml`]) uses |
| 153 |
static sums (with a slighly different signature: |
| 154 |
https://github.com/MLton/mltonlib/blob/master/com/sweeks/basic/unstable/static-sum.sig[`static-sum.sig`]). |
| 155 |
|
| 156 |
|
| 157 |
=== Example: Picking Monad Results |
| 158 |
|
| 159 |
Suppose that we need to write a parser that accepts a pair of integers |
| 160 |
and returns their sum given a monadic parsing combinator library. A |
| 161 |
part of the signature of such library could look like this |
| 162 |
[source,sml] |
| 163 |
---- |
| 164 |
signature PARSING = sig |
| 165 |
include MONAD |
| 166 |
val int : int t |
| 167 |
val lparen : unit t |
| 168 |
val rparen : unit t |
| 169 |
val comma : unit t |
| 170 |
(* ... *) |
| 171 |
end |
| 172 |
---- |
| 173 |
where the `MONAD` signature could be defined as |
| 174 |
[source,sml] |
| 175 |
---- |
| 176 |
signature MONAD = sig |
| 177 |
type 'a t |
| 178 |
val return : 'a -> 'a t |
| 179 |
val >>= : 'a t * ('a -> 'b t) -> 'b t |
| 180 |
end |
| 181 |
infix >>= |
| 182 |
---- |
| 183 |
|
| 184 |
The straightforward, but tedious, way to write the desired parser is: |
| 185 |
[source,sml] |
| 186 |
---- |
| 187 |
val p = lparen >>= (fn _ => |
| 188 |
int >>= (fn x => |
| 189 |
comma >>= (fn _ => |
| 190 |
int >>= (fn y => |
| 191 |
rparen >>= (fn _ => |
| 192 |
return (x + y)))))) |
| 193 |
---- |
| 194 |
|
| 195 |
In Haskell, the parser could be written using the `do` notation |
| 196 |
considerably less verbosely as: |
| 197 |
[source,haskell] |
| 198 |
---- |
| 199 |
p = do { lparen ; x <- int ; comma ; y <- int ; rparen ; return $ x + y } |
| 200 |
---- |
| 201 |
|
| 202 |
SML doesn't provide a `do` notation, so we need another solution. |
| 203 |
|
| 204 |
Suppose we would have a "pick" notation for monads that would allows |
| 205 |
us to write the parser as |
| 206 |
[source,sml] |
| 207 |
---- |
| 208 |
val p = `lparen ^ \int ^ `comma ^ \int ^ `rparen @ (fn x & y => x + y) |
| 209 |
---- |
| 210 |
using four auxiliary combinators: `{backtick}`, `\`, `^`, and `@`. |
| 211 |
|
| 212 |
Roughly speaking |
| 213 |
|
| 214 |
* `{backtick}p` means that the result of `p` is dropped, |
| 215 |
* `\p` means that the result of `p` is taken, |
| 216 |
* `p ^ q` means that results of `p` and `q` are taken as a product, and |
| 217 |
* `p @ a` means that the results of `p` are passed to the function `a` and that result is returned. |
| 218 |
|
| 219 |
The difficulty is in implementing the concatenation combinator `^`. |
| 220 |
The type of the result of the concatenation depends on the types of |
| 221 |
the arguments. |
| 222 |
|
| 223 |
Using static sums and the <<ProductType#,product type>>, the pick |
| 224 |
notation for monads can be implemented as follows: |
| 225 |
[source,sml] |
| 226 |
---- |
| 227 |
functor MkMonadPick (include MONAD) = let |
| 228 |
open StaticSum |
| 229 |
in |
| 230 |
struct |
| 231 |
fun `a = inL (a >>= (fn _ => return ())) |
| 232 |
val \ = inR |
| 233 |
fun a @ f = out a >>= (return o f) |
| 234 |
fun a ^ b = |
| 235 |
(match b o match a) |
| 236 |
(fn a => |
| 237 |
(fn b => inL (a >>= (fn _ => b)), |
| 238 |
fn b => inR (a >>= (fn _ => b))), |
| 239 |
fn a => |
| 240 |
(fn b => inR (a >>= (fn a => b >>= (fn _ => return a))), |
| 241 |
fn b => inR (a >>= (fn a => b >>= (fn b => return (a & b)))))) |
| 242 |
end |
| 243 |
end |
| 244 |
---- |
| 245 |
|
| 246 |
The above implementation is inefficient, however. It uses many more |
| 247 |
bind operations, `>>=`, than necessary. That can be solved with an |
| 248 |
additional level of abstraction: |
| 249 |
[source,sml] |
| 250 |
---- |
| 251 |
functor MkMonadPick (include MONAD) = let |
| 252 |
open StaticSum |
| 253 |
in |
| 254 |
struct |
| 255 |
fun `a = inL (fn b => a >>= (fn _ => b ())) |
| 256 |
fun \a = inR (fn b => a >>= b) |
| 257 |
fun a @ f = out a (return o f) |
| 258 |
fun a ^ b = |
| 259 |
(match b o match a) |
| 260 |
(fn a => (fn b => inL (fn c => a (fn () => b c)), |
| 261 |
fn b => inR (fn c => a (fn () => b c))), |
| 262 |
fn a => (fn b => inR (fn c => a (fn a => b (fn () => c a))), |
| 263 |
fn b => inR (fn c => a (fn a => b (fn b => c (a & b)))))) |
| 264 |
end |
| 265 |
end |
| 266 |
---- |
| 267 |
|
| 268 |
After instantiating and opening either of the above monad pick |
| 269 |
implementations, the previously given definition of `p` can be |
| 270 |
compiled and results in a parser whose result is of type `int`. Here |
| 271 |
is a functor to test the theory: |
| 272 |
[source,sml] |
| 273 |
---- |
| 274 |
functor Test (Arg : PARSING) = struct |
| 275 |
local |
| 276 |
structure Pick = MkMonadPick (Arg) |
| 277 |
open Pick Arg |
| 278 |
in |
| 279 |
val p : int t = |
| 280 |
`lparen ^ \int ^ `comma ^ \int ^ `rparen @ (fn x & y => x + y) |
| 281 |
end |
| 282 |
end |
| 283 |
---- |
| 284 |
|
| 285 |
|
| 286 |
== Also see |
| 287 |
|
| 288 |
There are a number of related techniques. Here are some of them. |
| 289 |
|
| 290 |
* <<Fold#>> |
| 291 |
* <<TypeIndexedValues#>> |